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,526
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,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 5, If Statements Part 2" :: Login/Create an Account :: 2 comments