Basic C question

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

Basic C question

#1 Post by mahaju »

Code: Select all

#include<stdio.h>
int main(){
    float a=2.5;
    printf("%f\n",a);
    printf(" as int = %d \n as float = %f \n as hex = %x",a,a,a);
    printf("\n%f",a);
    return 0;
}


The output is
2.500000
 as int = 0
 as float = 0.000000
 as hex = 40040000
2.500000
So what is up with the middle 3 lines??? I am not sure about the hex representation but I am sure it shouldn't show 0 for the int and float representations.

I used codeblocks 8.2 for compiling and it uses gcc I think.
Last edited by mahaju on Mon 18 Apr 2011, 11:18, edited 2 times in total.

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#2 Post by mahaju »

Also,

Code: Select all

#include<stdio.h>
int main(){
    float a=2.5;
    printf("%f\n",a);
    printf(" as int = %d\n", a);
    printf(" as float = %f\n",a);
    printf(" as hex = %u\n", a);
    printf("\n%f",a);
    return 0;
}
here as int = 0, as float = 2.500000 and as hex = 0
But why??
I thought it should show as int = 2
and how do I see what hex value it keeps in memory for floating points?
Last edited by mahaju on Mon 18 Apr 2011, 11:19, edited 1 time in total.

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#3 Post by Moose On The Loose »

mahaju wrote:Also,
#include<stdio.h>
int main(){
float a=2.5;
printf("%f\n",a);
printf(" as int = %d\n", a);
printf(" as float = %f\n",a);
printf(" as hex = %u\n", a);
printf("\n%f",a);
return 0;
}

here as int = 0, as float = 2.500000 and as hex = 0
But why??
I thought it should show as int = 2
and how do I see what hex value it keeps in memory for floating points?
printf takes in a list of addresses of things. The first one is assumed to be a format string. The rest are assumed to be the addresses of the things referred to by the format string. No check is done to make sure that the things pointed to are really of the type that the format string suggests they may be. If you get it wrong, the bytes in memory are dealt with as thought they were what you said they were and not as what they really are.

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#4 Post by mahaju »

So what does my output mean?
Why is it showing int int and hex representation as 0?
And what about my first post?

akash_rawal
Posts: 229
Joined: Wed 25 Aug 2010, 15:38
Location: ISM Dhanbad, Jharkhand, India

#5 Post by akash_rawal »

mahaju wrote: #include<stdio.h>
int main(){
float a=2.5;
printf("%f\n",a);
printf(" as int = %d \n as float = %f \n as hex = %x",a,a,a);
printf("\n%f",a);
return 0;
}


The output is
2.500000
as int = 0
as float = 0.000000
as hex = 40040000
2.500000

So what is up with the middle 3 lines???
Your mistake is that %d and %x take integers, but you are providing a float value.
So you must cast the values into integer before passing to printf using cast operator whose format is:

Code: Select all

([new_datatype]) [variable]
New source code:

Code: Select all

#include<stdio.h>
int main(){
float a=2.5;
printf("%f\n",a);
printf(" as int = %d \n as float = %f \n as hex = %x",(int) a,a,(int) a);
printf("\n%f",a);
return 0;
} 
And terminal output:

Code: Select all

2.500000
 as int = 2 
 as float = 2.500000 
 as hex = 2
2.500000
_____________________________________________________________
mahaju wrote: Also,
#include<stdio.h>
int main(){
float a=2.5;
printf("%f\n",a);
printf(" as int = %d\n", a);
printf(" as float = %f\n",a);
printf(" as hex = %u\n", a);
printf("\n%f",a);
return 0;
}

here as int = 0, as float = 2.500000 and as hex = 0
But why??
%u is for unsigned int, not for hexadecimal representation of float value.

Post Reply