why sizeof('a') is 4 in C? [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why are C character literals ints instead of chars?
#include<stdio.h>
int main(void)
{
char b = 'c';
printf("here size is %zu\n",sizeof('a'));
printf("here size is %zu",sizeof(b));
}
here output is (See live demo here.)
here size is 4
here size is 1
I am not getting why sizeof('a') is 4 ?

Because in C character constants, such as 'a' have the type int.
There's a C FAQ about this suject:
Perhaps surprisingly, character constants in C are of type int, so
sizeof('a') is sizeof(int) (though this is another area where C++
differs).

The following is the famous line from the famous C book - The C programming Language by Kernighan & Ritchie with respect to a character written between single quotes.
A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.
So sizeof('a') is equivalent to sizeof(int)

'a' by default is an integer and because of that you get size of int in your machine 4 bytes.
char is 1 bytes and because of this you get 1 bytes.

Related

Trouble comparing value in C [duplicate]

This question already has answers here:
How to convert integer to char in C? [duplicate]
(6 answers)
Closed 5 years ago.
I am having problem comparing an int value and a char value in C. Lets say I variable int value1 that is 0 and a char that has value '0'. I know that that char's value is actually an ascii number and that '0' is 48, but how do I compare int's 0 value with chars '0' value, in an if statement as a example?
You can use the idiom c - '0' to convert a char c to its equivalent digit.
This is an expression of type int.
Note that it works in any character encoding supported by the C Standard, since such an encoding must order 0 to 9 consecutively.

Difference between 'A' and "A" literals in C [duplicate]

This question already has answers here:
Difference between "3" and '3' in C
(5 answers)
Closed 9 years ago.
Am I right in saying that the difference between 'A' and "A" in C is:
'A' - Means a character 1 byte (8 bits) long containing A.
"A" - Means a string which is 2 bytes (16 bits) long which holds an A and a NULL character.
Are my explanations correct?
Thanks.
You are absolutely correct.But you got to bear in mind that when you use those as rvalues they are different.One has the type char, and can be used as an int as it is implicitly converted to the ASCII value, while the other has type char*.
Let me illustrate my point with a code if that helps:
int num='A'; //Valid, assigns 65 to num
char test=65; //Valid, as test will be 'A' after this
char *ptr="A" //Valid, assigns address of string "A" to pointer ptr
printf("%c,%d",'A','A'); // Output will be A,65
printf("%p",(void*)"A"); //Will print address where string "A" is
printf("%c","A"); ///WRONG
printf("%s","A"); //Works
Edit For the finer nuances, if you feel your understanding is up to that mark yet,refer to Mat's comment.Else read it after a few weeks when you have advanced further in your study of C.
Basically yes. The main nuance is byte vs. char. Where you say byte, you should say char. On most systems a char is one byte. There are a few that use larger objects to store a char.

C: Explain sizeof behaviour [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Size of character ('a') in C/C++
Can someone explain why in C sizeof(char) = 1 and sizeof(name[0]) = 1 but sizeof('a') = 4?
name[0] in this case would be char name[1] = {'a'};
I've tried to read through C's documentation to get this but I simply don't get it! if sizeof('a') and sizeof(name[0]) were both 4 I would get it, if they were both 1 that would make sense... but I don't get the discrepancy!
In C, character literals such as 'a' have type int, and hence sizeof('a') is equal to sizeof(int).
In C++, character literals have type char, and thus sizeof('a') is equal to sizeof(char).
References:
C99 Standard: 6.4.4.4 Character constants
Para 2:
An integer character constant is a sequence of one or more multibyte characters enclosed
in single-quotes, as in ’x’ or ’ab’.
C++03 Standard: 2.13.2 Character literals
Para 1:
A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by the letterL, as in L’x’. A character literal that does not begin with L is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set.
In c, sizeof operator consider 'a' as integer so you are getting 4 as a size

Why sizeof('c') is returning 4 instead of 1? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why are C character literals ints instead of chars?
http://ideone.com/lHYY8
int main(void)
{
printf("%d %d\n", sizeof('c'), sizeof(char));
return 0;
}
Why does sizeof('c') return 4 instead of 1?
Because in C character constants have the type int, not char. So sizeof('c') == sizeof(int). Refer to this C FAQ
Perhaps surprisingly, character constants in C are of type int, so
sizeof('a') is sizeof(int) (though this is another area where C++
differs).
One (possibly even more extreme) oddity that also somehow justifies this, is the fact that character literals are not limited to being single character.
Try this:
printf("%d\n", 'xy');
This is sometimes useful when dealing with e.g. binary file formats that use 32-bit "chunk" identifiers, such as PNG. You can do things like this:
const int chunk = read_chunk_from_file(...);
if(chunk == 'IHDR')
process_image_header(...);
There might be portability issues with code like this though, of course the above snippet assumes that read_chunk_from_file() magically does the right thing to transform the big-endian 32-bit value found in the PNG file into something that matches the value of the corresponding multi-character character literal.
The following is the famous line from the famous C book - The C programming Language by Kernighan & Ritchie with respect to a character written between single quotes.
A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.
So sizeof('a') is equivalent to sizeof(int)
And this question is a duplicate of why sizeof('a') is 4 in C?
cnicutar is completely right of course. I just wanted to add the reason for this. If you look at functions line fgetc, you'll notice that it also returns an int. It's because a char can represent any character from 0x00 to 0xFF, but an additional value is needed in order to represent EOF. So functions that return a character from input or a file often return an int, which can be compared with EOF, which is usually defined to be -1, but it can be anything that isn't a valid character.

sizeof('z') result unexpected [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Size of character ('a') in C/C++
Why does this program output 4 and not 1?
void main()
{
printf("%d",int(sizeof('z')));
}
'z' is a character and sizeof('z') must print 1?
'z' is a character literal and in C a character literal is of type int. So sizeof('z') equals sizeof(int) on your implementation.
Sizeof char.
Perhaps surprisingly, character
constants in C are of type int, so
sizeof('a') is sizeof(int)

Resources