Freshers Aptitude technical questions
Freshers Job Alert
Bookmark and Share

C Programming Tutorial

Learn C programming in an easy way.This C programming tutorial is completly free for your use.

How you can help us back is let us know about more tutorials and send us the tutorial with your small photo and name.We will publish tutorial in your name.

 

<< Tutorial Home


ITERATION, FOR LOOPS
The basic format of the for statement is,

for( start condition; continue condition; re-evaulation )
program statement;

 

 

/* sample program using a for statement */
#include <stdio.h>

main() /* Program introduces the for statement, counts to ten */
{
int count;

for( count = 1; count <= 10; count = count + 1 )
printf("%d ", count );

printf("\n");
}

Sample Program Output
1 2 3 4 5 6 7 8 9 10

The program declares an integer variable count. The first part of the for statement

for( count = 1;
initialises the value of count to 1. The for loop continues whilst the condition

count <= 10;
evaluates as TRUE. As the variable count has just been initialised to 1, this condition is TRUE and so the program statement

printf("%d ", count );
is executed, which prints the value of count to the screen, followed by a space character.
Next, the remaining statement of the for is executed

count = count + 1 );
which adds one to the current value of count. Control now passes back to the conditional test,

count <= 10;
which evaluates as true, so the program statement

printf("%d ", count );
is executed. Count is incremented again, the condition re-evaluated etc, until count reaches a value of 11.
When this occurs, the conditional test

count <= 10;
evaluates as FALSE, and the for loop terminates, and program control passes to the statement

printf("\n");
which prints a newline, and then the program terminates, as there are no more statements left to execute.

/* sample program using a for statement */
#include <stdio.h>

main()
{
int n, t_number;

t_number = 0;
for( n = 1; n <= 200; n = n + 1 )
t_number = t_number + n;

printf("The 200th triangular_number is %d\n", t_number);
}

Sample Program Output
The 200th triangular_number is 20100

The above program uses a for loop to calculate the sum of the numbers from 1 to 200 inclusive (said to be the triangular number).

An example of using a for loop to print out characters

#include <stdio.h>

main()
{
char letter;
for( letter = 'A'; letter <= 'E'; letter = letter + 1 ) {
printf("%c ", letter);
}
}

 

Sample Program Output
A B C D E

 

An example of using a for loop to count numbers, using two initialisations

#include <stdio.h>

main()
{
int total, loop;
for( total = 0, loop = 1; loop <= 10; loop = loop + 1 ){
total = total + loop;
}
printf("Total = %d\n", total );
}

Sample Program Output
Total = 55

In the above example, the variable total is initialised to 0 as the first part of the for loop. The two statements,

for( total = 0, loop = 1;
are part of the initialisation. This illustrates that more than one statement is allowed, as long as they are separated by commas.

SELECTION (IF STATEMENTS)
The if statements allows branching (decision making) depending upon the value or state of variables. This allows statements to be executed or skipped, depending upon decisions. The basic format is,

if( expression )
program statement;

Example;

if( students < 65 )
++student_count;

In the above example, the variable student_count is incremented by one only if the value of the integer variable students is less than 65.

The following program uses an if statement to validate the users input to be in the range 1-10.

#include <stdio.h>

main()
{
int number;
int valid = 0;

while( valid == 0 ) {
printf("Enter a number between 1 and 10 -->");
scanf("%d", &number);
/* assume number is valid */
valid = 1;
if( number < 1 ) {
printf("Number is below 1. Please re-enter\n");
valid = 0;
}
if( number > 10 ) {
printf("Number is above 10. Please re-enter\n");
valid = 0;
}
}
printf("The number is %d\n", number );
}

Sample Program Output
Enter a number between 1 and 10 --> -78
Number is below 1. Please re-enter
Enter a number between 1 and 10 --> 4
The number is 4

 

EXERCISE C10
Write a C program that allows the user to enter in 5 grades, ie, marks between 0 - 100. The program must calculate the average mark, and state the number of marks less than 65.
Answer

Consider the following program which determines whether a character entered from the keyboard is within the range A to Z.

#include <stdio.h>

main()
{
char letter;

printf("Enter a character -->");
scanf(" %c", &letter );

if( letter >= 'A' ) {
if( letter <= 'Z' )
printf("The character is within A to Z\n");
}
}

Sample Program Output
Enter a character --> C
The character is within A to Z

The program does not print any output if the character entered is not within the range A to Z. This can be addressed on the following pages with the if else construct.
Please note use of the leading space in the statement (before %c)

scanf(" %c", &letter );
This enables the skipping of leading TABS, Spaces, (collectively called whitespaces) and the ENTER KEY. If the leading space was not used, then the first entered character would be used, and scanf would not ignore the whitespace characters.

if else
The general format for these are,

if( condition 1 )
statement1;
else if( condition 2 )
statement2;
else if( condition 3 )
statement3;
else
statement4;

The else clause allows action to be taken where the condition evaluates as false (zero).

The following program uses an if else statement to validate the users input to be in the range 1-10.

#include <stdio.h>

main()
{
int number;
int valid = 0;

while( valid == 0 ) {
printf("Enter a number between 1 and 10 -->");
scanf("%d", &number);
if( number < 1 ) {
printf("Number is below 1. Please re-enter\n");
valid = 0;
}
else if( number > 10 ) {
printf("Number is above 10. Please re-enter\n");
valid = 0;
}
else
valid = 1;
}
printf("The number is %d\n", number );
}

Sample Program Output
Enter a number between 1 and 10 --> 12
Number is above 10. Please re-enter
Enter a number between 1 and 10 --> 5
The number is 5

COMPOUND RELATIONALS ( AND, NOT, OR, EOR )
Combining more than one condition
These allow the testing of more than one condition as part of selection statements. The symbols are
LOGICAL AND &&
Logical and requires all conditions to evaluate as TRUE (non-zero).

LOGICAL OR ||
Logical or will be executed if any ONE of the conditions is TRUE (non-zero).

LOGICAL NOT !
logical not negates (changes from TRUE to FALSE, vsvs) a condition.

LOGICAL EOR ^
Logical eor will be excuted if either condition is TRUE, but NOT if they are all true.

The following program uses an if statement with logical OR to validate the users input to be in the range 1-10.

#include <stdio.h>

main()
{
int number;
int valid = 0;

while( valid == 0 ) {
printf("Enter a number between 1 and 10 -->");
scanf("%d", &number);
if( (number < 1 ) || (number > 10) ){
printf("Number is outside range 1-10. Please re-enter\n");
valid = 0;
}
else
valid = 1;
}
printf("The number is %d\n", number );
}

Sample Program Output
Enter a number between 1 and 10 --> 56
Number is outside range 1-10. Please re-enter
Enter a number between 1 and 10 --> 6
The number is 6

switch() case:
The switch case statement is a better way of writing a program when a series of if elses occurs. The general format for this is,

switch ( expression ) {
case value1:
program statement;
program statement;
......
break;
case valuen:
program statement;
.......
break;
default:
.......
.......
break;
}

The keyword break must be included at the end of each case statement. The default clause is optional, and is executed if the cases are not met. The right brace at the end signifies the end of the case selections.

Rules for switch statements
values for 'case' must be integer or character constants
the order of the 'case' statements is unimportant
the default clause may occur first (convention places it last)
you cannot use expressions or ranges

 

#include <stdio.h>

main()
{
int menu, numb1, numb2, total;

printf("enter in two numbers -->");
scanf("%d %d", &numb1, &numb2 );
printf("enter in choice\n");
printf("1=addition\n");
printf("2=subtraction\n");
scanf("%d", &menu );
switch( menu ) {
case 1: total = numb1 + numb2; break;
case 2: total = numb1 - numb2; break;
default: printf("Invalid option selected\n");
}
if( menu == 1 )
printf("%d plus %d is %d\n", numb1, numb2, total );
else if( menu == 2 )
printf("%d minus %d is %d\n", numb1, numb2, total );
}

Sample Program Output
enter in two numbers --> 37 23
enter in choice
1=addition
2=subtraction
2
37 minus 23 is 14

The above program uses a switch statement to validate and select upon the users input choice, simulating a simple menu of choices.

 

 

<< Tutorial Home