what is scanf function
can someone help me to explain it?
Printable View
what is scanf function
can someone help me to explain it?
scanf() is made to take input from the user. To give a simple example:
This short example declares an integer a, asks the user to enter an integer and waits until an integer is entered.Code:int a = 0;
printf("Enter an integer: ");
scanf("%d", &a);
Let's dissect the scanf(): it has two parts, separated by a comma:
"%d": means that the input has to be interpreted as an integer. This is called a conversion character. You can also take "%c" which interpretes the input as a character ('a' or '#', or whatever). With "%s" you can input a whole string.
&a: means that the value that was entered should be stored in the variable a. The & means "the address of". In this example we put the value in the address where a is pointing to. If this sounds like magic, read first about pointers and addresses.
To summarize:
scanf("%d", &a); takes an integer and puts it in the address of a.
I hope you're a bit wiser now...
Get familiar with unix/linux (especially man pages) or type in man scanf and see what you get.