Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Character set of C

character:- It denotes any alphabet, digit or special symbol used to represent information.

Use:- These characters can be combined to form variables. C uses constants, variables, operators, keywords and expressions as building blocks to form a
basic C program.

Character set:- The character set is the fundamental raw material of any language and they are used to represent information. Like natural languages, computer
language will also have well defined character set, which is useful to build the programs.

The characters in C are grouped into the following two categories:

1. Source character set


a. Alphabets
b. Digits
c. Special Characters
d. White Spaces

2. Execution character set


a. Escape Sequence

Source character set

ALPHABETS
Uppercase letters A-Z
Lowercase letters a-z

DIGITS 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

SPECIAL CHARACTERS
~ tilde % percent sign | vertical bar @ at symbol + plus sign < less than

_ underscore - minus sign > greater than ^ caret # number sign = equal to

& ampersand $ dollar sign / slash ( left parenthesis * asterisk \ back slash

) right parenthesis ′ apostrophe : colon [ left bracket " quotation mark ; semicolon

] right bracket ! exclamation mark , comma { left flower brace ? Question mark . dot operator

} right flower brace

WHITESPACE CHARACTERS
\b blank space \t horizontal tab \v vertical tab \r carriage return \f form feed \n new line

\\ Back slash \’ Single quote \" Double quote \? Question mark \0 Null \a Alarm (bell)

Execution Character Set

Certain ASCII characters are unprintable, which means they are not displayed on the screen or printer. Those characters perform other functions aside from displaying text. Examples
are backspacing, moving to a newline, or ringing a bell.

They are used in output statements. Escape sequence usually consists of a backslash and a letter or a combination of digits. An escape sequence is considered as a single character but
a valid character constant.

These are employed at the time of execution of the program. Execution characters set are always represented by a backslash (\) followed by a character. Note that each one of character
constants represents one character, although they consist of two characters. These characters combinations are called as escape sequence.

Backslash character constants

Character ASCII value Escape Sequence Result

Null 000 \0 Null

Alarm (bell) 007 \a Beep Sound

Back space 008 \b Moves previous position

Horizontal tab 009 \t Moves next horizontal tab

New line 010 \n Moves next Line

Vertical tab 011 \v Moves next vertical tab

Form feed 012 \f Moves initial position of next page

Carriage return 013 \r Moves beginning of the line

Double quote 034 \" Present Double quotes

Single quote 039 \' Present Apostrophe

Question mark 063 \? Present Question Mark

Back slash 092 \\ Present back slash

Octal number \000

Hexadecimal number \x

Tokens
A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
Keywords: Keywords are pre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function
in a program. Since keywords are referred names for a compiler, they can’t be used as variable names because by doing so, we are trying
to assign a new meaning to the keyword which is not allowed. You cannot redefine keywords. However, you can specify the text to be
substituted for keywords before compilation by using C/C++ preprocessor directives. C language supports 32 keywords which are given
below:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
While in C++ there are 31 additional keywords other than C Keywords they are:
asm bool catch class
const_cast delete dynamic_cast explicit
export false friend inline
mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw
true try typeid typename
using virtual wchar_t
1. Identifiers: Identifiers are used as the general terminology for the naming of variables, functions and arrays. These are user-
defined names consisting of an arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first
character. Identifier names must differ in spelling and case from any keywords. You cannot use keywords as identifiers; they are
reserved for special use. Once declared, you can use the identifier in later program statements to refer to the associated value. A
special kind of identifier, called a statement label, can be used in goto statements.
Identifiers are used for the naming of variables, functions, and arrays. It is a string of alphanumeric characters that begins with an
alphabet, or an underscore( _ ) that are used for variables, functions, arrays, structures, unions, and so on. It is also known as
the user-defined word. Identifier names must differ in spelling and case from any keywords. We cannot use keywords as
identifiers; they are reserved for special use. Once an identifier is declared, we can use the identifier anywhere in the program to
refer to the associated value.

There are certain rules that should be followed while naming c identifiers:
 They must begin with a letter or underscore(_).
 They must consist of only letters, digits, or underscore. No other special character is allowed.
 It should not be a keyword.
 It must not contain white space.
 It should be up to 31 characters long as only the first 31 characters are significant.
 main: method name.
 a: variable name.
2. Constants: Constants are also like normal variables. But, the only difference is, their values can not be modified by the program
once they are defined. Constants refer to fixed values. They are also called literals.
Constants may belong to any of the data type.
3. Syntax:
const data_type variable_name; (or) const data_type *variable_name;
Types of Constants:
 Integer constants – Example: 0, 1, 1218, 12482
 Real or Floating-point constants – Example: 0.0, 1203.03, 30486.184
 Octal & Hexadecimal constants – Example: octal: (013 ) = (11) Hexadecimal: (013) = (19)
8 10, 16 10

 Character constants -Example: ‘a’, ‘A’, ‘z’


 String constants -Example: “GeeksforGeeks”
2. Strings: Strings are nothing but an array of characters ended with a null character (‘\0’). This null character indicates the end of the
string. Strings are always enclosed in double-quotes. Whereas, a character is enclosed in single quotes in C and
C++.Declarations for String:
 char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘\0’};
 char string[20] = “geeksforgeeks”;
 char string [] = “geeksforgeeks”;
 when we declare char as “string[20]”, 20 bytes of memory space is allocated for holding the string value.
 When we declare char as “string[]”, memory space will be allocated as per the requirement during the execution of the
program.
2. Special Symbols: The following special symbols are used in C having some special meaning and thus, cannot be used for some
other purpose.[] () {}, ; * = #
 Brackets[]: Opening and closing brackets are used as array element reference. These indicate single and multidimensional
subscripts.
 Parentheses(): These special symbols are used to indicate function calls and function parameters.
 Braces{}: These opening and ending curly braces mark the start and end of a block of code containing more than one
executable statement.
 Comma (, ): It is used to separate more than one statements like for separating parameters in function calls.
 Colon(:): It is an operator that essentially invokes something called an initialization list.
 Semicolon(;): It is known as a statement terminator. It indicates the end of one logical entity. That’s why each individual
statement must be ended with a semicolon.
 Asterisk (*): It is used to create a pointer variable.
 Assignment operator(=): It is used to assign values.
 Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your
program before actual compilation.
3. Operators: Operators are symbols that trigger an action when applied to C variables and other objects. The data items on which
operators act upon are called operands.
Depending on the number of operands that an operator can act upon, operators can be classified as follows:
 Unary Operators: Those operators that require only a single operand to act upon are known as unary operators.For Example
increment and decrement operators
 Binary Operators: Those operators that require two operands to act upon are called binary operators. Binary operators are
classified into :
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Conditional Operators
6. Bitwise Operators

Data types

All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell
the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable
based on the data-type with which it is declared. Every data type requires a different amount of memory.
Data types in C++ is mainly divided into three types:

1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare
variables. example: int, char , float, bool etc. Primitive data types available in C++ are:
 Integer
 Character
 Boolean
 Floating Point
 Double Floating Point
 Valueless or Void
 Wide Character
2. Derived Data Types: The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data
Types. These can be of four types namely:
 Function
 Array
 Pointer
 Reference
3. Abstract or User-Defined Data Types: These data types are defined by user itself. Like, defining a class in C++ or a structure.
C++ provides the following user-defined datatypes:
 Class
 Structure
 Union
 Enumeration
 Typedef defined DataType
This article discusses primitive data types available in C++.

 Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -
2147483648 to 2147483647.

 Character: Character data type is used for storing characters. Keyword used for character data type is char. Characters typically
requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255.

 Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false.
Keyword used for boolean data type is bool.

 Floating Point: Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used
for floating point data type is float. Float variables typically requires 4 byte of memory space.

 Double Floating Point: Double Floating Point data type is used for storing double precision floating point values or decimal
values. Keyword used for double floating point data type is double. Double variables typically requires 8 byte of memory space.

 void: Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which
does not returns a value.

 Wide Character: Wide character data type is also a character data type but this data type has size greater than the normal 8-bit
datatype. Represented by wchar_t. It is generally 2 or 4 bytes long.
All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell
the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable
based on the data-type with which it is declared. Every data type requires a different amount of memory.
Data types in C++ is mainly divided into three types:

1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare
variables. example: int, char , float, bool etc. Primitive data types available in C++ are:
 Integer
 Character
 Boolean
 Floating Point
 Double Floating Point
 Valueless or Void
 Wide Character
2. Derived Data Types: The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data
Types. These can be of four types namely:
 Function
 Array
 Pointer
 Reference
3. Abstract or User-Defined Data Types: These data types are defined by user itself. Like, defining a class in C++ or a structure.
C++ provides the following user-defined datatypes:
 Class
 Structure
 Union
 Enumeration
 Typedef defined DataType
This article discusses primitive data types available in C++.

 Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -
2147483648 to 2147483647.

 Character: Character data type is used for storing characters. Keyword used for character data type is char. Characters typically
requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255.

 Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false.
Keyword used for boolean data type is bool.

 Floating Point: Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used
for floating point data type is float. Float variables typically requires 4 byte of memory space.

 Double Floating Point: Double Floating Point data type is used for storing double precision floating point values or decimal
values. Keyword used for double floating point data type is double. Double variables typically requires 8 byte of memory space.

 void: Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which
does not returns a value.

 Wide Character: Wide character data type is also a character data type but this data type has size greater than the normal 8-bit
datatype. Represented by wchar_t. It is generally 2 or 4 bytes long.
All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell
the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable
based on the data-type with which it is declared. Every data type requires a different amount of memory.
Data types in C++ is mainly divided into three types:

1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare
variables. example: int, char , float, bool etc. Primitive data types available in C++ are:
 Integer
 Character
 Boolean
 Floating Point
 Double Floating Point
 Valueless or Void
 Wide Character
2. Derived Data Types: The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data
Types. These can be of four types namely:
 Function
 Array
 Pointer
 Reference
3. Abstract or User-Defined Data Types: These data types are defined by user itself. Like, defining a class in C++ or a structure.
C++ provides the following user-defined datatypes:
 Class
 Structure
 Union
 Enumeration
 Typedef defined DataType
This article discusses primitive data types available in C++.

 Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -
2147483648 to 2147483647.

 Character: Character data type is used for storing characters. Keyword used for character data type is char. Characters typically
requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255.

 Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false.
Keyword used for boolean data type is bool.

 Floating Point: Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used
for floating point data type is float. Float variables typically requires 4 byte of memory space.

 Double Floating Point: Double Floating Point data type is used for storing double precision floating point values or decimal
values. Keyword used for double floating point data type is double. Double variables typically requires 8 byte of memory space.

 void: Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which
does not returns a value.

 Wide Character: Wide character data type is also a character data type but this data type has size greater than the normal 8-bit
datatype. Represented by wchar_t. It is generally 2 or 4 bytes long.
All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell
the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable
based on the data-type with which it is declared. Every data type requires a different amount of memory.

Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.

Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.
There are enumeration constants as well.

Constants are treated just like regular variables except that their values cannot be modified after their definition.

Integer Literals
An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for
octal, and nothing for decimal.

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be
uppercase or lowercase and can be in any order.

Here are some examples of integer literals −


212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */

Following are other examples of various types of integer literals −


85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */

Floating-point Literals
A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point
literals either in decimal form or exponential form.

While representing decimal form, you must include the decimal point, the exponent, or both; and while representing exponential form,
you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.

Here are some examples of floating-point literals −


3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
Character Constants
Character literals are enclosed in single quotes, e.g., 'x' can be stored in a simple variable of char type.

A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').

There are certain characters in C that represent special meaning when preceded by a backslash for example, newline (\n) or tab (\t).

 Here, you have a list of such escape sequence codes −

Following is the example to show a few escape sequence characters −

Live Demo

#include <stdio.h>

int main() {
printf("Hello\tWorld\n\n");

return 0;
}

When the above code is compiled and executed, it produces the following result −
Hello World

String Literals
String literals or constants are enclosed in double quotes "". A string contains characters that are similar to character literals: plain
characters, escape sequences, and universal characters.

You can break a long line into multiple lines using string literals and separating them using white spaces.

Here are some examples of string literals. All the three forms are identical strings.
"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

Defining Constants
There are two simple ways in C to define constants −

 Using #define preprocessor.

 Using const keyword.

The #define Preprocessor


Given below is the form to use #define preprocessor to define a constant −
#define identifier value

The following example explains it in detail −

Live Demo

#include <stdio.h>

#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'

int main() {
int area;

area = LENGTH * WIDTH;


printf("value of area : %d", area);
printf("%c", NEWLINE);

return 0;
}

When the above code is compiled and executed, it produces the following result −
value of area : 50
The const Keyword
You can use const prefix to declare constants with a specific type as follows −
const type variable = value;

The following example explains it in detail −

Live Demo

#include <stdio.h>

int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;

area = LENGTH * WIDTH;


printf("value of area : %d", area);
printf("%c", NEWLINE);

return 0;
}

When the above code is compiled and executed, it produces the following result −
value of area : 50
Data types in C++ is mainly divided into three types:

1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare
variables. example: int, char , float, bool etc. Primitive data types available in C++ are:
 Integer
 Character
 Boolean
 Floating Point
 Double Floating Point
 Valueless or Void
 Wide Character
2. Derived Data Types: The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data
Types. These can be of four types namely:
 Function
 Array
 Pointer
 Reference
3. Abstract or User-Defined Data Types: These data types are defined by user itself. Like, defining a class in C++ or a structure.
C++ provides the following user-defined datatypes:
 Class
 Structure
 Union
 Enumeration
 Typedef defined DataType
This article discusses primitive data types available in C++.

 Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -
2147483648 to 2147483647.

 Character: Character data type is used for storing characters. Keyword used for character data type is char. Characters typically
requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255.

 Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false.
Keyword used for boolean data type is bool.

 Floating Point: Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used
for floating point data type is float. Float variables typically requires 4 byte of memory space.

 Double Floating Point: Double Floating Point data type is used for storing double precision floating point values or decimal
values. Keyword used for double floating point data type is double. Double variables typically requires 8 byte of memory space.

 void: Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which
does not returns a value.

 Wide Character: Wide character data type is also a character data type but this data type has size greater than the normal 8-bit
datatype. Represented by wchar_t. It is generally 2 or 4 bytes long.

You might also like