Jul 29, 2024
printf (print formatted).
printf("Enter your age:");
int age; // Variable for storing age
scanfUse scanf to get user input. It's the opposite of printf.
scanf("%d", &age); // Use & to pass the address of the variable
& is used for pointers; it gives the location of the variable.Printing the gathered information:
printf("You are %d years old.", age);
50, output will be: "You are 50 years old."double GPA;
printf("Enter your GPA:");
scanf("%lf", &GPA); // Use %lf for double
printf("Your GPA is %lf", GPA);
char grade;
printf("Enter your grade:");
scanf(" %c", &grade); // Use %c for character
printf("Your grade is %c", grade);
char name[20]; // Declare a string variable with max length
printf("Enter your name:");
scanf("%s", name); // Use %s for string
printf("Your name is %s", name);
scanf stops at the first space, so only the first word will be captured.fgets for Stringsfgets:
fgets(name, sizeof(name), stdin); // Read a string with spaces
stdin).**fgetsprintf("Enter your name:");
fgets(name, sizeof(name), stdin);
printf("Your name is %s", name);
fgets, a newline character may be included in the string. Be aware of how this affects output.scanf and fgets to become comfortable with user input.