Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Character constants==== Individual character constants are single-quoted, e.g. {{code|'A'}}, and have type {{code|int}} (in C++, {{code|char}}). The difference is that {{code|"A"}} represents a null-terminated array of two characters, 'A' and '\0', whereas {{code|'A'}} directly represents the character value (65 if ASCII is used). The same backslash-escapes are supported as for strings, except that (of course) {{code|"}} can validly be used as a character without being escaped, whereas {{code|'}} must now be escaped. A character constant cannot be empty (i.e. {{code|''}} is invalid syntax), although a string may be (it still has the null terminating character). Multi-character constants (e.g. {{code|'xy'}}) are valid, although rarely useful β they let one store several characters in an integer (e.g. 4 ASCII characters can fit in a 32-bit integer, 8 in a 64-bit one). Since the order in which the characters are packed into an {{code|int}} is not specified (left to the implementation to define), portable use of multi-character constants is difficult. Nevertheless, in situations limited to a specific platform and the compiler implementation, multicharacter constants do find their use in specifying signatures. One common use case is the [[OSType]], where the combination of Classic Mac OS compilers and its inherent big-endianness means that bytes in the integer appear in the exact order of characters defined in the literal. The definition by popular "implementations" are in fact consistent: in GCC, Clang, and [[Visual C++]], {{code|'1234'}} yields <code>0x3'''1'''3'''2'''3'''3'''3'''4'''</code> under ASCII.<ref>{{cite web |title=The C Preprocessor: Implementation-defined behavior |url=https://gcc.gnu.org/onlinedocs/cpp/Implementation-defined-behavior.html |website=gcc.gnu.org}}</ref><ref>{{cite web |title=String and character literals (C++) |url=https://docs.microsoft.com/en-us/cpp/cpp/string-and-character-literals-cpp?view=vs-2019#code-try-2 |website=Visual C++ 19 Documentation |access-date=20 November 2019 |language=en-us}}</ref> Like string literals, character constants can also be modified by prefixes, for example {{code|L'A'}} has type {{code|wchar_t}} and represents the character value of "A" in the wide character encoding.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)