473,508 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference in sizeof(char) and sizeof('char constant')

181 Recognized Expert New Member
Here is a small program which i saw a couple of years ago.

#include<stdio.h>
int main()
{
char x;

if(sizeof(int)==sizeof('a'))
printf("\n C compiler\n");
else
printf("C++ compiler\n");


}
output of program::
when stored as .c prints c compiler.
when stored as .cpp prints c++ compiler.

In c characters are stored as integer, then why does sizeof(char) or sizeof(x) [In above example x is a variable of type char] returns 1 in c, where as sizeof('a') returns 4?
Mar 26 '07 #1
1 2296
Banfa
9,065 Recognized Expert Moderator Expert
char is not an int and is guaranteed by the standard to have a sizeof 1. However in the C standard character constants, like 'a', are of type int.

In C++ character constants are of type char.

This program tries to take advantage of this, however it is not truly portable because it is possible to have a conforming C implementation where sizeof(int) == 1 although this would be unusual.
Mar 26 '07 #2

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

Similar topics

5
16545
by: nsgi_2004 | last post by:
I have a C book that says the sizeof a char is one byte. Is this true for C++?
17
9447
by: José de Paula | last post by:
Is it a given that sizeof(char) always yields 1, no matter the implementation? I ask because I saw some critics against char * ptr = malloc (sizeof(char) * NUM); in favour of simply char * ptr...
9
2995
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have...
9
4942
by: Sunner Sun | last post by:
Hi, all FAQ of comp.lang.c said "In C, type char is defined as occupying one byte, so it is usually 8 bits". (http://www.eskimo.com/~scs/cclass/notes/sx2a.html) But I found in C99 that "An...
0
412
by: davidb | last post by:
Hi, does someone know how to get the length of a 2 dimensional string array: here what i need: ---------------------------------------------------------------- char **getList(void){ char...
2
2587
by: arnaudk | last post by:
Why is sizeof(char*) = 4? I would have expected 1... Is it because a pointer is stored as an int ?
0
7226
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
7125
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...
0
7499
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...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
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
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
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.