100% found this document useful (1 vote)
15 views6 pages

Preprocessor and macros notes

The document explains the concept of a preprocessor in C, detailing its role in processing source files before compilation, including tasks like file inclusion, macro definitions, and conditional compilation. It describes different types of preprocessor directives such as #include and #define, providing examples for each. Additionally, it covers command-line arguments, explaining how they are passed to the main function and their significance in controlling program execution.

Uploaded by

aryananand548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
15 views6 pages

Preprocessor and macros notes

The document explains the concept of a preprocessor in C, detailing its role in processing source files before compilation, including tasks like file inclusion, macro definitions, and conditional compilation. It describes different types of preprocessor directives such as #include and #define, providing examples for each. Additionally, it covers command-line arguments, explaining how they are passed to the main function and their significance in controlling program execution.

Uploaded by

aryananand548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT-5

Ques-1 Explain preprocessor.

Ans-A software program that processes the source file before sending it to actual
compilation is called preprocessor. While processing the source file, the preprocessor
replaces header files and macros with defined files and values.
The line which starts with '#'(hash) will be processed by preprocessor before the actual
compilation starts.
Preprocessor in C is not a part of the compilation process but done just before compilation.
•Pre-processor only notice #(hash) started statements.
• #(hash) is called as Pre-processor directive.
• The word after #(hash) is called as Pre-processor command.
eg- #include , #define, #undef etc

Tasks performed by preprocessor are-


1. File Inclusion.
2. Macros definitions.
3. Conditional compilation.
Ques-2 Explain different types of preprocessor directives with suitable example.

Or Explain any 2 preprocessor directives with suitable example.

Ans- Different types of preprocessor directives are-

1. File Inclusion directives (#include)


2. Macros definition directives (# define)
3. Conditional compilation directives (#ifdef , #if , #else etc)

1. FILE INCLUSION

This directive is used to include header or source files in the current source file before
compilation. With the help of these directives, one can declare or define source code from other
files in the program.

One commonly used file inclusion directive is ‘#includes<stdio.h>’. It is used to include the
standard I/O library.

To include a user-defined header file use file inclusion directive –‘#include “filename”.

Eg- #include “cube.h”.

2.MACROS DEFINITION

*Ques-(Define macros definition in-term of variable and in-term of functions with suitable
example).

A macro in C is essentially a piece of code or a value that is associated with an identifier.

This identifier, known as the macro name, is defined using the #define preprocessor directive.

Macros provide a convenient way to substitute code values throughout program, improving both
readability and maintainability.

Macros are symbolic representations that the compiler replaces with the actual value or code
before the program is compiled.

Syntax

#define MACROS_NAME MACROS_VALUE

Example

#include <stdio.h>
#define PI 3.14159 //A macro for the value of PI in-term of variable
int main()
{
double radius = 5.0;
double area = PI * radius * radius;
printf("The area of the circle is: %lf\n", area);
return 0;
}
Types of Macros
a) Macros in term of variable

Macros in term of variables are simple substitutions of values or code.

They are similar to constants and are often used to improve code readability and maintainability
by giving meaningful names to commonly used values.

Example-

#define MAX_VALUE 100

#define PI 3.14159

b) Macros in term of function

Macros in term of functions mimic the behavior of functions. They accept arguments and can
perform complex operations, offering a convenient way to create reusable code.

#define SQUARE(x) ((x) * (x))

Eg-

#include<stdio.h>
#define PRODUCT(n) n*n //A macro in-term of function
void main()
{
int j;
j=64/PRODUCT (4);
printf(“%d”,j);
}
3. CONDITIONAL COMPILATION

Conditional Compilation Directives in C are a set of preprocessing directives that allow us to


include or exclude parts of a program based on certain conditions.
These directives are processed before the actual compilation of the code.
These are useful for making a program portable across different platforms or including debug
and release versions within the same code base.
Conditional compilation directives in C are-:
#if: This directive tests if a certain condition is true. If the condition evaluates to true, the
compiler includes the code between #if and the next #endif, #else, or #elif directive.
#ifdef: This checks if a macro is defined. If the macro is defined, the code following #ifdef
up to #endif, #else, or #elif is compiled.
#ifndef: Opposite of #ifdef. It checks if a macro is not defined. If the macro is not defined,
the code following #ifndef is compiled.
#else: Used with #if, #ifdef, or #ifndef. If the preceding condition is false, the compiler
includes the code following #else.
#elif: Short for “else if”. Allows for multiple conditional expressions. If the preceding #if,
#ifdef, or #ifndef is false, and the condition in #elif is true, the code following #elif is
compiled.
#endif: Marks the end of a conditional compilation block started by #if, #ifdef, #ifndef,
#else, or #elif.
#define: Used to define macros, which can be used in conditional compilation.
#undef: Used to undefine macros.
Ques-3 Explain command line argument with suitable example.
Ans- Command-line arguments are simple parameters that are given on the system's
command line, and the values of these arguments are passed on to your program during
program execution.
When a program starts execution without user interaction, command-line arguments are used
to pass values or files to it.
Command-Line Arguments in C
When the main function of a program contains arguments, then these arguments are known
as Command Line Arguments.
The main function can be created with two methods: first with no parameters (void) and
second with two parameters.
The parameters are argc and argv, where argc is an integer and the argv is a list of command
line arguments, argc denotes the number of arguments given, while argv[] is a pointer array
pointing to each parameter passed to the program.
If no argument is given, the value of argc will be 1.
The value of argc should be non-negative.
Syntax:
* Main function without arguments:
int main()

* Main function with arguments:

Properties of Command Line Arguments in C


 Command line arguments in C are passed to the main function as argc and argv.
 Command line arguments are used to control the program from the outside.
 argv[argc] is a Null pointer.
 The name of the program is stored in argv[0], the first command-line parameter in
argv[1], and the last argument in argv[n].
 Command-line arguments are useful when you want to control your program from
outside rather than hard coding the values inside the code.
 To allow the usage of standard input and output so that we can utilize the shell to
chain commands.
 To override defaults and have more direct control over the application. This is helpful
in testing since it allows test scripts to run the application.

WAP to find sum of first n natural number using command line argument

Note- atoi( ) to convert string input into integer

You might also like