Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is the #line directive in C?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The #line directive is one of the widely used directives in C. Its main purpose is to reset the line number and filename in the code. This means that we can reset any line of code to an arbitrary line number or a filename defined by the user.

#line is an unconditional preprocessor directive
#line is an unconditional preprocessor directive

Syntax

#line <new line number> <new file name>

Code

Let’s look at a code example for the use of #line.

#include <stdio.h>
int main()
{
printf("Hello world\n"); // this is line 6
printf("Line: %d\n",__LINE__); // printing line number, line 7
#line 23 //reseting to 23, although next line number is line 10.
printf("Line: %d\n",__LINE__); // printing line number
printf("Line: %d\n",__LINE__);
printf("Line: %d\n",__LINE__);
// let's try with filename (main.c) as well.
printf( "Line: %d, File: %s\n", __LINE__, __FILE__ );
// now we use line to reset filename to "new_filename.c"
// line number is set to 83
#line 83 "new_filename.c"
printf( "Line: %d, File: %s\n", __LINE__, __FILE__ );
return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring