Programming with C
TAKING INPUT
PREPARED BY: FARIA TABASSUM
FARIA.TABASSUM@CUB.EDU.BD
The scanf() function
 To take input from the user we need to use the scanf function
 The scanf() function is used to read any characters, strings and numeric
values from the keyboard
PREPARED BY: FARIA TABASSUM
FARIA.TABASSUM@CUB.EDU.BD
2
Basic format
scanf (“format string”, variable list);
 Format string basically denotes the type of data that we want to take
as input
 In the variable list we type in the names of the variables separated by
comma (,) and preceded by ampersand (&) symbol.
PREPARED BY: FARIA TABASSUM
FARIA.TABASSUM@CUB.EDU.BD
3
scanf for different data types
Integer:
int value;
scanf (“%d”, &value)
Float:
float value;
scanf (“%f”, &value);
Char:
char ch;
scanf (“%c”, &ch);
PREPARED BY: FARIA TABASSUM
FARIA.TABASSUM@CUB.EDU.BD
4
Multiple variables as input
We can take multiple variables as input by separating them with
commas.
PREPARED BY: FARIA TABASSUM
FARIA.TABASSUM@CUB.EDU.BD
5
1 2
Multiple variables as input
We can take multiple variables as input by separating them with a
comma.
PREPARED BY: FARIA TABASSUM
FARIA.TABASSUM@CUB.EDU.BD
6
1 2
1 and 2 are the same. Follow any method you want
Example
Write a C program that takes an integer number as input and print the
value of that number.
PREPARED BY: FARIA TABASSUM
FARIA.TABASSUM@CUB.EDU.BD
7
Example
Write a C program that takes three character variables as input and
print them in reverse order.
PREPARED BY: FARIA TABASSUM
FARIA.TABASSUM@CUB.EDU.BD
8
THE END
PREPARED BY: FARIA TABASSUM
FARIA.TABASSUM@CUB.EDU.BD
9

Lecture_4-Taking Inputs.pptx

  • 1.
    Programming with C TAKINGINPUT PREPARED BY: FARIA TABASSUM FARIA.TABASSUM@CUB.EDU.BD
  • 2.
    The scanf() function To take input from the user we need to use the scanf function  The scanf() function is used to read any characters, strings and numeric values from the keyboard PREPARED BY: FARIA TABASSUM FARIA.TABASSUM@CUB.EDU.BD 2
  • 3.
    Basic format scanf (“formatstring”, variable list);  Format string basically denotes the type of data that we want to take as input  In the variable list we type in the names of the variables separated by comma (,) and preceded by ampersand (&) symbol. PREPARED BY: FARIA TABASSUM FARIA.TABASSUM@CUB.EDU.BD 3
  • 4.
    scanf for differentdata types Integer: int value; scanf (“%d”, &value) Float: float value; scanf (“%f”, &value); Char: char ch; scanf (“%c”, &ch); PREPARED BY: FARIA TABASSUM FARIA.TABASSUM@CUB.EDU.BD 4
  • 5.
    Multiple variables asinput We can take multiple variables as input by separating them with commas. PREPARED BY: FARIA TABASSUM FARIA.TABASSUM@CUB.EDU.BD 5 1 2
  • 6.
    Multiple variables asinput We can take multiple variables as input by separating them with a comma. PREPARED BY: FARIA TABASSUM FARIA.TABASSUM@CUB.EDU.BD 6 1 2 1 and 2 are the same. Follow any method you want
  • 7.
    Example Write a Cprogram that takes an integer number as input and print the value of that number. PREPARED BY: FARIA TABASSUM FARIA.TABASSUM@CUB.EDU.BD 7
  • 8.
    Example Write a Cprogram that takes three character variables as input and print them in reverse order. PREPARED BY: FARIA TABASSUM FARIA.TABASSUM@CUB.EDU.BD 8
  • 9.
    THE END PREPARED BY:FARIA TABASSUM FARIA.TABASSUM@CUB.EDU.BD 9