Zone's C Programming Tutorials #3 Scanf function

ZoneAye

Known Member
Messages
12
Reaction score
5
Points
118
Hi guys this is my third C Programming Tutorial in this tutorial we will be looking at the Scanf function. It is a very easy function to learn/use. It is used to allow the user to input data and then store the data. This is a example of code using Scanf :smile:

int main()
{
// variable
int age;
// output to screen (question)
printf("Enter Age: ");
// storing input
scanf("%d", &age);

// outputting answer
printf("You are %d years old", age);
fflush(stdin);
getchar();
}

As you can see when we are storing a integer using scanf we still use the character conversation %d this is because we storing a integer ovb. When u use Print and Scanf always use fflush(stdin); if you don't your program may not work.


All the code will do when you run it is output this on the console:
"Enter Age : 17 " <-- enter age
"You are 17 years old" <-- out puts entered age etc

You will not see the scanf or any other code in the console this is a quick brake down:
*runs code

Enter age: " <-- waits for u to enter age MUST be whole number
scanf <-- stores input
"You are (age) years old" <-- outputs age entered

you can use scanf for basically anything when inputting data i would recommend messing about with it and try to figure out why & is next to age in the scanf statement its very easy :smile:

- Zone
 
Top