Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Define and Include in C

Last Updated on January 30, 2023 by Prepbytes

In this article, we will learn about preprocessor directives, define and include in C with the examples. We will also learn about the syntax of define a and include in C. In the end, we will discuss some of the uses of include and define keyword in C.

#include and #define are the preprocessor directives. #include is used to include the header files in a source code file. #define is used to define macros or constants in programs.

Introduction to Define and Include in C

Preprocessor directives in C are commands that are processed by the preprocessor before the source code is compiled. They begin with the "#" symbol and are not terminated by a semicolon. Some common preprocessor directives in C include:

  • #include: This directive is used to include header files in a source code file. It can be used with either angle brackets (< >) or double quotes (" ") to specify the location of the header file.
  • #define: This pre-processor directive in Cis used to define a constant or a macro. It can be used to give a name to a constant value or to create a macro that can be used throughout the code.
  • #ifdef, #ifndef, #endif: These directives are used for conditional compilation. They are used to include or exclude parts of the code based on whether a certain symbol is defined or not.
  • #pragma: This directive is used to specify implementation-specific options to the compiler.
  • #error: This directive is used to generate a compile-time error with a specified error message.

These are some of the most common preprocessor directives in C, but there are others as well. It is important to note that preprocessor directives are processed before the actual compilation, so they are not part of the C language but rather a feature of the preprocessor.

define in C

In a C program, the preprocessor command #define is used to define C macros. #define also known as macros directive. In a C program, the #define directive is used to declare constant values or expressions with names that may be used repeatedly. Every time a #define C pre processor directive is encountered, the defined macros name substitutes a specified constant value or expression in its place.

Syntax of #define in C

The syntax for #define preprocessor directive in C is as given below:

#define CNAME expression

                 OR

#define CNAME value
  • CNAME: It is the name of the expression or constant value. Typically, programmers define it in capital letters.
  • expression: It is any mathematical formula or piece of code that can be used.
  • value: It is a constant of any data type like int, char, float, etc.

Example of #define keyword in C

#include <stdio.h>

// defines the PI value to be 3.14 in the whole program
#define PI 3.14

int main() {
    float radius, circumference;
    
    radius = 56.0;
    
    // PI will be replaced by 3.14 in the below statement
    circumference = 2 * PI * radius;
    
    printf("Circumference of Circle : %0.2f", circumference);
    
    return 0;
}

Output:

Circumference of Circle : 351.68

Explanation: In the above code we have used #define and assigned the value 3.14 to “PI”. This will replace “PI” in the program with the value 3.14.

Uses of #define keyword in C

Some common uses of "#define" include:

  • Defining constants: The #define define C directive can be used to assign a constant value to a preprocessor macro, which can then be used throughout the program in place of the value. For example, instead of using the value 3.14159 throughout the program, one can use a macro named PI, defined as #define PI 3.14159.
  • Creating custom macros: The programmer can create custom macros using the #define c directive, which can be used to simplify complex expressions or to provide a more meaningful name for a value.
  • Conditional compilation: The preprocessor directives like #ifdef, #ifndef, #if, #else, and #endif can be used to control the compilation of certain sections of code based on the existence of a macro.
  • Creating shorthands for keywords: The programmer can use #define to create shorthands for keywords or commonly used phrases that are used frequently in the code.
  • Creating platform-specific code: The programmer can use #define c to create platform-specific code for different operating systems or architectures.
  • Creating custom debug statements: The programmer can use #define to create custom debug statements that can be enabled or disabled by changing a single line of code.

include in C

In C programming, the "include" directive is used to include header files in a source code file. The header file contains function and variable declarations that can be used in the source code file. The header file is included using the preprocessor directive "#include" followed by the name of the header file in double quotes or angle brackets.

Syntax of #include in C

The syntax for #include preprocessor directive in C is as given below:

#include 

              OR

#include "file_name"
  • File_name: It is a header file’s name that you want to include. A header file is a C file with a usual ".h" file extension that includes declarations and macro definitions that may be shared by many source files.

Example of #include in C

#include <stdio.h>

int main() {
    int a, b, product;
    
    // we can use printf() and scanf() function because 
    // these functions are pre-defined in the stdio.h header file
    printf("Enter two numbers to find their sum : ");
    
    scanf("%d %d", &a, &b);
    
    product = a*b;
    
    printf("Sum of %d and %d is : %d", a, b, product);
    
    return 0;
}

Output:

Enter two numbers to find their sum : Sum of 3 and 4 is : 12

Explanation:
In the above code, we have include the header file named “stdio.h” which contains definition of functions for taking input and producing output. We are able to use the scanf(), and printf() functions, because we have included the header file using the #include keyword.

Uses of #include in C

Some common uses of "#include" in C include:

  • Including standard library headers: Many of the standard C library functions, such as "printf", "scanf", and "malloc", are declared in headers like "stdio.h" and "stdlib.h". These headers are typically included at the top of a C source file to make the functions they contain available to the rest of the program.
  • Including user-defined header files: A programmer can create their own header files containing declarations for functions, variables, and other constructs that they use in multiple source files. Including these header files in multiple source files allows the programmer to share common declarations and reduce code duplication.
  • Including third-party headers: When using third-party libraries or frameworks in a C program, it is often necessary to include their associated header files in order to access the functionality they provide.
  • Conditional inclusion of headers: The preprocessor directives like #ifdef, #ifndef, #if, #else, and #endif can be used to include or exclude header files based on certain conditions.
  • Creating custom macros: The programmer can create custom macros using the #define directive, which can be included in source files using the #include directive.

Summary
Preprocessor commands #define and #include are created outside of the main() function. In order to compile our software, preprocessor commands must first be run. A constant value or expression that may be used across the whole program is declared using the #define keyword in c. A header file’s contents are included in our C program using the #include statement.

Leave a Reply

Your email address will not be published. Required fields are marked *