In this tutorial, we will discuss input output function in the C language.
The C programming language provides various built-in functions(in-built library function ) for several purposes as a mathematical calculation, string manipulation and input-output operation and many more.
input and output functions are very important and mostly used in the C programming language for input and output operation – to read any given data and to display information on the screen.
We must know about two input output functions as a lot of usage in C language for input and output task.
There are printf() and scanf() function
In the C programming language, the printf() function is used to display the character, string, integer, float data onto the output screen
The common pattern of printf() function
printf("formated string",argument);
Example
printf("%d",a);
Example
#include <stdio.h> //stdio.h header file contain definition of printf() function int main() { printf("code4coding.com is a coding site"); //display string content using printf() function return 0; }
When the above code is executed, it produces the following results
code4coding.com is a coding site
This is an example for integer output using printf() function in C language
#include <stdio.h> #include <stdlib.h> int main() { int age=34; printf("My age is: %d\n",age); getch(); return 0; }
When the above code is executed, it produces the following results
My age is: 34
#include <stdio.h> #include <stdlib.h> int main() { float average=67.98; printf("My average is: %.2f\n",average); getch(); return 0; }
When the above code is executed, it produces the following results
My average is: 67.980003
In the C programming language, the Scanf() function is used to read and store the character, string, numeric data as a standard input from the keyboard
Syntax
scanf("formated string",&argument);
Example
scanf("%d",a)
string input output
#include <stdio.h> #include <stdlib.h> int main() { char web[20]; printf("Enter website name: \n"); scanf("%s",web);//use scanf function for input printf("Your website name is: %s ",web);//use printf function for output getch(); return 0; }
When the above code is executed, it produces the following results
Enter website name: codeforcoding.com your website name is: codeforcoding.com
integer input output
#include <stdio.h> #include <stdlib.h> int main() { int age; printf("Enter your age: "); scanf("%d",&age);//use scanf() function for input printf("My age is: %d\n",age);//use printf() function for output getch(); return 0; }
When the above code is executed, it produces the following results
Enter your age: 23 My age is: 23
float input output
#include <stdio.h> #include <stdlib.h> int main() { float average; printf("Enter your average marks: "); scanf("%f",&average);//use scanf() function for input printf("My average is: %.2f\n",average);//use printf() function for output getch(); return 0; }
When the above code is executed, it produces the following results
Enter your average marks: 76.5 My average is: 76.50
Character input output
#include <stdio.h> #include <stdlib.h> int main() { char ch; printf("Enter a character: "); scanf("%c",&ch);//use scanf() function for input printf("your entered %c\n",ch);//use printf() function for output getch(); return 0; }
When the above code is executed, it produces the following results
Enter a character: A you entered A
Suggested for you
gets() and puts() function in C
User defined function in C Language
Function in C Language with example
Arithmetic function in C Language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…