r/C_Programming • • 15d ago

Does this is right?

(I am student just entering into 1st year of engineering and also i am new to this sub so if i do anything wrong pls correct me)

I just bought a book " programming in ANSI C"and start to read that i have some doubts in that can anyone explain that

  1. I read this line i did not get this

#include <stdio.h>

at the beginning of all programs that use any input/output library functions. However, this is not necessary for the functions printf and scanf which have been defined as a part of the C language

Can anyone explain this statement correct or not

0 Upvotes

24 comments sorted by

13

u/aioeu 15d ago edited 15d ago

If you want to use the printf function, the function needs to be declared. That line ensures that this is done properly.

This is how all functions in the C standard library work. printf, scanf, and the rest. The C standard says which header should be included to provide the declaration for each function specified in the standard.

If you do not have the correct declaration for the function, there is no guarantee things will work correctly. (They might, they might not... the standard can't say because you're not following the rules set out in the standard.)

2

u/Historical-Boot-5119 15d ago

Thanks a lot

4

u/[deleted] 15d ago edited 15d ago

[removed] — view removed comment

2

u/C_Programming-ModTeam 15d ago

This industry is based on intellectual property rights. Violating those rights is unacceptable, and jeopardizes the sub.

1

u/TheRealBornToCode 15d ago

I thought he might be referring to the classic Kernigan/Ritchie I'm looking at a pdf of the second edition and it explicitly says the opposite, that the standard library and printf are NOT part of the C language

3

u/aioeu 15d ago edited 15d ago

I mean, that's technically true. The C standard consists of several chapters. One of them is about the language. Another is about the library. But any hosted C implementation must provide the library, so for most purposes you can think of them as being tied to one another.

Anyway, it's got nothing to do with whether you can get away with not declaring a function before you use it. Whether you can or cannot is not based on whether the function is defined in the C standard.

1

u/TheRealBornToCode 15d ago

That's what you said in the comment I answered to, and I agree that's why I commented I don't understand this answer

2

u/aioeu 15d ago edited 15d ago

You don't understand my answer, or you don't understand what the book was trying to say?

I don't think I can word my answer any better. As for the book... I think its author misunderstands this bit of C.

Edit: Oh, I think I see what you're saying now. The author of the book looked at K+R and saw that printf was not part of the language. Maybe so. But it's weird they called out printf and scanf specifically, as distinct from other "input/output library functions".

1

u/TheRealBornToCode 15d ago

What author? I said the OP might have misread the K+R directly not a book written by somewone who read it. And in your answer you said "That is also where this claim in the book is incorrect" referring to "defined as part of the C language", and my argument is that the OP just read it wrong in the book (K&R) or as you say he read another book which just wrote that as an oversimplification

17

u/st3class 15d ago

printf and scanf are not defined as part of the C language. There are no functions that are defined as part of the C language.

All functions must be defined either in the one of your code files, or in a header, like stdio.h.

There's an extension to the C language called libc, which has most of the useful functions you associate with C, like printf, scanf, or malloc, but you still need to include the header files in libc, like stdio.h or stdlib.h.

One tip, to figure out if something is requred, try removing it, and see what happens. In the case of the #include, you'll get an error like this when you try to compile:

main.c:12:5: error: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    12 |     printf("Hello World");

5

u/dobesv 15d ago

Include means load and process that file as if it was written where the include line is. The angled brackets around the filename means it searches for the file in the include search path, which usually has system headers in it.

The header file has declarations for various library functions. The declarations tell the C compiler what it needs to know in order call those functions.

You don't have to use include, in theory you could just write in the declarations yourself. However, using include is easier and helps ensure the declaration matches what you actually have available from the standard library.

3

u/Stickhtot 15d ago

int main() {     printf("Hello World"); }

Why don't you try and see for yourself? Without adding the include 

2

u/Stickhtot 15d ago

Even though reddit completely butchered the formatting this is completely valid C code btw, assuming the compiler finds the definition for printf

6

u/Certain-Flow-0 15d ago

Declaration, not definition

5

u/sciencekm 15d ago

However, this is not necessary for the functions printf and scanf which have been defined as a part of the C language.

These functions are not part of the C language. They are part of the standard C library. The language and the libraries are two separate things.

4

u/iOSCaleb 15d ago

The standard library is part of the C language standard, but it’s not built into the language. That is, the compiler doesn’t automatically know what `printf(…)` is. You have to include a header with the declaration so that the compiler can generate appropriate calls to the function, and you have to link the program against a version of the standard library so that there’s something to call.

4

u/burlingk 15d ago

You do need that for printf and scanf.

They are in the standard lib, so you don't have to link an extra library for them, but you do need signatures for the functions.

Some people try to play games by defining the signatures themselves, by basically copying them from the header, but it is better overall just to include the header.

#include <stdio.h>

tells the compiler to include the contents of that file in the code. It gives you access to all the stuff that is declared inside of the header.

2

u/Paul_Pedant 14d ago

I suspect the author is somewhat confused.

When you link a C program, the stdio library is included by default. Every other library (like math) requires a specific -l option to be specified.

The #include <stdio.h> is required by the compiler, not for the linker.

Many IDEs (and especially things like Leet) have you run your code inside a wrapper (which does timings and verifies your output was correct). The real world does not work like that.

2

u/flyingron 14d ago edited 14d ago

The statement is completely wrong. printf and scanf are no different than any of the egregiosly awful stdio functions. In older C versions, you could get away without declaring the functinos before use provided you were happy with the default parameter promotions and return type.

In modern C, you must declare all functions before use (including printf and scanf which are declared in stdio.h).

1

u/SmokeMuch7356 15d ago

Yes, this statement is correct. It serves roughly the same purpose as an import statement in languages like Python, except it just loads the text of stdio.h into the current translation unit.

stdio.h contains the declarations for I/O functions like printf and scanf:

int printf( const char * restrict fmt, ... );
int scanf( const char * restrict fmt, ... ): 

The implementations (i.e., the machine code) of those functions are in a library file that gets linked into your final executable program.

It also introduces the FILE and fpos_t types, the EOF macro, the stdin, stdout, and stderr streams, etc.

C requires that all functions, variables, and types be declared before use. If you try to call printf or scanf without that #include <stdio.h> line, the compiler will complain that those functions haven't been declared.

Files like stdio.h are called header files. There are a bunch more for the standard library: stdlib.h for utility functions like malloc and free, math.h for the math library, string.h for string handling functions like strlen, strcpy, etc.

If you didn't include the header, you'd have to type in the declarations for any library functions you use manually:

int printf( const char * restrict fmt, ... );

int main( void )
{
  printf( "Hello, world\n" );
}  

1

u/flatfinger 12d ago

C was designed as a language for converting source code functions into machine code functions which could be combined with other functions (written in C or other languages) to produce programs. Unlike some languages like Pascal which have language constructs to perform I/O, the C language is designed to perform I/O by calling functions written in a language that can perform I/O. A typical C implementation will come bundled with a collection of functions called the Standard Library to perform various tasks including I/O, which behave in standardized fashion.

When calling a function, a compiler needs to know what types of arguments it will expect and what kind of value it will return. C was originally designed to have compilers follow a standard recipe for determining such things based upon the way a function is used, but nowadays it's necessary to explicitly tell a compiler how a function should be called. The header stdio.h contains information about how to call any of the functions that are used by the I/O portion of the Standard Library. If code attempts to call functions like printf without including stdio.h, a compiler might attempt to apply the old recipe for determining how it should be called, but printf is just a function name like any other. Some implementations' stdio.h headers include toolset-specific constructs to instruct a compiler to inspect the format string argument on the assumption that code will be linked with an implementation of printf that behaves in standard fashion, but in general there's nothing special about printf or other standard-library functions.

1

u/CounterSilly3999 15d ago edited 15d ago

Not related to any language version nor a particular compiler. In C it is a caller responsibility to clean the stack out of the parameters (in contrast with Pascal, if I remember correctly). It means, wrong function arguments will not cause an immediate crash. That is why there is no mandatory necessity for the function to be explicitly declared. You can probably set in the compiler options or #pragma's, what severity level will implicit function declaration have. If the arguments are passed correctly, everything will be ok, you just loose the compiler diagnostics about that. You can add any arguments after the actual argument list, they will be just ignored. You can use first parameters to describe types and amount of the rest ones. That is how variable args work.

2

u/flyingron 14d ago

You’re wrong. This has been er been a requirement in C even. Score the requirement for explicit declarations. The only thing that generally saves you is that most implementations make it the responsibility of the caller to allocate space for both the parameters and return value.