Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

454
Views
Why is sizeof a char literal not the same as sizeof(char)?

The program

#include <stdio.h>
int main(void) {
    printf("sizeof( char ) = %zu, sizeof 'a' = %zu.\n", sizeof( char ), sizeof 'a' );
    return 0;
}

outputs the following:

sizeof( char ) = 1, sizeof 'a' = 4.

I'm compiling with gcc (clang gives the same result) and these flags:

gcc -Wall -Wextra -Wswitch -pedantic -ansi -std=c11 -DDEBUG -ggdb3 -o

Section 6.5.3.4 paragraph 4 of the specification at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf says

4 When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

So I would expect sizeof the operand 'a' to be 1 because the type of 'a' is char, or is it being automatically "promoted" to an int, or something similar? (I notice that if I cast 'a' to char then sizeof( (char)'a' ) is 1).

Or am I looking at the wrong standard?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

In C opposite to C++ integer character constants (literals) have the type int.

So the value of the expression sizeof( 'a' ) is equal to the value of the expression sizeof( int ). While sizeof( char ) is always equal to 1.

From the C Standard (6.5.3.4 The sizeof and alignof operators)

4 When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1...

and (6.4.4.4 Character constants)

10 An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

Pay attention to that usually objects of the type char used in operations as operands or in expressions are converted to the type int due to the integer promotions.

over 4 years ago · Santiago Trujillo Report

0

Character constants (or more accurately, an integer character constant) have type int.

Section 6.4.4.4p2 of the C standard describes Character Constants:

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. A wide character constant is the same, except prefixed by the letter L, u, or U. With a few exceptions detailed later, the elements of the sequence are any members of the source character set; they are mapped in an implementation-defined manner to members of the execution character set

And the semantics, which include a description of the type are first described in paragraph 10:

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int

over 4 years ago · Santiago Trujillo Report

0

In C language, character literal is not a char type. C considers character literal as integer. So, there is no difference between sizeof('a') and sizeof(1).

size of character literal is equal to size of integer

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!