C Programming Course
C Programming Course
FOYSOL MAHMUD
10TH BATCH
Every C program consist of one or more modules called functions. One of the function that must be
called main. The program will always begin by executing the main function, which may access the
other function.
Each function must contain:
1. A function heading, which consists of the function name.
2. A list of argument declarations.
3. A compound statement.
Title (comment)
Here is an example to observe the structure of a C program:
//program to convert the dollars to equivalent taka.
#include <stdio.h> Library file excess
int main() Output Statement
Function Heading
{ Variable declaration
float taka, dollar, ex_rate;
printf("Enter the exchange rate of dollar to equivalent taka: ");
scanf("%f", &ex_rate);
printf("Enter the amount of dollar: "); Input Statement
scanf("%f", &dollar);
taka = dollar*ex_rate; Assignment Statement
printf("The amount of equivalent taka: %.2f\n", taka);
return 0;
}
Output of the progrtam:
C uses:
Uppercase letter A to Z.
Lowercase letter a to z.
Digits 0 to 9.
And some certain special character listed below:
+ - * / = % & # ! ?
^ “ ‘ \ | < > ( ) {
} [ ] : ; . _ Space
Identifiers (Variable name/ declaration)
• Both upper case and lower case letter are permitted (using of lowercase letters are
favorable).
• Underscore( _ ) character is also permitted.
• Number is also permitted but not for the first of the variable name
Valid Identifiers
x F14 Fm_14 _num
foysol Smaller_num MATRIX T14
Invalid Identifiers
4 t h The first character must be a
letter.
“x” Illegal characters(“)
order-no Illegal character (-).
error flag Illegal character (blank space).
Keywords
Data Types:
Arithmetic Operator:
Arithmetic operator
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
&& And
|| Or
Logical operator
LIBRARY FUNCTIONS
The C language is accompanied by a number of library functions that carry out various commonly
used operations or calculations. These library functions are not a part of the language per se, though
all implementations of the language include them. Some functions return a data item to their access
point; others indicate whether a condition is true or false by returning a 1 or a 0, respectively; still
others carry out specific operations on data items but do not return anything. Features which tend to
be computer-dependent are generally written as library functions. For example, there are library
functions that carry out standard input/output operations (e.g., read and write characters, read and write
numbers, open and close files, test for end of file, etc.), functions that perform
operations on characters (e.g., convert from lower- to uppercase, test to see if a character is uppercase,
etc.), functions that perform operations on strings (e.g., copy a string, compare strings, concatenate
strings, etc.), and functions that carry out various mathematical calculations (e.g., evaluate
trigonometric, logarithmic and exponential functions, compute absolute values, square roots, etc.).
Other kinds of library functions are also available.
scanf function
THE printf(……) FUNCTION
Output data can be written from the computer onto a standard output device using the library
function printf.
printf function
ASSIGNMENTS
//program to convert the dollars to equivalent taka.
#include <stdio.h>
int main()
{
float taka, dollar, ex_rate;
taka = dollar*ex_rate;
return 0;
}
c = (5*f-160)/9;
return 0;
}
CONTROL STATEMENT
The if-else statement is used to carry out a logical test and then take one of two possible actions,
depending on the outcome of the test (i.e., whether the outcome is true or false).
The else portion of the if-else statement is optional. Thus, in its simplest general form, the
statement can be written as
if(expression) statement
The expression must be placed in parentheses, as shown. In this form, the statement will be executed
only if the expression has a nonzero value (i.e., if expression is true). If the expression has a value of
zero (i.e., if expression is false), then the statement will be ignored.
if-else ladder:
if(expression 1) statement 1;
else if(expression 2) statement 2;
………
else statement;
expression
Example: statement
Where expression results in an integer value. Note that expression may also be of type char, since
individual characters have equivalent integer values.
exp → expression
s → statement
ASSIGNMENTS
return 0;
}
LOOPING
A group of statements is execute repeatedly until some condition has been satisfied.
1. for loop
2. while loop.
3. do-while loop.
The for statement is the third and perhaps the most commonly used looping statement in C. This
statement includes an expression that specifies an initial value for an index, another expression that
determines whether or not the loop is continued, and a third expression that allows the index to be
modified at the end of each pass.
The general form of the for statement is
Initialization
Increment/ Decrement
Condition
Example:
The while statement is used to carry out looping operations, in which a group of statements is
executed repeatedly, until some condition has been satisfied.
The general form of the while statement is
expression 1;
while ( expression 2){
statement
expression 3
}
expression 1;
do{
statement
expression 3;
} while(expression 2;)
sum = 0;
do{
scanf("%f", &num);
sum=sum+num;
i++;
}
while(i<n);
average=sum/n;
printf("\nSum=%f\n", sum);
printf("\nAverage=%0.2f\n", average);
return 0;
}
if(n>=1){
for(i=1;i<=n;i++){
if(i%2==1){
sum=sum+i;
}
}
printf("\nsum of all odd numbers between %d is %d.\n\n", n, sum);
}
else
printf("You have entered a wrong value. Please enter a positive
integer number.\n");
return 0;
}
ASSIGNMENTS
for(i=1;i<=rows;i++){
for(space=1;space<=rows-i;space++){
printf(" ");
}
for(k=0;k!=2*i-1;k++){
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Functions
We have already seen that C supports the use of library functions, which are used to carry out a number
of commonly used operations or calculations. However, C also allows programmers to define their
own functions for carrying out various individual tasks.
How it works:
A function is a self-contained program segment that carries out some specific, well-defined task. Every
C program consists of one or more functions. One of these functions must be called main. Execution
of the program will always begin by carrying out the instructions in main. Additional functions will be
subordinate to main, and perhaps to one another.
If a program contains multiple functions, their definitions may appear in any order, though they must
be independent of one another. That is, one function definition cannot be embedded within another. A
function will carry out its intended action whenever it is accessed (i.e., whenever the function is
"called") from some other portion of the program. The same function can be accessed from several
different places within a program. Once the function has carried out its intended action, control will
be returned to the point from which the function was accessed.
Generally, a function will process information that is passed to it form the calling portion of the
program, and return a single value. Information is passed to the function via special identifiers called
arguments (also called parameters), and returned via the return statement. Some functions, however,
accept information but do not return anything (as, for example, the library function printf) , whereas
other functions (e.g., the library function scanf) return multiple values.
Function
ASSIGNMENTS
for(;pow;pow--){
sum=sum*num;
}
return sum;
}
//main function
int main()
{
int x, y, result;
return 0;
}
if(x<y){
if(x<z){
minimum = x;
}
else{
minimum = z;
}
}
else{
if(y<z){
minimum = y;
}
else{
minimum = z;
}
}
return minimum;
}
//main function.
int main()
{
float x, y, z, smallest_number;
Array
Many applications require the processing of multiple data items that have common characteristics (e.g.,
a set of numerical data, represented by x1, x2, . . . ,xn). In such situations it is often convenient to place
the data items into an array.
The number of subscripts determines the dimensionality of the array. For example, x [ i ] refers to an
element in the one-dimensional array x . Similarly, y[ i ][ j ] refers to an element in the two dimensional
array y. (We can think of a two-dimensional array as a table, where y [ i ][ j ] is the jth element of the
ith row.) Higher-dimensional arrays can be also be formed, by adding additional subscripts in the same
manner (e.g., z [ i ] [ j ] [ k ] ) .
Where storage-class refers to the storage class of the array, data-type is the data type,
array is the array name, and expression is a positive-valued integer expression which indicates
the number of array elements. The storage-class is optional; default values are automatic for arrays
that are defined within a function or a block, and external for arrays that are defined outside of a
function.
Data type
Array name
ASSIGNMENTS
avg = sum/n;
printf("\n\nSum : %0.2f\n", sum);
printf( "Average: %0.2f\n" , avg);
return 0 ;
}