100% de satisfacción garantizada Inmediatamente disponible después del pago Tanto en línea como en PDF No estas atado a nada 4,6 TrustPilot
logo-home
Otro

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

Puntuación
-
Vendido
-
Páginas
6
Subido en
21-01-2025
Escrito en
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.

Mostrar más Leer menos

Vista previa del contenido

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.

Información del documento

Subido en
21 de enero de 2025
Número de páginas
6
Escrito en
2024/2025
Tipo
Otro
Personaje
Desconocido
$6.29
Accede al documento completo:

100% de satisfacción garantizada
Inmediatamente disponible después del pago
Tanto en línea como en PDF
No estas atado a nada

Conoce al vendedor
Seller avatar
rileyclover179

Documento también disponible en un lote

Thumbnail
Package deal
C Programming Exam Study Guide and Q&A (21 documents)
-
21 2025
$ 121.49 Más información

Conoce al vendedor

Seller avatar
rileyclover179 US
Ver perfil
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
0
Miembro desde
1 año
Número de seguidores
0
Documentos
252
Última venta
-

0.0

0 reseñas

5
0
4
0
3
0
2
0
1
0

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes