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

Leave a Reply

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