C Programming

C programming Tutorials: Part – 04

Written by Saiful Islam

Previous parts :

C programming tutorials: part 1

C programming tutorials: part 2

C programming tutorials: part 3

Hello friends,

Hope you have understood those tutorials I have given before. Today I will discuss about getting input from the user.

Getting input from users

  • We have to ask the user to give an input through ” printf ” function.
  • To get the input we have to use a new function named ” scanf “.

Structure for ” scanf ” :

scanf ( ” %d “, & x);

Here ” x ” is an integer variable.

[ Note : Don’t forget to use ” & ” before the variable name. ]

  • We have used a different structure of ” printf ” function to show the output.

Structure for ” printf “:

printf (” %d “, x);

[Warning: Don’t use ” & ” in ” printf ” function. ]

Example :

[warning: Don’t copy paste the program (copy paste doesn’t work) just type the program on your compiler. ]

Description :

  1. At first, you have asked your user to enter the amount of posts that he has written.

  2. After entering the number the input will be saved into ” x “. At last ” printf ” function will show the number that was saved in ” x ” , the output.

[Note: ” n ” has been used to go to get input from a new line. You should try to test the program without it to see what would happen if I didn’t use it.]

 

The running program

Asking the user :

Get input (type a number any press enter) :

Final output :

About the author

Saiful Islam

Leave a Comment