Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printf was not declared in this scope

Tags:

c

I have just started to learn C. But the first program in the book which i am referring generates an error that

"printf was not declared in this scope"

I am typing the code exactly as it is given in the book. Still the error is being given. book

code

like image 373
infiNity9819 Avatar asked Jul 25 '16 05:07

infiNity9819


People also ask

How do I fix printf is not declared in this scope?

to your program. It provides the declaration of printf and many other functions. For a complete list of functions defined in the file, lookup its documentation. You can lookup man-pages of functions to figure out which standard header file needs to be #include d to get the declaration of a function.

What does not declared in this scope mean C++?

As from the name we can understand that when the compiler of Arduino IDE is unable to recognize any variable or is unable to process any loop or any instruction having any undeclared variable so it gives the error “not declared in this scope”, which means that code is unable to understand the instruction given in the ...


3 Answers

The book is outdated.

Add

#include <stdio.h>

to your program. It provides the declaration of printf and many other functions. For a complete list of functions defined in the file, lookup its documentation.

like image 107
R Sahu Avatar answered Sep 19 '22 14:09

R Sahu


The compiler didn't find declaration for printf function. That's why it shows compilation error.

The correct declaration (ISO/IEC 9899:1999) of printf function is:

int printf(const char * restrictformat, ... );

You can either declare the function like above before calling it or you can include header file which contains declaration of that function. But it would be easiest and safest to just include the header file which contains declaration of your function (#include <stdio.h> for printf).

If you want to know why you need to supply declaration of the function before calling it, you can have a look at this question. The explanation is given below-

The C programming language was designed so that the compiler could be implemented as a one-pass compiler. In such a compiler, each compilation phase is only executed once. In such a compiler you cannot referrer to an entity that is defined later in the source file.

Moreover, in C, the compiler only interpret a single compilation unit (generally a .c file and all the included .h files) at a time. So you needed a mechanism to referrer to a function defined in another compilation unit. All identifiers in C need to be declared before they are used. This is true for functions as well as variables. For functions the declaration needs to be before the first call of the function. A full declaration includes the return type and the number and type of the arguments. This is also called the function prototype.

You can also define a function before calling it in the same compilation unit. Or you can just declare it before calling it. It is better idea (not always) to include the header file which contains the declaration of the function.

and consider buying a new book. The author should have mentioned the header file inclusion.

like image 23
abhiarora Avatar answered Sep 18 '22 14:09

abhiarora


printf() is declared in stdio.h header file.

Add this as the first line of your program:

#include <stdio.h>
like image 44
CinCout Avatar answered Sep 16 '22 14:09

CinCout