Tuesday, 3 September 2013

Taking inputs from User

Taking Inputs from Users

This will be the first program where you will learn to take an input from user. And to do so we will write a program that welcomes you. So lets begin.

Copy the code below and execute. You will be prompted to "Enter your name", and on doing so it will print a message with your name. Remember to press the Enter key after the input.

    Author: Ryan Sequeira
    Title:  A program that welcomes you

#include <stdio.h>

int main()
    char name[20];
    printf("Enter your name: ");    //prompt the user to enter his name
    scanf("%s",&name);    //scan users input
    printf("Welcome %s !!!",name);    //print the welcome message
    return 0;

Lets understand how the code works. Since the program needs to remember your name, we need to create a variable of type string (a string is a sequence of characters, we will learn it in detail later ).

    char name[20];

The line above creates a string variable that can remember 19 characters. **(one character is reserved for a special symbol to detect string termination).

The next two important lines of code are the scanf statement and printf with a variable.

    scanf("%s",&name);    //scan users input
    printf("Welcome %s !!!", name);    //print the welcome message

scanf is used for receiving formatted Input and
printf is used for displaying formatted Output.

We can break the scanf and printf into two parts.
  1. Control String
  2. Variables

Control String

The control string is used to describe the formatted structure of the input or the output. If we directly wrote the variable names inside the formatted string the compiler wouldn't be able to differentiate it from the other characters in the string. Hence we make use of place holders for variables and list the variables outside so that the compiler can substitute the value of the variable in that place(in case of printf).


As we use place holders, the variables outside must match the total number of places holders used in the control string. Also the type of the place holders must match the variables. For example consider the following statement:

    int age=0;
    float percentage=0.0;
    char name[19];
    scanf("%s %d %f",&name, &age, &percentage);    //scan users name, age and percentage
    printf("Welcome %s !!!", name);    //print the welcome message
    printf("\nYour are %d years old and you scored %f this year", age, percentage);    

As you can see, in the scanf statement we scan three inputs viz name, age and percentage. Here the %s matches the name, %d (placeholder for integer) the age and %f (placeholder for float/decimal) the percentage.
Similarly in the second printf statement %d is followed by %f and hence the age is ahead of percentage in the variable list.

Format Specifiers

PlaceholderVariable TypeDescription
%ccharsingle character
%d (%i)intsigned integer
%e (%E)float or doubleexponential format
%ffloat or doublesigned decimal
%g (%G)float or doubleuse %f or %e as required
%ointoctal value
%ppointer address stored in pointer
%sarray of char sequence of characters
%uintunsigned decimal
%x (%X)intunsigned hex value

Now that you know how to take input and display it try do it your self exercises.

Do it your self 

  1. Implement the code that scans name, age and percentage.
  2. Write a program that takes name and percentage of 3 users and display the names and percentage side by side
    • Example
      • Name   Percentage
      • Joel      33.5%
      • Marco  56.5%
      • Sam     90.6% 

No comments:

Post a Comment