C Tutorial (8) : scanf

printf() sends data to the screen. The scanf() function gets data from the keyboard. You must have a way to get data from your user. You can’t always assign data values using assignment statements.

scanf() is a built-in C function that comes with all C compilers. Its header file is the same as printf() (stdio.h). scanf() fills variables with values typed by the user.

scanf() looks a lot like printf() because scanf() uses conversion codes such as %s and %d. scanf() is the mirror-image function of printf(). Often you will write programs that ask the user for values with a printf() and get those values with scanf().

Here is the format of scanf():

scanf(controlString [, data]);

When your program gets to scanf(), C stops and waits for the user to type values. The variables listed inside scanf() (following the controlString) will accept whatever values the user types. scanf() quits when the user presses “Enter” after typing values.

Even though scanf() uses the same conversion characters as printf(), never specify escape sequences such as \n, \a, or \t. Escape sequences confuse scanf(). scanf() quits getting values from the user when the user presses Enter, so you don’t ever specify the \n.

A printf() before a scanf() sends a prompt to the user. If you don’t prompt the user for the value or values you want, the user has no way of knowing what values should be typed.

Let’s write a program with a few simple scanf() statements

#include <stdio.h>

// Set up the variables that scanf will fill
char firstInitial; 
char lastInitial; 
int age;
int favorite_number;

printf("What letter does your first name begin with?\n"); 
scanf(" %c", &firstInitial);

printf("What letter does your last name begin with?\n"); 
scanf(" %c", &lastInitial);

printf("How old are you?\n"); 
scanf(" %d", &age);

printf("What is your favorite number (integer only)?\n"); 
scanf(" %d", &favorite_number);

printf("\nYour intitials are %c.%c. and you are %d years old", 
        firstInitial, lastInitial, age);
printf("\nYour favorite number is %d.\n\n", favorite_number);

return 0;

The first two scanf() statements obtain character values (as you can tell from the %c conversion codes). The third scanf() gets an integer value from the keyboard and places it into a variable named age.

The variables firstInitial, lastInitial, and age will hold whatever the user types before pressing Enter. If the user types more than a single character in the first two examples, it can confuse the program and create problems for the later values.

Another point to notice about the scanf() statements is the spaces right before each %c or %d. The space isn’t always required here, but it never hurts, and it sometimes helps the input work better when you get numbers and characters in succession. Adding the extra space is a good habit to get into now while learning scanf().

scanf() requires that you put the ampersand before all variables (address of variable basically)

Problems with scanf()

One of the first problems with scanf() is that although the user must type exactly what scanf() expects, the user rarely does this. If the scanf() needs a floating-point value, but the user types a character, there is little you can do. The floating-point variable you supply will have bad data because a character is not a floating-point value.

For now, assume that the user does type what is needed. In tutorial 18 we will  describes some ways to overcome problems brought on by scanf()

There’s a problem with using scanf() to get character strings into character arrays that you need to know about now. scanf() stops reading string input at the first space. Therefore, you can get only a single word at a time with scanf(). If you must ask the user for more than one word, such as the user’s first and last name, use two scanf() statements (with their own printf() prompts) and store the two names in two character arrays.

The format and use of scanf() statements will become easier with practice. In later tutorials , you learn some tricks to ask your users for multiple pieces of data instead of just one within a particular category.

Preprocessor directives & Header files < Prev                           Next > Basic Arithmetic



  1. You actually make it seem really easy together with your presentation however I to find this matter to be actually one thing which I believe I would never understand. It sort of feels too complicated and extremely huge for me. I’m taking a look ahead in your next publish, I’ll attempt to get the dangle of it!


  2. Wow, incredible weblog format! How long have you been running a blog for? you make running a blog glance easy. The total glance of your website is excellent, as neatly as the content material!


  3. Thanks a bunch for sharing this with all folks you actually know what you’re speaking about! Bookmarked. Please additionally talk over with my website =). We could have a link exchange arrangement among us!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s