Wednesday 5 February 2014

chapter 2 Function printf() and scanf()

In my previous post we made a program but that program not gives any output. because the we were not using method/function for get printed something on monitor. Now we learn about functions. After function we again write our first program.
There are two type of function

  • Built in Function/Predefined function
  • User defined Function



Built in function


We can use many function in C. Such function that only use but modification ( definition) is not possible is known as Built in function or library function or predefined function. These function are already define by developer of C language. When we use any predefined function we must include a library file/ header file.


User defined function


First example of user defined function is main() function. This function must define by programmer. The execution of program is starts from main() function. Programmer writes whole codes or statement here, because of this the program become large and complex.Therefore programmer writes some of statement after main() function. Because of this the program can easy to understand.

We can give name of function in main function. These process is known as function calling. We will better understand in our example

Again rewrite our first program 'Sum'. Addition of 10 and 20.




int main()
{

int x,y,z;
x=10;
y=20;
z=x+y;
printf("%d",z);

return 0;
}

In our new program a predefined function printf() is used. This function can be called to get something printed on monitor.Print F stands for print as specified format.
it will print 30 as a string not like an integer.

Because of this 30 should get printed on monitor. But this program not give any output even this program is not execute because it has an error. In my next post we will rewrite this program. :)

note:
In our program we are using default value of x and y, that are already assigned by the programmer (me). It means when our program run it add the number 10 and 20 only.There is a limitation in our program. To solve this problem we must introduce a predefine function scanf().

printf() and scanf()


printf() and scanf() are predefined functions and its prototype is stored in stdio.h header file. printf() function is used to something gets printed on monitor of specification in format like integers , constant etc. The scanf() function is same as the printf() function but it reads characters from standard input device keybord instead of print.

Because of scanf() function the user can also provide the input to our program.

note:  if we want to use printf() and scanf() we must include stdio.h header file.It is compulsory to include header file for predefine functions...

What is header Files ?


header files and library files are the files in which the compiled/machinery code of predefine function is stores. Here we are only use two predefine functions printf() and scanf() and that's prototype ( like definaion ) is available in stdio.h header file.

Our first valid program is following:


The output of above program is :
#include<stdio.h>
void main()
{
int x,y,z;
printf("Enter a number ");
scanf("%d",&x);
printf("Enter another number ");
scanf("%d",&y); z=x+y;
printf("Sum is %d",z);
}



Now our first program is working.
note:"; " is used for terminating the statements.