printf() and scanf() Function in C

              printf() and scanf() Function in C

{tocify} $title= {Table of Contents}

In C programming, printf() and scanf() are two of the most commonly used standard input/output functions. In this article, we will discuss how these two functions (printf() and scanf()) are used for I/O in C, along with examples. The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file). These two commonly used functions for Input/output and both are library function of C. The printf() prints on screen while scanf() is used to take input from standard input like keyboard. Both functions are inbuilt library functions which is part of stdio.h header file. Before using both functions in our program, we must include stdio.h header file as shown below.

#include <stdio h. >

printf() function in C

The printf() function in the C programming language is used to print text to the standard output. It is defined in the standard library stdio.h. 

The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

Syntax:

printf("format string",argument_list);  

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

Example of printf() Function in C

Program: 


Output:


In this program, the #include line includes the standard input/output header file, which contains definitions for the printf function. The main function is the entry point of the program, and printf is used to print the message "Hello, World!" to the console. 

scanf() Function in C

The scanf() function is used for obtaining the input from the user. It returns the number of input values that are scanned. If there is some input failure or error then it returns EOF (end-of-file). The scanf() function is used to read input data from the console.

The scanf() function Is builtin function available in the C library. Scanf() function can read character, string, numeric & other data from keyboard in C language.

scanf() reads formatted data from user and assign them in the variables provided the additional arguments. Additional arguments must point to variables that have the same datatype as of user input data format.

Syntax:

scanf("format string",argument_list);  

Example of printf() Function in C

Program:

Let's see a simple example of input and output in C language that prints addition of 2 numbers.

 #include<stdio.h>    

int main(){    

int x=0,y=0,result=0;   

"printf("enter first number:");

scanf("%d",&x); 

printf("enter second number:"); 

scanf("%d",&y); 

result=x+y;  

printf("sum of 2 numbers:%d ""sum of 2 numbers:%d ",result);  

  

return 0;  

}   

 

Output:

enter first number:9

enter second number:9

sum of 2 numbers:18

Summary

The printf() and scanf() functions are essential tools for C programmers who are working with text-based interfaces. They allow developers to input and output data in a user-friendly format, and provide a wide range of options for formatting text and numbers. By mastering these functions, developers can create powerful and flexible programs that can handle a wide variety of input and output scenarios. I hope that this article was useful in helping you learn how to use printf() and scanf() functions in C programming. Explore our C articles to find out more about the language and consolidate your knowledge of the fundamentals. 

Post a Comment (0)
Previous Post Next Post