COM 400 - Computer Programming II - Introduction To C
COM 400 - Computer Programming II - Introduction To C
Introduction to C
1. Easy to read
2. Easy to modify
3. Consistent in format
4. Self documenting
Example:
A C program to output a greeting
#include<stdio.h>/*enables us to use
functions defined elsewhere */
void main()
{ printf(“hello world”); //printf is an
output function
}//end
The data item can then be accessed later in the program simply
by referring to the variable name.
Copyrighting
Basic Input/output …
Example:
The following example is used to illustrate the use of the input output
functions. We add Two numbers by allowing the user to enter them
on the keyboard, we then calculate the result and output it to the
user.
#include <stdio.h>
void main()
{
/*declare variables to hold the two numbers to be
added and the answer (integers)*/
int num1,num2,sum;
// Ask the user to input the two number – use printf
printf(“Enter the First Number:”);
scanf(“%d”,&num1);
printf(“\nEnter the second number:”);
scanf(“%d”,&num2);
//add the two numbers
sum=num1+num2 ;
//output the sum.
printf(“\n%d+%d=%d”,num1,num2,sum);
}//end
Character Data types
== Equal to
!= Not equal to
|| Logical or
Control Structures
These are statements used to control the sequence in which
instructions of a program are executed.
A. Selection Statements
A realistic C program may require that a logical test be carried
out at some point within the program.
One of several actions will then be carried out depending on the
outcome of the test.
This is known as branching.
This may be done using several control structure included in C.
1. The if–else statement
Used to carryout a logical test then take one of two possible
actions. The else part of the statement is optional. Thus, in its
most general form it is written: if (expression) statement.
1. The if–else statement
Example: The following example asks the user to enter the
total marks of the student. The program then prints proceed if
the marks are greater than 40.
#include <stdio.h>
void main()
{
float marks;
printf(“Enter the student’s marks::”);
scanf(“%f”,&marks);
if (marks>40)
{
printf(“Proceed”);
}//end if
} //end main
The other form of the if–else statement includes the else
statement. Its form is: if (expression) statement1 else statement 2.
The Example above can be written to output the statement rewind
if the marks is less than 40.
#include <stdio.h>
void main()
{
float marks;
printf(“Enter the student’s marks::”);
scanf(“%f”,&marks);
if (marks>=40)
{
printf(“Proceed”);
}
else
{
printf(“Rewind”);
}
} //end main
The if–else statement can also take the following form:
if(expression1)
statement1
else if(expression2)
statement2
else if(expression3)
statement3
.
.
.
else if(expression n)
statement n
else
statement n+1
Example: We can have a program that allocates the grade
depending on the students score in an exam: 70 and above is A,
60-69 is B, 50-59 is, 40-49 is D and below 40 is F (for fail)
#include<stdio.h>
void main()
{ float marks=0;
printf(“Enter the student’s marks::”);
scanf(“%f”,&marks);
if (marks>=70)
{
printf(“The grade is an A”);
}
else if (marks>=60 && marks <=69)
{
printf(“The grade is a B”);
}
else if (marks>=50 && marks <=59)
{
printf(“The grade is a C”);
}
else if (marks>=40 && marks <=49)
{
printf(“The grade is a D”);
}
else
{
printf(“The grade is an F”);
}
}//end main
2. The Switch statement
The switch statement is another selection statement provided in
C.
It causes a group of statements to be chosen from one of several
groups. The selection is based on one of several values of an
expression
Exercise: write the same program using a for loop but now the
digits should be written from 9 to 0. Thus the index should be
decreasing i.e. should be initialized to 9.