Tutorials Navigation
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
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,656)
- 02. How to: Matrix Numbers | Batch File(1,954)
- 03. How to Password Protect Files on Windows(865)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,879)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,243)
- 06. How to embed an image on TheTechGame(3,140)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(13,088)
- 08. Host bot lobbies! Full Tutorial!(11,611)
- 09. Unban yourself [Plutonium BO2](14,308)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,410)
- 11. Best Crosshair Settings for Valorant(6,550)
- 12. Othercide The Surgeon Boss Guide(2,568)
- 13. Othercide Remembrances Unlock Guide(4,517)
- 14. Othercide Beginners Tips and Tricks(2,734)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,881)
"C Programming Tutorial 4, Getting User Input" :: Login/Create an Account :: 0 comments