The char data type is employed for storing individual characters.
Characters must be enclosed in single quotes, such as ‘A’ or ‘c’, and we utilize the %c format specifier to print them.
char myGrade = ‘A’; printf(“%c”, myGrade); |
Alternatively, if you’re acquainted with ASCII, you can utilize ASCII values to represent specific characters.
Note that these values are numerical and are not enclosed in quotes ( ” ).
char a = 65, b = 66, c = 67; printf(“%c”, a); printf(“%c”, b); printf(“%c”, c); |
Tip: You can refer to our ASCII Table Reference for a comprehensive list of all ASCII values.
Attempting to store more than a single character will result in only the last character being printed.
char myText = ‘Hello’; printf(“%c”, myText); |
Note: Avoid using the char type for storing multiple characters to prevent potential errors. |
For storing multiple characters or entire words, employ strings, which will be covered in detail in a subsequent chapter.
char myText[] = “Hello”; printf(“%s”, myText); |
For now, understand that we utilize strings for storing multiple characters or text, while the char type is used for single characters. |