
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What are __FILE__, __LINE__, and __FUNCTION__ in C++
Here we will see what are the __FILE, __LINE__ and __FUNCTION__ in C++.
The __FILE__
This macro is used to get the path of the current file. This is useful when we want to generate log files. The following code will explain its functionality.
Example
#include<iostream> using namespace std; int errorLog (const char* file, const std::string& msg){ cerr << "[" << file << "] " << msg << endl; } #define LOG( msg ) errorLog( __FILE__, msg ) main() { LOG("This is a dummy error"); }
Output
[D:\Misc C and C++ Questions\test_prog.cpp] This is a dummy error
The __LINE__
This macro can find the current line number in source file. This line number is an integer value. When log statements are generating then __LINE__ plays some useful role. See the following example to get the idea.>
Example
#include<iostream> using namespace std; int errorLog (int line, const std::string& msg){ cerr << "[" << line << "] " << msg << endl; } #define LOG( msg ) errorLog( __LINE__, msg ) main() { LOG("This is a dummy error"); }
Output
[12] This is a dummy error
The __FUNCTION__
This macro can return the current function. When log statements are generating then __FUNCTION__ plays some useful role. See the following example to get the idea.
long double rintl(long double argument)
Example
#include<iostream> using namespace std; int errorLog (const char* func, const std::string& msg){ cerr << "[" << func << "] " << msg << endl; } #define LOG( msg ) errorLog( __FUNCTION__, msg ) void TestFunction(){ LOG("Send from Function"); } main() { TestFunction(); LOG("This is a dummy error"); }
Output
[TestFunction] Send from Function [main] This is a dummy error
- Related Articles
- What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__ in C/C++?
- What are auto and decltype in C++?
- What are Lvalues and Rvalues in C++?
- What are Aggregates and PODs in C++?
- What are rvalue and lvalue in C#?
- What are signed and unsigned keywords in C++?
- What are increment (++) and decrement (--) operators in C#?
- What are Base and Derived Classes in C#?
- What are C operators and Punctuators?
- What are the differences between bitwise and logical AND operators in C/C++
- What are variables and types of variables in C++?
- What are local variables and global variables in C++?
- What are cin, cout and cerr streams in C++?
- What are string and String data types in C#?
- What are reading and writing characters in C language?

Advertisements