Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C question on datatypes

Status
Not open for further replies.

raja.bhavanam

Junior Member level 3
Joined
Nov 22, 2005
Messages
28
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,502
Hi,

Recently I came across the below C question. I know the output but I want to know how compiler will analyse the comparison. Appreciate your views

Thanks,
-Raja



signed int is = -1;
unsigned int iu = is;

signed char cs = -1;
unsigned char cu = cs;


if (is == iu) printf("is and iu are equal\n");
else printf("is and iu are not equal\n");

if (cs == cu) printf("cs and cu are equal\n");
else printf("cs and cu are not equal\n");


Output:
is and iu are equal
cs and cu are not equal
 

Hi,

Since your variables have different types and you didn’t explicitly use the cast operator, before a comparison of two variables, the compiler promotes them (provided the types are compatible) to the biggest type of the two, larger or equal to an int (I’m talking of sizes in bytes, of course).

Now since in your first case both types are int-sized, there's no promotion and the result of the comparison is true.

In the second case however, a unsigned char is promoted to int with all upper bytes 0, while the signed char promotes to int by sign extension, meaning all upper bytes are 1 (in 2’s complement notation).

For example, assuming sizeof(int) = 2, cu is promoted to 0x00FF (or the int 255), while cs is promoted to 0xFFFF (or the int -1). These are obviously different…

If you used the cast operator like:
Code:
if (cs == (signed char)cu)
or
Code:
if ((unsigned char)cs == cu)
the comparisons’ results were true in both cases.

Arthur
 

r u sure about this??
i have checked ur code in Keil IDE
i found is = iu and cs = cu; both are equal...
i dont understand why compiler will promote char to int..??
which compiler ur talking about.??i would like to understand this topic...
 

Well, I’m no compiler writer, but I’m pretty sure that this is the general (ISO/ANSI?) rule of implicit type conversions that all conformant compilers should adhere to.

I normally use cast operators in every type conversion, even trivial ones, just to avoid these kinds of ambiguities. But since you doubted, I’ve compiled this code with IAR, Freescale’s CodeWarrior, MSVC and an online compiler I found on the web (https://www.delorie.com/djgpp/compile/) and they all confirmed my statements.

Now if you said you’ve tried it and it’s not like that, it might be because your compiler doesn’t follow the standard or it’s configured not to (I’ve never used Keil).

Please note that the chars are not stored as integers, the compiler just generates [extra] code before operations (comparisons, assignments etc.) involving two differently typed operands. Their sizeof will still report the size of a char.

Arthur
 

Dear arthur0,

Your point may be valid...
Thanks for this knowledge base..
I havent checked it with other comilers...
Being an embedded engineer i have crosschecked that with most popukar
embedded IDE and i found that result....

Added after 1 minutes:

Dear arthur0.
u also sai that

"Now if you said you’ve tried it and it’s not like that, it might be because your compiler doesn’t follow the standard or it’s configured not to (I’ve never used Keil).
"

I havent said anytime that its not like that....
please dont misunderstand me..
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top