Ads

The printf and the scanf (C Library Functions)

C - FUNCTIONS:

- Till now we know that keywords are the only C elements identified by compiler, then how could be the C-programming language so powerful with just 32 keywords?
-Here comes the role of functions.
- Each compiler is provided with set of pre-defined functions whose object code is present in library. And they are linked to our file at linker stage to form an executable file.

What is a function?
- A function has got set of instructions. Whenever you require a specific set of instructions to be executed multiple times in a single program we can write those instructions in a function definition and call that function every time instead of writing those lengthy instructions again and again.
- A C program is made of functions, in each and every C program there is at least one function. One such function is main(). The main() is a function which indicates the C compiler that, the program starts here.

Syntax:
int main()   /* start of main function */
{

} /* end of main function */

- There are two types of functions
i) User Defined Function: These functions are defined by user.
ii) Pre-defined or built in Function: These functions are pre-defined and the object-code for them are present in library files (and are linked at linker stage).

- Right now we are going to concentrate only on predefined functions, we will deal with user defined functions in forthcoming chapters.
- Some pre-defined functions are printf(), scanf(), getchar(), putchar(),strlen() etc. And of course, if there are predefined functions then definitely there might be definition somewhere in the files, yes, you guessed it right, in library files.
- But our compiler cannot identify a predefined function if we use it directly in our program, without indicating its respective header file.
- Let me get it to you in simple way. The code for the function is defined in library files and their declarations are present in header files, that’s the reason we indicate the required header file in each and every program depending on the functions we use.
- For example declarations for functions printf() and scanf()are present in header file called stdio.h and declaration for functions getch() and putch() are present in header file called conio.h , so whenever we are going to use printf or scanf functions in a C program we have to include header file stdio.h in Linking section.

Syntax:
#include<stdio.h>

Printf and scanf functions:

- As we discussed earlier that, whenever we write a program which includes predefined functions we include respective header files. In here we use printf() and scanf(), thus we include header file stdio.h, so in example programs below you will find stdio.h in each and every program.

Printf():

- The C library function printf prints the data you write in between inverted comma’s within the brace’s.
- It has a return value and it returns the number of characters it printed.

- The declaration of printf function in stdio.h is

extern int printf (const char *format, ...);

-  It’s not that important right now to know what is meant by return type or an argument. We learn them in separate chapter called FUNCTIONS. But it is better that you know how a printf is declared.

Syntax:
printf("the content you want to print",arguments_list);

EX:
printf("This is what printf prints");

Program:

/* A program which explains the usage of pre-defined function printf() */ 

#include<stdio.h>
int main()
{
 printf("\n Hello World");
 return 0;
}


Output:
Hello World




No comments:

Post a Comment