sizeof('z') result unexpected [duplicate] - c

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)

Related

Why sizeof() operator gives different value for 'a' and "a" in C? [duplicate]

This question already has answers here:
Sizeof(char[]) in C
(8 answers)
Why the sizeof character constant is 4 bytes? [duplicate]
(2 answers)
Sizeof string literal
(2 answers)
Closed 4 years ago.
As far as I know a character constant, viz, 'a', is stored in ASCII format which is internally treated as integer, 97 in case of 'a', that's why sizeof('a') returns 4 on executing but when I use sizeof("a") it returns 2. I have not found any explanation regarding that yet.
My code:
#include <stdio.h>
void main()
{
int x,y;
x = sizeof('a');
y = sizeof("a");
printf("%d\n",x);
printf("%d",y);
}
which gives the output:
4
2
'a' is an integer. It has a size of 4 on most computers. It may also be something else, however. 2 is also common on more specialized hardware.
"a" is a string literal. There are two characters: a and \0. They have a size of 1 each, for a total of size 2. However, when you try to assign that, what you get usually is a const char* and that may have a different size.

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.

Why sizeof('\n') and sizeof(char) not equal? [duplicate]

This question already has answers here:
In C, why is sizeof(char) 1, when 'a' is an int?
(5 answers)
Logic behind sizeof() for character constants and function names [duplicate]
(2 answers)
Closed 8 years ago.
I've tested sizeof('\n') is 4, but if when it is assigned to a char variable, then it occupies 1 byte of memory.
char enter = '\n';
sizeof(enter); // 1
What are their sizes different?
The type of '\n' is int, therefore sizeof('\n') equals sizeof(int); on the other hand, sizeof(char) always equals 1.

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('a') is 4 in C? [duplicate]

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.

Resources