I see this in KR C programming language. I really did not understand what it means. If it is line number of current source then we could use __LINE__. But what is the use of #line actually?

Recommended Answers

All 2 Replies

i never understood it either, as the more obscure commands in the C pre-processor are beyond the scope of my typical work.

that said, a good overview of the C pre-processor and descriptions of all the commands in the processing language can be found at http://gcc.gnu.org/onlinedocs/cpp/index.html#Top .. line control has its own section

Read this link.

In order for the compiler to produce meaningful references to line numbers in preprocessed source, the preprocessor inserts #line directives where necessary (for example, at the beginning and after the end of included text).

When the preprocessor parses your *.c program it will create a *.i file that expands all the #include and other preprocessor statements. In doing that all the include files are merged into one *.i file. When doing that it adds the #line directives so that it can keep track of the line numbers from the original file. Then when the compiler compiles the *.i file it will know the line numbers from the original files and use them to produce meaningful error messages. Without #line directives you would get line numbers from the *.i file, not the *.c or *.h file that you wrote.

I know of no reason why a programmer would want to insert #line directives in his program.

commented: thanks for parsing that. +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.