C Programming


Hello World

#include <stdio.h>

int main() {
printf("Hello, world!\n");
return 0;
}



Accept Details

#include <stdio.h>

int main()
{
int this_is_a_number;

printf( "Please enter a number: " );
scanf( "%d", &this_is_a_number );
printf( "You entered %d", this_is_a_number );
getchar();
return 0;
}

Statement Examples

            /* Comment Line */
a = 4 * 6; /* Every Statement Must End with Semicolon) a is 24 */
a = a + 5; /* a equals the original value of a with five added to it */
a == 5 /* Does NOT assign five to a. Rather, it checks to see if a equals 5.*/

/* Relational Operators */

a < 5 /* Checks to see if a is less than five */
a > 5 /* Checks to see if a is greater than five */
a == 5 /* Checks to see if a equals five, for good measure */

> greater than 5 > 4 is TRUE
< less than 4 < 5 is TRUE
>= greater than or equal 4 >= 4 is TRUE
<= less than or equal 3 <= 4 is TRUE
== equal to 5 == 5 is TRUE
!= not equal to 5 != 4 is TRUE



If Statement

Basic If Syntax

The structure of an if statement is as follows:
 
if ( statement is TRUE )
Execute this line of code
Here is a simple example that shows the syntax:
 
if ( 5 < 10 )
printf( "Five is now less than ten" );
Here, we're just evaluating the statement, "is five less than ten", to see if it is true or not; with any luck, it's not! If you want, you can write your own full program including stdio.h and put this in the main function and run it to test.

To have more than one statement execute after an if statement that evaluates to true, use braces, like we did with the body of the main function. Anything inside braces is called a compound statement, or a block. When using if statements, the code that depends on the if statement is called the "body" of the if statement.

For example:
 
if ( TRUE )
{
/* between the braces is the body of the if statement */
Execute all statements inside the body
}
I recommend always putting braces following if statements. If you do this, you never have to remember to put them in when you want more than one statement to be executed, and you make the body of the if statement more visually clear.

If...Else Statement


if(TRUE)
{
/* Execute these statements if TRUE */
}
else
{
/* Execute these statements if FALSE */
}

Example :
#include <stdio.h>

int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */

printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}



Nested if

/* if inside if is nested if  */

if(Condition)
{
           if(Condition)
           {
                Statement 1;
           }
           else
            {
                Statement 2;
            }
}
else
{
            if(Condition)
            {
                  Statement 3;
            }           
            else
            {
                  Statement  4;

            }
}

/* This you can use to find out greatest from 3 nos */


LOOP

Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. (They may be executing a small number of tasks, but in principle, to produce a list of messages only requires repeating the operation of reading in some data and displaying it.) Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition.

before going further, you should understand the concept of C's true and false, because it will be necessary when working with loops (the conditions are the same as with if statements).

There are three types of loops: for, while, and do..while. Each of them has their specific uses. They are all outlined below.

FOR - for loops are the most useful type. The syntax for a for loop is

for ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}


Comments