100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.6 TrustPilot
logo-home
Other

Preprocessor Directives in C: Macros, Includes, and Conditional Compilation

Rating
-
Sold
-
Pages
6
Uploaded on
21-01-2025
Written in
2024/2025

This document explains the basic preprocessor directives in C programming, like #include, #define, and #ifdef. You’ll learn how to use macros for constants, include files, and control code. The guide includes examples to show how these directives work in real programs. Perfect for second-year Computer Science students, this guide will help you get comfortable with preprocessor directives in C.

Show more Read less

Content preview

Preprocessor Directives in C
Preprocessor directives in C are commands that are processed before the actual
compilation of the program begins. They are used to perform actions such as file
inclusion, macro substitution, conditional compilation, and more. Preprocessor
directives are not part of the C language itself but are handled by the
preprocessor before the code is compiled.

Preprocessor directives are identified by the # symbol and are typically placed at
the beginning of a program or before the main code block.

1. File Inclusion (#include)
The #include directive is used to include header files in a C program. Header files
typically contain declarations of functions, macros, and data types that can be
shared across multiple source files.

There are two types of file inclusion:

 Standard Library Header Files: These are predefined by the C library and
are included using angle brackets (<>).
 User-defined Header Files: These are created by the programmer and are
included using double quotes ("").

Example: Including Standard Header File

#include <stdio.h> // Including standard library header file

int main() {
printf("Hello, World!\n");
return 0;
}

Example: Including User-defined Header File

#include "myheader.h" // Including user-defined header file

int main() {

, // Code using functions or variables declared in myheader.h
return 0;
}


2. Macro Definition (#define)
The #define directive is used to define macros, which are symbolic constants or
expressions that are substituted by their value during preprocessing. Macros can
be used to define constants, small functions, or complex expressions.

Constant Definition Example:

#include <stdio.h>

#define PI 3.14 // Defining a constant PI

int main() {
printf("Value of PI: %.2f\n", PI);
return 0;
}

Macro Function Example:

#include <stdio.h>

#define SQUARE(x) ((x) * (x)) // Defining a macro for square of a number

int main() {
int num = 5;
printf("Square of %d: %d\n", num, SQUARE(num));
return 0;
}

In the above example, the macro SQUARE(x) is substituted by the expression (x) *
(x) at compile time.

Document information

Uploaded on
January 21, 2025
Number of pages
6
Written in
2024/2025
Type
Other
Person
Unknown
$6.29
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
rileyclover179

Also available in package deal

Thumbnail
Package deal
C Programming Exam Study Guide and Q&A (21 documents)
-
21 2025
$ 121.49 More info

Get to know the seller

Seller avatar
rileyclover179 US
View profile
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
252
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions