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.

A Basic C programming question

Status
Not open for further replies.

mahaju

Full Member level 2
Joined
Mar 17, 2007
Messages
125
Helped
7
Reputation
14
Reaction score
2
Trophy points
1,298
Activity points
2,252
#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.

---------- Post added at 02:19 ---------- Previous post was at 02:04 ----------

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?
 

see this
it described how to convert float to hex.

and how do I see what hex value it keeps in memory for floating points?

access through a pointer to unsigned integer as both of them have same size. and print the value as hex. but it is not a fool-proof method.
Code:
#include<stdio.h>
int main()
{
        float a=2.5;
        unsigned long *p;
        p=&a;
        printf("%f\n",a);
        printf(" as int = %d\n", (int)a);
        printf(" as float = %f\n",a);
        printf(" as hex = %x\n", (long double)a);
        printf("%f\n",a);
        printf("a as hex =%x\n",*p);
        return 0;
}

output is
Code:
2.500000
 as int = 2
 as float = 2.500000
 as hex = 0
2.500000
a as hex =40200000
*NOTE: This is the out put of above code compiled with gcc under linux. the output will vary with compiler to compiler.
 
  • Like
Reactions: mahaju

    mahaju

    Points: 2
    Helpful Answer Positive Rating
In the line
printf(" as int = %d\n", (int)a);
is it necessary to explicitly type cast the float variable a?
I didn't do it, so ss that the reason why my code outputs 0?
And in
printf(" as hex = %x\n", (long double)a);
why did you type cast to long double, and I see it outputs 0 as well. Why is that?
Also,
printf("a as hex =%x\n",*p);
Can you give me a brief introduction to why this works, that is, why does it show the hex value?
Does it have anything to do with the fact that %x, %d etc and the data type long pertains to whole numbers while the data type float pertains to fractions?
 

In the line
printf(" as int = %d\n", (int)a);
is it necessary to explicitly type cast the float variable a?
Yes you need to or else how to tell the compiler that it is going to print a integer.( type demotion loose precession)
printf("a as hex =%x\n",*p);
Can you give me a brief introduction to why this works, that is, why does it show the hex value?
Does it have anything to do with the fact that %x, %d etc and the data type long pertains to whole numbers while the data type float pertains to fractions?

Why this works ??? Well you asked how it is put in the memory, Right??? So if it is in memory then it must be accessed through pointer. is not it?
So I choose a pointer of same size and assign the address to the pointer.
Now I need to print the value as a normal number in HEX i choose to print it as Unsigned Long.

But it is a hackish thing. it might not give result properly on all platform.
 
  • Like
Reactions: mahaju

    mahaju

    Points: 2
    Helpful Answer Positive Rating
dude
it is compiler dependent..we cannot predect the output correctly.....
 

please someone should explain the meaning of this c program to me urgently

ADC_Value = ADC_Read(2);

DisplayVolt = ADC_Value * 2;

volt[0] = DisplayVolt/1000 + 48;

volt[1] = (DisplayVolt/100)%10 + 48;

volt[3] = (DisplayVolt/10)%10 + 48;

Lcd_Out(2,5,volt);
 

it is a program to display in the lcd.. it may be written with some logic of the programmer. but in a simple way volt[0] [1][2] as used to convert adv value into unit , tens , hundred places of the value to display 3 digit value of adc....
 

@Samba007.Thank you,please if i want the program above to display my adc values in micro,milli and tens value how will i modify it.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top