-2

I understand that "const" keyword makes the variable assigned to as a Read Only variable , However I don't know the reason why the size of a constant char is 4 bytes (GNU C compiler for ex.) making it different than size of the "char" which is one byte .

As stated on cquestions blog first question that 'A' is a const char

printf("%d",sizeof('A'));

Output: 4

Jay Shenawy
  • 953
  • 1
  • 12
  • 22
  • 1
    `printf("%d",sizeof(const char));` is `1`. – BLUEPIXY May 31 '15 at 23:03
  • 1
    You have not really tested the output of the sentence: `printf("%d",sizeof(const char));`. This denotes that you have not done enough research on your problem, since you have not tested the real output of the program. So: -1. – pablo1977 May 31 '15 at 23:07
  • @BLUEPIXY it's undefined behaviour as `%d` is the wrong format specifier for `size_t` – M.M May 31 '15 at 23:09
  • you are right , I thought I have tested it ,But I didn't that was actually taken from this blog that states this wrong note http://www.cquestions.com/2012/02/data-type-questions-in-c.html = – Jay Shenawy May 31 '15 at 23:09
  • 3
    `sizeof(char)` is 1. Not because a char has one byte (8 bits), but because that's what the standard defines. A qualifier does not change this. – too honest for this site May 31 '15 at 23:10
  • 1
    @Olaf in fact it has nothing to do with how many bits make up a `char`, it has to have type `1` in any implementation, and of course a qualifier does not affect that. – Iharob Al Asimi May 31 '15 at 23:12
  • @MattMcNabb yes, OP shoud be `printf("%zu",sizeof(const char));` or `printf("%d",(int)sizeof(const char));` – BLUEPIXY May 31 '15 at 23:13
  • @iharob: Didn't I write exactly that? – too honest for this site May 31 '15 at 23:13
  • @MohamedElShenawy `sizeof(const char)` does not appear on that page you linked – M.M May 31 '15 at 23:16
  • 1
    @Olaf: `sizeof (char) == 1` *precisely* because a `char` is one byte. A "byte" is not necessarily 8 bits; it is by definition the size of a ` char`. – Keith Thompson May 31 '15 at 23:17
  • @MattMcNabb : please navigate to the explanation of the first question, he is stating that 'A' is a constant character and that's why it has a different size – Jay Shenawy May 31 '15 at 23:18
  • 2
    @MohamedElShenawy he said "character constant", that is not the same as `const char`. "Character constant" means something like `'A'` for example. `const char` means an object with type `const char` . – M.M May 31 '15 at 23:19
  • 2
    @KeithThompson: Yes, I forgot something like "here: 8 bits" or so. Was a bit too brief; i should not expect others to try follow my thoughts. However, I think I stated that correctly: sizeof(char) is defined to be 1 (C11: 6.5.3.4#4). – too honest for this site May 31 '15 at 23:21
  • @MattMcNabb : Then this is because my lack of information, Thank you all – Jay Shenawy May 31 '15 at 23:22
  • One last question please considering the answer below: Char mychar= 'A'; Does 'A' being an (integer character constant of type int )assigned to a char make sense? @MattMcNabb – Jay Shenawy May 31 '15 at 23:39
  • @MohamedElShenawy yes, narrowing conversions are well-defined in C if the value is in range for the narrower type, which `'A'` is. – M.M May 31 '15 at 23:51
  • @Olaf: Yes, the direct statement in the standard is that `sizeof (char)` is 1. Other parts of the standard define the term "byte" and so forth. – Keith Thompson Jun 01 '15 at 01:13

1 Answers1

3

'A' has type int not const char, as well as all character constants.

This was extracted from the c11 draft

6.4.4.4 Character constants

  1. 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.

  1. 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.

I made bold the important part so it's immediately visible.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Actually I didn't know about this , However assuming I am printing the size of "const char" it's also 4 , what is the reason ? – Jay Shenawy May 31 '15 at 23:06
  • You learned wrong, `'C'` is `int`, not `const char` it's specified by the standard. No way `sizeof(const char)` is `4`, what compiler is that? Or is it `sizeof(const char *)`? it's not the same. – Iharob Al Asimi May 31 '15 at 23:07
  • No I was wrong , I took this from the answer of the first question here http://www.cquestions.com/2012/02/data-type-questions-in-c.html which I have learned from you now that it's wrong – Jay Shenawy May 31 '15 at 23:11
  • They give the write answer, since they explain how each compiler does it, some of them don't respect the standard perhaps, others implement `int` with `2` bytes. – Iharob Al Asimi May 31 '15 at 23:15
  • yes I was just screwed by their word that A is Character constant , I thought that means a const char. – Jay Shenawy May 31 '15 at 23:26