Introduction to Characters

The main data types used in C, including numbers, strings, and arrays, will each have its own chapter in this book, but a more basic type, the character, deserves a little discussion as well. Unlike the number types, character variables can be used for non-numeric characters such as letters and punctuation.

Creating a character variable is easy:

char initial;

Assigning values to character variables is only slightly more complicated, requiring the use of single (straight) quotation marks:

char right_answer;
right_answer = 'D';

Notice that a simple char variable contains only a single character. In the next section you'll learn how to make longer chars.

To print the value of a character using the printf() function, ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.