C/printf

Advertisement


Question
Hello narendra,
         
         printf is a function. so where its definition is stored. what is the need of header file..

Answer
I think, yesterday itself I answered this question and I think you only asked this!

Anyway, I will answer this again:
1. printf is a function. so where its definition is stored?
It's definition is in a library called libc.so
This library gets linked to any program that you compile.
Compiler itself picks up some standard libraries and hence you won't see its linking in the command.

2. what is the need of header file..?
Header file contains the declaration.
If you know the difference between definition and declaration, then you will understand this.
For your compiler to compile your code, it needs to know any variable or function name before it gets used.
Otherwise it gives errors.
The reason is, without knowing the type of the variable or function prototype, it will not be able to does further processing. For this it needs to know these info.
Now, you use your printf() and many similar functions thru out your program and you haven't defined them.
The definition is in a library and that comes into picture only during linking! So, how to know the prototype of printf() function.
This is done with the help of header files.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.