๐ค C Input and Output – Using printf()
and scanf()
In C programming, printf()
is used to display output to the user, while scanf()
is used to take input from the user. These functions are part of the standard input/output library (stdio.h
).
๐น printf() – Output Function
The printf()
function is used to print text, variables, and formatted data to the screen.
๐ Example: Printing a Message
This simple program displays a greeting message.
#include <stdio.h> int main() { printf("Hello, C Programmer!\n"); return 0; }
๐น scanf() – Input Function
The scanf()
function is used to get input from the user. It requires the address of the variable using the &
operator.
๐ Example: Taking Input from the User
This program asks for your name and age, then prints it back.
#include <stdio.h> int main() { char name[50]; int age; printf("Enter your name: "); scanf("%s", name); printf("Enter your age: "); scanf("%d", &age); printf("Hello %s, you are %d years old.\n", name, age); return 0; }
๐ธ Format Specifiers
Here are some common format specifiers used in printf()
and scanf()
:
%d
โ Integer%f
โ Float%c
โ Character%s
โ String
โ ๏ธ Important Notes
- Always use
&
before variable names inscanf()
except for strings (arrays). - Use
\n
insideprintf()
to print a new line.
๐งช Practice Time
Modify the input program to take your favorite color or your birth year. Experiment with different format specifiers!