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

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.

Related

How does the %(modulus) operator in c works? [duplicate]

This question already has answers here:
What is special about numbers starting with zero?
(4 answers)
Closed 3 years ago.
I was trying the following code
printf("%d", 010 % 10);
I was expecting it output be 0, but it is 8.
Why? Is there any way to get the last digit of an integer which is taken as input.
Any numeric literal in c or c++ starting with a zero will be interpreted as octal
So your calculation is 8 modulo 10, which is 8

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('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.

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