• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 6 pages
Other

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

Document preview thumbnail
Preview 2 out of 6 pages

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.

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

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




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

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions