Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 01 Sep 2014, 21:05
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Basic C question
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [5 Posts]  
Author Message
mahaju


Joined: 11 Oct 2010
Posts: 493
Location: between the keyboard and the chair

PostPosted: Mon 04 Apr 2011, 21:02    Post subject:  Basic C question  

Code:
#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, 07:18; edited 2 times in total
Back to top
View user's profile Send private message 
mahaju


Joined: 11 Oct 2010
Posts: 493
Location: between the keyboard and the chair

PostPosted: Mon 04 Apr 2011, 21:17    Post subject:  

Also,
Code:
#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, 07:19; edited 1 time in total
Back to top
View user's profile Send private message 
Moose On The Loose


Joined: 24 Feb 2011
Posts: 515

PostPosted: Mon 04 Apr 2011, 21:58    Post subject:  

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.
Back to top
View user's profile Send private message 
mahaju


Joined: 11 Oct 2010
Posts: 493
Location: between the keyboard and the chair

PostPosted: Mon 04 Apr 2011, 22:12    Post subject:  

So what does my output mean?
Why is it showing int int and hex representation as 0?
And what about my first post?
Back to top
View user's profile Send private message 
akash_rawal

Joined: 25 Aug 2010
Posts: 232
Location: ISM Dhanbad, Jharkhand, India

PostPosted: Mon 11 Apr 2011, 09:15    Post subject:  

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:
([new_datatype]) [variable]

New source code:
Code:

#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:

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.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [5 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0615s ][ Queries: 12 (0.0112s) ][ GZIP on ]