Skip to content

Input Statement

Input is the activity of supplying data to the C Program from the user at run time.
scanf() function is used for input in C Lang.

syntax:
scanf(“%fsp”,&var);

& – address of

#include<stdio.h>

void main()
{
    int x;

    printf(“Enter any number:”);
    scanf(“%d”,&x);

    printf(“Your number is %d”,x);

}

Points regarding scanf():
1. When we have more than one format specifiers inside scanf, input should be separated with space or enter at run time.
2. no need to use \n, when printf() is following scanf()
3. character datatype input should be taken before numeric input

//Example
#include<stdio.h>

void main()
{
    int x;
    char ch;
    printf(“Enter any number:”);
    scanf(“%d”,&x);
    printf(“Enter any character:”);
    scanf(“%c”,&ch);
    printf(“x=%d\n”,x);
    printf(“ch=%c”,ch);
}

Practice:
1. Write C program to create two integer variables and accept user input into them and display their sum.
Hint: sum=x+y;

2. Write C program to accept any three numbers from the user and display their average.
Hint: avg=(a+b+c)/3;

3. Write C Program to calculate and print simple interest by the principle, rate of interest and tenure entered by the user.
Hint: si=(p*n*r)/100;

4. Write C Program to calculate area of circle for the user entered radius value.
Hint: area=pi * r* r;
where pi is a float variable initialised with 3.14
r is int variable whose value is entered by user.
area is float variable.

5. Write C Program to create 4 variables from all basic datatypes of C Lang, accept user input for them and display their values.

>>>Character i/o functions
Character variable can accommodate a single ASCII character since it holds 1 byte of memory.

In general, we will use %c format specifier to read or write character data.

//Example
#include <stdio.h>

int main() {
    char ch;
    ch=’$’;
    printf(“ch=%c”,ch);
     
    return 0;
}

//Example
#include <stdio.h>

int main() {
    char ch;
    printf(“Enter any character:”);
    scanf(“%c”,&ch);
    printf(“Your Character is=%c”,ch);
     
    return 0;
}

#include<stdio.h>

void main()
{
    printf(“Menu”);
    printf(“\n1.add”);
    printf(“\n2.modify”);
    printf(“\n3.delete”);
    printf(“\n4. Exit”);
    printf(“\nEnter your choice:”);
    char ch;
    scanf(“%c”,&ch);
    printf(“Your choice is %c”,ch);

}

built-in i/o functions of c language defined in stdio.h are two types:
1. formatted i/o functions Eg:scanf, printf, fprintf, sprintf
2. unformatted i/o functions Eg: putchar, getchar etc.

formatted i/o functions can work with any type of data with the help of format specifiers.

unformatted i/o functions can only work with character data.

putchar()
used to display value of a character variable onto screen

Example:
putchar(ch);

getchar()
used to read one character of input from the keyboard and returns the character

Example:
a=getchar();

//Example code
#include <stdio.h>

int main() {
    char ch;
    printf(“Enter any character:”);
    ch=getchar();
    printf(“Your Character is: “);
    putchar(ch);
     
    return 0;
}

Other important functions that work with character data are getch, getche defined in conio.h.  The common feature of these two functions is they won’t wait for Enter key pressing.  They accept single character input immediately.

getch()
used to accept a single character input from the user but it won’t display (echo) the input on the screen.

getche()
used to accept a single character input from the user along with displaying (echo) the input on the screen.


//Example program
#include <stdio.h>
#include <conio.h>

int main() {
    char ch;
    printf(“Enter any character:”);
    ch=getch();  //rewrite the same program using getche() and observe the difference
    printf(“\nYour Character is: “);
    putch(ch);

    return 0;
}