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:
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:
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 : -
- C Language Intro
- Structure of C Language
- Variable & data Types
- Printf and Scanf Function
- Operator in c Language
- If Statement in c Language
- If else and Nasted if in C
- Switch Case In C
- While loop in C Language
- Exit Control Loop (Do While)
- For Loop in c
- C Language Pratice Program
- Goto Statement in c Programming
- Array in c
- Pointer in c Language
- String in c Language