Difference between revisions of "Standard Predefined Macros"

From Embedded Systems Learning Academy
Jump to: navigation, search
Line 19: Line 19:
  
 
The above macros can be used as a part of debugging any errors in a particular part of the program.
 
The above macros can be used as a part of debugging any errors in a particular part of the program.
 +
It eliminates the need to write multiple debug messages. Also, it eliminates the problem of keeping any debug prints updated as code is modified.
  
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">

Revision as of 07:31, 18 December 2016

Standard Predefined Macros

C exposes a plethora of predefined macros which can be very useful in our day-to-day coding.

Also, we do encounter a few simple interview questions along these lines.

Most common ones are: How can we print the line number of a program or the file name.

Following are few of the macros and how they can be used:

__LINE__

This macro expands to the current executing line number.

__FILE__

This macro expands to the name of the current executing file.

__func__

This macro expands to the name of the current executing function.

The above macros can be used as a part of debugging any errors in a particular part of the program. It eliminates the need to write multiple debug messages. Also, it eliminates the problem of keeping any debug prints updated as code is modified.

#include <iostream>

using namespace std;

void test_standard_predefined_macros();

int main()
{
	test_standard_predefined_macros();

	return 0;
}

void test_standard_predefined_macros()
{
	cout << "File: " << __FILE__ << endl;
	cout << "Function: " << __func__ << endl;
	cout << "Line: " << __LINE__ << endl;
}


Output:
File: ../testPredefinedMacros.cpp
Function: test_standard_predefined_macros
Line: 18