Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,531,964

C Programming Tutorial 4, Getting User Input

Tutorial Name: C Programming Tutorial 4, Getting User Input  

Category: PC Tutorials

Submitted By: Nissan

Date Added:

Comments: 0

Views: 799

Related Forum: PC Building Forum

Share:

C Programming Tutorial 4, Getting User Input


The program we are going to be using to learn C programming language is "CodeBlocks" and here is a link to the download its a free program.

Link : [ Register or Signin to view external links. ]

All of these tutorials are going to be done in an console application.

in this tutorial i will be showing you how to get input from user

So lets get started :

so the first thing we are going to do here is create an integer variable to store the number that the user enters and that will look like :


int num1 = 0;


now what we are going to do is just print out onto the screen "Please Enter A Number" so the user know they have to enter a number.

now you code will look like :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1 = 0;
    printf("Please Enter A Number: ");
    return 0;
}


and now what we are going to do use a scanf and this is going to allow us to read the what number the user has types out and that code will look like :

scanf("%i", &num1);


we put %i because we are reading an integer and we put num1 because that is the name of your variable.

now all we are going to do is print the number the user entered back out on to the screen and that will look like :

printf("%i\n", num1);


now if we just run this you can see it asks us to enter a number i am going to enter in 7 and hit enter and as you can see its printed 7 back out on to the screen :

[ Register or Signin to view external links. ]

now lets go ahead and make this more interesting lets have the user enter two numbers and add those two numbers together so what we are going to do is copy the code we have just done now your code will look like :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1 = 0;
    printf("Please Enter A Number: ");
    scanf("%i", &num1);

    int num2 = 0;
    printf("Please Enter A Second Number: ");
    scanf("%i", &num2);
    printf("%i", num1 + num2);
    return 0;
}


and now after you have entered your second number it will add the number from num1 and num2 together and print it back out onto your screen.

now i am just going to run the console application and it will ask me to enter a number then it will ask me to enter another number and print out the sum of those numbers and it will look like :

[ Register or Signin to view external links. ]

and as your see i have chose the numbers 8 and 5 and the sum is 13.

i hope you have learned something from this tutorial

Ratings

Current rating: 7.33 by 3 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"C Programming Tutorial 4, Getting User Input" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.