• Log in or sign up for an account here: https://www.sythe.org/login

[TUT][C] Using the IF statement

Affix

Newcomer
Joined
Oct 15, 2008
Sythe Gold
0
Messages
12
Like many other languages C has an if statement and it is pretty easy to grasp the concept. The if statement in C is very similar to the one you may have seen if you code PHP.

Ok lets start we will create a little application to ask for a name and compare it.

Start your C application by adding stdio.h and create your main function

HTML:
#include 

int main() {

}

Now we will create a variable called name the variable will be char.

HTML:
char name[1024];

Now we need to get the user to do some input by asking the name and receiving user input using the printf and sprintf functions. Your code should be similar to the followng.

HTML:
printf("Hello, Please enter your name blow!\n");
scanf("%s", name);

Now say we want to check if a specific name is entered into the variable we need to use an if statement.

The if Statement looks like this

HTML:
if(name == "Affix") {
//ENTER THE TRUE FUNCTION
} else {
//ENTER THE FALSE FUNCTION
}

The if statement above will check if Affix or iHack is entered as the name. Now if they are entered we will pass the TRUE function if the input it not valid we pass the FALSE function. I am simply going to display a message

Code:
if(name == "Affix") {
printf("%s, You Fucking Rock\n", name);
} else {
printf("%s, You Suck\n", name);
}

Now you have your fully functioning IF statment in C. Congradulations.
 
Member of the Month Winner
Sythe's 10th Anniversary
A tutorial is meant to teach people something.

Did you go in to depth about what conditional statements actually are and what they do, and for the matter, what is more effective in multiple cases of comparison? Perhaps this would be better if you actually went in depth rather than just tell a user "this is an if statement. If you want something to happen under a condition, put this here, otherwise put that there." A tutorial should include the "how" and "why", no matter how simple.
 
Runescape Gamblingrunescape gambling
Back
Top