user567879
user567879

Reputation: 5349

Handling numbers in C

Couldnt understand how numbers are handled in C. Could anyone point to a good tutorial.

#include<stdio.h>
main()
{
    printf("%f",16.0/3.0);
}

This code gave: 5.333333

But

#include<stdio.h>
main()
{
    printf("%d",16.0/3.0);
}

Gave some garbage value: 1431655765

Then

#include<stdio.h>
main()
{
    int num;
    num=16.0/3.0;
    printf("%d",num);
}

Gives: 5

Then

#include<stdio.h>
main()
{
    float num;
    num=16/3;
    printf("%f",num);
}

Gives: 5.000000

Upvotes: 0

Views: 641

Answers (6)

Op De Cirkel
Op De Cirkel

Reputation: 29503

printf is declared as

int printf(const char *format, ...);

the first arg (format) is string, and the rest can be anything. How the rest of the arguments will be used depending on the format specifiers in format. If you have:

printf("%d%c", x, y);

x will be treated as int, y will be treated as char. So,

 printf("%f",16.0/3.0);

is ok, since you ask for float double (%f), pass float double(16.0/3.0)

printf("%d",16.0/3.0);

you ask for int(%d), you pass float double (double and int have different internal representation) so, the bit representation of 16.0/3.0 (double) corresponds to bit representation of 1431655765(int).

 int num;
 num=16.0/3.0;

compiler knows that you are assigning to int, and converts it for you. Note that this is different than the previous case.

Upvotes: 3

DeeP
DeeP

Reputation: 11

You can understand numbers in C by using concept of Implecit Type Conversion.

During Evaluation of any Expression it adheres to very strict rules of type Conversion. and your answer of expression is depends on this type conversion rules.

If the oparands are of different types ,the 'lower' type is automatically converted into the 'higher' type before the operation proceeds. the result is of the higher type.

1: All short and char are automatically converted to int then

2: if one of the operands is int and the other is float, the int is converted into float because float is higher than an ** int**.

if you want more information about inplicit conversion you have to refer the book Programming in ANSI C by E Balagurusamy.

Thanks. Bye:DeeP

Upvotes: 1

Prince John Wesley
Prince John Wesley

Reputation: 63708

printf("%d",16.0/3.0);

The result of 16.0/3.0 is 5.333333 which is represented in Single precision floating-point format as follows

0 | 10101010 | 10101010101010101010101

If you read it as 32bit integer value, the result would be 1431655765.


num=16.0/3.0;

is equivalent to num = (int)(16.0/3.0). This converts the result of float value(5.33333) to integer(5).


printf("%f",num);

is same as printf("%f",(float)num);

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70959

printf formats a bit of memory into a human readable string. If you specify that the bit of memory should be considered a floating point number, you'll get the correct representation of a floating point number; however, if you specify that the bit of memory should be considered an integer and it is a floating point number, you'll get garbage.

Upvotes: 0

LavaSlider
LavaSlider

Reputation: 2514

It is because the formatting strings you are choosing do not match the arguments you are passing. I suggest looking at the documentation on printf. If you have "%d" it expects an integer value, how that value is stored is irrelevant and likely machine dependent. If you have a "%f" it expects a floating point number, also likely machine dependent. If you do:

printf( "%f", <<integer>> );

the printf procedure will look for a floating point number where you have given an integer but it doesn't know its and integer it just looks for the appropriate number of bytes and assumes that you have put the correct things there.

16.0/3.0 is a float
int num = 16.0/3.0 is a float converted to an int
16/3 is an int
float num = 16/3 is an int converted to a float

You can search the web for printf documentation. One page is at http://linux.die.net/man/3/printf

Upvotes: 1

Dair
Dair

Reputation: 16250

Ok, the first 1 is giving correct value as expected.

Second one you are passing a float while it is treating it as an int (hence the "%d" which is for displaying int datatypes, it is a little complicated to explain why and since it appears your just starting I wouldn't worry about why "%d" does this when passed a float) reading it wrong therefore giving you a wierd value. (not a garbage value though).

Third one it makes 16.0/3.0 an int while assigning it to the int datatype which will result in 5. Because while making the float an int it strips the decimals regardless of rounding.

In the fourth the right hand side (16/3) is treated as an int because you don't have the .0 zero at the end. It evaluates that then assigns 5 to float num. Thus explaining the output.

Upvotes: 2

Related Questions