Since the nature of a char
in C++ is compiler-dependent when the unsigned
qualifier is not present, is there an argument I could pass on to GCC which would force all char
s to be compiled as unsigned
?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
You can use the option:
-funsigned-char
.source
The flag you are looking for is
-funsigned-char
.From the documentation:
You can use
-funsigned-char
for that, or-fsigned-char
for signed.As the other answers say, gcc's
-funsigned-char
option forces plainchar
to be unsigned.But that may not be the best solution to your problem. You want unsigned characters, but by using a compiler-specific option you're encoding that information in the build command (the Makefile, build script, or just the command you type to compile your code). If the semantics of your program depend on having unsigned characters, it's better to record that information in your source code. It's clearer, and it's reduces the chance that someone will build your program incorrectly.
If you want unsigned characters, using
unsigned char
. If you want signed characters, usesigned char
. If you just want characters, and you're sure your program's behavior doesn't depend on whether they're signed or unsigned (say, if all stored values are in the range 0..127), usechar
.Not enough reputation to comment @Keith's answer.
The main reason I see to use
-funsigned-char
or-fsigned-char
is when you want the following codeto display either
-128
or128
.The comment about not relying on specific compilation settings is valid but here it is a bit simplifying reality. The main problem is that C leaves some part of the language to the implementation for efficiency reason, and usually you want to tune that implementation to suit best your application. In my opinion a professional developer should always go through all compiler flags and select the best for his needs. If you rely on some specific settings, your unit tests will of course cover that case, or assert it. If you port an application, you will look into the compilation settings of the original port.