Thread: strange negative numbers ?!!

  1. #1
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70

    strange negative numbers ?!!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
    
    	FILE *out;
    	FILE *in;
    	char *ce;
    
    
    	if((ce=(char *)malloc(sizeof(char))) == NULL)
        {
            printf("Error: No enough memory\n");
            getchar();
            return 1;
        }
    
    	in = fopen("CP1256.txt","rb");
    	out = fopen("output.txt","wb");
    
    	if(in == NULL)
    	{
    		printf("Can`t open file:%s\n",argv[1]);
    		exit(0);
    	}
    
    
    	while(fread(ce,sizeof(char),1,in) > 0)
    	{
    		printf("%d\n",*ce);   //it`s print negative numbers 
    	}
    
    	fclose(in);
    	fclose(out);
    
    	return 0;
    }
    can i know why the *ce contain negative numbers ??
    to be a 1-1, learn C

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    main.c
    ../main.c: In function `main':
    ../main.c:25: error: `argv' undeclared (first use in this function)
    ../main.c:25: error: (Each undeclared identifier is reported only once
    ../main.c:25: error: for each function it appears in.)
    *** Errors occurred during this build ***
    To what does ce point?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    point to the first char in a the input file ??
    to be a 1-1, learn C

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    because char is a signed data type, or at least, you're printing it as signed.
    use printf("&#37;u\n",ce) instead of printf("%d\n",ce)

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Meshal View Post
    point to the first char in a the input file ??
    Sorry, I'm going blind.
    Code:
    if((ce=(char *)malloc(sizeof(char))) == NULL)
    I have to wonder why, though.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Doodle77 View Post
    because char is a signed data type, or at least, you're printing it as signed.
    use printf("&#37;u\n",ce) instead of printf("%d\n",ce)
    That won't fix the problem, but it is true that that should be changed. The conversion of char to int happens before the printf function is called. If this change is made by itself, it will simply reinterpret the negative numbers as very large integers.

    You also need to change the the type of ce from char * to unsigned char *.

    Explanation on why changing printf is not enough (ignore if not interested):
    Basically, there are two steps in interpreting the type of additional argument passed to printf and similar functions. The first is the natural conversion from to the basic types such as void *, int, unsigned, and double. Actual conversion is done based on the type of the argument passed, including sign extension. Then inside the printf function, each argument is interpreted based on the type indicated in the format string. If the two types are not the same you get undefined behavior, but if the difference is only the the signedness, the results are easy to predict -- they would be the same as the cast from the signed type to the unsigned type (or vice versa).
    Last edited by King Mir; 10-21-2007 at 04:39 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    thanks all guy`s , the problem fixed when i change the char to unsigned char , i couldn`t notice that ..
    to be a 1-1, learn C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we input negative numbers in the output windiw
    By hitesh1511 in forum C Programming
    Replies: 1
    Last Post: 08-22-2006, 12:07 AM
  2. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 01:51 AM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. Doesnt fseeko64 work with negative offsets?
    By johny145 in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2005, 12:45 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM