Data Point: This Chapter Covers Following Topics
Data Point: This Chapter Covers Following Topics
Chapter 1
Introduction
In recent years, there has been a major trend toward the use of C among serious
programmers.
Among the many reasons for C’s popularity are the following :
1. C is largely machine-independent. Programs written in C are easily ported from
one computer to another.
2. C is widely available. Commercial C compilers are available for most personal
computers, mini-computers, and mainframes.
3. C includes certain low-level features that are normally available only in assembly
or machine language.
4. Programs written in C compile into smaller programs that execute efficiently.
5. C is a flexible, high-level, structured programming language.
History
C was developed by Dennis Retchie in 1972 at the Bell Telephone Laboratories in
U.S.A. C was derived from a Language known as BCPL which was evolved at the
Massachusetts Institute of Technology in the late 60’s. BCPL was used to develop an
operating system known as MULTICS for early multi-user time shared computers. One
of the aims of BCPL was to achieve efficiency in compiled code. Thus BCPL was defined
such that a translator could produce efficient machine Language code. C being a
successor of BCPL has a similar philosophy. C Language has been defined so that it has
the advantages of a high level language namely machine independence. At the same
time it is concise, providing only the bare essentials required in a language so that a
translator can translate it in to an efficient machine language code. Until 1978, C was
confined to use within Bell Laboratories. In 1978, when Brian Kernighan and Ritchie
published a description of the language, known as “k & rc” computer professionals got
impressed with C’s many desirable features. By mid 1980s, popularity of C became
wide spread Numerous C Compiler were written for computers of all sizes.
Structure of a C Program:
The structure of a C program can by explained by taking an example of a C program to
calculate area and perimeter of a rectangle. The program is written as follows.
/* Example program */
/* This program finds the area and perimeter of a rectangle */
# include <stdio.h>
main( )
{
int p,q, area, perimeter;
p=4
q=6
area = p*q;
perimeter = 2 * (p+q);
printf(“area = %d\n”, area);
printf(“perimeter = % d\n”, perimeter);
________________________________________________________________________________
_
1 of 1
DATA POINT
}
/* End of main */
Comments
In the above program the first line in the program starts with /* and ends with */. Any
thing written between /* and */ is called a comment. In the C Language comments are
an aid to the programmer to read and understand a program. It is not a statement of
the language. The compiler ignores comments It is a good practice to include
comments in a program which will help in understanding a program.
PREPROCESSOR DIRECTIVE
Now let us observe the line
# include <stdio.h>
This is called a preprocessor directive. It is written at the beginning of the program. It
commands that the contents of the file stdio.h should be included in the compiled
machine code at the place where # include appears. The file stdio.h contains the
standard input/output routines. All preprocessor directives begin with pound sign #
which must be entered in the first column. The # include line must not end with a
semicolon. Only one preprocessor directive can appear in one line.
Main( ) Function
he next line is main( ). It defines what is known as a function in C. A C program is made
up of many functions. The function main( ) is required in all C programs. It indicates the
start of a C program. We will use main( ) at the beginning of all programs. Observe that
main( ) is not followed by a comma or semicolon.
Braces {And}
Braces{and} enclose the computations carried out by main ( ). Each line in the
program is a statement. Every statement is terminated by a semicolon; The statement
itself can be written anywhere in a line. More than one statement can be on a line as a
semicolon separates them. However it is a good practice to write one statement per
line.
Declaration
In the above program the first statement int p, q, area, perimeter; This statement is
called a declaration. It informs the compiler that p,q, area and perimeter are variable
names and that individual boxes must be reserved for them in the memory of the
computer. Further it tells that the data to be stored in the memory boxes named p,q,
area and perimeters are integers namely, whole numbers without a fractional part(e.g,
0,1,2,...). The statement ends with a semicolon The effect of statement is shown in
figure given below;
p q area Parameter
Fig. 1.1 Effect of defining p, q, area and perimeter as integer variable names.
Assignment Statements
The statement p=4; is an assignment statement. It commands that the integer 4 be
stored in the memory box named p. when the statement is executed the integer 4 will
be stored in the memory box named p as shown in fig. 1.2 . This statement assigns a
value 4 to the variable name p.
p
4
The library function used for display is printf( ). the general form of this function is
printf (format string, variable 1, variable2, ——variable n);
The format string is enclosed in quotes. It specifics any message to be printed and the
manner in which the contents of variables are to be displayed for each of the variables
in the list of variables.
printf(“area = % d\n”, area);
The format string is %d \n The symbol %d says interpret the variable area occurring
after the comma in the printf statement as an integer and display its value”. The
symbol \n causes the display to advance to the next line. Thus when this statement is
carried out we will see on the screen
area = 24
EXERCISE
Q.1 Where was C originally developed and by whom ?
Q.2 What are the major components of a C program ? What significance is attached to
name “main” ?
Q.3 How can comments be included within a C program ? Where can comments be
placed?
Summery:
1) C is a middle level language developed by denies Ritchi in 1972.
2) C is a procedure oriented language each program is divide into small block of
codes
3) This is called a preprocessor directive. It is written at the beginning of the
program to include library files in our program.
4) Each and every program must have main function, because execution of
program starts from main function.
5) Variable is nothing but a tool to reserve space for you in computer memory to
store values and calculations.
6) = is a assignment statement assign right hand side expression value to left hand
side
________________________________________________________________________________
_
3 of 3
DATA POINT
7) printf is a output statement from stdio file use to put messages on screen.
8) scanf is a input statement use to get values from user through keyboard.
9) We use conversion specification for getting values from keyboard and to put
values on screen using scanf, printf.
10) Each statement end with semicolon in c.
Chapter 2
NUMERIC CONSTANTS AND VARIABLES
CONSTANTS
The term constant means that it does not change during the execution of a program
constants may be classified as:
(i) Integer Constant
(ii) Floating point Constant
Integer Constant
Integer constants are whole numbers without any fractional parts. There are three
types of Integer constant:
(i) Decimal constant (base 10)
(ii) Octal Constant (base 8)
(iii) Hexa decimal constant(base 16)
Allowed digit in decimal constant 0,1,.2,3,4,5,6,7,8,9,. The digits of one octal constant
can be
0,1,2,3,4,5,6,7 and that in hexadecimal constants are 0,1,2,3,4,5,6,7,8.9,A,B,C,D,E,F,
Octal Constant
An octal constant must have at least one digit and start with the digit 0. It must be
written without a decimal point. It may have either sign + or -. A constant which has no
sign is taken as positive.
The following are valid octal constants.
(i) 0245
(ii) - 0467
(iii) +04013
The following are invalid octal constants
(i) 25( Does not begin with 0 )
(ii) 0387 ( 8 is not an octal digit )
(iii) 04.32 (Decimal point not allowed )
Hexadecimal constant
A hexadecimal constant must have at least one hexadecimal digit and start with Ox or
OX.
It may have either sign
The following are valid hexadecimal constants
(i) OX 14 AF
(ii) OX 34680
(iii) – OX 2673E
The following are invalid hexadecimal constants:
(i) Ox14AF
(ii) OX34680
(iii) -Ox2673E
The following are invalid hexadecimal constants:
________________________________________________________________________________
_
4 of 4
DATA POINT
________________________________________________________________________________
_
5 of 5
DATA POINT
e.q. si_int
m_hra
pop_e_89
C Keywords
Keywords are the words whose meaning has already been explained to the C compiler.
Keywords can not be used as variable names because if we do so we are trying to
assign a new meaning to the keyword, which is not allowed by the computer. The
keywords are also called ‘Reserved words’. There are 32 keywords available in C.
following is list of keywords in C.
EXERCISE
Q.1 Determine which of the following are valid identifiers. If invalid, explain why ?
(a) record 1 (b) return (c) name_and_address (d) name-and-address
Q.2 What is the range of long double?
Q.3 What are the rules for naming variables?
Q.4 Discuss the various types of constants?
Summery
1) Keywords are the words reserved for compiler.
2) C is a case sensitive language so capital A and small a are two different
variables in c.
3) No special characters are allowed in variable name.
Chapter 3
DATA INPUT AND OUTPUT
SINGLE CHARACTER INPUT - THE getchar( ) FUNCTION
Single characters can be entered in to the computer using the C library function
getchar( ). The getchar function is a part of the standard C Language i/o Library. It
returns a single character from a standard input device. The function does not require
any arguments, though a pair of empty parentheses must follow the word getchar. In
general terms a reference to the getchar function is written as
character variable = getchar( );
Here character variable refers to some previously declared character variable
________________________________________________________________________________
_
6 of 6
DATA POINT
putchar(C);
The first statement declares that C is a character type variable. The second statement
causes the current value of C to be transmitted to the standard output device.
Here control string refers to a string containing certain required formatting information,
and arg1, arg2,...arg n are arguments that represent the individual input data items.
The arguments represent pointers that indicate the addresses of the data item within
the computers memory. The control string comprises individual groups of characters
with one character group for each input data item. Each character group must begin
with a a percent sign( % ). In its simplest form a single character group will consist of
the percent sign, followed by a conversion character which indicates the type of the
corresponding data item.
...
scanf (“%3d %3d %3d”, & a, & b, & c);
...
}
Suppose the input data items are entered as
123
Then the following assignment is will result
a = 1, b = 2, c = 3
It data is entered as
123 456 789
The following assignments would be
a = 123, b = 456, c = 789
Now suppose that the data had been entered as
123456789
The assignments would be
a = 123, b = 456, c = 789
# include <stdio.h>
main ( )
{
char line [80];
gets(line);
________________________________________________________________________________
_
8 of 8
DATA POINT
puts(line);
}
Now suppose following string is entered from the standard input device
I am happy
Now the output of the program will be
I am happy
EXERCISE
Q.1 A C program contains the following statements :
# include <stdio.h>
char a, b, c;
(i) Write appropriate getchar statement that will allow values for a, b and c to be
entered into the computer
(ii) Write appropriate putchar statements that will allow the current values of a,b and c
to be written out of the computer.
Q.2 When entering a string via the scanf function using an s_type conversion factor,
how is the string terminated?
Q.3 What is the purpose of the printf function? How is it used within a C program ?
compare
with the putchar function ?
Summery
1) getchar() is use to get a single character through keyboard but u have to confirm
input by pressing enter.
2) getche() is use to get a single character through keyboard, it echo to screen.
3) getch()is use to get a single character through keyboard without echoing to
screen
Chapter 3
Operators in C:
1. Arithmetic operators
There are five arithmetic operators in C . They are
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after integer
The operator % is known as the modulus operator
e.g. Suppose that a and b are integer variables whose values are 10 and 3 respectively,
several arithmetic expressions involving these variables are shown below together with
their
resulting values
Expression Value
a+b 13
a-b 7
a * b 30
a/b 3
a%b 1
Now suppose that v1 and v2 are floating - point variables whose values are 12.5 and
2.0
respectively. several arithmetic expressions involving these variables are shown below,
together
with their resulting values
Expression Value
v1 + v2 14.5
v1 - v2 10.5
v1 * v2 25.0
v1 / v2 6.25
Now suppose c1 and c2 are character - type variables that represent the characters P
and T,
respectively. Several arithmetic expression that make use of these variables are shown
below
together with their resulting values (based upon the ASCII character set)
________________________________________________________________________________
_
9 of 9
DATA POINT
Expression Value
C1 80
C1 + C2 164
C1 + C2 +5 169
C1 + C2 +'5' 217
P is encoded as (decimal ) 80, T is encoded as 84, and 5 is encoded as 53 in the ASCII
character set, as shown above.
Suppose that a and b are integer variables whose values are 11 and - 3, respectively.
Several
arithmetic expressions involving these variables are shown below, together with their
resulting
values
Expression Value
a+b 8
a-b 14
a*b -33
a/b -3
Type cast
The value of an expression can be converted to a different data type. If desired to do
so the expression must be preceded by the name of the desired data type enclosed in
parentheses. (data type) expression This type of construction is known as a cast ( It is
also called a type cast.) Suppose that i is an integer variable whose value is 7, and f is
floating- point variable whose value is 8.5. The expression (i +f) % 4 is invalid because
the first operand (i + f) is floating point rather than integer. However the expression
((int) (i + f)) % 4 forces the first operand to be an integer and is therefore valid,
resulting in the integer remainder 3.
UNARY OPERATORS
C includes a class of operators that act upon a single operand to produce a new value
such operators are known as unary operators. Unary operators usually precede their
single operands, though some unary operators are written after their operands.
printf ( i = %d\n”, i )
LOGICAL OPERATORS
There are two logical operators also available in C. They are
Operator Meaning
&& and
|| or
The result of a logical and operation will be true only if both operands are true where
as the result of a logical or operation will be true if either operand is true or if both
operands are true. In other words the result of a logical or operation will be false only if
both operands are false.
Suppose i is an integer variable whose value is 7, f is a floating point variable whose
value is 5.5 and C is a character variable that represents the character ‘w’, several
complex logical expressions that make use of these variables are shown below:
Expression Interpretation Value
(i > = 6) && (c = = 'w') true 1
(i >= 6) && (c = = 119) true 1
(f < 11) && (i > 100) false 0
(c = 'p') || ((i + f) < =10) true 1
EQUALITY OPERATORS
There are two equality operators in C which are as follows
= = Equal to
!= Not equal to
The six operators mentioned above are used to form logical expressions representing
conditions that are either true or false.
The resulting expression will be of type integer, since true is represented by the integer
value
1 and false is represented by the value 0.
e.g . Let us suppose i, j and k are integer variables whose values are 1,2 and 3
respectively. Several logical expressions involving these variables are shown below.
Expression Interpretation Value
i<j true 1
(i + j) > = k true 1
(j + k) > (i + 5) false 0
k!=3 false 0
j==2 true 1
ASSIGNMENT OPERATORS
The commonly used assignment operator is = Assignment expressions that make use
of this operator are written in the form
Identifier = expression
________________________________________________________________________________
_
11 of 11
DATA POINT
a=3
x=y
sum = a+b
area = length * width.
EXERCISE
Q.1 What is an operator ? Describe several different type of operator included in C.
Q.2 A C program contains the following declarations
int i, j;
long ix;
short s;
float x;
double dx;
char c;
Determine the data type of each of the following expressions,
(a) x + c (c) i + x (e) ix + 4
(b) dx + x (d) s + 4 (f) s + c
Q.3 A C program contains the following declaration and initial assignments
int i = 8, j = 5;
double x = 0.005, y = - 0.01;
char c = ‘c’, d = ‘d’;
determine the value of each of the following expressions which involve the use of
library functions
(a) abs ( i - 2 * j ) (e) log (x)
(b) abs ( x + y) (f) sqrt ( x * x + y * y)
(c) toupper (d) (g) strlen ( “ hello\ 0”)
(d) floor (x) (h) pow (x - y)
Chapter 4
THE DECISION CONTROL & LOOP STRUCTURE
(a) The if statement
(b) The if - else statement
(c) The conditional operators
The if Statement
The general form of if statement looks like this:
if (this condition is true)
execute this statement;
Here the keyword if tells the compiler that what follows, is a decision control
instruction. The condition following the keyword if is always enclosed within a pair of
parentheses. If the condition, whatever it is true, then the statement is executed. It the
condition is not true then the statement is not executed instead the program skips past
it. The condition in C is evaluated using C’s relational operators. The relational
operators help us to build expression, which are either true or false
________________________________________________________________________________
_
12 of 12
DATA POINT
e.q
X = = Y X is equal to Y
X ! = Y X is not equal to Y
X <Y X is less than Y
X>Y X is greater than Y
X< = Y X is less than or equal to Y
X> = Y X is greater than or equal to Y
Demonstration of if statement
main( )
{
int num;
printf(“Enter a number less than 10”);
scanf(“%d”, & num);
if(num < = 10)
printf(“The number is less than 10”);
}
/* calculation of bonus */
main( )
{
int bonus, CY, Yoj, yr-of-ser;
printf(“Enter current year and year of joining”);
scanf(“%d %d”, &cy, &yoj);
yr-of-ser = CY-Yoj;
if(yr-of-ser > 3)
{
bonus = 2500;
printf(“Bonus = Rs. %d”, bonus);
}
}
If - else The if statement by itself will execute a single statement or a group of
statements when the condition following if is true. it does nothing when the condition is
false. It the condition is false then a group of statements can be executed using else
statement. The following program illustrates this
/* Calculation of gross salary */
main( )
{
float bs, gs, da, hra;
printf(“Enter basic salary”);
scanf(“%f”, & bs);
if(bs <1500)
{ hra = bs * 10/100;
da = bs * 90/100;
}
else
{ hra = 500;
da = bs * 98/100;
}
gs = bs+hra+da;
printf(“gross salary = Rs. %f”, gs);
}
Nested if - else If we write an entire if - else construct within the body of the if
statement or the body of an else statement. This is called ‘nesting’ of ifs.
e.g.
if(condition)
{
if (condition)
________________________________________________________________________________
_
13 of 13
DATA POINT
do this;
else
{ do this;
and this;
}
}
else
do this;
________________________________________________________________________________
_
14 of 14
DATA POINT
itself. Now let’s now write the same program using a do-while loop.
main( )
{
do
{
printf (“Hello \n”);
} while (5<1);
}
In the above program the printf( ) would be executed once, since first the body of the
loop is
executed and then the condition is tested.
________________________________________________________________________________
_
15 of 15
DATA POINT
statements inside the loop which have not yet been executed. When the keyword
continue is
encountered inside any C loop, control automatically passes to the beginning of the
loop. for
e.g.
main( )
{
int i,j;
for(i = 1; i< = 2; i++)
{
for(j=1; j<=2; j++)
{
if (i= =j)
continue;
printf(“\n%d%d\n”, i,j);
}
}
The output of the above program would be....
12
21
when the value of i equal to that of j, the continue statement takes the control to the
for loop
(inner) bypassing rest of the statements pending execution in the for loop(inner).
EXERCISE
Q.1 Write a loop that will calculate the some of every third integer beginning with i = 2
( i.e.
calculate the some 2 + 5 + 8 + 11 + ————) for all values of i that are less than 100.
Write the loop three different ways.
Q.2 Write a switch statement that will examine the value of integer variable called flag
and
print one of the following messages, depending on the value assigned to flag.
(a) HOT, if flag has a value of 1
(b) LUKE WARM, if flag has value of 2
(c) COLD, if flag has value of 3
(d) OUT OF RANGE, if flag has any other value.
Q.3 Describe the output that will be generated by each of following C programs.
(a) # include <stdio.h> (b) # include < stdio.h>
main ( ) main ( )
{{
int i = 0, x = 0; int i, j, x = 0;
while ( i < 20) { for ( i = 0; i < 5 ; ++ i)
if ( i % 5 = = 0) { for ( i = 0; j < i; ++ j) {
________________________________________________________________________________
_
16 of 16
DATA POINT
x + = i; x + = ( i + j - 1);
printf (“%d”, x); printf ( “ %d”, x);
}}
++i; printf (“\n x = %d”, x)
}}
printf (“ \n x = %d”, x);
}
Chapter 5
COMPETENCY OBJECTIVES
The objective of this Section is to provide the advanced features for programming with
C language. It includes complete explanations of these features. At the end of the
course, a student should be able to :-
1) Understand and implement Arrays.
2) Appreciate the use of functions.
3) Use the standard library string functions.
4) Develop dynamic data structures in C.
5) Understand structures and unions in C.
6) Apply graphics features in C language.
ARRAY DECLARATION
To begin with, like other variables an array needs to be declared so that the compiler
will know what kind of an array and how. large an array we want.
for e.g. int marks [30];
Here int specifies the type of variable, marks specifies the name of the variable. The
number 30 tells how many elements of the type int will be in our array. This number is
often called the ‘dimension’ of the array. The bracket [ ] tells the compiler that we are
dealing with an array.
________________________________________________________________________________
_
17 of 17
DATA POINT
}
for ( i = 0; i <= 29; i ++)
sum = sum + marks [i];
avg = sum /30;
printf (“\n Average marks = % f”, avg);
}
ARRAY INITIALISATION
To initialise an array while declaring it. Following are a few examples which
demonstrate this
int num [6] = {2, 4, 12, 5, 45, 5};
int n [ ] = {2, 4, 12, 5, 45, 5};
float press [ ] = { 12.3, 34.2, -23.4, - 11.3}
The following points should be noted
(a) Till the array elements are not given any specific values, they are suppose to
contain garbage values.
(b) If the array is initialized where it is declared mentioning the dimension of the
array is optional as in the 2nd example above.
MULTIDIMENSIONAL ARRAYS
In C one can have arrays of any dimensions. To understand the concept of
multidimensional
arrays let us consider the following 4 x 5 matrix
0 10 4 3 -10 12
1 2 3 0 61 8
2 0 16 12 8 0
3 12 9 18 45 -5
Column numbers (j)
Row number (i)
Let us assume the name of matrix is x
To access a particular element from the array we have to use two subscripts on for row
number and other for column number the notation is of the form X [i] [j] where i stands
for row subscripts and j stands for column subscripts.
The first example defines tables as a floating point array having 50 rows and 50
columns. The
number of elements will be 2500 (50 X50).
The second declaration example establishes an array line of type character with 24
rows and
40 columns. The number of elements will be (24 X 40) 1920 consider the following two
dimensional
array definition int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 11, 12, };
Thus the array values can be shown pictorially as
0123
01234
15678
2 9 10 11 12
Column number
Row number
Values [0] [0] = 1 Values [0] [1] = 2 Values [0] [2] = 3 Values [0] [3] = 4
Values [1] [0] = 5 Values [1] [1] = 6 Values [1] [2] = 7 Values [1] [3] = 8
Values [2] [0] = 9 Values [2] [1] = 10 Values [2] [2] = 11 Values [2] [3] = 12
Here the first subscript stands for the row number and second one for column number.
First
subscript ranges from 0 to 2 and there are altogether 3 rows second one ranges from 0
to 3
and there are altogether 4 columns.
Alternatively the above definition can be defined and initialised as
int values [3] [4] = {
{ 1, 2, 3, 4}
{ 5, 6, 7, 8}
{9, 10, 11, 12}
};
Here the values in first pair of braces are initialised to elements of first row, the values
of
second pair of inner braces are assigned to second row and so on. Note that outer pair
of
curly braces is required. If there are two few values within a pair of braces the
remaining elements will be assigned as zeros.
Here is a sample program that stores roll numbers and marks obtained by a student
side by
side in matrix
main ( )
{
int stud [4] [2];
int i, j;
for (i =0; i < =3; i ++)
{
printf (“\n Enter roll no. and marks”);
scanf (“%d%d”, &stud [i] [0], &stud [i] [1] );
}
for (i = 0; i < = 3; i ++)
printf (“\n %d %d”, stud [i] [0], stud [i] [1]);
}
The above example illustrates how a two dimensional array can be read and how the
values
stored in the array can be displayed on screen.
EXERCISE
Q.1 Describe the array defined in each of the following statements
(a) char name [30] (d) # define A 66
(b) float c [6]; # define B 132
(c) int params [5] [5] (e) double account [50] [20] [80]
Q.2 How can a list of strings be stored within a two-dimensional array ? How can the
individual strings be processed? What library functions are available to simplify string
processing?
Q.3 Write a C program that will produce to table of value of equation
y = 2 e- 0.1t sin 0.5t
________________________________________________________________________________
_
19 of 19
DATA POINT
Where t varies between 0 and 60. Allow the size of the t - increment to be entered as
an
input parameter.
A computer program cannot handle all the tasks by it self. Instead its requests other
program
like entities - called ‘functions in C - to get its tasks done. A function is a self contained
block
of statements that perform a coherent task of some kind.
e.g
#include <stdio.h>
message();
{
message ( );
printf (“\n Hello “);
}
main ( )
{
message ( )
printf (“\n I am in main “);
}
output of the program will be
Hello
I am in main
Here main ( ) is the calling function and message is the called function. When the
function message ( ) is called the activity of main ( ) is temporarily suspended while the
message ( ) function wakes up and goes to work. When the message ( ) function runs
out of statements to execute, the control returns to main ( ), which comes to life again
and begins executing its code at the exact point where it left off.
The General form of a function is
function (arg1, arg2, arg3)
type arg1, arg2, arg3
{
statement 1;
statement2;
statement3;
statement4;
}
Chapter 6
FUNCTIONS
There are basically two types of functions
(i) Library functions e.g printf ( ), scanf ( ) etc
(ii) user defined function e.g the function message( ) mentioned above.
The following point must be noted about functions
(i) C program is a collection of one or more functions
(ii) A function gets called when the function name is followed by a semicolon for e.g.
main ( )
{
message ( );
}
(iii) A function is defined when function name is followed by a pair of braces in which
one
or more statements may be present for e.g.
message ( )
{
statement 1;
statement2;
statement 3;
}
(iv) Any function can be called from any other function even main ( ) can be called from
other functions. for e.g.
main ( )
{
message ( );
________________________________________________________________________________
_
20 of 20
DATA POINT
}
message ( )
{
printf (“ \n Hello”);
main ( );
}
(v) A function can be called any number of times for eg.
main ()
{
message ( );
message ( );
}
message ( )
{
printf (“\n Hello”);
}
(vi) The order in which the functions are defined in a program and the order in which
they
get called need not necessarily be same for e.g.
main ( );
{
message1 ( );
message2 ( );
}
message2 ( )
{
printf (“\n I am learning C”);
}
message1 ( )
{
printf ( “\n Hello “);
}
(vii) A function can call itself such a process as called ‘recursion’.
(viii) A function can be called from other function, but a function cannot be defined in
another
function. Thus the following program code would be wrong, since argentina is
being defined inside another function main ( ).
main ( )
{
printf (“\n I am in main”);
argentina ( )
{
printf {“\n I am in argentina”);
}
}
(ix) Any C program contains at least one function
(x) If a program contains only one function, it must be main ( )
(xi) In a C program if there are more than one functional present then one of these
functional
must be main ( ) because program execution always begins with main ( )
(xii) There is no limit on the number of functions that might be present in a C program.
(xiii) Each function in a program is called in the sequence specified by the function calls
in
main ( )
(xiv) After each function has done its thing, control returns to the main ( ), when main (
) runs
out of function calls, the program ends.
________________________________________________________________________________
_
21 of 21
DATA POINT
(ii) Using functions it becomes easier to write programs and keep track of what they
are doing. If the operation of a program can be divided in to separate activities, and
each activity placed in a different function, then each could be written and checked
more or less independently. Separating the code in to modular functions also makes
the program easier to design and understand.
main ( )
{
float square ( );
float a, b;
printf (“\n Enter any number “);
scanf (“%f” &a);
b = square (a);
printf (“\n square of % f is % f, “ a, b);
}
float square (float x)
{
float y;
y= x *x;
return ( y);
}
sample run
Enter any number 2.5
square of 2.5 is 6.2500000
CALL BY VALUE
In the preceding examples we have seen that whenever we called a function we have
always
passed the values of variables to the called function. Such function calls are called
‘calls by
value’ by this what it meant is that on calling a function we are passing values of
variables to it.
The example of call by value are shown below ;
sum = calsum (a, b, c);
f = factr (a);
In this method the value of each of the actual arguments in the calling function is
copied into
corresponding formal arguments of the called function. With this method the changes
made
to the formal arguments in the called function have no effect on the values of actual
argument
________________________________________________________________________________
_
22 of 22
DATA POINT
CALL BY REFERENCE
In the second method the addresses of actual arguments in the calling function are
copied in
to formal arguments of the called function. This means that using these addresses we
would
have an access to the actual arguments and hence we would be able to manipulate
them the
following program illustrates this.
main ( )
{
int a = 10, b =20,
swapr (&a, &b);
printf (“\n a = %d b= %d”, a, b);
}
swapr (int *x, int * y)
{
int t;
t = *x
*x = *y;
*y = t;
}
The output of the above program would be
a = 20 b =10
EXERCISE
Q.1 Each of the following is the first live of a function definition explain the meaning of
each
(a) float f (float a, float b) (c) Void f (int a)
(b) long f ( long a ) (d) char f (void)
Q.2 Write a function that will calculate and display the real roots of the quadratic
equation:
ax 2 + bx + c = 0 using the quadratic formula
x=
2a
- b ± b - 4ac 2
Assume that a, b and c are floating - point arguments where values are given and that
x1 and x2 are floating point variables. Also assume that b2 > 4 * a * c , so that the
calculated roots will always be real.
Q.3 Write a function that will allow a floating - point number to be raised to an integer
power. In other words, we wish to evaluate the formula
y = Xn
where y and x are floating - point variables and n is an integer variable.
For string handling C provides a standard set of library functions. Though there exists
many
such functions four of them will be discussed here.
________________________________________________________________________________
_
23 of 23
DATA POINT
Chapter 7
STANDARD LIBRARY STRING FUNCTIONS
The strcmp( ) Function
This function is used to check whether two strings are same or not. If both the strings
are
same it return a 0 or else it returns the numeric difference between the ASCII values of
nonmatching characters e.q. the following program
# include <stdio.h>
main( )
{
char string1 [ ] = “orange”;
char string2 [ ] = “banana”;
printf(“%d\n”, strcmp(string1, string2));
printf(“%d\n”, strcmp(string2, “banana”);
printf(“%d”, strcmp(string1, “Orange”));
getch( );
}
output
13
0
32
In the first printf statement we use the strcmp( ) function with string1 and string2 as it
arguments.
As both are not equal (same) the function strcmp( ) returned 13, which is the numeric
difference between “orange” and “banana” ie, between string2 and b.
In the second printf statement the arguments to strcmp() are string2 and “banana”. As
string2
represents “banana”, it will obviously return a 0.
In the third printf statement strcmp( ) has its arguments “orange” and “Orange”
because
string1 represents “Orange”. Again a non-zero value is returned as “orange” and
“Orange”
are not equal.
strcpy( ) Function
The function copies one string to another for e.g. the following program
# include <stdio.h>
main( )
{
char source [ ] = “orange”;
char target [20];
strcpy(target, source);
clrscr( );
printf(“source: %s\n”, source);
printf(“target:%s”, target);
getch( );
}
output will be
source : orange
target : orange
strcat( )
This function concatenates the source string at the end of the target string for e.g,
“Bombay”
and “Nagpur” on concatenation would result in to a string “Bombay Nagpur”. Here is
an example
of strcat( ) at work.
main( )
{
char source [ ] = “Folks”;
char target [30] = “Hello”;
strcat(target, source);
printf(“\n source string = %s”, source);
printf(“\n target string = %s”, target);
________________________________________________________________________________
_
24 of 24
DATA POINT
}
And here is the output
source string = folks
target string = Hello folks
strlen( )
This function counts the number of characters present in a string. Its usage is
illustrated in the
following program.
main( )
{
char arr[ ] = “Bamboozled”
int len1, len 2;
len1 = strlen(arr);
len2 = strlen(“Hunpty Dumpty”);
printf(“\n string = %s length = %d”, arr, len1);
printf(“\n string = %s length = %d”, “Humpty Dumpty”, len2); }
The output would be
string = Bamboozled length=10
string = Humpty Dumpty length = 13
while calculating the length of the string it does not count ‘\0’.
EXERCISE
Q.1 Write a function xstrstr ( ) that will return the position where one string is present
within
another string. If the second string doesn’t occur in the first string xstrstr ( ) should
return a0.
For example in the string “ some where over the rainbow”, “over” is present at
position”
Q.2 Write a program to encode the following strings such that it gets convert into an
unrecognisable from. Also write a decode function to get back the original string.
Chapter 8
DYNAMIC DATA STRUCTURES IN C
POINTERS
THE & and * Operators
A pointer is a variable that represents the location of a data item, such as a variable or
an array element. Pointers are used frequently in C, as they have a number of useful
applications. For example, pointers can be used to pass information back and forth
between a function and its reference point. Pointers provide a way to return multiple
data items from a function via function arguments to be specified as arguments to a
given function. Pointers are also closely associated with arrays and therefore provide
an alternate way to access individual array elements. Within the computer’s memory,
every stored data item occupies one or more adjacent memory cells. The number of
memory cells required to store a data item depends on the type of data item. For
example, a single character will be stored in 1 byte of memory integer usually requires
two adjacent bytes, a floating point number may require four adjacent bytes. Suppose
V is a variable that represents some particular data item. The compiler will
automatically assign memory cells for this data item. The data item can be accessed if
we know the location of the first memory cell. The address of V’s memory location can
be determined by the expression &V, where & is a unary operator, called the address
operator, that evaluates the address of its operand.
This new variable is called a pointer to V, since it “Points” to the location where V is
stored in
memory. Remember, however, that PV represents V’s address, not its value. Thus, PV
is
called pointer variable.
address of V value of V
PV V
Relationship between PV and V (where PV = &V and V = *PV)
The data item represented by V can be accessed by the expression *PV where * is a
unary operator, that operates only on a pointer variable. Therefore, PV and V both
________________________________________________________________________________
_
25 of 25
DATA POINT
represent the same data item. Furthermore, if we write PV = &V and U = PV, then U
and V will both represent the same values i.e., the value of V will indirectly be assigned
to U.
Example :
int quantity = 179 ;
The statement instructs the system to find a location for the integer quantity and puts
the
value 179 in that location. Let us reassume that the system has chosen the address
location
5000 for quantity.
Quantity
179
5000
Variable
Value
Address
Representation of a variable
Remember, since a pointer is a variable, its value is also stored in the memory in
another
location.
The address of P can be assumed to be 5048.
Variable
Quantity
P
Value
179
5000
Address
5000
5048
Pointer as a variable
This tells the compiler three things about the variable Pt_name.
1. The * tells that the variable Pt_name is a pointer variable.
2. Pt_name needs a memory location.
3. Pt_name ponts to a variable of type data type.
Example : int * P ;
Declares the variable P as a pointer variable that points to an integer data type.
float * y ;
declares y as a pointer to a floating point variable.
Once pointer variable has been declared, it can be made to point to a variable using an
assignment statement such as
P = & quantity ;
which causes P to point to quantity. P contains the address of quantity. This is known
as
pointer initialization.
Pointer expressions
Like other variables, pointer variables can be used in expressions. For example, if P1
and P2
are properly declared and initialized pointers, then the following statements are valid.
1) Y = * P1 ;
2) Sum = Sum + * P1 ;
3) Z = S - * P2 / * P1 ;
4) * P2 = * P2 + 10 ;
Note that there is a blank space between / and * in the item 3 above.
If P1 and P2 are pointers then the expressions such as,
P1 + 4 , P2 - 2 , P1 - P2 , P1 ++ , — P2 are allowed
also,
Sum =Sum + *P2 ;
________________________________________________________________________________
_
26 of 26
DATA POINT
P1 ++ ;
- -P2 ;
P1 > P2
P1 = = P2
P1 ! = P2
are all allowed expressions.
The expressions such as,
P1 / P2 or P1 * P2 or P1/3
are not allowed.
Pointer assignments
After declaring a pointer, pointer is assigned a value, so that it can point to a particular
variable.
eg. int * P ;
int i ;
P=&i;
This is called assignment expression in which pointer variable P is holding the address
of i.
Pointer arithmetic
Two pointer values can be added, multiplied, divided or subtracted together.
eg. if int i ;
int j ;
int * P , * q ;
i = 5 , j = 10 ;
Now, various pointer arithmetic can be performed
eg. * j = * i + * j ;
The value of variable j is changed from 10 to 15.
*j=*j-*i;
The value of variable j is changed from 10 to 5.
* i = * i ** j ;
The value of i is changed from 5 to 50 ;
Consider another example,
if there is array and a pointer is pointing to it
int i [10] ;
int * P ;
P=i;
Now, arithmetic operations like
P=P+4;
Will move the pointer P from the starting address of the array to the fourth subscript of
array.
Similarly, if P1 and P2 are both pointers to the same array, then P2 - P1 gives the
number of
elements between P1 and P2.
arithmetic operations like
P1/P2 or P1 x P2 or P/3 are not allowed.
Pointer Comparison
In addition to arithmetic operations, pointers can also be compared using the relational
operators.
The expressions such as
P1 > P2 , P1 = = P2 , P1 ! = P2 are allowed.
However, any comparison of pointers that refer to separate and unrelated variables
make no
sense. Comparisons can be used meaningfully in handling arrays and strings.
________________________________________________________________________________
_
27 of 27
DATA POINT
Example :
Write a program that uses a table of integers whose size will be specified interactively
at run time.
Program -
# include <stdio. h>
# include <stdlib.h>
________________________________________________________________________________
_
28 of 28
DATA POINT
# define NULL O
main ( )
{
int * P, * table ;
int size ;
printf ( “\n What is the sizeof table ? “ ) ;
scanf ( “ % d”, &size ) ;
printf ( “\n” ) ;
if (( table = (int * ) malloc (size * sizeof (int)) = = NULL )
{
printf (“No space available \ n”) ;
exit ( 1) ;
}
printf (“\n address of the first byte is % u\n”, table );
printf(“\n Input table values”);
for ( P = table; P < table + size; P++ )
scanf (“%d”, *P );
for ( P = table + size - 1; P > = table; P- - )
printf (“%d is stored at address %u\n”, *P, P );
}
static int X [ 6 ] = { 1, 2, 3, 4, 5, 6 } ;
________________________________________________________________________________
_
29 of 29
DATA POINT
Suppose the base address of X is 1000 and assuming that each integer requires two
bytes,
the five elements will be stored as follows :
ELEMENTS x [0] x[1] x[2] x[3] x[4] x[5]
VALUE 1 2 3 4 5 6
Address 1000 1002 1004 1006 1008 1010
BASE ADDRESS
The name X is defined as a constant pointer pointing to the first clement,
x[0] and therefore the value of X is 1000, the location whose X[0] is stored. That is ;
X = & x[0] = 1000
If we declare P as an integer pointer, then we can make the pointer P to point to the
array X
by the following assignment :
P=X;
This is equivalent to P = & X[0] ;
Now we can access every value of x using P++ to move from one element to another.
The
relationship between P and X is shown below :
P = & x[0] ( = 1000)
P+1 = & x[1] ( = 1002)
P+2 = & x[2] ( = 1004)
P+3 = & x[3] ( = 1006)
P+4 = & x[4] ( = 1008)
P+5 = & x[5] ( = 1010)
The address of an element is calculated using its index and the scale factor of the data
type.
For instance,
address of X[3] = base address + (3 x Scale factor of int)
= 1000 + (3 x 2) = 1006
When handling array, instead of using array indexing, we can use pointers to access
array elements. Note that *(x+3) gives the value of X[3]. The pointer accessing method
is more faster than array indexing.
POINTERS TO FUNCTIONS
________________________________________________________________________________
_
30 of 30
DATA POINT
type ( * fp) ( ) ;
This tells the compiler that fp is a pointer to a function which returns type value. We
can make a function pointer to point to a specific function by simply assigning the
name of the function to the pointer.
For example,
double (*P1)( ), mul ( ) ;
P1 = mul ;
declare P1 as a pointer to a function and mul as a function and then make P1 to point
to the function mul. To call the function mul, we may now use the pointer P1 with the
list of parameters.
That is,
(*P1) (x,y)
is equivalent to mul ( x,y )
va_list ptr ;
va_start ( ptr, tot_num ) ;
max = va_arg (ptr, int ) ;
for ( count = 1 ; count < tot_num ; count + + )
{
num = va_arg (ptr, int ) ;
if ( num > max )
max = num ;
}
return ( max ) ;
}
Here we are making two calls to findmax( ) first time to find maximum out of 5 values
and second time to find maximum out of 3 values. Note that for each call the first
argument is the count of arguments that are being passed after the first argument. The
value of the first argument passed to findmax ( ) is collected in the variable tot_num
findmax( ) begins with a declaration of pointer ptr of the type va_list. Observe the next
statement carefully
va_start ( ptr, tot_num ) ;
This statements sets up ptr such that it points to the first variable argument in the list.
If we are considering the first call to findmax ( ) ptr would now point to 23. The next
statement max = va_arg ( ptr, int ) would assign the integer being pointed to by ptr to
max. Thus 23 would be assigned to max, and ptr will point to the next argument i.e.
15.
POINTERS TO POINTERS
The concept of pointers can be further extended. Pointer we know is a variable which
contains address of another variable. Now this variable itself could be another pointer.
These we now have a pointer which contains another pointer’s address. The following
example should make this point clear.
main ()
{
int i = 3 ;
int * j ;
int * * k ;
j=&i;
k=&j;
printf (“\n address of i = % \d”, & i );
printf (“\n address of i = % \d”, j );
printf (“\n address of i = % \d”, * k );
printf (“\n address of j = % \d”, & j );
printf (“\n address of j = % \d”, k );
printf (“\n address of k = % \d”, & k );
printf (“\n address of k = % \d”, &k );
}
In this program i is an integer type value, j is a pointer to this variable and k is another
pointer
type variable pointing to j.
ijk
3 6485 3276
6485 3276 7234
All the addresses are assumed addresses K is pointing to the variable j. These K is a
pointer to pointer. In principle, there could be a pointer to a pointer’s pointer, of a
pointer to a pointer to a pointer’s pointer. There is no limit on how far can we go on
extending this definition.
ARRAY OF POINTERS
The way there can be an array of ints or an array of floats, similarly there can be an
array of pointers. Since a pointer variable always contain an address, an array of
pointers would be nothing but collection of addresses. The addresses present in the
array of pointers can be addresses of isolated variables or addresses of array elements
or any other addresses. All rules that apply to an ordinary array apply to the array of
pointers as well.
eg. main ( )
{
int * arra [ 4 ];
________________________________________________________________________________
_
32 of 32
DATA POINT
EXERCISE
Q.1 What is the purpose of an automatic variable? What is its scope?
Q.2 A C program contains the following declaration:
static char * color[6] { “red”, “green”, “blue”, “white”, “black”, “yellow”}
(a) What is the meaning of color?
(b) What is the meaning of (color + 2)?
(c) What is the value of * color ?
(d) What is the value of * (color +2)?
Chapter 9
STRUCTURES AND UNION
Introduction
A structure is a convenient tool for handling a group of logically related data items.
Structure help to organize complex data in a more meaningful way. It is powerful
concept that we may after need to use in our program Design. A structure is
combination of different data types using the & operator, the beginning address of
structure can be determined. This is variable is of type structure, then & variable
represent the starting address of that variable.
structure Definition
A structure definition creates a format that may be used to declare structure variables
consider
the following example.
struct book_bank
{
char title [20];
char author [15];
int pages;
float price; };
Here keyword struct hold the details of four fields these fields are title, author, pages,
and price, these fields are called structure elements. Each element may belong to
different types of data. Here book_bank is the name of the structure and is called the
structure tag. It simply describes as shown below.
struct book-bank
Title array of 20 characters
Author array of 15 characters
Pages integer
Price float
________________________________________________________________________________
_
33 of 33
DATA POINT
# include <stdio.h>
main( )
{
struct person
{
char name [25];
char age;
};
struct person sample[5];
int index;
char into[8];
for( index = 0; index <5; index ++)
{
print(“Enter name;”);
gets(sample [index]. name);
printf(“%age;”);
gets(info);
sample [index]. age = atoi (info);
}
for (index = 0; index <5; index++)
{
printf(“name = %5\n”, sample [index]. name);
printf(“Age = %d \n”, sample [index]. age);
getch( );
}
}
The structure type person is having 2 elements:
Name is an array of 25 characters and character type variable age
struct salary
{
char name[20];
char department[10];
int basic_pay;
int dearness_allowance;
int city_allowance;
} employee;
________________________________________________________________________________
_
34 of 34
DATA POINT
This structure defines name, department, basic pay and 3 kinds of allowance. we can
group
all the items related to allowance together and declare them under a substructure as
shown
below:
struct salary
{
char name [20];
char department[10];
struct
{
int dearness;
int hous_rent;
int city;
}
allowance;
}
employee;
The salary structure contains a member named allowance which itself is a structure
with 3 members. The members contained in the inner, structure namely dearness,
hous_rent, and city can be referred to as :
# include <stdio.h>
typedef struct
{
char *name;
int acc_no;
char acc_types;
float balance;
} account;
main( )
{
void change(account *pt);
static account person = {“chetan”, 4323, ‘R’, 12.45};
printf(“%s %d %c %2.f \n”, person. name,
person.acc_type, person. acc_type,
person. balance);
change(&person);
printf(“%s %d %c %2.f \n”, person.name, person.acc_type,
person.acc-type, person. balance);
getch( );
}
void change(account *pt)
{
pt - > name =” Rohit R”;
pt - > acc_no = 1111;
pt - > acc_type = ‘c’;
pt - > balance = 44.12;
}
output
chetan 4323 R 12.45
Rohit R 1111 c 44.12
________________________________________________________________________________
_
35 of 35
DATA POINT
UNIONS
Unions, like structure contain members, whose individual data types may vary. This is a
is major distinction between them in terms of storage .In structures each member has
its own storage location, where as all the members of a union use the same location.
Like structures, a union can be declared using the keyword union is follows:
union item
{
int m;
float x;
char c;
} code;
This declares a variable code of type union item. The union contains item members,
each with a different date type. However, we can use only one of them at a time. This
is due to the fact that only one location is allocated for a union variable, irrespective of
its size
________________________________________________________________________________
_
36 of 36
DATA POINT
Storage 4 bytes
1000
c
m
x
1001 1002 1003
The compiler allocates a piece of storage that is large enough to hold the largest
variable type in the union. In the declaration above, the member x requires 4 bytes
which is the largest among the members. The above figure shown how all the three
variables share the same address, this assumes that a float variable requires 4 bytes
of storage.
To access a union member, we can use the same syntax that we as for structure
members,
that is,
code. m
code. x
code. c are all valid
When accessing member variables, we should make sure that we are accessing the
member
whose value is currently in storage. For example
code. m = 565;
code. x = 783.65;
printf(“%d”, code. m); would produce erroneous output.
# include <stdio.h>
main( )
{
union
{
int one;
char two;
} val;
val. one = 300;
printf(“val. one = %d \n”, val. one);
printf(“val. two = %d \n”, val. two); }
The format of union is similar to structure, with the only difference in the keyword
used. The above example, we have 2 members int one and char two we have then
initialised the member ‘one’ to 300. Here we have initialised only one member of the
union. Using two printf statements, then we are displaying the individual members of
the union val as: val. one = 300
val. two = 44 As we have not initialised the char variable two, the second printf
statement will give a random value of 44.
The general formats of a union thus, can be shown as.
union tag
{ member 1;
member 2;
---
-- -
member m;
};
EXERCISE
Q.1 struct book_bank
________________________________________________________________________________
_
37 of 37
DATA POINT
{
char title[20];
char author[15];
int pages;
float price;
};
The above structure requires __________ bytes of memory.
________________________________________________________________________________
_
38 of 38
DATA POINT
Chapter 10
DISK I/O FUNCTIONS
scanf( ), printf( ), getch ( ) etc which we have studied were console I/O functions. Let us
now turn our attention to disk I/O. Disk I/O operations are performed on entities called
files. The brief categorisation of Disk I/O functions is given below
Opening a file
We make the following declaration before opening a file
FILE * fp
# include <stdio.h>
main( )
{
FILE *fp;
fp = fopen(“PRI.C”, “r”);
________________________________________________________________________________
_
39 of 39
DATA POINT
if (fp= = NULL)
{ puts (“ cannot open file”);
exit( );
}
}
Closing the file
The closing of the file is done by fclose( ) through the statement,
fclose ( fp );
it (ft = =NULL)
{
puts(“canot open target file”);
fclose(fs);
exit( );
}
while (1)
{
ch = fgetc(fs);
if(ch = = EOF)
break;
else
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
}
fseek function
fseek function is used to move the file position to a desired location within the file. It
takes the
following form:
fseek(file ptr, offset, position)
File ptr is a pointer to the file concerned, offset is a number variable of type long and
position
is an integer number. The offset specifics the number of positions(bytes) to the moved
from
the location specified by position.
The position can take one of the following three values
Values Meaning
0 Beginning of file
1 Current position
2 End of file
offset may be positive meaning move forwards or negative meaning move backwards.
The
following examples illustrate the operation of the fseek function:
tatement Meaning
fseek(fp,0L,0) Go to beginning
fseek(fp, 0L, 1) Stays at current position
fseek(fp, 0L, 2) Go to end of the file, past the last character of the file
fseek(fp, m, 0) Move to (m+1)th byte in the file
fseek(fp, m, 1) Go forwared by m bytes
fseek(fp, -m, 1) Go backward by m bytes from the current position
fseek(fp, - m, 2) Go backward by m bytes from the end
ftell
ftell takes a file pointer and returns a number of type long that corresponds to the
current
________________________________________________________________________________
_
41 of 41
DATA POINT
position. This function is useful in saving the current position of a file, which can be
used later
in the program. It takes the following form
n = ftell(fp);
n would give the relative offset(in bytes) of the current position. This means that n
bytes have
already been read (or written).
rewind takes a file pointer and resets the position to the start of the file.
for e.g.
rewind(fp);
n = ftell(fp);
n would return 0
Binary Mode
A file can be opened in binary mode as follows:
fp = fopen(“Poem. txt”, “rb”)
Here fp is file pointer
Poem. txt is file name
rb denotes that file is opened in binary mode for read operation.
Text Mode:-
The only function available for storing in a disk file is the fprintf( ) in text mode. Here
numbers are stored as string of characters when written to the disk. These 1234, even
though it occupies two bytes in memory, when transferred to the disk using fprintf( ), it
would occupy four bytes, one byte per character. Similarly the floating point number
1234.56 would occupy 7 bytes on disk. These, numbers with more digits would require
more disk space. In binary by using the functions (fread( ) and fwrite( )) numbers are
stored in binary format. It means each number would occupy the same number of
bytes on disk as it occupies in memory.
________________________________________________________________________________
_
42 of 42
DATA POINT
}
suppose we now execute the program by typing VK COPY Hellow.C Hello.CBK Here argc
=
3(command plus 2 arguments
argv[0] points to “C:\VKCPY\0”
argv[1] points to “HELLO.C\0”
argv[2] points to “HELLO.CBK\0”
argv[3] is NULL
________________________________________________________________________________
_
43 of 43