Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C language: meaning of operator "#" ?

Tags:

c

This code can be compiled by gcc -g -o main main.c, no warnings.

// main.c
# 41 "stdio.h"
# 22
#
int main(void){
    (void)printf("foo");
    return 0;
}

My question is:

  1. What is the meaning of operator "#" before "include". We use #include <stdio.h> to include the header file, why not include <stdio.h> ?

  2. Why # 22 or # 41 can be compiled, I have never seen it before.

Anybody can help me ?

like image 376
Anthony Cooper Avatar asked Aug 27 '14 10:08

Anthony Cooper


People also ask

What is the operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What is operator in C and its types?

Summary. An operator is a symbol which operates on a variable or value. There are types of operators like arithmetic, logical, conditional, relational, bitwise, assignment operators etc. Some special types of operators are also present in C like sizeof(), Pointer operator, Reference operator etc.

What are operators explain?

In mathematics and computer programming, an operator is a character that represents a specific mathematical or logical action or process. For instance, "x" is an arithmetic operator that indicates multiplication, while "&&" is a logical operator representing the logical AND function in programming.


2 Answers

This looks like the output of the preprocessor. The line

# 41 "stdio.h"

can be interpreted like:

  • 41 has the meaning:

It specifies the line number which should be reported for the following line of input. Subsequent lines are counted from linenum.

  • "stdio.h" is just the filename from which the lines are taken.

The following line and all subsequent lines are reported to come from the file it specifies, until something else happens to change that. filename is interpreted according to the normal rules for a string constant: backslash escapes are interpreted.

And all this information is taken from https://gcc.gnu.org/onlinedocs/cpp/Line-Control.html#Line-Control and https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html#Preprocessor-Output

For the single #:

The null directive consists of a ‘#’ followed by a newline, with only whitespace (including comments) in between. A null directive is understood as a preprocessing directive but has no effect on the preprocessor output. The primary significance of the existence of the null directive is that an input line consisting of just a ‘#’ will produce no output, rather than a line of output containing just a ‘#’. Supposedly some old C programs contain such lines.

taken from https://gcc.gnu.org/onlinedocs/cpp/Other-Directives.html#Other-Directives

like image 71
Ferenc Deak Avatar answered Oct 16 '22 07:10

Ferenc Deak


A # at the start of a line introduces a preprocessing directive. Preprocessing is conceptually an earlier stage of translation than compilation, which is why it uses an easily-recognisable syntax.

If the # is followed by anything other than include, define, undef, line, error or pragma, then it is a non-directive (6.10p1), and is ignored.

This means that # at the start of a line is a simple way for the compiler to pass information between stages of translation; here it is being used to pass source file and line number information.

like image 2
ecatmur Avatar answered Oct 16 '22 07:10

ecatmur