C scanf Function
Continue from last C post, I will discuss scanf function today. The scanf function is similar to printf function with the opposite functionality. The printf function outputs the string on the screen whereas scanf function lets the user input values from the keyboard. The example of scanf function which I learned is scanf (“%d”, &number). As you can see, right now I only know how to use integer C variable type. I will learn to use other variable types as the tutorial progresses. Notice that there is an & operator in front of number variable. In my experience for basic C++, I remembered that it is either a reference or pointer. Here is my entire program code for the scanf exercise:
/* Generated by NetBeans */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
/* End of generation */
int brothers, sisters, children;
printf("How many brothers and sisters do you have?n");
printf("Brothers: ");
scanf("%d", &brothers);
printf("Sisters: ");
scanf("%d", &sisters);
printf("How many children do you have? ");
scanf("%d", &children);
printf("You have %d brothers and %d sisters.nYou have %d children.n", brothers, sisters, children);
fflush(stdin);
/* Generated by NetBeans */
return (EXIT_SUCCESS);
}
As you can see in the code above, I use NetBeans both for my PHP development and C programming IDE. NetBeans automatically included the necessary C libraries and common statements for the project. The fflush(stdin) statement escapes the enter key according to the tutorial.
There are no posts related to C scanf Function.