liveBook · Manning

Char(n)

As explained in Modern C, Third Edition

char is a C character type that occupies exactly one byte and is intended to represent individual characters.

In C, char is the narrow integer type used for printable characters and text. It occupies exactly one byte, although its signedness can depend on the platform. section 5.2 section 12.1

One byte by definition

char letter = 'A'; sizeof(char) == 1 The declaration uses char for character data, while sizeof(char) is defined as 1. When letter participates in arithmetic, it is promoted to signed int before the operation. section 5.2 section 12.1

Keep these distinctions explicit:

  • Use char for character and string data. section 12.1
  • Treat char as distinct from signed char and unsigned char, even when implementations give types the same class or precision. section 5.2
  • Use unsigned char as the byte-level view of an object; every object can be viewed as unsigned char[sizeof A]. section 12.1
  • Remember that narrow integer types are promoted to signed int before arithmetic or comparison. section 5.2
Table 5.1 Base types according to the four main type classes. Types with a gray background don’t allow for arithmetic; they are promoted before doing arithmetic. Type char is special since it can behave like unsigned or signed, depending on the platform. All types in this table are considered to be distinct types, even if they have the same class and precision.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
ClassSystematic nameOther nameRank
IntegersUnsignedbool_Bool0
unsigned char1
unsigned short2
unsigned intunsigned3
unsigned long4
unsigned long long5
[Un]signedchar1
Signedsigned char1
signed shortshort2
signed intsigned or int3
signed longlong4
signed long longlong long5
Floating pointRealfloat
double
long double
Complexfloat _Complexfloat complex
double _Complexdouble complex
long double _Complexlong double complex

Use the base-type table to compare char with the other narrow types and to see its platform-dependent signedness. section 5.2

How Modern C, Third Edition covers it

How deep it goes
Defines size, signedness variation, object representation, and arithmetic promotion, but does not develop advanced pointer techniques.
Examples
Uses printable character data, sizeof(char), arithmetic promotion, and an unsigned-char view of an object.
What it assumes
Assumes basic C types and expressions, while defining narrow types, promotions, sizeof, and character-based object views.
What you can do after
Evaluate/understand char declarations, byte size, promotion behavior, and the distinction between character data and object representation.
How it connects
Connects char with strings, narrow integer promotion, signed char, unsigned char, sizeof, and the C memory model.

Also explained in

Compare how 5 books explain char(n) →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.