100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Preprocessor Directives in C: Macros, Includes, and Conditional Compilation $7.79
Add to cart

Other

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

 0 purchase

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 Com...

[Show more]

Preview 2 out of 6  pages

  • January 21, 2025
  • 6
  • 2024/2025
  • Other
  • Unknown
All documents for this subject (31)
avatar-seller
rileyclover179
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.

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller rileyclover179. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $7.79. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

62774 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 15 years now

Start selling
$7.79
  • (0)
Add to cart
Added