Character Set in C
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:
-
Alphabetic Characters (A-Z, a-z): These represent both uppercase and lowercase English letters. For example, 'A' to 'Z' and 'a' to 'z'.
-
Digits (0-9): These represent numeric characters. For example, '0' to '9'.
-
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: #
-
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.
-
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).
-
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.
-
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:
-
Declaring and Storing Character Variables: In C, you can declare character variables to store individual characters. For example:
-
String Handling: C represents strings as arrays of characters. The character set is used extensively in creating and manipulating strings. For example:
-
Input and Output: The character set is used when reading input and displaying output using functions like printf and scanf. For example:
-
Comparisons: Characters can be compared using relational operators, and string comparisons are essential for tasks like sorting and searching within strings.
-
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.
-
Character Constants: Characters are used as constants, like newline characters ('\n') or escape sequences, in control statements and text processing.
-
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.
-
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.
-
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:
-
Displaying Digits:
You can use printf to display digits:
-
Converting Digits to Integers:
You can convert a digit character to its corresponding integer using arithmetic:
-
User Input of Digits:
You can read digits as characters using scanf:
-
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:
-
Variable and Identifier Names: Alphabetic characters are fundamental for naming variables, functions, and identifiers in C. For example:
-
String Handling: Alphabetic characters are used within strings, allowing you to work with text data. For instance:
-
Character Comparison: Alphabetic characters are compared using relational operators, which is essential for tasks like sorting and searching within strings.
-
Character Classification: Functions from the <ctype.h> library like isalpha are used to check if a character is an alphabet. For example:
-
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:
-
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:
-
Digits (0-9):
- Description: Digits represent numeric characters.
- Examples: '0' to '9'.
- Code Example:
-
Special Characters:
- Description: Special characters include various symbols used in programming and text processing.
- Examples: +, -, *, /, %, =, <, >, !, &, |, ^, ~, ,, ., ;, :, ', ", ?, (), {}, [], $, #, and more.
- Code Example:
-
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:
-
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:
-
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:
-
Alphabetic Characters:
- Description: These are characters representing letters of the alphabet (both uppercase and lowercase).
- Examples: 'A', 'b', 'Z', 'x', etc.
- Code Example:
-
Numeric Digits:
- Description: These characters represent numeric digits from 0 to 9.
- Examples: '0', '1', '9', etc.
- Code Example:
-
Special Characters:
- Description: Special characters include various symbols and punctuation marks used in C programming.
- Examples: '+', '-', '*', '/', '=', '%', '!', etc.
- Code Example:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
-
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: '∑', '∫', '≠'
-
-
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:
-
-
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:
-
Alphabetic Characters:
- Description: Alphabetic characters represent letters from the alphabet, both uppercase and lowercase.
- Examples: 'A', 'b', 'Z', 'm'
- Code Example:
-
Digit Characters:
- Description: Digit characters represent numeric digits from 0 to 9.
- Examples: '0', '7', '9'
- Code Example:
-
Special Characters:
- Description: Special characters include various symbols, operators, and punctuation marks used in C programming.
- Examples: '+', '-', '&', '*', '$', '?'
- Code Example:
-
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:
-
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:
-
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:
-
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 Character | Description |
---|---|
+ | 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 Character | Decimal Value | Hexadecimal Value |
---|---|---|
NUL (Null) | 0 | 00 |
SOH (Start of Header) | 1 | 01 |
STX (Start of Text) | 2 | 02 |
ETX (End of Text) | 3 | 03 |
EOT (End of Transmission) | 4 | 04 |
ENQ (Enquiry) | 5 | 05 |
ACK (Acknowledgment) | 6 | 06 |
BEL (Bell) | 7 | 07 |
BS (Backspace) | 8 | 08 |
HT (Horizontal Tab) | 9 | 09 |
LF (Line Feed) | 10 | 0A |
VT (Vertical Tab) | 11 | 0B |
FF (Form Feed) | 12 | 0C |
CR (Carriage Return) | 13 | 0D |
SO (Shift Out) | 14 | 0E |
SI (Shift In) | 15 | 0F |
DLE (Data Link Escape) | 16 | 10 |
DC1 (Device Control 1) | 17 | 11 |
DC2 (Device Control 2) | 18 | 12 |
DC3 (Device Control 3) | 19 | 13 |
DC4 (Device Control 4) | 20 | 14 |
NAK (Negative Acknowledgment) | 21 | 15 |
SYN (Synchronous Idle) | 22 | 16 |
ETB (End of Transmission Block) | 23 | 17 |
CAN (Cancel) | 24 | 18 |
EM (End of Medium) | 25 | 19 |
SUB (Substitute) | 26 | 1A |
ESC (Escape) | 27 | 1B |
FS (File Separator) | 28 | 1C |
GS (Group Separator) | 29 | 1D |
RS (Record Separator) | 30 | 1E |
US (Unit Separator) | 31 | 1F |
DEL (Delete) | 127 | 7F |
Printable Characters
ASCII Value | Character |
---|---|
32 | (space) |
33 | ! |
34 | " |
35 | # |
36 | $ |
37 | % |
38 | & |
39 | ' |
40 | ( |
41 | ) |
42 | * |
43 | + |
44 | , |
45 | - |
46 | . |
47 | / |
48 | 0 |
49 | 1 |
50 | 2 |
51 | 3 |
52 | 4 |
53 | 5 |
54 | 6 |
55 | 7 |
56 | 8 |
57 | 9 |
58 | : |
59 | ; |
60 | < |
61 | = |
62 | > |
63 | ? |
64 | @ |
65 | A |
66 | B |
67 | C |
68 | D |
69 | E |
70 | F |
71 | G |
72 | H |
73 | I |
74 | J |
75 | K |
76 | L |
77 | M |
78 | N |
79 | O |
80 | P |
81 | Q |
82 | R |
83 | S |
84 | T |
85 | U |
86 | V |
87 | W |
88 | X |
89 | Y |
90 | Z |
91 | [ |
92 | \ |
93 | ] |
94 | ^ |
95 | _ |
96 | ` |
97 | a |
98 | b |
99 | c |
100 | d |
101 | e |
102 | f |
103 | g |
104 | h |
105 | i |
106 | j |
107 | k |
108 | l |
109 | m |
110 | n |
111 | o |
112 | p |
113 | q |
114 | r |
115 | s |
116 | t |
117 | u |
118 | v |
119 | w |
120 | x |
121 | y |
122 | z |
123 | { |
124 | | |
125 | } |
126 | ~ |
127 | (DEL) |
Character Equivalence
Character | ASCII Value | C Character Equivalence |
---|---|---|
'A' | 65 | Uppercase letter 'A' |
'B' | 66 | Uppercase letter 'B' |
'C' | 67 | Uppercase letter 'C' |
'a' | 97 | Lowercase letter 'a' |
'b' | 98 | Lowercase letter 'b' |
'c' | 99 | Lowercase letter 'c' |
'0' | 48 | Digit '0' |
'1' | 49 | Digit '1' |
'2' | 50 | Digit '2' |
'\n' | 10 | Newline character |
'\t' | 9 | Tab character |
'\r' | 13 | Carriage return character |
' ' | 32 | Space character |
'*' | 42 | Asterisk (*) |
'%' | 37 | Percent sign (%) |
'@' | 64 | At symbol (@) |
'$' | 36 | Dollar sign ($) |
'#' | 35 | Hash or pound sign (#) |
'&' | 38 | Ampersand (&) |
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:
-
We use a for loop to iterate through character values from 0 to 127.
-
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:
-
We use a for loop to iterate through character values from 0 to 127.
-
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.