1

I think title is self-explanatory, but for better format.

Is sizeof(unsigned char) also guaranteed to be 1?


I don't believe this question to be a duplicate because:

  • C is different from C++ (tag excerpt: ...It was originally designed as an extension to C, and keeps a similar syntax, but is now a completely different language...).
  • char can be either signed or unsigned. There's no reason to assume that sizeof(unsigned char) == sizeof(char) unless you get assured by the standard, hence the question.
  • Even if you confuse C with C++, answers do not overlap.
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
mip
  • 8,355
  • 6
  • 53
  • 72
  • O.K. but tell me, how is it a duplicate? – mip Dec 14 '14 at 00:05
  • @remyabel I've read it and? That answers my question, not if there are machines where sizeof(char) != 1. – mip Dec 14 '14 at 00:08
  • The answer doesn't change between C and C++. The duplicate answers your question. What more do you want? –  Dec 14 '14 at 00:10
  • 1
    @remyabel Of course it could change, because C is not C++. And I am asking about completely different thing. `char` can be either signed or unsigned, there's no guarantee that `char` = `unsigned char`. – mip Dec 14 '14 at 00:13
  • And your proof is where? –  Dec 14 '14 at 00:14
  • 4
    @remyabel: The answer to another question answering this one is not the condition for duplication. They are only duplicates if the *questions* are duplicates. There is certainly no reason to simply assume that this is the same in C++ or that `unsigned char` and `char` are the same. – Puppy Dec 14 '14 at 00:17
  • http://stackoverflow.com/questions/13169451/do-i-have-the-guarantee-that-sizeoftype-sizeofunsigned-type?rq=1 – Tunaki Feb 09 '17 at 16:05
  • @Tunaki "strongly related" is an understatement - *that* question is truly an exact duplicate, unlike the one David Heffernan selected. I was considering reopening the question until I followed the link. Doc, when an edit puts your question back up on the front page you're going to attract votes both up and down from people who won't bother to tell you why. So the edit didn't affect points directly, it's just a side effect. – Mark Ransom Feb 09 '17 at 17:00
  • @MarkRansom I agree that this question can be marked as duplicate of the one mentioned by Tunaki, which I overlooked before. Possibly someone could change this, cause what David has done really hurts my eyes. – mip Feb 09 '17 at 17:06
  • @MarkRansom I meant bullet points in the list!!! Not the votes! – mip Feb 09 '17 at 17:19
  • I don't get it. Why do you feel the current duplicate doesn't answer your question. It has *When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.* – NathanOliver Feb 09 '17 at 18:26
  • 1
    @NathanOliver questions are about principles. To ask if `sizeof(char)` is always 1 and to ask if `sizeof(char) == sizeof(unsigned char)` is a completely different question. Moreover current duplicate is about C. When you want to find an answer you look at C++ reference and not C, becuase C is not a subset of C++ contrary to the popular belief. Tag excerpt should bring your attention. http://www.cprogramming.com/tutorial/c-vs-c++.html – mip Feb 09 '17 at 18:35
  • I get that C is not a subset of C++ but in this case they have the exact same behavior as it was taken directly from C. There are times when dupe closing a C++ question to a C question is okay and IMHO this is one of them. The question exactly answers what you ask. – NathanOliver Feb 09 '17 at 18:39
  • @NathanOliver don't you think it can confuse newbie programmers, when you mark C++ question as duplicate of C question? When I look for an answer, I don't search for unrelated questions and checkj if they have some remarks, which can answer my question. Note that when you mark question as duplicate you prevent users from providing answers, which could explain question in a better way. – mip Feb 09 '17 at 18:50
  • TBH I would close this question, because I believe it is one of my shittiest questions, if I could. But it has received answers, so i shouldn't delete it. Yet I don't agree that it is a duplicate (of C question). – mip Feb 09 '17 at 18:52

2 Answers2

8

Yes.

[expr.sizeof]/1:

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1.

Also, [basic.fundamental]/1:

A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
8

Reference: http://en.cppreference.com/w/cpp/language/sizeof

sizeof(char), sizeof(signed char), and sizeof(unsigned char) always return 1.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Harrison Ray
  • 159
  • 3