Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: May 23, 2024
Difficulty: Easy

Character Set in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In C programming language, a character set is defined as a set of characters that can be used to write the source code. This blog will discuss the types and importance of character set in C, followed by their conversion. Later we will also print all the characters in the C character set with the help of a C program.

Character set in C

What is Character Set in C?

In C, a character set refers to a predefined collection of characters recognized by the language. This set typically includes the alphabet (both lowercase and uppercase), digits, special symbols, and control characters. The most commonly used character set in C is ASCII (American Standard Code for Information Interchange), which assigns numeric codes to characters. These codes are used to represent characters in memory and facilitate operations involving characters, such as input/output operations, string manipulation, and comparison.

ASCII valueContext
32space
48-570-9
65-90A-Z
97-122a-z
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Use of Character Set in C

It is used to specify the acceptable characters that can be inserted into the source code or interpreted while the programme is executing.

In C, the character set is applied in the following ways:

  • Character representation: The character set represents characters in the source programme. The ASCII code 65, for instance, is used to represent the letter A.
     
  • To represent special symbols: In the source programme, special symbols are represented by the character set. For instance, addition in C is denoted by the symbol +.
     
  • To create words and phrases: In the source programme, words and expressions are created using the character set. For instance, the letters h, e, l, and o combine to make the word hello.

Types of Character set in C

Types of character set in C

C programming language supports four types of characters:

  • Alphabets
  • Digits 
  • Special characters
  • White space

Alphabets

The C programming language allows for all upper- and lower-case alphabets in its programs.

Lower Case alphabets: a-z (26 characters)

Upper case alphabets: A-Z (26 characters)

Digits

The C programming language also supports digits for building numeric expressions.

Digits: 0-9 (10 characters) 

Special Characters

Certain special characters are used in C programs in arithmetic expressions, punctuation marks, conditional operators, etc. These characters can also be used in defining the attributes of a program in a better way.

Special Characters: ` ~ @ ! $ # ^ * % & ( ) [ ] { } < > + = _ – | / \ ; : ‘ “ , . ? 

White spaces

The white space characters in C refer to vertical and horizontal spaces in the source code. 

White spaces: Blank Spaces, Tab, Carriage Return, New Line, horizontal tab, vertical tab, null

Special Characters in C

In C programming, special characters are often used to represent control characters or to format output. Here are some commonly used special characters:

  1. \n: Represents a newline character.
  2. \t: Represents a tab character.
  3. \b: Represents a backspace character.
  4. \r: Represents a carriage return character.
  5. \: Represents a backslash character.
  6. ': Represents a single quote character.
  7. ": Represents a double quote character.

The various character sets that can be used with the C language are shown in the following table:

Type of CharacterDescription
Uppercase Alphabets  A-Z (26 characters)
Lowercase Alphabetsa-z (26 characters)
Digits 0-9 (10 characters) 
White spacesBlank Spaces, Tab, Carriage Return, New Line, horizontal tab, vertical tab, null
Special Characters@ ! $ # ^ * % & ( ) [ ] { } < > + = _ – | / \ ; : ‘ “ , . ?

Character set conversion in C

The conversion of a string or a character from one character set to another is called charset conversion in C. This conversion is useful when the programmer works with data involving different character sets while reading and writing text files.

The standard library offering functions for character set conversion in C are iconv(), wcstombs(), mbstowcs(). While iconv() performs conversions between different character sets in C, wcstombs() and mbstowcs() perform conversions between wide characters and multibyte character strings, respectively, 

These libraries make managing and performing operations on multilingual text data easier. 

How to Print Characters of the C Charset

The programs below would help you print all the characters of the C charset.

C Program to print Characters from 0-9 using ASCII value
Code:

  • C

C

#include <stdio.h>
int main()
{
   int j;
   printf("ASCII ==> character\n");
   // loop to print all ASCII values from 48 to 57
   for(int j = 48; j <= 57; j++)
       // display the ASCII value to the corresponding j value
       printf("%d ==> %c\n", j, j);
   return 0;
}

Output:

ASCII ==> character
48 ==> 0
49 ==> 1
50 ==> 2
51 ==> 3
52 ==> 4
53 ==> 5
54 ==> 6
55 ==> 7
56 ==> 8
57 ==> 9 

 

C Program to print Characters from A-Z using ASCII value
Code:

  • C

C

#include <stdio.h>
int main()
{
   int j;
   printf("ASCII ==> character\n");
   // loop to print all ASCII values from 65 to 90
   for(int j = 65; j <= 90; j++)
       // display the ASCII value to the corresponding j value
       printf("%d ==> %c\n", j, j);
   return 0;
}

Output:

ASCII ==> character
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

 

C Program to print Characters from a-z using ASCII value
Code:

  • C

C

#include <stdio.h>
int main()
{
   int j;
   printf("ASCII ==> character\n");
   // loop to print all ASCII values from 97 to 122
   for(int j = 97; j <= 122; j++)
       // display the ASCII value to the corresponding j value
       printf("%d ==> %c\n", j, j);
   return 0;
}

Output:

ASCII ==> character
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
 

Importance of Character Set in C

The importance of the Character set in C are:

  • Character set in C allows programmers to compose the program statements.
     
  • The Character set in C supports different languages and scripts, thus allowing programmers to work with multilingual text data.
     
  • Character set conversion is very useful for reading and writing different text files.
     
  • Communication between different networks involves dealing with different character sets. Thus charset conversion plays an important part here for encoding and decoding purposes.

Frequently Asked Questions

What is a special symbol in C?

In C programming, a special symbol refers to characters like +, -, *, /, =, etc., used for arithmetic operations, assignment, or comparison.

What are the special characters?

Special characters in C include newline (\n), tab (\t), backslash (\), single quote ('), double quote (") used for formatting and representing non-printable characters.

How to find special characters in a string in C?

To find special characters in a string in C, you can iterate through the string using a loop and check each character against a list of special characters using conditional statements or functions like isalpha() or isdigit().

Conclusion 

This blog has covered all the aspects of Character Set in C. Understanding the character set in C is fundamental for effective programming. Special symbols and characters play crucial roles in formatting output, representing non-printable characters, and performing various operations.

We hope this blog has helped you understand the character set in C better. Keep learning! We recommend you read some of our other articles on C: 

  1. Data types in C
  2. Ternary Operator in C
  3. Conditional Operator in C
  4. Bubble Sort Program in C
     

Best of Luck! 

Topics covered
1.
Introduction
2.
What is Character Set in C?
3.
Use of Character Set in C
4.
Types of Character set in C
4.1.
Alphabets
4.2.
Digits
4.3.
Special Characters
4.4.
White spaces
5.
Special Characters in C
6.
Character set conversion in C
7.
How to Print Characters of the C Charset
7.1.
C
7.2.
C
7.3.
C
8.
Importance of Character Set in C
9.
Frequently Asked Questions
9.1.
What is a special symbol in C?
9.2.
What are the special characters?
9.3.
How to find special characters in a string in C?
10.
Conclusion