Difference between revisions of "Standard Predefined Macros"
From Embedded Systems Learning Academy
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...") |
Proj user18 (talk | contribs) |
||
Line 5: | Line 5: | ||
Also, we do encounter a few simple interview questions along these lines. | Also, we do encounter a few simple interview questions along these lines. | ||
− | Most common ones are | + | 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: | Following are few of the macros and how they can be used: |
Revision as of 06:19, 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.
#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