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.
- Control String
- 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).
Variables
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:
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.
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
Placeholder | Variable Type | Description |
---|---|---|
%c | char | single character |
%d (%i) | int | signed integer |
%e (%E) | float or double | exponential format |
%f | float or double | signed decimal |
%g (%G) | float or double | use %f or %e as required |
%o | int | octal value |
%p | pointer address stored in pointer | |
%s | array of char | sequence of characters |
%u | int | unsigned decimal |
%x (%X) | int | unsigned hex value |
Now that you know how to take input and display it try do it your self exercises.
Do it your self
- Implement the code that scans name, age and percentage.
- 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