Character Set in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Character Set includes a set of valid characters we can use in our program in different environments. C language has broadly two character sets.

  • Source Character Set (SCS): SCS is used to parse the source code into internal representation before preprocessing phase. This set includes Basic Character Set and White-space Characters.
  • Execution Character Set (ECS): ECS is used to store character string constants. Other than Basic Character Set, this set contains Control Characters and Escape Sequences.

Character Set in C

In C, the character set used to represent characters in source code is based on the ASCII (American Standard Code for Information Interchange) character set. The ASCII character set includes the following elements:

  1. Alphabetic Characters (A-Z, a-z): These represent both uppercase and lowercase English letters. For example, 'A' to 'Z' and 'a' to 'z'.

  2. Digits (0-9): These represent numeric characters. For example, '0' to '9'.

  3. Special Characters: These include various special characters used in programming and text processing. Some common special characters include:

    • Arithmetic Operators: +, -, *, /, %
    • Relational Operators: <, >, ==, !=, <=, >=
    • Logical Operators: &&, ||, !
    • Punctuation: ;, :, ,, ., ?, !
    • Brackets and Parentheses: (, ), [, ], {, }
    • Quotes: ', "
    • Backslash: \
    • Ampersand: &
    • Dollar Sign: $
    • Hash or Pound Sign: #
  4. Whitespace Characters: These include space (' '), tab ('\t'), newline ('\n'), carriage return ('\r'), and form feed ('\f'). These characters are used for formatting and layout in code and text.

  5. Control Characters: These include non-printable characters like the escape character ('\e') and others used for control and formatting, such as '\b' (backspace), '\t' (tab), and '\n' (newline).

  6. Escape Sequences: C allows you to use escape sequences to represent characters that are difficult to type or invisible. For example, '\n' represents a newline character, and '\t' represents a tab character.

  7. Extended Characters: C also includes characters beyond the ASCII set, especially for international character encoding. These include characters with accents, diacritics, and symbols from various languages.

It's important to note that the C standard library, such as <ctype.h>, provides functions and macros for character handling and manipulation, making it easier to work with characters and strings in C programs. The character set serves as the basis for representing and processing text and characters in C code.

Use of Character Set in C

The character set in C, which is primarily based on the ASCII character set, is fundamental to working with characters and text in C programming. Here are several key uses of the character set in C:

  1. Declaring and Storing Character Variables: In C, you can declare character variables to store individual characters. For example:

  2. String Handling: C represents strings as arrays of characters. The character set is used extensively in creating and manipulating strings. For example:

  3. Input and Output: The character set is used when reading input and displaying output using functions like printf and scanf. For example:

  4. Comparisons: Characters can be compared using relational operators, and string comparisons are essential for tasks like sorting and searching within strings.

  5. Conversions: You can convert between characters and other data types, such as integers and floating-point numbers. This is helpful for character manipulation and arithmetic.

  6. Character Constants: Characters are used as constants, like newline characters ('\n') or escape sequences, in control statements and text processing.

  7. Loop Control: Characters play a role in loop control, especially in character-by-character processing. For instance, reading characters in a loop to process a file character by character.

  8. Character Classification: The character set is used for character classification functions provided by the <ctype.h> library, such as isalpha, isdigit, and islower, to check character properties.

  9. Working with Text Data: C programming often involves text processing, such as parsing data from files or user input. The character set is essential for these tasks.

The character set forms the foundation for working with textual data, and it underlies many of the operations you perform when dealing with characters and strings in C programming. It allows you to represent, manipulate, and process text efficiently and effectively in C programs.

Types of Characters in C

Digits

In C, characters can be categorized into several types. One of these types is "Digits." Digits represent numeric characters. In the ASCII character set, the digits range from '0' to '9'. They are commonly used for numerical values and calculations in C programming. Below, I'll explain digits, provide examples, and show how they can be used in C code.

Digits in C:

Digits in C are the numeric characters '0' through '9'. They are used to represent numerical values and are fundamental for arithmetic operations, numerical input, and output. Digits are represented as characters, but they can be converted to integer values when needed.

Examples:

  • '0' is the character representation of the digit 0.
  • '1' is the character representation of the digit 1.
  • '9' is the character representation of the digit 9.

Usage in C Code:

  1. Displaying Digits:

    You can use printf to display digits:

  2. Converting Digits to Integers:

    You can convert a digit character to its corresponding integer using arithmetic:

  3. User Input of Digits:

    You can read digits as characters using scanf:

  4. Arithmetic Operations:

    Digits can be used in arithmetic operations:

Code Example:

Here's a simple C program that takes two digits as input, adds them, and displays the result:

Output:

If you enter '5' and '3' as input, the program will calculate and display the sum:

In this example, the program reads the input digits as characters, converts them to integers, and performs the addition.

Alphabets

In C, characters can be categorized into several types. One of the primary categorizations is based on character classification, which includes alphabets, digits, whitespace characters, and special characters. Let's focus on alphabets in this explanation.

Alphabets:

Alphabets are characters representing letters of the alphabet, both uppercase and lowercase. In C, characters are typically represented using single quotes (') and are enclosed within single quotes.

Here are examples of alphabetic characters:

  • Uppercase Alphabets: 'A', 'B', 'C', ..., 'Z'
  • Lowercase Alphabets: 'a', 'b', 'c', ..., 'z'

These characters are essential for representing textual data, variables, identifiers, and various forms of input and output. Alphabetic characters are widely used in C for tasks such as:

  1. Variable and Identifier Names: Alphabetic characters are fundamental for naming variables, functions, and identifiers in C. For example:

  2. String Handling: Alphabetic characters are used within strings, allowing you to work with text data. For instance:

  3. Character Comparison: Alphabetic characters are compared using relational operators, which is essential for tasks like sorting and searching within strings.

  4. Character Classification: Functions from the <ctype.h> library like isalpha are used to check if a character is an alphabet. For example:

  5. Conversions: You can convert between uppercase and lowercase alphabets or between alphabets and integers. This is helpful for character manipulation and arithmetic.

Alphabetic characters are fundamental to text processing and play a crucial role in various aspects of C programming, including variable naming, string handling, and character comparisons. They are versatile and widely used in many programming tasks.

Special Characters

In C, characters are divided into several categories based on their role and usage in programming. These categories include:

  1. Alphabetic Characters (A-Z, a-z):

    • Description: Alphabetic characters represent both uppercase and lowercase English letters.
    • Examples: 'A' to 'Z' and 'a' to 'z'.
    • Code Example:
  2. Digits (0-9):

    • Description: Digits represent numeric characters.
    • Examples: '0' to '9'.
    • Code Example:
  3. Special Characters:

    • Description: Special characters include various symbols used in programming and text processing.
    • Examples: +, -, *, /, %, =, <, >, !, &, |, ^, ~, ,, ., ;, :, ', ", ?, (), {}, [], $, #, and more.
    • Code Example:
  4. Whitespace Characters:

    • Description: Whitespace characters are used for formatting and layout in code and text. They include space (' '), tab ('\t'), newline ('\n'), carriage return ('\r'), and form feed ('\f').
    • Code Example:
  5. Control Characters:

    • Description: Control characters are non-printable characters used for control and formatting. Examples include '\b' (backspace), '\t' (tab), and '\n' (newline).
    • Code Example:
  6. Escape Sequences:

    • Description: Escape sequences are combinations of characters that represent special characters that are difficult to type or invisible. For example, '\n' represents a newline character, and '\t' represents a tab character.
    • Code Example:

These categories of characters play different roles in C programming and are used for various purposes, including arithmetic operations, comparisons, text manipulation, control flow, and more. Understanding these character types is crucial for effective C programming and text processing.

White Spaces

In C, characters can be categorized into several types, and understanding these character types is essential for text processing and character manipulation. Let's explore the different types of characters in C along with examples and code snippets:

  1. Alphabetic Characters:

    • Description: These are characters representing letters of the alphabet (both uppercase and lowercase).
    • Examples: 'A', 'b', 'Z', 'x', etc.
    • Code Example:
  2. Numeric Digits:

    • Description: These characters represent numeric digits from 0 to 9.
    • Examples: '0', '1', '9', etc.
    • Code Example:
  3. Special Characters:

    • Description: Special characters include various symbols and punctuation marks used in C programming.
    • Examples: '+', '-', '*', '/', '=', '%', '!', etc.
    • Code Example:
  4. Whitespace Characters:

    • Description: These are characters used for formatting, spacing, and layout.
    • Examples: ' ' (space), '\t' (tab), '\n' (newline), '\r' (carriage return), '\f' (form feed), etc.
    • Code Example:
  5. Control Characters:

    • Description: Control characters are non-printable characters used for control and formatting purposes.
    • Examples: '\b' (backspace), '\v' (vertical tab), '\a' (alert or bell), '\e' (escape), etc.
    • Code Example:
  6. Escape Sequences:

    • Description: These are combinations of characters, starting with a backslash (''), used to represent special characters or control sequences.
    • Examples: '\n' (newline), '\t' (tab), '\r' (carriage return), '\', ''', '"', etc.
    • Code Example:
  7. Extended Characters:

    • Description: C supports extended character sets for internationalization and special symbols beyond ASCII.
    • Examples: Characters with accents (e.g., 'é', 'ñ'), currency symbols (e.g., '€', '¥'), and various other symbols.
    • Code Example:
  8. Character Constants:

    • Description: These are pre-defined character constants representing specific characters, including newline ('\n') and carriage return ('\r').
    • Examples: '\n', '\r', '\t', etc.
    • Code Example:

These different character types are essential for various tasks in C programming, such as string manipulation, text processing, and input/output operations. Understanding their usage and characteristics is crucial for working effectively with characters in C.

Execution character set

In C, there are various types of characters, and understanding them is essential for effective character handling and text processing. These character types include:

  1. Execution Character Set:

    The execution character set is the set of characters supported by the C compiler and runtime environment for representing text in C programs. It includes a range of characters, such as alphabetic characters, digits, special characters, whitespace, and control characters.

    • Examples:

      • Alphabetic characters: 'A', 'a', 'B', 'z'
      • Digits: '0', '1', '2', '9'
      • Special characters: '+', '-', '*', '/', '!', '=', '>', '<', etc.
      • Whitespace characters: ' ' (space), '\t' (tab), '\n' (newline)
      • Control characters: '\b' (backspace), '\r' (carriage return), '\e' (escape), etc.
    • Code Example:

  2. Extended Characters:

    Extended characters represent characters beyond the basic ASCII character set. These characters are essential for supporting various languages and symbols. In C, extended characters are typically encoded using multibyte character encodings like UTF-8.

    • Examples:

      • Extended Latin characters with accents: 'é', 'ñ', 'ç'
      • Greek letters: 'α', 'β', 'γ'
      • Mathematical symbols: '∑', '∫', '≠'
  3. Escape Sequences:

    Escape sequences are special character combinations used to represent characters that are difficult to type or not visible. These sequences begin with a backslash ''. Common escape sequences include '\n' for a newline and '\t' for a tab.

    • Examples:

      • '\n': Represents a newline character.
      • '\t': Represents a tab character.
      • '\b': Represents a backspace character.
      • '\e': Represents an escape character.
    • Code Example:

  4. Wide Characters (wchar_t):

    Wide characters, represented by the data type wchar_t, are used for extended character sets and internationalization. They allow for the representation of characters from various languages and character sets. The wchar.h library provides functions and macros for working with wide characters.

    • Example:

Understanding the different types of characters is important when dealing with character manipulation, text processing, and supporting diverse character sets in C programs.

Escape Sequence

In C, characters can be categorized into various types, each with its own characteristics and uses. Here are some of the main types of characters in C, along with explanations and examples:

  1. Alphabetic Characters:

    • Description: Alphabetic characters represent letters from the alphabet, both uppercase and lowercase.
    • Examples: 'A', 'b', 'Z', 'm'
    • Code Example:
  2. Digit Characters:

    • Description: Digit characters represent numeric digits from 0 to 9.
    • Examples: '0', '7', '9'
    • Code Example:
  3. Special Characters:

    • Description: Special characters include various symbols, operators, and punctuation marks used in C programming.
    • Examples: '+', '-', '&', '*', '$', '?'
    • Code Example:
  4. Whitespace Characters:

    • Description: Whitespace characters are used for formatting and layout and include space (' '), tab ('\t'), newline ('\n'), carriage return ('\r'), and form feed ('\f').
    • Examples: ' ', '\t', '\n'
    • Code Example:
  5. Control Characters:

    • Description: Control characters are non-printable characters used for control and formatting, such as '\b' (backspace), '\t' (tab), and '\n' (newline).
    • Examples: '\b', '\t', '\n'
    • Code Example:
  6. Escape Sequences:

    • Description: Escape sequences are combinations of characters that have special meanings. They often start with a backslash ('') followed by one or more characters.
    • Examples: '\n' (newline), '\t' (tab), '\' (backslash), '"' (double quote), ''' (single quote)
    • Code Example:
  7. Extended Characters:

    • Description: Extended characters include characters beyond the ASCII set, especially for international character encoding. These can represent characters with accents, diacritics, and symbols from various languages.
    • Examples: 'é', 'ñ', 'Ω', '東'
    • Code Example:

These character types are essential for representing and processing text and data in C programming. Escape sequences, in particular, are used to represent non-printable characters and special characters in a format that is more human-readable. For example, '\n' represents a newline character, '\t' represents a tab character, '\' represents a backslash, and so on. Escape sequences are commonly used in strings and character constants in C code to make it more readable and maintainable.

Summary of Special Characters in C

Special CharacterDescription
+Addition or positive sign
-Subtraction or negative sign
*Multiplication
/Division
%Modulus (remainder)
<Less than
>Greater than
==Equal to
!=Not equal to
<=Less than or equal to
>=Greater than or equal to
&&Logical AND
||Logical OR
!Logical NOT
;Statement terminator
:Used in control structures like the switch statement
,Separator in lists and function arguments
.Member access operator for structures
?Conditional (ternary) operator
!Exclamation mark (Logical NOT)
&Bitwise AND and address-of operator
$Dollar sign (not used in standard C)
#Hash or pound sign (used in preprocessor directives)

Purpose of Character Set in C

ASCII Values

Control Characters

Control CharacterDecimal ValueHexadecimal Value
NUL (Null)000
SOH (Start of Header)101
STX (Start of Text)202
ETX (End of Text)303
EOT (End of Transmission)404
ENQ (Enquiry)505
ACK (Acknowledgment)606
BEL (Bell)707
BS (Backspace)808
HT (Horizontal Tab)909
LF (Line Feed)100A
VT (Vertical Tab)110B
FF (Form Feed)120C
CR (Carriage Return)130D
SO (Shift Out)140E
SI (Shift In)150F
DLE (Data Link Escape)1610
DC1 (Device Control 1)1711
DC2 (Device Control 2)1812
DC3 (Device Control 3)1913
DC4 (Device Control 4)2014
NAK (Negative Acknowledgment)2115
SYN (Synchronous Idle)2216
ETB (End of Transmission Block)2317
CAN (Cancel)2418
EM (End of Medium)2519
SUB (Substitute)261A
ESC (Escape)271B
FS (File Separator)281C
GS (Group Separator)291D
RS (Record Separator)301E
US (Unit Separator)311F
DEL (Delete)1277F

Printable Characters

ASCII ValueCharacter
32(space)
33!
34"
35#
36$
37%
38&
39'
40(
41)
42*
43+
44,
45-
46.
47/
480
491
502
513
524
535
546
557
568
579
58:
59;
60<
61=
62>
63?
64@
65A
66B
67C
68D
69E
70F
71G
72H
73I
74J
75K
76L
77M
78N
79O
80P
81Q
82R
83S
84T
85U
86V
87W
88X
89Y
90Z
91[
92\
93]
94^
95_
96`
97a
98b
99c
100d
101e
102f
103g
104h
105i
106j
107k
108l
109m
110n
111o
112p
113q
114r
115s
116t
117u
118v
119w
120x
121y
122z
123{
124|
125}
126~
127(DEL)

Character Equivalence

CharacterASCII ValueC Character Equivalence
'A'65Uppercase letter 'A'
'B'66Uppercase letter 'B'
'C'67Uppercase letter 'C'
'a'97Lowercase letter 'a'
'b'98Lowercase letter 'b'
'c'99Lowercase letter 'c'
'0'48Digit '0'
'1'49Digit '1'
'2'50Digit '2'
'\n'10Newline character
'\t'9Tab character
'\r'13Carriage return character
' '32Space character
'*'42Asterisk (*)
'%'37Percent sign (%)
'@'64At symbol (@)
'$'36Dollar sign ($)
'#'35Hash or pound sign (#)
'&'38Ampersand (&)

C program to print all the characters of C character Set

The C character set, also known as the ASCII character set, includes characters with integer values from 0 to 127. You can create a C program to print all these characters. Here's a simple C program to print all the characters of the C character set:

In this program:

  1. We use a for loop to iterate through character values from 0 to 127.

  2. Inside the loop, we use the printf function to print each character and its corresponding ASCII value.

When you run this program, it will display all the characters in the C character set, along with their ASCII values, in the console.

Please note that the program will only print the characters with ASCII values from 0 to 127. Some characters with ASCII values beyond 127 may be specific to extended character sets and are not part of the basic ASCII character set.

Commonly used characters in C with their ASCII values

The C character set, also known as the ASCII character set, includes characters with integer values from 0 to 127. You can create a C program to print all these characters. Here's a simple C program to print all the characters of the C character set:

In this program:

  1. We use a for loop to iterate through character values from 0 to 127.

  2. Inside the loop, we use the printf function to print each character and its corresponding ASCII value.

When you run this program, it will display all the characters in the C character set, along with their ASCII values, in the console.

Please note that the program will only print the characters with ASCII values from 0 to 127. Some characters with ASCII values beyond 127 may be specific to extended character sets and are not part of the basic ASCII character set.

Practice Problems on Character Set in C

Q1. Character Classification: Write a C program to input a character and determine whether it's an uppercase letter, a lowercase letter, a digit, or a special character. Print the corresponding category.

A.

Q2. Character Count: Create a program that counts the number of vowels (both uppercase and lowercase) in a given string. The program should also count the number of consonants and special characters in the string.

A.

Q3. Character Frequency: Write a program that counts the frequency of each character in a given string and displays the results. Ignore whitespace and consider both uppercase and lowercase letters as the same character.

A.

Q4. Palindrome Check: Develop a program to check if a given string is a palindrome, meaning it reads the same backward as forward. The program should ignore spaces, punctuation, and letter casing.

A.

Q5. Character Reversal: Create a program that takes a string as input and reverses the characters in the string. For example, if the input is "Hello," the program should output "olleH."

A.

FAQs

Q. What constitutes the white spaces in the C language?

A. In C, white spaces are characters used for formatting and separation within the source code. They include the following:

  • Space (' '): The space character is used to create space between words and other characters.

  • Tab ('\t'): The tab character is used to create horizontal indentation or separation between elements in the code.

  • Newline ('\n'): The newline character is used to represent the end of a line, and it's commonly used for formatting and line breaks.

  • Carriage Return ('\r'): The carriage return character is used to return the cursor to the beginning of the current line. It's often used in combination with newline to represent line breaks.

  • Form Feed ('\f'): The form feed character is used for form feed and page break functions.

White spaces are used to improve code readability, structure, and organization, but they are generally ignored by the C compiler, so they don't affect the program's logic.

Q. What are ASCII values in C?

A. ASCII (American Standard Code for Information Interchange) values in C represent the integer values associated with characters in the ASCII character set. Each character in this set is assigned a unique numeric code from 0 to 127, which can be used to represent characters in C programs. For example, 'A' corresponds to ASCII value 65, 'a' to ASCII value 97, '0' to ASCII value 48, and so on.

ASCII values are essential for character manipulation, comparison, and encoding in C programming.

Q. What is wchar_t?

A. wchar_t is a data type in C that is used to represent wide characters. Wide characters are used for internationalization and the representation of characters from various languages that are not covered by the basic ASCII character set. wchar_t is typically used in conjunction with wide-character functions for handling multibyte and wide-character encodings.

Q. What is the use of special characters if we have digits in the C language?

A. Special characters and digits serve different purposes in C programming:

  • Special Characters: These include symbols, punctuation marks, and operators that have specific meanings in C. They are used for operations like arithmetic, logical operations, control flow, and string manipulation. Special characters are crucial for expressing complex logic and program structure.

  • Digits (0-9): Digits are used to represent numeric values in C. While they can be used in arithmetic operations, their primary purpose is for working with numbers, performing mathematical calculations, and representing numerical data.

In C, both special characters and digits play distinct and essential roles in programming. They are not interchangeable because they serve different functions. Special characters are used for coding, control, and text manipulation, while digits are used for representing numeric values.

Conclusion

  • C language has two types of character sets namely: Source Character Set (SCS), Execution Character Set (ECS).
  • C Source code is converted into SCS by CPP before preprocessing. CPP converts character and string constants into ECS after preprocessing.
  • Space characters are visually blank but they affect the text. Control characters are visually absent, but they have different functions to perform such as causing a bell sound (\a), moving the cursor to the left (\b) etc.
  • ctype.h has a lot of utility functions to work with characters like isalpha, isdigit etc.