Curriculum
Input and output (I/O) are crucial aspects of programming, allowing programs to interact with users and external data sources. In C programming, you can perform input and output operations using various functions provided by the standard I/O library (stdio.h).
In this lesson, we’ll explore how to read input from the user and display output in C programs.
C provides several standard I/O functions to handle input and output. Two commonly used functions are:
printf
: Used to display formatted output on the screen.printf("Hello, World!");
In this example, "Hello, World!"
is the string to be displayed.
scanf
: Used to read formatted input from the user or a fileint age; scanf("%d", &age);
int age; scanf("%d", &age);
In this example, %d
is the format specifier for an integer, and &age
is the memory address where the entered integer will be stored.
printf
The printf
function is used to print formatted output to the standard output (usually the screen). It allows you to display text, variables, and other data types.
int num = 42; printf("The value of num is: %d", num);
In this example, %d
is a format specifier for an integer, and it is replaced by the value of num
in the output.
printf
supports various format specifiers to control the appearance of output. Some commonly used format specifiers include:
%d
: for integers%f
: for floating-point numbers%c
: for characters%s
: for strings%x
: for hexadecimal numbersExample:
float temperature = 98.6; char grade = 'A'; printf("Temperature: %.2fnGrade: %c", temperature, grade);
In this example, %.2f
specifies that the floating-point number should be displayed with two decimal places.
scanf
The scanf
function is used to read formatted input from the user or a file. It requires format specifiers to match the input data type.
int age; printf("Enter your age: "); scanf("%d", &age);
In this example, %d
is used as the format specifier to read an integer value from the user, and &age
is the address of the variable where the input will be stored.
You can use multiple format specifiers to read multiple values in a single scanf
statement. Ensure that the format specifiers match the order and types of inputs.
int num1, num2; printf("Enter two numbers separated by a space: "); scanf("%d %d", &num1, &num2);
In this example, two integers are expected from the user separated by a space.
To read or display strings, you can use %s
as the format specifier. However, be cautious when using %s
with printf
to avoid buffer overflow issues.
char name[50]; printf("Enter your name: "); scanf("%s", name); printf("Hello, %s!", name);
In this example, %s
is used to read and display a string, but it’s important to ensure that the input string does not exceed the allocated buffer size (50 in this case).
Here’s a summary of commonly used I/O functions and format specifiers:
printf
for displaying output with format specifiers.scanf
for reading input with format specifiers.%d
for integers, %f
for floating-point numbers, %c
for characters, %s
for strings, %x
for hexadecimal numbers, and more.Input and output operations are essential for building interactive and useful C programs. By using printf
and scanf
functions along with format specifiers, you can format and read data from users and external sources, making your programs more versatile and user-friendly. Understanding and effectively using these I/O techniques is a fundamental skill for C programmers.