Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programs » C preprocessor programs

Print error message from any function with function name, line number in C



Example of __LINE__ and __func__ preprocessor directive in C language: Here, we will learn how to print error message from any function with function name, line number in C language?
Submitted by IncludeHelp, on September 07, 2018

Sometimes, it is necessary to print some message on logic failure or anytime with the function name and line number, so that program can be debugged and fixed the issue.

Prerequisite:

  • __LINE__: To print current line number
  • __func__: To print the name of the current function

Program:

#include <stdio.h>

float devide(int a, int b)
{
	float result;
	
	if(b!=0){
		result = (float)a/(float)b;
	}
	else{
		result=0;
		printf("Error at line %d in function \"%s\"\n",((__LINE__)-3),__func__);
		//ERROR at 3 lines above: ((__LINE__)-3)
	}

	return result;
}

int main()
{
	printf("%f\n",devide(10,3));
	printf("%f\n",devide(10,0));	
	return 0;
}

Output

3.333333
Error at line 9 in function "devide"
0.000000





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.