Tutorials Navigation
C Programming Tutorial 5, If Statements Part 2
Tutorial Name: C Programming Tutorial 5, If Statements Part 2
Category: PC Tutorials
Submitted By: Nissan
Date Added:
Comments: 2
Views: 1,524
Related Forum: PC Building Forum
Share:
C Programming Tutorial 5, If Statement Part 1
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 :
By creating a new character variable and we are not going to set it to anything i am going to ask the user to enter a character and the character the user enters will be stored into your character variable.
so now you code will look like this after you have created a new character variable :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char MyChar;
return 0;
}
so now what we are going to do is print out text onto the screen asking the user to enter a character and now after that we are going to have to read the character the user enters and now your code will look like :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char MyChar;
printf("Please enter a character: ");
scanf("%c", &MyChar);
return 0;
}
in the scanf we put %c because we are reading an char and for a int we would put %i so on and we put ,MyChar (the name of your variable) because thats where we want to store what character the user entered.
Now lets say we wanted to check and see if the character the user enters is a capital A so what we would do is :
if(MyChar == 'A'){
}
and if the character that the user entered is a capital A then your program will execute everything inside of that if statement so for an example lets do :
if(MyChar == 'A'){
printf("The character you have entered is a capital A");
}
now your code will look like :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char MyChar;
printf("Please enter a character: ");
scanf("%c", &MyChar);
if(MyChar == 'A'){
printf("The character you have entered is a capital A"")
}
return 0;
}
so now lets debug the program and make sure it works :
[ Register or Signin to view external links. ]
As you can see it works now lets say if you enter any other number or letter you want it to print out that you have printed out an invalid character you would do :
if(MyChar == 'A'){
printf("You have printed out a capital a");
}else{
printf("You have printed out an invalid character");
}
and now your code will look like :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char MyChar;
printf("Please enter a character: ");
scanf("%c", &MyChar);
if(MyChar == 'A'){
printf("The character you have entered is a capital A");
}else{
printf("You have printed out an invalid character");
}
return 0;
}
now you can debug it and see if it work and as you can see it does :
[ Register or Signin to view external links. ]
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,492)
- 02. How to: Matrix Numbers | Batch File(1,903)
- 03. How to Password Protect Files on Windows(857)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,716)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,133)
- 06. How to embed an image on TheTechGame(3,098)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(12,981)
- 08. Host bot lobbies! Full Tutorial!(11,241)
- 09. Unban yourself [Plutonium BO2](14,240)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,385)
- 11. Best Crosshair Settings for Valorant(6,525)
- 12. Othercide The Surgeon Boss Guide(2,539)
- 13. Othercide Remembrances Unlock Guide(4,460)
- 14. Othercide Beginners Tips and Tricks(2,708)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,845)
"C Programming Tutorial 5, If Statements Part 2" :: Login/Create an Account :: 2 comments