Module-II
CSET333: Problem Solving Using C 1
#define in C
In C programming, #define is a preprocessor directive that is used
to define macros.
The macros are the identifiers defined by #define which are replaced
by their value before compilation.
We can define constants and functions like macros using #define.
Syntax of C #define
For Defining Constants
For Defining Expressions
For Defining Expression with Parameters
CSET333: Problem Solving Using C 2
Example:
Output:
CSET333: Problem Solving Using C 3
Variable Length Arrays in C
In C, variable length arrays (VLAs) are also known as runtime-
sized or variable-sized arrays. The size of such arrays is defined at
run-time.
Variably modified types include variable-length arrays and pointers
to variable-length arrays. Variably changed types must be declared
at either block scope or function prototype scope.
Syntax
CSET333: Problem Solving Using C 4
Example:
Output:
CSET333: Problem Solving Using C 5
Flexible Array Members in a structure in C
For the structures in C programming language from C99 standard
onwards, we can declare an array without a dimension and whose
size is flexible in nature.
Such an array inside the structure should preferably be declared as
the last member of the structure and its size is variable(can be
changed at runtime).
The structure must contain at least one more named member in
addition to the flexible array member.
What must be the size of the structure below?
CSET333: Problem Solving Using C 6
Const Qualifier in C
Using the const qualifier in C is a good practice when we want to
ensure that some values should remain constant and should not be
accidentally modified.
In C programming, the const qualifier can be used in different
contexts to provide various behaviors. Here are some different use
cases of the const qualifier in C:
1. Constant Variables
In this case, const is used to declare a variable var as a constant
with an initial value of 100. The value of this variable cannot be
modified once it is initialized.
CSET333: Problem Solving Using C 7
Example:
Output:
CSET333: Problem Solving Using C 8
2. Pointer to Constant
Example:
Output:
CSET333: Problem Solving Using C 9
3. Constant Pointer to Variable
The above declaration is a constant pointer to an integer variable,
which means we can change the value of the object pointed by the
pointer, but cannot change the pointer to point to another variable.
Example:
Output:
CSET333: Problem Solving Using C 10
4. Constant Pointer to Constant
Example:
Output:
CSET333: Problem Solving Using C 11
Volatile qualifier in C
The volatile qualifier is applied to a variable when we declare it.
It is used to tell the compiler, that the value may change at any time.
These are some properties of volatile:
The volatile keyword cannot remove the memory assignment
It cannot cache the variables in register.
The value cannot change in order of assignment.
Let us see, how we can use the volatile keyword:
Example:
CSET333: Problem Solving Using C 12
Restrict qualifier in C
restrict keyword is mainly used in pointer declarations as a type
qualifier for pointers.
It doesn’t add any new functionality. It is only a way for the
programmer to inform about an optimization that the compiler can
make.
When we use restrict with a pointer ptr, it tells the compiler that ptr is
the only way to access the object pointed by it, in other words,
there’s no other pointer pointing to the same object i.e. restrict
keyword specifies that a particular pointer argument does not alias
any other and the compiler doesn’t need to add any additional
checks.
If a programmer uses restrict keyword and violates the above
condition, the result is undefined behavior.
restrict is not supported by C++. It is a C-only keyword.
CSET333: Problem Solving Using C 13
Example:
Output:
CSET333: Problem Solving Using C 14
C Functions
Syntax of Functions in C
The syntax of function can be divided into 3 aspects:
1. Function Declaration
2. Function Definition
3. Function Calls
1. Function Declarations
Syntax:
Example:
CSET333: Problem Solving Using C 15