473,503 Members | 11,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof command in c

1 New Member
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
7 4078
r035198x
13,262 MVP
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
931 Recognized Expert Moderator Contributor
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
svlsr2000
181 Recognized Expert New Member
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
931 Recognized Expert Moderator Contributor
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
HEMANTHPG
6 New Member
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
Avi23
7 New Member
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
Silent1Mezzo
208 New Member
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

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

Similar topics

3
3535
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
0
421
by: besaar | last post by:
i got the current project that got the serial no.of a hard disk for software protection but i got an error,if u solve it send me,thanx alllllll. 1.file1 Option Strict On Option Explicit On...
12
7567
by: Anil | last post by:
Hi, Where can I get the source code that has the implementation of the below C operators/functions: malloc sizeof new strcmp strtok
5
6175
by: Chris McDonald | last post by:
I've been trying to wean myself off using parentheses after the sizeof operator (and after the return keyword, too), but my understanding is challenged by the 4th use of sizeof in the following...
90
8279
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
56
3802
by: William Xu | last post by:
I test it with char, short, int, long, float, double and struct pointers, all return 4 bytes. Does the standard say anything about sizeof a pointer? (I didn't find it in the standard...) --...
2
1361
by: =?Utf-8?B?RnJhbg==?= | last post by:
sHi! I have a very strange problem with the behaviour of the sizeof command. I have tested it in vc6,vc 2001 and vc 2003. The problem is quite simple. When I made a sizeof to the next struct,...
4
2130
by: Sebastian Fahr | last post by:
Hey there I have a problem with my own Objects. I have to write a programm in school that read objects from a binary file. This Object has the following structure: #pragma once class...
16
3473
by: RichG | last post by:
Question for all... I'm trying to determine the size of a global structure. It has to be declared globally because it's used in several different files. When compiling (BCC32) the code I get the...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.