Character set
The character set in C refers to the collection of all valid characters that can be used to write a C
program. These characters are the fundamental building blocks for forming words, expressions, numbers,
and other elements within the source code.
The C character set typically includes:
• Alphabets:
o Uppercase letters: A-Z
o Lowercase letters: a-z
• Digits:
o Numerals: 0-9
• Special Characters:
o A wide range of symbols used for various purposes, including:
▪ Arithmetic operators: +, -, *, /, %
▪ Relational operators: <, >, =, !, &, |
▪ Punctuation: (, ), {, }, [, ], ;, :, ', ", ,, ., ?
▪ Other symbols: ~, @, #, $, ^, _, `\`, |
• Whitespace Characters:
o Characters that create space or line breaks, such as:
▪ Space
▪ Tab (\t)
▪ Newline (\n)
▪ Carriage return (\r)
▪ Form feed (\f)
Each character within the C character set has an equivalent ASCII (American Standard Code for Information
Interchange) value, which is how they are represented internally by the computer. The C compiler
recognizes and processes these characters to understand and execute the program code.
Keyword
Keywords are predefined or reserved words that have special meanings to the compiler. These are part of
the syntax and cannot be used as identifiers in the program. A list of keywords in C or reserved words in the
C programming language are mentioned below:
auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
Identifiers
In C programming, identifiers are the names used to identify variables, functions, arrays, structures, or any
other user-defined items. It is a name that uniquely identifies a program element and can be used to refer
to it later in the program.
Rules for Naming Identifiers in C
A programmer must follow a set of rules to create an identifier in C:
• Identifier can contain following characters:
o Uppercase (A-Z) and lowercase (a-z) alphabets.
o Numeric digits (0-9).
o Underscore (_).
• The first character of an identifier must be a letter or an underscore.
• Identifiers are case-sensitive.
• Identifiers cannot be keywords in C (such as int, return, if, while etc.).
Difference Keyword and Identifiers
Feature Keyword Identifier
A keyword is a reserved word with a An identifier is a user-defined name used
special meaning in C that cannot be used to refer to variables, functions, arrays,
Definition as an identifier. etc.
Keywords are predefined in the C Identifiers are used by programmers to
Usage
language and are used to define the name variables, functions, and other
Feature Keyword Identifier
structure and control flow of the user-defined elements (e.g., age, sum,
program (e.g., int, if, while). main).
Example int, return, for, if, while totalAmount, studentAge, calculateTotal
Keywords cannot be modified or used Identifiers can be created and used as
Modification for any other purpose. per the programmer’s needs.
Identifiers are used for variable names,
Keywords are part of the syntax of C and
function names, and more throughout
are used to structure the program.
Position in Code the code.
Keywords are case-sensitive (e.g., int and Identifiers are also case-sensitive (e.g.,
Case Sensitivity Int are different). age and Age are different).
Datatypes
Data types in C refer to an extensive system used for declaring variables or functions of different types. The
type of a variable determines how much space it occupies in storage and how the bit pattern stored is
interpreted. In this chapter, we will learn about data types in C. A related concept is that of "variables",
which refer to the addressable location in the memory of the processor. The data captured via different
input devices is stored in the computer memory. A symbolic name can be assigned to the storage location
called variable name.
C is a statically typed language. The name of the variable along with the type of data it intends to store
must be explicitly declared before actually using it.C is also a strongly typed language, which means that
the automatic or implicit conversion of one data type to another is not allowed.
Basic Types
They are arithmetic types and are further classified into: (a) integer types and (b) floating-point types.
Integer
The integer datatype in C is used to store the integer numbers (any number including positive, negative and
zero without decimal part). Octal values, hexadecimal values, and decimal values can also be stored in int
data type in C.
• Range: -2,147,483,648 to 2,147,483,647
• Size: 4 bytes
• Format Specifier: %d
Example
int num;
Program
#include <stdio.h>
int main() {
int var = 22;
printf("var = %d", var);
return 0;}
Output
Var = 22
Character Data Type
Character data type allows its variable to store only a single character. The size of the character is 1 byte. It
is the most basic data type in C. It stores a single character and requires a single byte of memory in almost
all compilers.
• Range: (-128 to 127) or (0 to 255)
• Size: 1 byte
• Format Specifier: %c
Program
#include <stdio.h>
void main() {
char ch = 'A';
printf("ch = %c", ch);
Output
ch = A
Float Data Type
In C programming, float data type is used to store single precision floating-point values. These values are
decimal and exponential numbers.
• Range: 1.2E-38 to 3.4E+38
• Size: 4 bytes
• Format Specifier: %f
Program
#include <stdio.h>
void main() {
float val = 12.45;
printf("val = %f", val);
Output
val = 12.450000
Double Data Type
The double data type in C is used to store decimal numbers (numbers with floating point values) with
double precision. It can easily accommodate about 16 to 17 digits after or before a decimal point.
• Range: 1.7E-308 to 1.7E+308
• Size: 8 bytes
• Format Specifier: %lf
Program
#include <stdio.h>
void main() {
double val = 1.4521;
printf("val = %lf", val);
}
Output
val = 1.452100
Void Data Type
The void data type in C is used to indicate the absence of a value. Variables of void data type are not
allowed.
void fun( ){
// function body
Boolean Data Type
The boolean data type is used to store logic values i.e. truth values which are true or false. It takes only 1
byte of space to store logic values. Here, true means 1, and false means 0. In the boolean data type any
value other than '0' is considered as 'true'. Boolean values are most commonly used in data structures to
decide the flow of control of a program and decision [Link] programming languages, we have
various data types to store different types of data. Some of the most used data types are integer, string,
float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or
False.
#include <stdio.h>
#include <stdbool.h>
int main() {
bool a = true;
bool b = false;
bool c = 5;
printf("a: %d\n", a);
printf("c: %d\n", b);
printf("c: %d", c);
Derived Datatype
Arrays
An array is a collection of multiple values of same data type stored in consecutive memory locations. The
size of array is mentioned in square brackets []. For example,
int marks[ ]={50,56,76,67,43};
Pointers
A pointer is a special variable that stores address or reference of another variable/object in the memory.
The name of pointer variable is prefixed by asterisk (*). The type of the pointer variable and the
variable/object to be pointed must be same.
int x;
int *y;
y = &x;
Functions
A function is a named block of code that performs a specific task. It allows you to write a piece of logic once
and reuse it wherever needed in the program. This helps keep your code clean, organized, and easier to
understand.
Functions play a vital role in building modular programs. They allow you to break down complex problems
into smaller, manageable parts.
#include <stdio.h>
void hello() {
printf ("ABC\n");
void main( ) {
hello( );
User-defined Data Types in C
There are two user-defined data types struct and union, that can be defined by the user with the help of
the combination of other basic data types.
Struct Data Type
One of the unique features of C language is to store values of different data types in one variable. The
keywords struct and union are provided to derive a user-defined data type. For example,
struct student
char name[20];
int marks, age;
};
Union Data Type
A union is a special case of struct where the size of union variable is not the sum of sizes of individual
elements, as in struct, but it corresponds to the largest size among individual elements. Hence, only one of
elements can be used at a time. Look at following example:
union ab
int a;
float b;
};
enum Data Type
enumeration (or enum) is a user defined data type that contains a set of named integer constants. It is
used to assign meaningful names to integer values, which makes a program easy to read and maintain.
For example
enum calculate {
SUM, DIFFERENCE, PRODUCT, QUOTIENT
};
The following table provides the details of standard integer types with their storage sizes and value ranges
Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 8 bytes -9223372036854775808 to 9223372036854775807
unsigned long 8 bytes 0 to 18446744073709551615
Constants
In C programming, const is a keyword used to declare a variable as constant, meaning its value cannot be
changed after it is initialized. It is mainly used to protect variables from being accidentally modified, making
the program safer and easier to understand. These constants can be of various types, such as integer,
floating-point, string, or character constants.
Syntax
We define a constant in C using the const keyword. Also known as a const type qualifier, the const keyword
is placed at the start of the variable declaration to declare that variable as a constant.
const data_type var_name = value;
Example
const int var = 5;
Int var = 9 ;
Program
#include <stdio.h>
int main( ) {
// Defining constant variable
int a = 10;
printf("%d", a);
return 0;
Output
10
Variables
A variable in C is a named piece of memory which is used to store data and access it whenever required. It
allows us to use the memory without having to memorize the exact memory address.
Syntax
data_type name;
Example
int num;
char letter;
float decimal;
Rules for Naming Variables in C
We can assign any name to a C variable as long as it follows the following rules:
• A variable name must only contain letters, digits, and underscores.
• It must start with an alphabet or an underscore only. It cannot start with a digit.
• No white space is allowed within the variable name.
• A variable name must not be any reserved word or keyword.
• The name must be unique in the program.
Program
#include <stdio.h>
int main() {
int num = 3;
printf("%d", num);
return 0;
Output
Expression
An expression is a combination of operators, constants and variables. An expression may consist of one or
more operands, and zero or more operators to produce a value.
Example
a+b
s-1/7*f
Types of Expressions:
• Constant expressions:
Constant Expressions consists of only constant values. A constant value is one that doesn't
change. Examples:
5, 10 + 5 / 6.0, 'x’
• Integral expressions:
Integral Expressions are those which produce integer results after implementing all the automatic
and explicit type conversions.
Examples:
x, x * y, x + int( 5.0)
where x and y are integer variables.
• Floating expressions:
Float Expressions are which produce floating point results after implementing all the automatic and
explicit type conversions.
Examples:
x + y, 10.75
where x and y are floating point variables.
• Relational expressions:
Relational Expressions yield results of type bool which takes a value true or false. When arithmetic
expressions are used on either side of a relational operator, they will be evaluated first and then the
results compared. Relational expressions are also known as Boolean expressions.
Examples:
x <= y, x + y > 2
• Logical Expressions
Logical Expressions combine two or more relational expressions and produces bool type results.
Examples:
x > y && x == 10, x == 10 || y == 5
• Pointer expressions:
Pointer Expressions produce address values.
Examples:
&x, ptr, ptr++
where x is a variable and ptr is a pointer.
• Bitwise expressions:
Bitwise Expressions are used to manipulate data at bit level. They are basically used for testing or
shifting bits.
Examples:
x << 3
Statement
A computer program is a list of "instructions" to be "executed" by a computer.
In a programming language, these programming instructions are called statements.
Example
printf("Hello World!");
Symbolic Constants
Symbolic constants in C are identifiers that represent fixed values in a program. Unlike variables, whose
values can change during execution, symbolic constants have values that remain constant throughout the
program. They provide a way to give meaningful names to fixed values, improving code readability and
maintainability. Symbolic constants are defined using the #define preprocessor directive or
the const keyword. Let’s delve into each method and explore symbolic constants in detail:
Using #define Directive:
The #define directive is a preprocessor directive used to define symbolic constants.
syntax:
#define constant_name value
Example:
#define PI 3.14159 #define MAX_SIZE 100
Advantages of #define:
1. Simple and Efficient: #define directives are straightforward and efficient in replacing constant values
throughout the code.
2. No Memory Allocation: Symbolic constants defined using #define do not occupy memory, as they
are replaced during the preprocessing stage.
3. No Scope Limitation: #define constants have global scope and are accessible from any part of the
program.
Disadvantages of #define:
1. No Type Checking: #define does not perform type checking, so errors may occur if the constant is
used incorrectly.
2. Lack of Debugging Support: Since #define constants are replaced before compilation, they may not
appear in debugging information, making debugging more challenging.
Comments
The comments in C are human-readable notes in the source code of a C program used to make the
program easier to read and understand. They are not a part of the executable program by the compiler or
an interpreter.
Example:
#include <stdio.h>
int main() {
// This is a comment, the below
// statement will not be executed
// printf("Hi from comment");
printf("Hello");
return 0;
Output:
Hello
Types of Comments in C
In C, there are two types of comments in C language:
• Single-line Comments
• Multi-line Comments
Single-line Comments
Single-line comments are used to comment out a single line of code or a part of it. The single line
comments in C start with two forward slashes (//), and everything after the slashes on that line is
considered a comment. They are also called C++ Style comments as they were first introduced in C++ and
later adopted in C also.
Syntax
// This is a single line comment
Multi-line Comments
Multi-line comments in C are used write comments that span more than one line. They are generally used
for longer descriptions or for commenting out multiple lines of code. In C language, these comments begin
with /* and end with */. Everything between these markers is treated as a comment.
Syntax:
/* This is a multi-line comment
which can span multiple lines */