C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 14)
14.
What will be the output of the program in 16-bit platform (Turbo C under DOS) ?
#include<stdio.h>

int main()
{
    printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));
    return 0;
}
8, 1, 4
4, 2, 8
4, 2, 4
10, 3, 4
Answer: Option
Explanation:

Step 1:

printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));

The sizeof function returns the size of the given expression.

sizeof(3.0f) is a floating point constant. The size of float is 4 bytes

sizeof('3') It converts '3' in to ASCII value.. The size of int is 2 bytes

sizeof(3.0) is a double constant. The size of double is 8 bytes

Hence the output of the program is 4,2,8

Note: The above program may produce different output in other platform due to the platform dependency of C compiler.

In Turbo C, 4 2 8. But in GCC, the output will be 4 4 8.

Discussion:
10 comments Page 1 of 1.

Om awasthi said:   1 decade ago
It will give answer 4, 1, 8.

Because 3.0f treats as float value which size 4 bit.
And '3' treats as char value which size 1 bit.

And 3.0 since data type is not declare here by default is treats as double data type which size 8 bit.

Protiti said:   7 years ago
If '3' gets converted into ASCII as per the description, then does the same happen on including any character within single quotes in the sizeof ()?

The output should be 1 byte, for the character.

Kirti said:   1 decade ago
When you use sizeof (3.0f) means you are dealing with float but as you use size of (3.0) it means it as a double. As you know float size is 4 and double size is 8.

Ila said:   1 decade ago
Yeah. It will print 4, 1, 8 in turbo c compiler. Online compiler is a gcc compliler which supports windows also. Thats why its output is changed.

Harshjoga said:   1 decade ago
It depends on the compiler whether it prints 418 or 428.

Because some of the compilers take the char data type as 2 byte.

And others as 1 byte.

Sudhir kushwaha said:   1 decade ago
When we write 3 in single inverted comma then how it can be of 2 byte. It prints 4 1 8 in Turbo C++ compiler.

Aman said:   1 decade ago
Yes. Same here. But, I dont understand the online compiler is giving the different answer i.e. 4,4,8.

Arun said:   1 decade ago
Can any diff b/w sizeof (3. 0f) and sizeof (3. 0). Anyone explain plzzzz.

Gyan maurya said:   1 decade ago
Please give clear and briefly description of sizeof(3) and sizeof(3.0).

Srivibha said:   1 decade ago
It prints 4 1 8 in Turbo C++ compiler.

Post your comments here:

Your comments will be displayed after verification.