Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C

Learn step by step



C Input/Output

ANSI standard has defined many library functions for input and output in C language. Function scanf() and printf() are the most commonly used to take input and display output respectively. We consider an example:


#include <stdio.h> //To run printf() function.
int main()
{
printf("Tuton - Redefining online Education"); //displays the content inside quotation
return 0;
} RUN

Explanation of How this program works
  • Every program starts from main() function.
  • printf() is a C library function to display output which only works if #include is included at the beginning.
  • Here, stdio.h is a header file (standard input output header file) and #include is command to paste the code from the header file when necessary. When compiler encounters printf() function and if it doesn't find stdio.h header file, compiler will shows error.
  • Code return 0; indicates the end of the program. You can ignore return 0; statement but, it is good practice to use return 0 in program.
I/O of integers in C:

#include<stdio.h>
int main()
{
int p=12;
printf("Number=%d",p);
return 0;
} RUN

I/O of floats in C:

#include<stdio.h>
int main()
{
float q;
printf("Enter value: ");
scanf("%f",&q);
printf("Value=%f",q); //%f is used for floats instead of %d
return 0;
} RUN

Conversion format string "%f" is used for floats to take input and to display floating value of a variable.

I/O of characters and ASCII code:

#include <stdio.h>
int main(){
char var1;
printf("Enter character: ");
scanf("%c",&var1);
printf("You entered %c.",var1);
return 0;
}

Output

Enter character: g
You entered g.

Conversion format string "%c" is used in case of characters.

For more details how ASCII character code work click here.



Related Videos