Jump to content

Need help in clearing some concepts of ANSI C

- - - - -

  • Please log in to reply
10 replies to this topic

#1
sharpshooter09

sharpshooter09

    Newbie

  • Members
  • PipPip
  • 17 posts


int main()

{

    

    unsigned x = 1;

    signed char y = -1;

    

    

    if ( x > y )

        printf(" x > y " );

    else

        printf(" x <= y ");

           

    

    

    getch();

    return 0;

    

} 


I was required to find the output of some code so I built the above code, but the code I was required to print the output of was mainly

    unsigned x = 1;

    signed char y = -1;

    

    

    if ( x > y )

        printf(" x > y " );

    else

        printf(" x <= y ");

 


I got the output as x <= y but yet haven't understood the concept of unsigned and signed char. Self study is a bit tricky :S

-----------------------------------------------------------------------------------------------------------------

Edit: I have another doubt related to the following piece of code:



int main()

{

    int x = 10;

    if ( x = 20 )

        printf(" TRUE " );

    else

        printf(" FALSE ");


    return 0;

    getch();

}





Why does the program return TRUE?
I'm assuming that it has something to do with the integer value. :cursing:

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
When you're dealing with an int, the size of it depends on the compiler and platform.

Let's assume you're on a 32-bit CPU, and that an int is 32 bits. In general, the first bit indicates whether the number is positive (0) or negative (1), and the rest indicate the value.
An unsigned int uses the same 32 bits, but uses the first bit to store additional positive values.

When you compare an unsigned int to a signed int, the compiler has to treat them as both being signed or both being unsigned. In this case, your signed char gets treated as an unsigned char, then promoted to an unsigned int to compare it with the int. So the value -1 (11111111) is converted to 255 and compared to 1.

In your second example, you assigned the value 20 to x instead of comparing x to 20. 20 is non-zero, so considered true.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
    if ( x = 20 )
This is always true. You're assigning value, instead of comparing, which is ==. You can use a technique where you first write a value and then a variable, in that case when using assignment instead of comparison you will get a compiler error.

if (20 = x) // this will signal an error

if (20 == x) // this will not


The difference between signed and unsigned numbers is range; unsigned numbers go from 0 to +2^datatype_width_in_bits - 1 while unsigned numbers go from -2^datatype_width_in_bits to +2^datatype_width_in_bits - 1. You can get size (width) of a datatype with sizeof operator and multiplying that number with 8 (sizeof return number of Bytes, 1 Byte = 8 bits).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
sharpshooter09

sharpshooter09

    Newbie

  • Members
  • PipPip
  • 17 posts

WingedPanther said:

When you're dealing with an int, the size of it depends on the compiler and platform.

Let's assume you're on a 32-bit CPU, and that an int is 32 bits. In general, the first bit indicates whether the number is positive (0) or negative (1), and the rest indicate the value.
An unsigned int uses the same 32 bits, but uses the first bit to store additional positive values.

When you compare an unsigned int to a signed int, the compiler has to treat them as both being signed or both being unsigned. In this case, your signed char gets treated as an unsigned char, then promoted to an unsigned int to compare it with the int. So the value -1 (11111111) is converted to 255 and compared to 1.

In your second example, you assigned the value 20 to x instead of comparing x to 20. 20 is non-zero, so considered true.

Thanks, that cleared the concept pretty well.

#5
sharpshooter09

sharpshooter09

    Newbie

  • Members
  • PipPip
  • 17 posts

Flying Dutchman said:

    if ( x = 20 )
This is always true. You're assigning value, instead of comparing, which is ==. You can use a technique where you first write a value and then a variable, in that case when using assignment instead of comparison you will get a compiler error.


I did get a compile time error

#6
sharpshooter09

sharpshooter09

    Newbie

  • Members
  • PipPip
  • 17 posts
Could one of you also explain the use of the ++x operator? I fail to understand this mini problem posted in the book which asked me to evaluate ++x++ if a is an integer assigned a value of 10.

The answer is 12, I compiled it on my own but I didn't understand the use of ++x. ( x++ was pretty easy to understand )

#7
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
The difference between ++x and x++ is the time at which the increment is made.

With x++ the increment is made AFTER the variable value is used in the expression. Therefore:

int c = 0;

while(c != 3) {

printf("%d ", c++);

}

Would output 0 1 2

With ++x the increment is made before using the variable value in the expression. Therefore the value used is the one already incremented:

int c = 0;

while(c != 3) {

printf("%d ", ++c);

}

Would output 1 2 3

#8
sharpshooter09

sharpshooter09

    Newbie

  • Members
  • PipPip
  • 17 posts

Revolt said:

The difference between ++x and x++ is the time at which the increment is made.

With x++ the increment is made AFTER the variable value is used in the expression. Therefore:

int c = 0;

while(c != 3) {

printf("%d ", c++);

}

Would output 0 1 2

With ++x the increment is made before using the variable value in the expression. Therefore the value used is the one already incremented:

int c = 0;

while(c != 3) {

printf("%d ", ++c);

}

Would output 1 2 3

Well, in those terms, could you explain ++x++?

(edit: What I understood was that 1 increment was made to the value of 10 before using and 1 after using the expression, hence 1+10+1 = 12, am I right?)

#9
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
That question has more to do with operator precedence: C++ Operator Precedence - Cppreference

According to the precedence table on that site that operation is the same as ++(x++) so what happens is the following (if I'm mistaken someone please correct me):
  • x is marked for increment after the execution of the entire expression (x++)
  • x is incremented by 1 thus if x was 10 it becomes 11 (++x)
  • Finally after the execution of the instruction, x is incremented thus getting the value 12

EDIT: Actually, ++x++ isn't a correct C instruction (just tried compiling it) since x++ doesn't return a left value and ++x only works in left values.

#10
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
I would add that


(++x)++; // would be legal


The reason being prefix increment operator i.e. ++x returns an l-value (meaning it returns a variable which can be assigned another value) where as the post increment returns only an r-value (not a variable - more accurately literal) so post increment operator cannot be used as an l-value.

To be clear, revolt called this an operator precedence issue because pre and post increment are TWO separate operators for the language.

Lastly,

writing

if(x=4)

           cout << x << endl;


will NOT generate an error in ANSI c. I am sure you would have another reason for the error. Most certainly it would be because you did not have any execution statement after the if i.e. error would be something like "missing ; after if"

Edited by fayyazlodhi, 09 July 2011 - 12:02 PM.
Separate Ops

Today is the first day of the rest of my life

#11
sharpshooter09

sharpshooter09

    Newbie

  • Members
  • PipPip
  • 17 posts

Revolt said:

That question has more to do with operator precedence: C++ Operator Precedence - Cppreference

According to the precedence table on that site that operation is the same as ++(x++) so what happens is the following (if I'm mistaken someone please correct me):
  • x is marked for increment after the execution of the entire expression (x++)
  • x is incremented by 1 thus if x was 10 it becomes 11 (++x)
  • Finally after the execution of the instruction, x is incremented thus getting the value 12

EDIT: Actually, ++x++ isn't a correct C instruction (just tried compiling it) since x++ doesn't return a left value and ++x only works in left values.

So, in a way I was correct about the order of use.

Thanks for further clearing my doubt




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users