Printf Scanf
Printf Scanf Function The printf and scanf functions are used together to read and print formatted data in C. They are powerful tools for working with us...
Printf Scanf Function The printf and scanf functions are used together to read and print formatted data in C. They are powerful tools for working with us...
The printf and scanf functions are used together to read and print formatted data in C. They are powerful tools for working with user input and output.
Printf takes a format string as its first argument and a variable number of arguments that match the format specifiers in the string. Scanf takes a string format and an variable number of arguments that match the format specifiers in the string. It then reads and prints the corresponding values from the first argument.
Format specifiers are placeholders for values in the format string. There are various types of format specifiers, including:
%d: to print an integer
%s: to print a string
%f: to print a float
%c: to print a character
%o: to print an object (only for pointers)
%m: to print a variable by name
Example:
c
#include <stdio.h>
int main() {
int age = 25;
float salary = 3000.0;
char name[50];
printf("My age is %d and my salary is %.2f\n", age, salary);
printf("My name is %s\n", name);
return 0;
}
Output:
My age is 25 and my salary is 3000.00
My name is John
Key Takeaways:
Both printf and scanf return the number of successfully matched format specifiers.
Use %d, %s, etc. for basic data types.
Use %o for printing pointers.
Use %m to print variables by name.
Format specifiers can be combined with % to achieve complex formatting