Chapter 2
Chapter 2
Character Set
Identifiers
Keywords
Data Types
Constants and Variables
Statements
Expressions
Operators
Precedence of Operators
Input-Output Assignments
Types of Characters in C
a. Alphabets
C programming language supports a total of 52 different characters- 26 uppercase and 26 lowercase.
Type of Character Description Characters
Lowercase Alphabets a to z a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
Uppercase Alphabets A to Z A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
b. Digits
C programming language support all the digits ranging from 0-9 that help in constructing numeric
values or expressions in a program.
Type of Character Description Characters
Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
c. Special Characters
C Programming language allows programmers to use the following special characters:
Type of Character Examples
Special Characters `~@!$#^*%&()[]{}<>+=_–|/\;:‘“,.?
comma , slash /
period . backslash
semicolon ; percentage %
colon : left and right brackets [ ]
question mark ? left and right parenthesis ( )
d. White Spaces
In C Programming, white spaces contain:
Blank Spaces
Tab
New Line
Carriage Return
for float go if
4. Variables
A variable is the name of the memory location. It is used to store data. Its value can be changed, and
it can be reused many times.
Types of Variables in C
b) Global Variable-A variable that is declared outside the function or block is called a global
variable.
1. void function1(){
2. int x=10;//local variable
3. static int y=10;//static variable
4. x=x+1;
5. y=y+1;
6. printf("%d,%d",x,y);
7. }
d) Automatic Variable- All variables in C that are declared inside the block, are automatic
variables by default. We can explicitly declare an automatic variable using auto keyword.
1. void main(){
2. int x=10;//local variable (also automatic)
3. auto int y=20;//automatic variable
4. }
e) External Variable- We can share a variable in multiple C source files by using an external
variable. To declare an external variable, you need to use the extern keyword.
myfile.h
extern int x=10;//external variable (also global)
program1.c
1. #include "myfile.h"
2. #include <stdio.h>
3. void printValue(){
4. printf("Global variable: %d", global_variable);
5. }
5. Constants
If you want to define a variable whose value cannot be changed, you can use the const keyword.
This will create a constant.
For example,
You can also define a constant using the #define preprocessor directive
#define PI 3.1415
6. Literals
Literals are the Constant values that are assigned to the constant variables. Literals represent fixed
values that cannot be modified.
For example,
const int =10; is a constant integer expression in which 10 is an integer literal.
Types of literals
There are four types of literals that exist in C programming:
o Integer literal It is a numeric literal that represents only integer type values.
Example: const int a=23; // constant integer literal
o Float literal It is a literal that contains only floating-point values or real numbers.
Example: const float a=4.5; // constant float literal
o Character literal A character literal contains a single character enclosed within single quotes.
Example: const char c='ak'; // constant character literal
o String literal A string literal represents multiple characters enclosed within double quotes.
Example: const string s= "javatpoint"; // constant string literal
7. Data Types
A data type specifies the type of data that a variable can store such as integer, floating, character, etc.
o Int
o Float
o Double
o Char
1. Integer (int): Refers to positive and negative whole numbers (without decimal),
such as 10, 12, 65, 3400, etc.
Example:
#include <stdio.h>
void main()
int i = 5;
2. Character (char): Refers to all the ASCII character sets within single quotes such
as ‘a’, ‘A’, etc.
Example:
#include <stdio.h>
void main()
char c = 'b';
3. Floating-point (float): Refers to all the real number values or decimal points,
such as 3.14, 10.09, 5.34, etc.
Example:
#include <stdio.h>
void main()
float f = 7.2357;
4. Double (double): Used when the range exceeds the numeric values that do not
come under either floating-point or integer data type.
Example:
#include <stdio.h>
void main()
double d = 71.2357455;
int Integer
float Floating-point
double Double
char Character
void Void
Array
Pointers
Structure
Union
o Array
An array in C is a collection of multiple values of a similar data type and is stored in a contiguous
memory location. An array can consist of chars, integers, doubles, etc.
Declaration of Array in C
data_type array_name[array_size];
o Pointer
The pointer data type is used to store the address of another variable. A pointer can store the
address of variables of any data type. Pointers allow users to perform dynamic memory
allocation. They also help to pass variables by reference.
A pointer with no address is called a null pointer. A pointer with no data type is a void Pointer. It
is defined by using a ‘*’ operator.
o Structure
It is a data type that can store variables of similar or different data types. For example, we can use
structures to store information about an employee, such as the employee name, employee ID,
salary, and more. Each employee’s record will be represented by an object of the structure. The
size of the structure is the sum of the storage size required by each variable. The ‘struct’ keyword
defines a structure.
o Union
A union is a group of elements with similar or different data types. In a union, the memory
location is the same for all the elements. Its size will be equal to the memory required for the
largest data type defined. We use the keyword ‘union’ to define a union. You can declare many
variables. However, just one variable can store the value at a time.
Enum syntax:
4. Void
The void is just an empty data type that depicts that no value is available. Typically, the void is
used for functions. When we declare a function as void, it doesn’t have to return anything.
Function returns as void – A function with no return value will have the return type as
void.
Function arguments as void – A function with no parameter can accept the void.
Pointers to void – It represents the address of an object, but not its type.
Example – The below function will not return any value to the calling function.