Skip to contentSkip to conversation

OFF-TOPIC : Confusion about basic C program behaviour

DISCUSSION Learner 1 participant 1 message Mar 14, 2005

lore
Share & tools
lore

Learner

author
#0 lore raw

Thank you for the useful link .

--- Gerardo García Peña <[email protected]>

Quoted 59 lines · wrote:
Quoted 4 lines · Learner

Hi,

I know this is not the correct place to post this
question, but I feel it would be pretty easy for

the

Quoted 7 lines

kernel gurus to explain this.

TEST CODE
==========

#include <stdio.h>

Quoted 7 lines

main()
{
float a1=1.0;
float a2=2.0;

  printf(" \n");
  printf(" Size of Float > %d \n", sizeof(float)

);

Quoted 26 lines
  printf(" Size of Int   > %d \n", sizeof(int) );
  printf(" \n");

printf(" CASE 1 : f-f > %f - %f \n\n", a1, a2);
printf(" CASE 2 : f-d > %f - %d \n\n", a1, a2);
printf(" CASE 3 : d-f > %d - %f \n\n", a1, a2);
printf(" CASE 4 : d-d > %d - %d \n\n", a1, a2);
}

OUTPUT :

Size of Float > 4
Size of Int > 4

CASE 1 : f-f > 1.000000 - 2.000000

CASE 2 : f-d > 1.000000 - 0

CASE 3 : d-f > 0 - 0.000000

CASE 4 : d-d > 0 - 1072693248

==================================

The CASE1 & CASE2 & CASE4 outputs are expected.

Quoted 11 lines

Any explanation for CASE3 behaviour.
I presume it should behave similar to CASE2 and
print 0 - 2.000000 .

Could somebody please pin-point what is
lacking in my presumtion .

Thanks !

Quoted 9 lines

I believe this happens because float is promoted to double when it is
passed as an arg... so you should think that double = 8 and int = 4.

I have been googling and I have found this:

http://www-ccs.ucsd.edu/c/function.html#argument%20promotion

Quoted 2 lines

__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/