Conversions in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dianagaby2002
    New Member
    • Jul 2012
    • 9

    Conversions in C

    I don't understand these conversions in C I have for homework. Could you explain me or give some links?

    1)Given int n= 200, m= 200;
    long r;
    And supposing sizeof(int) is 2, what is the result of: r = n*m; ?

    Answer is -25536

    Why?

    2)Given static int i, t[10]; i and t are not explicit initialised

    Which will be the result of the expression:
    (i=0) || (t[i] <0)

    Result is 1

    3) a) Given char c = 130;

    c will become -126

    b) What will the following sequence return for this input: 20?

    #include<stdio. h>
    void main(){
    char a;
    scanf("%c", &a);
    printf("%c", a);
    }

    Result is: 2

    Thank you!
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please refer to posting coursework questions and answers.

    I'll give you some hints...

    Question 1 not only assumes sizeof(int) is 2, but it also assumes that negative numbers are represented in two's-complement format (this is nearly always the case).

    Question 2 says the variables "are not explicitly initialized". What are the non-explicit ways for variables to be initialized? By the way, the expression should be:
    (i==0) || (t[i] <0)

    Question 3a assumes that type char is signed (it needn't be) and that negative numbers are represented in two's-complement format.

    Question 3b; I suppose you mean that the input is the two characters '2' and '0'. Review the manpages for scanf and printf to see precisely how each interprets the %c format specifier. This question probably assumes that stdin and stdout are unbuffered.

    You should look up the following terms if you are not familiar with them:
    • two's-complement
    • buffered I/O
    • nonbuffered I/O or unbuffered I/O

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Question 1 assumes sizeof(long) is 2. long is guaranteed to be the sizeof an int but may be longer. So is the answer here is indeterminate.

      Question 2 is 1. static int is initialized to 0 by default.

      Question 3a is bad code. 130 is larger than a char, unless the char is unsigned. Your compiler should give a warning here.

      Question 3b, scanf only reads one byte into a char. You get the 2 of 20.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Regarding the assumptions for question 1...

        The C Standard guarantees that sizeof(int) is no smaller than 2; but it might be larger. The Standard goes on to guarantee that sizeof(long) is no smaller than 4 or sizeof(int), whichever is bigger; but it might be larger.

        I believe that the Question 1 answer reported by the OP is fully explained by:
        > the assumption that sizeof(int) is 2;
        > the assumption that negative numbers are represented by two's complement format; and
        > the fact [not assumption] that sizeof(long) > 2.

        Another hint for Q1... the following code snippet has a different value for r than reported by the OP for Q1.
        Code:
        int n = 200;
        long m = 200;
        long r;
        r = n * m;
        printf("%ld\n",r);

        Comment

        Working...