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]
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
Slno | Macro Name | Description |
---|---|---|
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