by David Hamrick

Home
Links

Starting Out
Compiler
Hello World Program
Comments

Variables
Int, Char, Float

Scanf

Projects
ATM project
Maze project
Unit Conversion project




Scanf

Scanf is a very useful function that we find in the stdio library that we use for printf. Scanf may seem a little complitcated at first but once you get the hang of it its very easy. Scanf is basically a very easy way to  collect input from the keyboard. Whatever you type and then press enter will be put into the variable you tell it to. For example

int a;
scanf("%d",&a)

That program would collect whatever input you put in to the console. Whenever you want want to collect a number you use %d, but thats not the case for all things.

%d - int
%f - float
%c - single char
%s - string of chars // we'll discuss this later

That is the basic way to use to scanf. Here is an example of how to use it.

#include <stdio.h>

int main()
{
int a;
printf("Enter a number : ");
scanf("%d",&a);
printf("\nYou entered the number %d",a);

return 0;
}

Hosted by www.Geocities.ws

1