Standard Predefined Macros

From Embedded Systems Learning Academy
Revision as of 06:01, 18 December 2016 by Proj user18 (talk | contribs) (Created page with "= 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 ques...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 along this line: 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.

#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