Introduction to Computer Programming
Introduction to Computer Programming
PROGRAMMING
1
An algorithm is a sequence actions that is used to solve a problem.
4
Computer organization describes; the internal organization of a computer, the instruction set, instruction
format, how to make use of registers, allocate and de allocate memory, talking to various I/O devices, etc.
5
Running the same program on machines with different hardware configurations.
$ vim HelloWorld.c
$ gcc HelloWorld.c
$ ./a.out
Hello World!$
However you will see the prompt ($ ) appearing just after the message “ Hello W orld! ” you can avoid
this by adding the new line character (\ n ) to the end of the message. Then your modified program
should look like the following:
/* Program-2.3 - My First Program */
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
2.3 C Tokens
The smallest element identified by the compiler in a source file is called a token. It may be a single
character or a sequence of characters to form a single item. Tokens can be classified as keywords,
literals, identifiers, operators, etc. Literals can be further classified as numeric constants, character
constants and string constants.
Language specific tokens used by a programming language are called keywords. Keywords are also
called as reserved words. They are defined as a part of the programming language therefore cannot be
used for anything else. Any user defined literals or identifiers should not conflict with keywords or
compiler directives. Table 2.1 lists keywords supported by the C language.
Table 2.1 – The C language keywords
/* Program-2.5b */
#include <stdio.h>
int main()
{
printf("Hi there\n");
printf("How are you?");
return 0;
}
Now the output is
Hi there
How are you?$
By adding another \ n at then end of the second string you can move the prompt ($ ) to the next line.
Exercise 2.1: Write a C program to display the following text on the screen.
University of Moratuwa
Katubedda,
Moratuwa,
Sri Lanka
www.mrt.ac.lk
2.4.1 Escape Codes
Escape codes are special characters that cannot be expressed otherwise in the source code such as new
line, tab and single quotes. All of these characters or symbols are preceded by an inverted (back) slash
(\ ). List of such escape codes are given in Table 2.2. Note that each of these escape codes represents
one character, although they consists of two characters.
Table 2.2 – Escape codes
2.6 Variables
A variable has a value that can change. It is a memory location that can hold a value of a certain data
type. Programmers refer to a variable by its name (identifier) so that it can be accessed during the course
of the program. Programmers cannot use any of the keywords as variable names.
2.6.1 Declaring Variables
In order to use a variable in C, the programmer must first declare it specifying the data type. The most
important restriction on using a variable in C is that they have to be declared at the beginning of the
program. The syntax to declare a new variable is to first write the data type then followed by a valid
variable identifier as given in the following examples:
int a;
float total;
unsigned int index_no;
short int number_of_students;
Above set of expressions declared, variables “a ” as an integer, “ total ” as a floating point number,
“ index_ no ” as unsigned integer (since there are no negative index numbers) and “ num
ber_ of_ students ” as a short integer.
Multiple variables belonging to the same data type can be defined as separate set of expressions or by
listing variable names one after the other (should be separated by a coma sign (,)).Consider the following
examples:
int a;
int b;
float total;
float sub_total;
above variables can also be defined as:
int a,b;
float total, sub_total;
After declaring a variable it should be initialised with a suitable value. In C, an uninitialised variable
can contain any garbage value therefore the programmer must make sure all the variables are initialised
before using them in any of the expressions. Initialising a variable can be done while declaring it, just
after declaring it or later within the code (before accessing/evaluating its value within an expression). In
initialising a variable any of the following three approaches are valid:
int a;
a = 10;
or
int a=10;
or
int a;
a = 10;
2.6.2 Constants
The value of a constant cannot be changed after an initial value is assigned to it. The C language supports
two types of constants; namely declared constants and defined constants. Declared constants are more
common and they are defined using the keyword const . With the const prefix the programmer can
declare constants with a specific data type exactly as it is done with variables.
const float pi = 3.141;
Programmers can define their own names for constants which are used quite often in a program. Without
having to refer to a variable such a constant can be defined simply by using the # define
/* Program-2.8 */
#include <stdio.h>
int main()
{
int a = 3;
float b = 10.0;
float c;
c = b/a;
printf("A is %d\n",a); //decimal value
printf("B is %d\n",b); //decimal value
printf("Answer is %f\n",c); //floating point value
return 0;
}
Executing Program-2.8 will display the following:
A is 3
B is 0
Answer is 3.333333
In Program-2.8 you may wish to see the answer appearing in a more manageable form like 3.3 or 3.33
rather than 3.333333. This can be achieved by using modifiers along with the format characters in order
to specify the required field width.
The format % .0 f will suppress all the digits to the right of the decimal point, while the format % .2 f
will display first two digits after that decimal point.
Exercise2.4: Modify Program-2.8 so that it displays an answer which is correct up to 2 decimal points.
Exercise 2.5: Write a program to assign the number 34.5678 to a variable named “number” then display
the number rounded to the nearest integer value and next the number rounded to two decimal places.
Program-2.9 illustrates the use of character variables.
/* Program-2.9 */
#include <stdio.h>
int main()
{
char first_letter;
first_letter = ‘A’;
printf("Character %c\n", first_letter); //display character
printf("ASCII value %d\n", first_letter); //display ASCII value
return 0;
}
Executing Program-2.9 will display the following:
Character A
ASCII value 65
Expressions can be built up from literals, variables and operators. The operators define how the variables
and literals in the expression will be manipulated. C supports several types of operators and they can be
classified as:
Assignment operator
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Special operators
Pointer operators
Pointer operators and special operators will not be covered in this chapter. They will be introduced
later under relevant sections.
.
3.1 Assignment Operator
The assignment operator is the simple equal sign (=). The assignment operator is used to assign a
value to a variable. The format of an assignment statement is:
variable- nam e = expression;
The expression can be a single variable or a literal, or it may contain variables, literals and operators.
The assignment operation always takes place from right to left. Consider the following set of examples:
a = 5; // value of variable ‘a’ becomes 5
a = 5+10; // value of variable ‘a’ becomes 15
a = 5 + b; // value of variable ‘a’ becomes 5 + value of b
a = b + c; // value of variable ‘a’ becomes value of b + value of c
a = (x*x + y*y)/2;
In C lots of shortcuts are possible. For example, instead of the statement,
a = a + b;
the programmer may use the shorthand format:
a += b;
Such operators are called compound assignment operators. The assignment operator can be combined
with the major arithmetic operations such as; +, -, *, / and %. Therefore many similar assignments can
be used such as:
a -= b; // is same as a = a-b;
a *= b; // is same as a = a*b;
a /= b; // is same as a = a/b;
a %= b; // is same as a = a%b;
Operator Action
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division
++ Increment (extended)
-- Decrement (extended)
return 0;
}
Executing Program-3.1 with “a ” as 5 and “b” as 2 will display the following:
Enter a: 5
Enter b: 2
a+b = 7
a-b = 3
a*b = 10
a/b = 2
a%b = 1
3.2.1 Increment and Decrement Operators
Increment and decrement operators are very useful operators. The operator + + means “add 1” or
“increment by 1”. Similarly operator - - mean “subtract 1” or “decrement by 1”. They are also equivalent
to + = 1 and - = 1 . For example, instead of using the following expression to increment a variable:
a = a + 1;
you may use:
a +=1; or a++;
Also expression:
a = a - 1;
can be written as:
a -=1; or a--;
Both increment and decrement operators can be used as a prefix or as a suffix. The operator can be
written before the identifier as a prefix (+ + a ) or after the identifier as a suffix (a+ + ). In simple
operations such as a+ + or + + a both have exactly the same meaning. However in some cases there is
a difference. Consider the following set of statements:
int a, x;
a = 3;
x = ++a;
After executing above segment of code, “a ” will be 4 and “x ” will be 4 .
In line 3, first the variable “a ” is incremented before assigning it to variable “x ”. Consider the
following segment of code.
Operator Action
Relational Operators
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
== Equal
!= Not equal
Logical Operators
&& AND
|| OR
! NOT
3.3.1 Precedence of Relational and Logical Operators
As arithmetic operators, relational and logical operators also have precedence. Table 3.4 summarises
the relative precedence of the relational and logical operators. These operators are lower in precedence
than arithmetic operators.
Operator Action
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ One’s complement
>> Right shift
<< Left shift
return 0;
}
Executing Program-3.2 with character “b” as the input will display the following:
Character to convert: b
Converted character: B
Executing Program-3.2 with character “Q” as the input will display the following:
Character to convert : Q
Converted character : q
A Program is usually not limited to a linear sequence of instructions. In real life, a programme usually
needs to change the sequence of execution according to some conditions. In C, there are many control
structures that are used to handle conditions and the resultant decisions. This chapter introduces if- else
and sw itch constructs.
return 0;
}
Executing Program-4.1 with different inputs will behave as follows:
Case 1: Enter marks: 73
You have passed the exam!
return 0;
}
Executing Program-4.2 with 1250.25 as the keyboard input display the following:
Enter amount: 1250.25
Discount: 62.51
Total: 1187.74
In Program-4.1 if the condition is TRUE, the set of statements inside the block are executed. If the
condition is FALSE (if the amount is less than 1000) those statements will not be executed.
Example 4.2 – Modify Program-4.2 so that it displays the message “No discount…” if the amount is
less than 1000.
Another if clause is required to check whether the amount is less than 1000. The second if clause can
be used before or after the first (existing) if clause. Program-4.3 below has been modified from Program-
4.2 to address this.
/* Program-4.3 */
#include <stdio.h>
int main()
{
float amount,final_amount, discount;
printf("Enter amount: ");
scanf("%f", &amount); //get amount
return 0;
}
Notice that in Program-4.5, some of the conditional expressions inside the if clause are not as simple as
they were earlier. They combine several expressions with logical operators such as AND ( & & ) and OR
(| | ). These are called compound relational tests.
Due to the top down execution of the if-else-if ladder Program-4.5 can also be written as follows:
/* Program-4.6 */
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks); //get marks
return 0;
}
In Program-4.6, when the marks are entered from the keyboard the first expression ( m arks > 7 5 ) is
evaluated. If m arks is not greater than 75, the next expression is evaluated to see whether it is greater
than 50. If the second expression is not satisfied either, the program evaluates the third expression and
so on until it finds a TRUE condition. If it cannot find a TRUE expression statement(s) after the else
keyword will get executed.
Exercise 4.4 – Rewrite the program in Example 4.3 using nested conditions (i.e. using braces ‘{‘ and
‘}’.
Example 4.4 – A car increases it velocity from u ms-1 to v ms-1 within t seconds. Write a program to
calculate the acceleration.
The relationship among acceleration (a), u, v and t can be given as v u at . Therefore the
vu
acceleration can be found by the formula a . In the program we implement users can input
t
any values for u, v and t. However, to find the correct acceleration t has to be non zero and positive
(since we cannot go back in time and a number should not be divided by zero). So our program should
make sure it accepts only the correct inputs. The Program-4.7 calculates the acceleration given u, v and
t.
if(t >= 0)
{
a = (v-u)/t;
printf("acceleration is: %.2f m/s", a);
}
else
printf ("Incorrect time");
return 0;
}
/* Program-4.8 */
#include <stdio.h>
int main()
{
int a,b;
printf("Enter value of a: ");
scanf("%d", &a); //get starting velocity
b = a > 50 ? 1 : 2;
printf("Value of b: %d", b);
return 0;
}
Executing Program-4.8 display the following:
Enter value of a: 51
Value of b: 1
Enter value of a: 45
Value of b: 2
If the input is greater than 50 variable “b” will be assigned “1” and if not it will be assigned “2”.
Conditional operator can be used to implement simple if-else constructs. However use of conditional
operator is not recommended in modern day programming since it may reduce the readability of the
source code.
1 – Microsoft Word
2 – Yahoo messenger
3 – AutoCAD
4 – Java Games
switch (a)
{
case 1: //if input is 1
printf("\nPersonal Computer Software");
break;
case 2: //if input is 2
printf("\nWeb based Software");
break;
case 3: //if input is 3
printf("\nScientific Software");
break;
case 4: //if input is 4
printf("\nEmbedded Software");
1 - Microsoft Word
2 - Yahoo messenger
3 - AutoCAD
4 - Java Games
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
Answer : 8.00
In certain cases you may need to execute the same statement(s) for several cases of a switch block. In
such cases several cases can be grouped together as follows:
switch (x)
{
case 1:
case 2:
case 3:
printf(“Valid input”);
break;
default:
printf(“Invalid input”);
break;
}
A Program is usually not limited to a linear sequence of instructions or conditional structures and it is
sometimes required to execute a statement or a block of statements repeatedly. These repetitive
constructs are called loops or control structures. The C language supports three constructs; namely for
, w hile and do- w hile loops. Rest of this chapter introduces these control structures.
return 0;
}
Execution of program-5.1 displays:
This is a loop
This is a loop
This is a loop
This is a loop
This is a loop
In the above example, the variable “ counter ” starts with an initial value of “1 ”. The second expression
inside the parenthesis determines the number of repetitions of the loop. It reads as; “as long as the counter
is less than or equal to 5 repeat the statement(s)”. The third expression is the incrimination of the counter;
it is achieved by the + + operator. You may also decrement the counter depending on the requirement,
but you have to use suitable control expression and an initial value.
In the first round of execution the “ counter ” is set to “1 ”. Then the expression “ counter < = 5 ” is
evaluated. Since the current value of the “ counter ” is “1 ” expression is evaluated as TRUE. Therefore
the printf function gets executed. Then the “ counter ” is incremented by the + + operator and now its
new value becomes “2 ”. At this point the first round of the loop is completed. Then in the second round
the expression is evaluated again. Since the “counter” is “2” the expression will be TRUE. Therefore
the printf function gets executed for the second time. Then the “ counter ” is incremented once more
and its new value becomes “3 ”. This process continues for another 2 rounds. After a total of five rounds
the “ counter ” becomes “6 ”. When the expression is evaluated at the beginning of the sixth round the
“ counter ” is greater than 5 therefore expression becomes FALSE. Then the loop will terminate and
the control is given to rest of the instructions which are outside the loop.
Exercise 5.1 – Write a C program to display all the integers from 100 to 200.
Example 5.1 – Write a program to calculate the sum of all the even numbers up to 100.
return 0;
}
return 0;
}
Execution of Program-5.4 with 5 as the input will display the following:
Enter number of times to repeat: 5
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
In Program-5.4 the number of times to loop, depends on the user input. In such cases use of w hile loop
is desirable than the for loop. In program-5.4 variable “ num ” act as the counter. The conditions inside
the w hile loop check whether the counter is not equal to zero (num != 0 ) if so it will execute
the printf function. Next we need to make sure that the program loops only 5 times. That is achieved
by decrementing the counter (num - - ) at each round. If the counter is not decremented the program will
loop forever.
Exercise 5.5 – What would happen if we enter -1 as the number of times to loop in Program-5.4? Modify
Program-5.4 so that it works only for positive integers.
return 0;
}
Execution of Program-5.5 with some inputs will display the following:
Enter price (0 to end): 10
Enter price (0 to end): 12.50
Enter price (0 to end): 99.99
Enter price (0 to end): 0
Total : 122.49
Program-5.5 accepts prices of items from the keyboard and then it computes the total. User can enter
any number of prices and the program will add them up. It will terminate only if zero or any negative
number is entered. In order to calculate the total or terminate the program there should be at least one
input from the keyboard. Therefore in this type of a program do- w hile loop is recommended than the
w hile loop.
Exercise 5.6 – Modify Program-4.8 such that it first displays the menu. Then based on the user selection
it should display the correct message. After displaying the correct message it should again display the
menu. The program should exit when the user enter 0.
In summary, the for loop is recommended for cases where the number of repetitions is known in
advance. The w hile loop is recommended for cases where the number of repetitions are unknown or
unclear during the development process. The do- w hile loop is recommended for cases where the loop
to be executed needs to run at least once regardless of the condition. However, each type of loop can be
interchanged with the other two types by including proper control mechanisms.
return 0;
}
Exercise 5.7 – Write a C program to display the following symbol pattern:
*
**
***
****
*****
******
int main()
{
int n;
for(n=10;n>0;n--)
{
printf("Hello World!\n");
if(n == 5)
{
printf("Countdown aborted!");
break;
}
}
return 0;
}
Under normal circumstances the Program-5.7 will display the “ Hello W orld! ” message 10 times.
Notice that in this example the for loop is written as a decrement rather than an increment. During the
first 5 iterations the program executes normally displaying the message “ Hello W orld! ”. Then at the
beginning of the sixth iteration variable “n ” becomes “5 ”. Therefore the if condition which evaluates
whether “n= = 5 ” becomes TRUE, so it will execute the printf function and then the b reak instruction.
At this point the loop will terminate because of the break keyword.
Example 5.4 – Write a C program to display the message “Hello World!” 10000 times. The program
should allow users to terminate the program at any time by pressing any key before it displays all the
10000 messages.
int main()
{
int i;
for(i=0;i<=10000;i++) // loop 10000 times
{
printf("Hello World! %d\n",i);
if(kbhit() != 0) // if a key is pressed
{
printf("Loop terminated");
break; //terminate loop
}
}
return 0;
}
int main()
{
int i;
for(i=-5;i<=5;i++) // loop from -5 to 5
{
if (i == 0) // if 0 skip
continue;
printf("5 divided by %d is: \t %.2f \n", i, (5.0/i));
}
return 0;
}
In program-5.9, 5 is divided by all the integers from - 5 to + 5. However a number should not be divided
by 0 . In Program-5.9, when “i” is 0 (when the if condition is TRUE) the continue keyword is used to
skip the rest of the iteration which will skip the printf function.
Arrays are a series of elements of the same data type placed consecutively in memory that can be
individually referenced by adding an index to a unique name. Using an array we can store five values of
type int with a single identifier without having to declare five different variables with a different
identifier. Arrays are useful when you store related data items, such as grades received by the students,
sine values of a series of angles, etc. Like any other variable in C an array must be declared before it is
used. The typical declaration of an array is:
data- type array- nam e[ no- of- elem ents] ;
Notice that the array name must not be separated from the square brackets containing the index. When
declaring an array, the number of array elements should be constant. As arrays are blocks of static
memory locations of a given size, the compiler must be able to determine exactly how much memory to
allocate at compilation time.
int main()
{
int i,sum;
int marks[5]; //array of 5 elements
float average;
sum=0;
for(i=0;i<5;i++)
{
printf("Enter marks for subject %d: ", i+1);
scanf("%d", &marks[i]); //get the marks
}
for(i=0;i<=5;i++) //total marks
{
sum +=marks[i];
The C functions that you have used so far (such as printf and scanf ) are built into the C libraries, but
you can also write your own functions. Therefore functions can be classified as built-in and user defined.
A modular program is usually made up of different functions, each one accomplishing a specific task
such as calculating the square root or the factorial. In general a modular program consists of the m ain(
) function followed by set of user defined functions as given below:
# include ……
# define …..
Prototyp es of functions
int m ain( )
{
……….
}
function_ 1 ( )
{
………..
}
function_ 2 ( )
{
………..
}
…………
function_ n( )
{
………..
}
The source code contains other elements in addition to the function blocks. It starts with the
# include directive, followed by the # define directive (if any) then followed by the prototypes of
functions. The prototype is a declaration of a function used in the program. Then comes the program
building block which includes the m ain( ) function and implementation of the user defined
functions.
7.1 A Function
A function is a subprogram that can act on data and return a value. Some functions may accept certain
input parameters (such as printf ), some may return a value of a certain data type (such as kbhit ) and
some may accept as well as return a value (sqrt ).
Every C program has at least one function, the m ain( ) . When a program is executed the m ain( ) is
called automatically. The m ain( ) may call other functions and some of them might call other functions.
Each function has a unique name and when the name is encountered while in execution the control of
the program is transferred to the statement(s) within the function. When the function is ended (returns)
the control is resumed to the next statement following the function call.
Well designed functions perform a specific and easily understood task. Complicated tasks should be
broken down into multiple functions and then each can be called in the proper order.
int main()
{
float radius;
printf("Enter radius: ");
scanf("%f",&radius); //read radius
int main()
{
int radius;
printf("Enter radius: ");
scanf("%d",&radius);
area(radius);
area((float)radius); //convert to float
return 0;
}
void area(float r)
{
printf("\nFloating point input");
printf("\nArea is: %.2f", (pi*r*r));
}
void area(int r)
{
printf("\nInteger input");
printf("\nArea is: %.2f", (pi*r*r));
}
Execution of Program-7.2 displays:
Enter radius: 1
Integer input
Area is: 3.14
Floating point input
Area is: 3.14
int main()
{
int b,c;
a=5;
b=10;
c=15;
printf("\nValue before 1st function a= %d, b= %d c= %d" ,a,b,c);
swap1(b,c);
printf("\nValue after 1st function a= %d, b= %d c= %d" ,a,b,c);
swap2(b);
printf("\nValue after 2nd function a= %d, b= %d c= %d" ,a,b,c);
printf("Test");
return 0;
}
void swap2(int x)
{
int z;
z = x;
x = a;
a = z;
}
Pointers are another important feature of the C language. Although it may appear a little confusing for
a novice programmer they are a powerful tool and handy to use once they are mastered. The power of
C compared to most other languages lies with proper use of pointers. Pointers are useful due to following
reasons:
They enable us to access a variable that is defined outside a function.
Pointers are more efficient in handling data tables and sometimes even arrays.
Pointers tend to reduce the length and complexity of a program.
They increase the execution speed.
Use of pointers allows easy access to character strings.
Computers use memory to store both instructions and values of variables of a program. The computer’s
memory is a sequential collection of storage cells with the capacity of a single byte. Each of these
memory cells has an address associated with it.
Whenever a variable is declared the system allocates some memory to hold the value of the variable.
Such memory location can be accessed by providing the memory address. Consider the following
example:
int number = 35;
The above expression allocates a memory location to hold the value of variable “num ber ” that can
hold an integer (4 bytes) and it also initialises the variable. Suppose the address of that memory location
is 2000. Then after executing above expression the memory address 2000 should hold 35. During
execution of the program, the system always associates the name “ num ber ” with the memory address
2000. We may have access to the value “3 5 ” by using either the name “ num ber ” or the address 2000.
Since memory addresses are simple numbers, they can also be assigned to some variables. Such variables
that hold memory addresses are called pointers. Therefore a pointer is nothing but a variable that
contains an address which is a location of another variable in memory.
int main()
{
int number = 20;
int *pnt;
pnt = &number;
int main()
{
int a,b;
a = 5;
b = 10;
return 0;
}
int main()
{
char *a;
a = "Hello World";
return 0;
}
Exaction of Program-8.3 will display:
String: Hello World
First character: H
Starting memory address: 4235496
First character: 72
In Program-8.3 the first printf function displays the string pointed by pointer “a ”. The second printf
function display the value pointed by pointer “a ” which is the first character of the string. The third
printf function displays the starting memory address of the string which is the value of pointer “a ”.
The final printf function displays the ASCII value of the first character in the string.
int main()
{
int marks[5]= {89, 45, 73, 98, 39};
The programs that we developed up to now were neither able to produce permanent output nor were
they able to read data inputs other than from the keyboard. Using files you can save your output data
permanently and retrieve them later.
A file in general is a collection of related records, such as student information, marks obtained in an
exam, employee salaries, etc. Each record is a collection of related items called fields, such as “student
name”, “date of birth”, “subjects registered”, etc. The common operations associated with a file are:
Read from a file (input)
Write to a file (output)
Append to a file (write to the end of a file)
Update a file (modifying any location within the file)
In C language data is transferred to and from a file in three ways:
Record input/output (one record at a time)
String input/output (one string at a time)
Character input/output (one character at a time)
6
Structures are used to store records with different data types.
int main()
{
FILE *fp;
char c;
return 0;
}
Execution of Program-9.1 will display
Hello World!
This is my first file
In Program-9.1 a connection is first established to the file. Then the expression if( fp != NULL) evaluates
whether the connection is successful. If it is not successful the program will display the message “ Error
w hile opening file... ” If it is successful it reads the first character from the file. If the character is
not the end of the file (indicated by the End Of File (EOF) mark) it displays the
int main()
{
FILE *fp;
char buffer[100]; //char array with 100 elements
char *result; // hold the result of the fgets function
return 0;
}
9.1.4 Writing Data to a File
You can also write data to file either one character at a time or a string at a time.
Writing Character to a File
To write a character to a file the putc function can be used. It has the form:
putc( c, fp)
where c is the character while fp is the file pointer. It returns an int value which indicates the success
or the failure of the function. It returns the int value of the character if it is successful, if not it returns
EOF mark.
Example 9.3 – Write a C program to store the message “Introduction C Programming” in a file named
“message.txt”.
Program-9.3 is an implementation of the above requirement. The function putc is used to write
characters in the message to the file. To find the number of characters in the message the strlen
int main()
{
FILE *fp;
int i; //loop counter
char *message;
message = "Introduction C Programming";
return 0;
}
Writing a String to a File
The advantage of putc is that it allows you to control every byte that you write into the file. However
sometimes you may want to write one string at a time. Two functions, namely fputs and fprintf can
be used for this purpose. The fprintf function is identical to the printf function only difference being
that it writes to a file rather than to the screen. The format of each function is:
fputs( string, file_ pointer)
fprintf( file_ pointer, “% s”, string)
Exercise 9.1 – Modify Program-9.3 such that it uses the fputs rather than the fputc function to write the
message to the file.
Exercise 9.2 – Develop a simple telephone directory which saves your friends contact information in a
file named directory.txt. The program should have a menu similar to the following:
----------------Menu-------------------------
1. Add new friend.
2. Display contact info.
3. Exit
Objectives
Compile/link C programs on Linux using gcc.
Correct/fix syntax errors and bugs taking clues from compiler error messages and warnings.
Debug programs through program testing.
Learn how to detect programming errors through program testing.
Prerequisites
Students are expected to be familiar with a text editor on Linux (such as vi or vim).
A basic understanding of compiling and linking in concept.
Knowledge in area such as C language syntax, input output function is recommended.
Requirements
The GNU C and C++ compiler7.
Remarks
This assignment will be continued next week as well.
No evaluation will be done based on these lab sessions.
All the source codes and executables related to this lab session should be saved in a folder
named Lab2 _ 3 which should be created within your hom e directory.
7
http://www.gnu.org/
Step 1: The program given below calculates the roots of the quadratic equation ax 2 bx c 0
provided that b 4ax 0 .
2
Type the following program using a text editor and then save it as test 2 .c.
#include <stdio.h> //Pre-processor directives
#include <math.h>
double a, b, c, delta;
printf("Enter a, b, c: );
scanf("%lf %lf %lf", &a, &b, &c); //read a, b and c
double delta = b*b - 4*c;
8
Why is it that we have to type ./a.out to execute the file a.out in the working directory? (i.e. why won’t typing
a.out alone work?)
int main()
{
double a, b, c, delta;
printf("Enter a, b, c: ");
scanf("%lf %lf %lf", &a, &b, &c); //read a, b and c
if (a ==0.0 a) // if a zero
printf("Coefficient a should be non-zero.\n");
else // if a not zero
{
delta = b * b - 4 * a * c;
if(delta < 0)
printf("complex roots!\n");
else
{
double t = sqrt(delta);
double r1 = (-b - t) / (2 * a);
double r2 = (-b + t) / (2 * a);
printf("%.2f %.2f\n", r1, r2);
}
}
return 0;
}
Discussion
Try to find answers to following questions (no need of submission).
1. Are all languages compiler based like C?
2. Explain the difference between a compiler error and a warning?
3. What is a bug with reference to software?
4. Does program testing show the presence of bugs or their absence? Explain your answer.
5. Given that you have the C source file test3.c, write down the commands for the following:
a. To compile (compile only) the file using gcc.
b. What are the output files produced by this command?
c. To compile and link the file with the standard C library. What is the name of the
executable produced?
d. Modify the command in (c) to change the name of the executable file to test from its
default name.
e. To link the object file produced in (a) above, with the math library in addition to the
standard C library.
6. What would happen if a program uses library other than the standard C library and the
particular library was not specified to the linker when linking the program?
7. What does GNU stand for?
Exercises
Exercise B.1 – Write a program to input a temperature reading in either Celsius(c) or Fahrenheit(f) scale
and convert it to the other scale. The temperature reading consists of a decimal number followed
by letter ”F” or ”f ” if the temperature is in Fahrenheit scale or letter ”C” or ”c” if it is in Celsius
scale. You may use a similar format for the output of the program.
5( f 32)
Note: c
9
$ ./a.out
Temperature reading: 100C
212F
$ ./a.out
Temperature reading: 100F
37C
Exercise B.2 – Given a date as a triplet of numbers (y , m , d), with y indicating the year, m the
month (m = 1 for January, m = 2 for February, etc.), and d the day of the month, the
corresponding day of the week f (f = 0 for Sunday, f = 1 for Monday, etc.) can be found as
follows:
(a) if m < 3
(b) let m = m + 12 and let y = y - 1
(c) let a = 2m + 6 (m + 1) / 10
(d) let b = y + y/4 – y/100 + y/400
(e) let f1 = d + a + b + 1
(t) let f = f1 mod 7
(g) stop.
Write a program that will read a date and print the corresponding day of the week. All divisions
indicated above are integer divisions.
Discussion
Given that the else clause in the following code fragment belongs to the outer statement, write down 2
possible ways in which it can be implemented properly. No need of submission.
int a, b, c;
. . .
if (a > b )
if (b < c)
{
...
}
else
{
...
}
Objectives
Develop C programs using an Integrated Development Environment (IDE).
Correct/fix syntax errors and bugs taking clues from error messages, warnings and debug
features provided by the IDE.
Prerequisites
Students are expected to be familiar in developing C program on a Linux platform.
Requirements
The KDevelop IDE.
Remarks
This lab session will not be evaluated.
All the source codes and executables related to this lab session should be saved in a folder
named Lab5 which should be created within your hom e directory.
Integrated Development Environment (IDE) is a GUI based application or set of tools that allows a
programmer to write, compile, edit, and in some cases test and debug within an integrated, interactive
environment. These tools provide lot of features that make programming a fun. It may take sometime
for you to understand each and every feature provided by the IDE but it will certainly enhance your
productivity. Some of these tools go beyond conventional program development and testing and even
provide facility to manage various documents related to the project or source code and to keep track of
different versions of the source code. KDevelop is one of the heavily used IDE for developing C/C++
programs in the Linux platform. From this lab session onwards if you prefer you can use KDevelop for
program development.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello, world!\n");
return 0;
}
Step 7: Now its time to run your first C program using KDevelop. First you need to compile your program.
Use Build Com pile File menu or click on the Com pile File icon ( ) on the toolbar. You
may also use the keyboard shortcut Shift+ F8 as well.
While compilation you may get waning message about a make file. For the moment forget
about it.
return 0;
}
Step 2: Compile the program by clicking the Com pile File icon on the toolbar. Then you may see a
list of message (in the messages tab) appearing which may look like the follwoing:
source='main.c' object='main.o' libtool=no \
depfile='.deps/main.Po' tmpdepfile='.deps/main.TPo' \
depmode=gcc3 /bin/sh ../admin/depcomp \
gcc -DHAVE_CONFIG_H -I. -I. -I.. -O2 -O0 -g3 -Wall -c `test -f
'main.c' || echo './'`main.c
main.c: In function `main':
main.c:31: warning: format argument is not a pointer (arg 2)
main.c:36: parse error before '}' token
gmake: *** [main.o] Error 1
*** failed ***
It informs you about 2 errors in line 31 and 36 (line number may be different in your program). To go to
the line having an error double click on the message "main.c:31: warning: format argument
is not a pointer (arg 2)". Then the cursor automatically moves to the line.
Step 3: Correct the error in the program.
Step 4: Then go to the next error message and double click on it. Then your cursor will move to the
appropriate line within the program. Correct the error.
Step 5: Then compile your program again. If you correct both errors now your program should compile
without any error or warning and you should see the message "success".
Step 6: Then execute your program by selecting Build Execute menu or by pressing F9 key.
Executing about program with 5 as the input will display:
Enter number to find factorial: 5
Factorial of 5 is: 0
Press Enter to continue!
Step 2: To debug you program select Debug Start menu or click the Debug icon ( ) on the
toolbar.
If you do not see the shell appearing select Debug St op from the menu or click on the
Stop icon. Then use the Options KDevlop Setup.. menu open the KDevlop Setup
window locate Debu gg er and check the checkbox labelled Enable separate term inal
for application i/ o . Then click on the OK button. Now try to debug your program again.
Step 3: When the program request you to enter a number to find the factorial type 5 and press Ent er
key. Then go back to the IDE and locate the small Yellow triangle. This will indicate the current
line that is in execution.
Step 4: Highlight the token num and then right click. Then from the pop-up menu select W atch: num
. This will allow you to check the value of variable num while in debugging mode. Similarly do it
for variables i and fact .
On the left hand side of the window you should be able to see the current values of variables
num , fact and i.
Step 5: Then from the Debug menu select Run .
Notice that the value of variable i get changed but not other variables.
Step 6: Again select the Run from Debug menu. You will still see only variable i is getting changed
where as variable fact should also get changed. Repeat Debug Run menu until you
complete all the 5 loops.
Then select Debug Stop menu or click the Stop icon ( ) to stop debugging your program.
Now you should be able to understand that there is some error in the statement fact * = i;
which is supposed to change the value of variable fact . However you did observe that variable
i is getting changed. Then the error should be with the value of fact . Do you remember that the
starting value of fact is 0 . If not, debug your program again and see that fact is always 0 . So
regardless of the value of i the multiplication is always 0 . So this is the error.
Change initial value of variable fact to be 1 and Run your program again. Now enter 5 and
see whether you are getting the correct answer. Try your program with several other inputs.
Exercise C.1 – Consider Exercise B.2. Modify it so that you use the sw itch statement to print the days
as Sunday, Monday, Tuesday, .. using the value f derived from the given algorithm.
Exercises
Exercise D.1 – Write a program to input a series of positive integers and determine whether they are
prime. The program should terminate if a negative integer is given as the input. A prime number
is a number that is divisible by only one and itself. However one is not considered a prime
number. Execution of your program should produce something similar to the following:
$ ./a.out
1
2
Prime
3
Prime
4
5
Prime
6
-1
$
Exercise D.2 – Write a program to find out whether a given number is a perfect number. The program
should terminate if a negative integer is given as the input. A perfect number is a number whose
factors other than itself add up to itself.
Example: 6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14
$ ./a.out
6
Perfect
7
28
Perfect
11
-1
$
Exercise D.3 – Write a program to display the following symbol pattern. You must use loops.
******
*****
****
***
**
*
**
***
****
*****
******
Exercises
Exercise E.1 – Write a program to calculate and display the total and the average of 10 integer
numbers input from the keyboard. An example would be as follows:
Enter 10 integers: 5 78 96 54 34 12 3 7 88 5
Total = 382
Average = 38.2
Exercise E.2 – Write a program to find and display the minimum and the maximum among 10
numbers entered from the keyboard. Use a single-dimensional array to store the numbers
entered. The numbers can be non-integers. An example would be as follows:
Enter 10 numbers: 5 7.8 9.6 54 3.4 1.2 3 7 8.8 5
Minimum = 1.2
Maximum = 54
Exercise E.3 – Suppose there are 4 students each having marks of 3 subjects. Write a program to read
the marks from the keyboard and calculate and display the total marks of each student. Use a
2D (two-dimensional) array to store the marks. An example would be as follows:
Enter the marks of four students, on four rows:
50 60 80
60 75 90
30 49 99
66 58 67
Exercises
Exercise F.1 – Write a program to read in two matrices A and B of dimensions 34 and 43 respectively,
and compute and display their product AB (of dimensions 33). Assume that the elements of
the matrices are integers. Use functions to while implementing this program.
$ ./a.out
Matrix A:
1 2 4 5
3 3 4 4
4 4 5 5
Matrix B:
1 3 5
7 7 7
3 4 5
3 4 5
Matrix AB:
42 53 64
48 62 76
62 80 98
$
Hint: Define global variables of two-dimensional arrays to store the values of the matrices. Break
down the program in to three main functions to:
Read in the elements of matrices A and B
Compute the product of the two matrices
Display the resultant matrix AB
This lab session will be evaluated and it will carry 4% of your final marks.
All the source codes and executables related to this lab session should be saved in a folder
named Lab1 0 which should be created within your hom e directory. If not zero marks will be
given.
Before you leave the lab make sure you show your instructor that you have saved your file in
Lab1 0 folder.
Exercises
Exercise G.1 – Given a string S of length N from a text file, count the total number of words T, in S.
Use file-handling mechanism of C language to access the files.
Assume words to be delimited by a space, an exclamation mark, a question mark, a comma, a
period, a semicolon and a colon. Further, you may assume that there may be only one word
delimiter at most between any two words in S.
N , the number of characters in S (i.e. length of S), will be at least 1 and at most 100.
The input will be given in the text file problem .in and the output should be written to the text
file problem .out .
An example would be as follows:
problem.in
This is a test.
$ ./a.out
Number of words: 4
The C language is accomplished by a number of library functions that performs various tasks. Following
is a list of commonly used functions. For a more complete list reader should refer to the manual (or help)
of the version of C compiler that is being used.
H.1 Character Manipulation Functions
Function Header file Prototype description
isalnum ctype.h int isalnum(int character) Determines if the character is alphanumeric. If
true return nonzero value, else 0.
isalpha ctype.h int isalpha(int character) Determines if the character is alphabetic. If true
return nonzero value, else 0.
isascii ctype.h int isascii(int character) Determines if the character is an ASCII
character. If true return nonzero value, else 0.
isdigit ctype.h int isdigit(int character) Determines if the character is a decimal digit. If
true return nonzero value, else 0.
islower ctype.h int islower(int character) Determines if the character is a lowercase. If
true return nonzero value, else 0.
ispunct ctype.h int ispunct(int character) Determines if the character is a punctuation
character. If true return nonzero value, else 0.
isspace ctype.h int isspace(int character) Determines if the character is a whitespace. If
true return nonzero value, else 0.
isupper ctype.h int isupper(int character) Determines if the character is a uppercase. If
true return nonzero value, else 0.
isxdigit ctype.h int isdigit(int character) Determines if the character is a hexadecimal
digit. If true return nonzero value, else 0.
toascii ctype.h int toascii(int character) Convert value of argument to ASCII.
tolower ctype.h int tolower(int character) Convert a character to lowercase.
toupper ctype.h int toupper(int character) Convert a character to uppercase.