Thread: char confusion

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    75

    Post char confusion

    I'm trying to find out the ranges of different data types through direct computation........this is what I did.......

    Code:
    #include <stdio.h>
    main()
    {
    	char	ch;
    	signed short	sh;
    	signed int	in;
    	signed long	lo;
    	unsigned short	ush;
    	unsigned int	uin;
    	unsigned long	ulo;
    	ch = sh = in = lo = ush = uin = ulo = 0;
    
    	printf("Ranges through computation:\n\n");
    
    	while (ch < (ch+1))
    		ch++;
    	printf("The range of a char is from %d to %d\n", ch+1, ch);
    
    	while (sh < (sh+1))
    		sh++;
    	printf("The range of a short is from %d to %d\n", sh+1, sh);
    
    	while (in < (in+1))
    		in++;
    	printf("The range of an int is from %d to %d\n", in+1, in);
    
    	while (lo < (lo+1))
    		++lo;
    	printf("The range of a long is from %ld to %ld\n", lo+1, lo);
    
    	while (ush < (ush+1))
    		++ush;
    	printf("The range of an unsigned short is from %u to %u\n", ush+1, ush);
    
    	while (uin < (uin+1))
    		++uin;
    	printf("The range of an unsigned int is from %u to %u\n", uin+1, uin);
    
    	while (ulo < (ulo+1))
    		++ulo;
    	printf("The range of an unsigned long is from %lu to %lu\n", ulo+1, ulo);
    
    	return 0;
    }
    The ranges of all data types in the program print correctly, except for the char type (the corresponding code is red) in which the loop continues for ever ........why is it so?

    Thanks.

    PS: The program only works when the code in red is deleted
    Last edited by noodles; 06-21-2006 at 05:17 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (ch < (ch+1))
    My guess is that the +1 silently promotes the whole thing to an integer expression, so on the supposedly last iteration, you end up with 255 < 256 rather than the modulo result of 255 < 0 (which is what you expected).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    The sign bit is causing trouble - for char, when ch is 127, ch++ is
    giving -128, so ch++ gives -127, -126.. all the way to +127 again,
    hence an infinite loop.

    Decimal -> Binary
    127 -> 0111 1111
    -128 -> 1000 0000
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    75

    Smile

    Thanks a lot everyone.........(especially Salem)

    The problem was that (ch+1) evaluated to 128 when ch equalled 127.

    Typecasting (ch+1) to char solved the problem:

    Code:
    	while (ch < (char)(ch+1))
    		ch++;
    	printf("The range of a char is from %d to %d\n", (char)(ch+1), ch);
    Last edited by noodles; 06-21-2006 at 07:01 AM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can use CHAR_MAX and look-alikes in <limits.h> instead of computing the numbers directly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You do realize that it would take you in excess of tens of years to finish the program

    A better way is to take advantage of the fact that, due to the way modern computers handle numbers, the limits are usually one less than a power of two.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Thanks for information, jafet!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM