Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,531,980

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

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

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

NissanPosted:

Thanks for the feedback.

KatsumiPosted:

I actually gave this a try due to me having a lot of free time on my hands and it turned out quite well (after a lot of effort -_-), thanks for the tutorial