Thread: few doubts

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    77

    few doubts

    Q1.

    Code:
    #include <stdio.h>#include <conio.h>
    int main()
    {
    	char c=125;
    	c=c+10;
    	printf("%d",c);
    	getch();
    	return 0;	
    }
    why is it printing -121 ....
    (after overflow at 127 (at FF for char assuming char uses 1 byte)
    wont it start counting from 0?...(expecting 8 as answer)
    )

    Q2.

    Code:
    #include <stdio.h>#include <conio.h>
    int main()
    {
            if(sizeof(int)>-8)
    {printf("true");}
    else
    {
    printf("false");
    }
    	getch();
    	return 0;	
    }
    (why is it showing "false"?)

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by progmateur View Post
    why is it printing -121 ....
    (after overflow at 127 (at FF for char assuming char uses 1 byte)
    wont it start counting from 0?...(expecting 8 as answer)
    because it doesn't start over at zero. char is a signed data type, which means it flips over to negative numbers when it overflows.

    (why is it showing "false"?)
    because sizeof() always returns a positive number.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Commenting on Q1:

    In C, the type char is implementation defined if it is signed or unsigned. In C, it is implementation defined whether signed integers use two's complement (most common), one's complement, or sign-magnitude.

    The following values from the <limits.h> file will tell you what the relevant information for your implementation is:

    CHAR_BIT (paraphrase: number of bits in a char)
    CHAR_MIN
    CHAR_MAX

    It seems that your implementation is using CHAR_BIT is 8, CHAR_MIN is -128, and CHAR_MAX is 127. That indicates that your implementation has char as a signed type using two's complement. This is a fairly common implementation.
    Last edited by pheininger; 12-09-2013 at 08:54 AM. Reason: spelling

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by pheininger View Post
    In C, it is implementation defined whether signed integers use two's complement (most common), one's complement, or sign-magnitude.
    without explaining what that means, or providing links to such explanations, you're likely to confuse the OP even more.

    your implementation has char as a signed type using two's complement. This is a fairly common implementation.
    it's actually the most common implementation, by a huge margin. so common, in fact, that you can usually assume that this is how it will be implemented. GCC, MSVC, and ICC all use that format. those three account for the vast majority of the market. the only time you're likely to encounter a different implementation is in obscure, special-purpose compilers for things like embedded systems. any PC, Linux, or Mac implementation is almost certainly going to exhibit the exact behavior you're seeing.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Elkvis View Post
    without explaining what that means, or providing links to such explanations, you're likely to confuse the OP even more.

    it's actually the most common implementation, by a huge margin. so common, in fact, that you can usually assume that this is how it will be implemented. GCC, MSVC, and ICC all use that format. those three account for the vast majority of the market. the only time you're likely to encounter a different implementation is in obscure, special-purpose compilers for things like embedded systems. any PC, Linux, or Mac implementation is almost certainly going to exhibit the exact behavior you're seeing.
    Maybe it's the most common implementation today but why rely on something undefined? The unqualified type char can be either signed or unsigned depending upon the implementation. I think it's potentially worse not to mention this. I don't think that mentioning limits.h or anything is necessary at this stage, though. It may be beneficial for the OP to know that if they want to guarantee that it's signed or unsigned they can simply type unsigned char or signed char.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    This is where limits.h is useful. It may seem like a pain but to be safe you have to check the limits. In your first example, do this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #define UB(...) do { printf("%s:%d: Undefined behaviour detected\n", __FILE__, __LINE__); exit(-1); } while(0)
    
    int main(void) {
        char c = 125;
        if (c > CHAR_MAX - 10)
            UB();
        c += 10;
        printf("%d\n",c);
        
        return 0;
    }
    If you do lots of operations and want to guarantee that they are safe, then you could define some convenience macros

    Code:
    #define SAFE_ADD(VAR, AMOUNT) do { \
        if (VAR > CHAR_MAX - (AMOUNT)) \
            UB(); \
        VAR += (AMOUNT); \
    } while(0)
    Looks a bit ugly but then you can do something like this instead of the above check each time you operate on c
    Code:
    int amt;
    scanf("%d", &amt);
    
    char c = 125;
    SAFE_ADD(c, amt);
    printf("%d\n",c);

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Hodor View Post
    I think it's potentially worse not to mention this.
    in this case, I disagree. it's obvious that the OP is a beginner, and bombarding him/her with information that is largely irrelevant to the task at hand is likely to confuse and discourage. the question was about why negative numbers come up when it overflows, and talking about implementation-defined behavior simply serves to muddy the water. it's completely unnecessary to worry about such things as a beginner.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Elkvis View Post
    in this case, I disagree. it's obvious that the OP is a beginner, and bombarding him/her with information that is largely irrelevant to the task at hand is likely to confuse and discourage. the question was about why negative numbers come up when it overflows, and talking about implementation-defined behavior simply serves to muddy the water. it's completely unnecessary to worry about such things as a beginner.
    Well, we disagree. That's ok =) Personally I think that it's never too early to talk about implementation-defined behaviour. *shrug*

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elkvis View Post
    in this case, I disagree. it's obvious that the OP is a beginner, and bombarding him/her with information that is largely irrelevant to the task at hand is likely to confuse and discourage. the question was about why negative numbers come up when it overflows, and talking about implementation-defined behavior simply serves to muddy the water. it's completely unnecessary to worry about such things as a beginner.
    I agree with Hodor on this one.

    The OP's question suggested an expectation that a char would behave like an unsigned type (i.e. when overflow occurs, wrap at zero).

    It is therefore appropriate both to point out that unadorned char may be either signed or unsigned, and that signed types do not behave in the expected way.

    I've seen people say that before (without using the expression "implementation-defined"), only to trigger lots of comments - from either the OP or other forum members - like "with my compiler char is unsigned" or "my compiler has a signed char type, and overflow wraps", behaving as if the answer given is wrong. It then becomes necessary to fall back on the fact that the standard specifies that it is implementation-defined whether char is signed or unsigned, and also states that overflow of all signed native integral types (including a signed char) results in undefined behaviour.


    I also disagree with your premise that, because three compilers dominate the market, that you should encourage beginners to assume particular behaviours that those compilers support. Code has a nasty habit of being reused even when its author does not intend it will be reused. Code written assuming some behaviour is well-defined that, in reality, is undefined causes no end of problems - because the bugs become VERY difficult to track down. And programmers who make such assumptions will probably miss out on significant job opportunities.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My doubts
    By tony2011 in forum C Programming
    Replies: 42
    Last Post: 11-17-2011, 03:46 AM
  2. pointer doubts...
    By sanddune008 in forum C Programming
    Replies: 11
    Last Post: 07-08-2009, 02:51 AM
  3. Some doubts on DLLs
    By BrownB in forum Windows Programming
    Replies: 1
    Last Post: 05-30-2007, 02:25 AM
  4. C++0x - a few doubts
    By Mario F. in forum C++ Programming
    Replies: 33
    Last Post: 09-23-2006, 01:22 PM
  5. ASP,PHP doubts
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-14-2002, 11:17 AM