By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 985 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

sizeof command in c

P: 1
hi,
in a small program i just typed


printf("%d", sizeof("3"));

i got output 2

can any one explain about this?
Jun 19 '07 #1
Share this Question
Share on Google+
7 Replies


10K+
P: 13,264
hi,
in a small program i just typed


printf("%d", sizeof("3"));

i got output 2

can any one explain about this?
Hi and welcome to TSDN. This is a C question and so I'll move it to that forum.
What are your guesses as to the output?
Jun 19 '07 #2

Meetee
Expert Mod 100+
P: 931
hi,
in a small program i just typed


printf("%d", sizeof("3"));

i got output 2

can any one explain about this?
Here you want the size of int which is 2. Integer has size of 2 bytes. Different data types has different sizes.

Regards
Jun 19 '07 #3

Expert 100+
P: 181
Here you want the size of int which is 2. Integer has size of 2 bytes. Different data types has different sizes.

Regards
here "3 " is treated as string constant with null termination not integer. so u have 2 characters '3' and null hence the size is two.
Now a days most of the compiler uses 4 bytes for int and it depends on what standard they use.
Jun 19 '07 #4

Meetee
Expert Mod 100+
P: 931
here "3 " is treated as string constant with null termination not integer. so u have 2 characters '3' and null hence the size is two.
Now a days most of the compiler uses 4 bytes for int and it depends on what standard they use.
Yes, size of integer is compiler dependent. I took 3 as integer but you r right :)
Jun 19 '07 #5

P: 6
Its a string ("3")

so we have 2 char data type

one is '3' and another is '\0' since its a string (strings ends with '\0')


totally you will get its size as 2
Jun 19 '07 #6

P: 7
i dont think it works on this way.

As the Sizeof "int" tend to be the natural word size of the processor.
If ur processor is of 16-bits, then sizeof (int) will return 2, n if it is of 32 bits then sizeof (int ) will return 4 .
Jun 20 '07 #7

100+
P: 208
i dont think it works on this way.

As the Sizeof "int" tend to be the natural word size of the processor.
If ur processor is of 16-bits, then sizeof (int) will return 2, n if it is of 32 bits then sizeof (int ) will return 4 .
Notice the difference between
sizeof(3);
AND
sizeof("3");

The first case its sizeof(int) in the second it's not
Jun 20 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.