Unit#4

You might also like

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

Dr.

Pawan Kumar Verma


UNIT#4 Associate Professor
CSE Department
AGENDA
Part A: Pre-processors: Types, Directives, Preprocessors Operators (#,##,\) ,
Macros: Types, Use, predefined Macros.

Part B: Pointer: Introduction, declaration of pointer variables, Operations on


pointers: Pointer arithmetic, Arrays and pointers, Dynamic memory allocation.

Part C: String: Introduction, predefined string functions, Manipulation of text


data, Command Line Arguments.
Part A: Pre-processors: Types, Directives, Preprocessors Operators (#,##,\) ,
Macros: Types, Use, predefined Macros.
WHAT ARE PRE-PROCESSORS?
A preprocessor is a software tool that performs preliminary operations on the
source code before it undergoes compilation. It handles directives that begin with
the # symbol and is responsible for tasks such as file inclusion, macro substitution,
and conditional compilation.

The primary purpose of the preprocessor is to prepare the source code for the actual
compilation process performed by the compiler.

Key tasks of the preprocessor include:


1. File Inclusion
2. Macro Substitution
3. Conditional Compilation
FILE INCLUSION
The #include directive is used to include the contents of another file in the source code.
This is essential for reusing code from external libraries or header files.

In this example:

• The #include <stdio.h> directive includes


the standard input/output library.
• The #include "myheader.h" directive
includes the contents of the myheader.h
file, which declares a function called
printMessage().
MACRO SUBSTITUTION
The #define directive is employed to
create macros, which are symbolic names
representing a sequence of code. During
preprocessing, these macros are
substituted with their corresponding code
wherever they are used in the source file.

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.

The \ operator allows the definition of


the LONG_MACRO over multiple lines,
improving code readability.
WHAT IS MACROS ?
Macros refer to a feature provided by the preprocessor, which allows developers to
define symbolic names or identifiers that are replaced by a sequence of code during
the preprocessing stage. Macros are created using the #define directive.

They are commonly used to define constants, perform code substitution, and create
reusable code snippets.

MACRO_NAME: This is the name of the macro, and it acts as a symbolic


representation.
replacement_text: This is the code or value that the macro will be replaced with
wherever it is used in the source code.
TYPES OF MACROS : OBJECT-LIKE MACROS
Simple substitution of one piece of code with another.
TYPES OF MACROS : FUNCTION-LIKE MACRO
Macros that resemble function calls.
TYPES OF MACROS : VARIADIC MACROS

Macros that accept a variable number of arguments.


USE OF MACROS

• Code Reusability: Macros provide a way to reuse code snippets and constants.

• Code Generation: Macros enable the generation of code based on specific


conditions.

• Readability: Macros can enhance code readability by providing meaningful


names to constants.
PREDEFINED MACROS

• __FILE__: Name of the current file as a string.

• __LINE__: Current line number as an integer.

• __DATE__: Compilation date as a string.

• __TIME__: Compilation time as a string.


Part B: Pointer: Introduction, declaration of pointer variables, Operations on
pointers: Pointer arithmetic, Arrays and pointers, Dynamic memory allocation.
INTRODUCTION TO POINTERS
A pointer is a special variable in C that stores the memory address of another
variable.

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.

Dynamic Memory Management:


• Pointers are essential for dynamic memory allocation, allowing programs to request and
release memory during runtime.

Efficient Data Structures:


• Pointers are crucial for creating and manipulating complex data structures like linked lists,
trees, and dynamic arrays.
DECLARATION OF POINTER VARIABLES

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

A string in C is an array of characters that terminates with a null character '\0'.


Note: The fgets reads up to the specified number of characters or until a newline character ('\n') is encountered,
including the newline character in the string. Also, the null character ('\0') is automatically added to the end of
the string, making it a valid C string.

STRING : EXAMPLE
In this program:

• We declare a character array


inputString to store the user
input.
• We use printf to prompt the user
to enter a string.
• We use fgets to read the input
string from the user, and we
check for any errors during the
input process.
• If the input is successful, we use
printf to display the entered
string.
PREDEFINED STRING FUNCTIONS

• strlen(): Calculates the length of a string.

• strcpy(): Copies one string to another.

• strcat(): Concatenates two strings.

• strcmp(): Compares two strings.

• strchr(): Finds the first occurrence of a character in a string.

• strstr(): Finds the first occurrence of a substring in a string.


COMMAND LINE ARGUMENTS
In C programming, command
line arguments allow you to
pass information to a program
when it is executed. These
arguments are provided after
the program name in the
command line. The main
function in C can take two
arguments: argc (argument
count) and argv (argument
vector).
COMMAND LINE ARGUMENTS: ADDITION OF TWO
INTEGER NUMBERS (EXAMPLE)
Thank you.. ☺

You might also like