Predefined Macros In C Programming Language

In today’s video tutorial lets look at couple of predefined Macros in C programming Language.

Uses of Predefined Macros

It’s very useful for developers while debugging the code, in a large project. Using these predefined macros you could write less code and integrate more features into the program. For example, to get current system date/time and timestamp you need not write lengthy code, just write the predefined macro and use the output in your source code.

Video Tutorial: Built-in Preprocessor Directive: C Program


[youtube https://www.youtube.com/watch?v=kcBc26sp_o0]

YouTube Link: https://www.youtube.com/watch?v=kcBc26sp_o0 [Watch the Video In Full Screen.]

Source Code: Predefined Macros In C Programming Language

#include<stdio.h>

int main()
{
    printf("File: %s\n", __FILE__);
    printf("Date: %s\n", __DATE__);
    printf("Time: %s\n", __TIME__);
    printf("Timestamp: %s\n", __TIMESTAMP__);
    printf("Line: %d\n", __LINE__);
    printf("ANSI: %d\n", __STDC__);
    printf("Function Name: %s\n", __func__);
    printf("Pretty Function name: %s\n", __PRETTY_FUNCTION__);

    return 0;
}

Output:
File: C:\Technotip\main.c
Date: Jul 18 2020
Time: 14:11:52
Timestamp: Sat Jul 18 14:11:48 2020
Line: 9
ANSI: 1
Function Name: main
Pretty Function name: main

SlnoMacro NameDescription
1__FILE__The name(along with full path) of the current file, as a string literal.
2__DATE__Current System date, as a string literal.
3__TIME__Current System time, as a string literal.
4__TIMESTAMP__Current System date and time(non-standard), as a string literal.
5__LINE__Current line in the source code file where __LINE__ is written, as numeric literal.
6__STDC__its value is 1, when the compiler compiles with the ANSI standard.
7__func__function name in which the __func__ resides.
8__PRETTY_FUNCTION__function name in which the __PRETTY_FUNCTION__ resides.

__PRETTY_FUNCTION__ and __func__ returns same value, but not all compilers support both. Sometimes its compiler specific.

Note: We do not include or import any file to make these Macros work. It looks like all these are working out-of-the box, so sometimes these are called as Magic Constants.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

#pragma Directive: C Program

In this video tutorial lets look at using #pragma preprocessor directive.

#pragma Directive

#pragma is a special purpose directive which can be used to turn on and off certain features.

Note: #pragma commands or keywords vary from one compiler to another. So make sure to check the keywords and/or syntax for the compiler you are using.

Video Tutorial: #pragma Preprocessor Directive: C Program


[youtube https://www.youtube.com/watch?v=3jsqavQ5faU]

YouTube Link: https://www.youtube.com/watch?v=3jsqavQ5faU [Watch the Video In Full Screen.]

Source Code: #pragma Directive: C Program

#include<stdio.h>

void init();
void end();

#pragma startup init
#pragma exit end

int main()
{
    printf("We're inside main method\n");

    return 0;
}

void init()
{
    printf("We're inside init method\n");
}

void end()
{
    printf("We're inside end method\n");
}

Output:
We’re inside init method
We’re inside main method
We’re inside end method

#pragma startup and #pragma exit are keywords.

#pragma startup followed by a function name: this makes the function name specified to be executed before main method.

#pragma exit followed by a function name: this makes the function name specified to be executed just before program termination.

Note:
#pragma startup and #pragma exit doesn’t work in GCC compiler. So the equivalent behavior can be obtained by using below syntax:

Source Code: For GCC Compiler: C Program

#include<stdio.h>

void __attribute__((constructor)) init();
void __attribute__((destructor)) end();

int main()
{
    printf("We're inside main method\n");

    return 0;
}

void init()
{
    printf("We're inside init method\n");
}

void end()
{
    printf("We're inside end method\n");
}

Output:
We’re inside init method
We’re inside main method
We’re inside end method

__attribute__((constructor)) acts like #pragma startup and __attribute__((destructor)) acts like #pragma exit.

#include<stdio.h>

void init();
void end();

int main()
{
    printf("We're inside main method\n");

    return 0;
}

void __attribute__((constructor)) init()
{
    printf("We're inside init method\n");
}

void __attribute__((destructor)) end()
{
    printf("We're inside end method\n");
}

Output:
We’re inside init method
We’re inside main method
We’re inside end method

We could even use __attribute__((constructor)) and __attribute__((destructor)) in function definition, as shown in above source code.

Source Code: Suppressing warnings using #pragma Directive: C Program

#include<stdio.h>

#pragma warn -rvl /* No Return Value */#pragma warn -par /* Parameter Not Used */#pragma warn -rch /* Unreachable Code */
int main()
{
    int count = 1;

    printf("%d\n", count);

    return 0;

    count++;

}

int total()
{
    printf("Sum of a and b is c\n");
}

void end(int x)
{
    printf("We're inside end method\n");
}

Output:
1

Here function total has a return type of integer, but it doesn’t return any value from inside, so it must throw “no return value” warning. But that warning is suppressed by #pragma warn -rvl .

Function end takes a integer argument, but the value is nowhere used inside the function definition. Compiler should throw “Parameter Not Used” warning, but it’s suppressed by #pragma warn -par.

Inside main method, there is count++ after return. This code is never executed. So the compiler should throw “Unreachable Code” warning, which is suppressed by #pragma warn -rch.

Note: -(minus or subtraction) symbol before rvl, par and rch means compiler removes the warning. + sign indicated the warning will be shown in the editor after compilation of the program.

Also note that,

#pragma warn -rvl /* No Return Value */#pragma warn -par /* Parameter Not Used */#pragma warn -rch /* Unreachable Code */

doesn’t work on GCC compiler.

Important Note: The functions init() and end() should not receive any arguments and should not return any value, if we want to handle their invocation via #pragma.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

#undef Directive: C Program

In today’s video tutorial lets see how to use #undef preprocessor directive.

Related Read:
C Program To Find Area of Circle Using Macro Expansion
Conditional Compilation In C: #if #elif #else #endif

What Does #undef Do?

#undef removes or undefines a macro name which is previously created or defined using #define directive.

Syntax

#undef MACRO_NAME

Note: If a Macro template is associated with a Macro expansion, you need not mention the macro expansion to undefine the macro. You simple undefine using this syntax: #undef MACRO_NAME

Video Tutorial: #undef Preprocessor Directive: C Program


[youtube https://www.youtube.com/watch?v=1FwlFR9PQDE]

YouTube Link: https://www.youtube.com/watch?v=1FwlFR9PQDE [Watch the Video In Full Screen.]

Source Code: #undef Directive: C Program

#include<stdio.h>

#define iOS
#undef iOS

int main()
{
    #if defined(iOS)
        printf("I Love Apple Devices\n");
    #else
        printf("Macro iOS has been undefined\n");
    #endif // defined

    return 0;
}

Output:
Macro iOS has been undefined

In above source code we’re undefining the Macro iOS immediately after defining it. So defined(iOS) returns false or 0. So code inside #else block gets compiled and executed.

#include<stdio.h>

#define iOS

int main()
{
    #if defined(iOS)
        printf("I Love Apple Devices\n");
        printf("Gift me a latest iPhone!\n");
    #else
        printf("Macro iOS has been undefined\n");
    #endif // defined

    return 0;
}

Output:
I Love Apple Devices
Gift me a latest iPhone!

Here Macro iOS is defined, so defined(iOS) returns true. That’s why block of code inside #if directive gets compiled and executed.

#include<stdio.h>

#define iOS

int main()
{
    #undef iOS
    #if defined(iOS)
        printf("I Love Apple Devices\n");
    #else
        printf("Macro iOS has been undefined\n");
    #endif // defined

    return 0;
}

Output:
Macro iOS has been undefined

Even though #define iOS is defined at global level you can undefine it inside main() method.

Note: defined() is a preprocessor operator which returns true if the passed macro name is defined orelse returns false or zero if its not defined.

Instead of defined() preprocessor operator you could even use #ifdef or #ifndef to check if the Macro template or Macro name is defined or not defined.

#include<stdio.h>

#define iOS

int main()
{
    int flag = 1;

    do
    {
        #if defined(iOS)
            printf("I Love Apple Devices\n");
            printf("This displays all your iPhone Device Details\n");
        #else
            printf("I'm inside do while block.\n");
        #endif // defined

        printf("Enter your choice? (0/1)\n");
        scanf("%d", &flag);

    }while(flag);


    #undef iOS

    #if defined(iOS)
        printf("I Love Apple Devices\n");
        printf("This displays all your iPhone Device Details\n");
    #else
        printf("Macro iOS has been undefined\n");
    #endif // defined

    return 0;
}

Output:
I Love Apple Devices
This displays all your iPhone Device Details
Enter your choice? (0/1)
1
I Love Apple Devices
This displays all your iPhone Device Details
Enter your choice? (0/1)
1
I Love Apple Devices
This displays all your iPhone Device Details
Enter your choice? (0/1)
0
Macro iOS has been undefined

In above program we are defining iOS macro globally. Inside main method we use do while loop and keep executing #if #else #endif directives. Since iOS is defined, the block of code inside #if gets executed each time. Once user enters 0 as input, the control exits do while loop execution. Next it encounters #undef iOS, so defined(iOS) now returns false or 0, hence code inside #else directive gets executed.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Conditional Compilation In C: #ifndef #else #endif

In this video tutorial lets learn about preprocessor command or directives like #ifndef, #else and #endif. These directives are used for conditional compilation. #ifndef works completely opposite to #ifdef

Related Read:
Conditional Compilation In C: #ifdef #else #endif

How Does #ifndef Work?

The block of code between #ifndef and #endif works only if the macro name is NOT defined. If the macro name is defined, the compiler will skip the entire block of code inside #ifndef from compiling.

When To Use #ifndef

We’ve explained this in detail for video tutorial: Conditional Compilation In C: #ifdef #else #endif It holds good for this video tutorial too.

Video Tutorial: Conditional Compilation In C: #ifndef #else #endif


[youtube https://www.youtube.com/watch?v=Y7UEY7Cxz7Y]

YouTube Link: https://www.youtube.com/watch?v=Y7UEY7Cxz7Y [Watch the Video In Full Screen.]

Source Code: Conditional Compilation In C: #ifndef #else #endif

#include<stdio.h>

#define iOS

int main()
{
    #ifndef iOS
        printf("I Love Apple Devices\n");
    #else
        printf("Code for Non Apple Devices\n");
    #endif // iOS

    return 0;
}

Output:
Code for Non Apple Devices

#include<stdio.h>


int main()
{
    #ifndef iOS
        printf("I Love Apple Devices\n");
    #else
        printf("Code for Non Apple Devices\n");
    #endif // iOS

    return 0;
}

Output:
I Love Apple Devices

In above programs, if iOS macro name is defined, then the set of code present inside #else block gets compiled and executed. If macro name iOS is not defined, then the code inside #ifndef block gets compiled and executed.

Note:

1. Using #else is optional. You can just use #ifndef and #endif.

2. Working of #ifndef is opposite to that of #ifdef.

3. This is similar to if else condition, but here the block of code which do not match the criteria doesn’t even get compiled. It’s treated like regular comments.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

#error Preprocessor Directive: C Programming

In this video tutorial, lets see how we can make use of #error preprocessor command or directive.

Where Is It Useful?

If you’re a Software developer working with C programming language, then your project will have a lot of code and dependency source codes. While compiling, compiler will take a lot of time. In big projects usually you forget the purpose of compilation by the time it completes compilation. In such cases, make use of #error directive. If there are any errors at certain locations the compiler will stop further compilation as and when it encounters #error directive. This saves a lot of time and resources. So #error directive is usually helpful in debugging your project code.

It’s also helpful in projects with critical user inputs. Suppose user enters wrong data due to which the project itself might crash and utilize a lot of resource while trying to execute. In such cases it’s better to terminate the program execution.

Video Tutorial: #error Preprocessor Directive: C Programming


[youtube https://www.youtube.com/watch?v=_w2OBtOvzTg]

YouTube Link: https://www.youtube.com/watch?v=_w2OBtOvzTg [Watch the Video In Full Screen.]

Source Code: #error Preprocessor Directive: C Programming

#include<stdio.h>

#define iOS

#ifndef iOS
    #error First Include iOS Macro
#else
    int main()
    {
        printf("I Love Apple Devices!\n");

        return 0;
    }
#endif // iOS

Output:
I Love Apple Devices!

Here #ifndef directive checks if the macro iOS is defined. If its NOT defined it returns true or 1. If it’s defined then it returns 0 or false.

So if the macro iOS is not defined, then the code inside #ifndef gets compiled and executed, else the code inside #else block gets compiled and executed.

When Macro iOS is not defined

When Macro iOS is not defined, the code inside #ifndef gets compiled, where the compiler encounters #error directive. Now the compiler immediately stops any further compilation and displays the error message associated with #error directive.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert