Explain printf and scanf with example | Printf & scanf Function Syntax

Explain printf and scanf with example | Printf & scanf Function Syntax

 Printf() And Scanf ()

 The printf() and scanf() functions are two of the most commonly used functions in the C programming language. They are used to input and output data respectively, and are essential tools for developers who are working with text-based interfaces. In this article, we will explore these functions in detail, including their syntax, parameters, and common use cases.

The printf() Function

The printf() function is used to output data to the console or a file. It is used to display text, numbers, and other data types in a user-friendly format. Here is an example of how the printf() function is used:


 
In this example, the printf() function outputs the value of the integer variable age using the %d format specifier. The format specifier tells the printf() function what type of data to expect, and how to format it. In this case, %d tells the printf() function to expect an integer value, and to display it as a decimal number.

Here are some other commonly used format specifiers:

%f: Float value
%c: Character value
%s: String value
%p: Pointer value
The printf() function can also be used to format text with various options, such as padding, justification, and width. Here are some examples: 



The scanf() Function

The scanf() function is used to read data from the console or a file. It is used to input text, numbers, and other data types from the user. Here is an example of how the scanf() function is used:

scanf function example



In this example, the scanf() function waits for the user to input an integer value, which is then stored in the age variable using the & operator. The & operator is used to get the memory address of the variable, which allows the scanf() function to modify it.

Here are some other commonly used format specifiers for the scanf() function:

%f: Float value
%c: Character value
%s: String value
The scanf() function can also be used to read multiple values at once. Here is an example:

format speciafer

In this example, the scanf() function reads two values, an integer and a float, using the %d and %f format specifiers respectively. The values are stored in the age and height variables, which are then printed to the console using the printf() function.

Conclusion

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.



Watch Video 



All C  Topics : -

  1. C Language Intro

  2. Structure of C Language 

  3. Variable & data Types 

  4. Printf and Scanf Function

  5. Operator in c Language 

  6. If Statement in c Language 

  7. If else and Nasted if in C

  8. Switch Case In C 

  9. While loop in C Language 

  10. Exit Control Loop (Do While)

  11. For Loop in c 

  12. C Language Pratice Program 

  13. Goto Statement in c Programming 

  14. Array in c

  15. Pointer in  c Language 

  16. String in c Language 

Post a Comment (0)
Previous Post Next Post