Professional Documents
Culture Documents
Unit#4
Unit#4
Unit#4
The primary purpose of the preprocessor is to prepare the source code for the actual
compilation process performed by the compiler.
In this example:
In this example:
• The #define PI 3.14159 line creates a
macro named PI with the value
3.14159.
• The #define SQUARE(x) ((x) * (x)) line
creates a macro named SQUARE that
takes a parameter x and returns the
square of x.
CONDITIONAL COMPILATION
Directives such as #if, #ifdef, #ifndef, #else,
#elif, and #endif allow for conditional
compilation. Portions of the code can be
included or excluded based on specified
conditions, enhancing code flexibility.
In this example:
• The #define DEBUG line is commented out
by default, so the debugging code block is
not included in the program.
• If you uncomment #define DEBUG, the
#ifdef DEBUG directive evaluates to true,
and the debugging information will be
included during compilation.
This allows you to selectively include or exclude portions of code based on whether a certain macro is
defined. Conditional compilation is often used for debugging, feature toggles, or platform-specific code
in C programs.
PREPROCESSOR OPERATORS
##: The token-pasting operator,
#: The stringizing operator, converts macro
concatenates two tokens into one.
parameters to string literals.
In this example, the STR macro takes a The CONCAT macro concatenates the
parameter s and uses the # operator to values of x and y without any space in
convert it into a string literal. between.
PREPROCESSOR OPERATORS
\: The line continuation operator, used to split long macro definitions into multiple
lines.
They are commonly used to define constants, perform code substitution, and create
reusable code snippets.
• Code Reusability: Macros provide a way to reuse code snippets and constants.
Pointers provide a way to manipulate data directly at the memory level, offering
flexibility and efficiency in programming.
Pointers provide a way to manipulate data directly at the memory level, offering
flexibility and efficiency in programming.
KEY FEATURES
Memory Address:
• Every variable in a program is stored in a specific location in the computer's memory.
• Pointers allow us to work with these memory addresses.
Indirection:
• Pointers enable indirection, meaning they can indirectly access or modify the value at a
memory location.
Explanation:
• datatype: Specifies the type of data the pointer is intended to point to.
• *: Indicates that the variable is a pointer.
• pointer_name: The name assigned to the pointer variable.
EXAMPLE
OPERATIONS ON POINTERS: POINTER ARITHMETIC
Pointer arithmetic in C involves performing arithmetic operations on pointers to
manipulate memory addresses and access data at different locations.
Increment and
Decrement
Arithmetic
Operations
OPERATIONS ON POINTERS: ARRAYS AND POINTERS
In C, arrays and pointers are closely related concepts, providing flexibility and efficiency in
handling data.
Relationship
Between Arrays and
Pointers
Accessing Array
Elements Using
Pointers
OPERATIONS ON POINTERS: ARRAYS AND POINTERS
Iterating Through
Arrays Using
Pointers
Passing Arrays to
Functions Using
Pointers
OPERATIONS ON POINTERS: ARRAYS AND POINTERS
Dynamic Arrays
Using Pointers
Part C: String: Introduction, predefined string functions, Manipulation of text
data, Command Line Arguments.
INTRODUCTION TO STRINGS
STRING : EXAMPLE
In this program: