help understanding code

This is a discussion on help understanding code within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { char c; for (c = 0; c < 256; c++) printf("%c\n", c); return 0; ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Question help understanding code

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char c;
    
    	for (c = 0; c < 256; c++)
    		printf("%c\n", c);
    
    	return 0;
    }
    Can anyone explain why the code above doesn't work properly, but yet, the codes below do?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	unsigned c;
    
    	for (c = 0; c < 256; c++)
    		printf("%c\n", c);
    
    	return 0;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int c;
    
    	for (c = 0; c < 256; c++)
    		printf("%c\n", c);
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    As an intergerm the character is retrived as a character ordinal.

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by _Cl0wn_
    As an intergerm the character is retrived as a character ordinal.
    I really don't understand what you just said.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    hmm, the only difference is that unsigned c hold more numbers (just number without signs (-)).

    you char will hold from -127 to 128 or something like that, and you want to hold until 255, so this is why it wont work.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    This

    >char c;

    is a signed char and has range -127 to 128. An unsigned has range 0 to 255. A char is 8 bits.

    This

    >unsigned c;

    is an unsigned int. The size of an int depends on your machine. Try this:

    printf ("%d\n", sizeof (int));

    to see how many bytes an int is on your machine.

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char c;
    
      for (c = 0; c < 256; c++)
        printf("%c\n", c);
    
      return 0;
    }
    This looks harmless and correct, right? But on compilers where CHAR_BIT is 8, it'll run until the heat death of the universe, provided your computer lives that long :-) The problem is that char can either be signed or unsigned and also that the largest value a char can possibly hold in 8 bits is 255. The loop condition will never be met :-)

    The other code you posted used integer types, which can hold a considerably larger value, so the loop will get to 256 and end.
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  2. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21