Você está na página 1de 38

1

Language: A language is a communication media


between two parties.
Programming languages: A programming language is a
language used for writing programs to direct processing
steps to be carried out by a computer.
Programming languages are classified into 3 categories
1. Low level language (or) Machine Language
(or)
Binary language.
2. Middle level language (or) Assembly language
3. High level language.
1. Machine Language:
Although computers can be programmed to understand
many different computer languages, there is only one
language understood by the computer without using a
translation program. This language is called the machine
language or the machine code of the computer. Machine
code is the fundamental language of a computer and is
normally written as strings of binary 1s and 0s. The
circuitry of a computer is wired in such a way that it
immediately recognizes the machine language and converts
it into the electrical signals needed to run the computer.
Programs written in machine language can be executed
very fast by the computer. This is mainly because the CPU
directly understands machine instructions and no
translation of the program is required. However, writing a
program in machine language has several disadvantages.
They are
1. Machine Dependent.
2. Difficult to program

3. Error prone
4. Difficulty to modify
2. Assembly language:
One of the first steps in improving the program preparation
process was to substitute letter symbols-mnemonics for the
numeric operation codes of machine language. A mnemonic
(or memory aid) is any kind of mental trick we use to help
us remember. Mnemonics come in various shapes and
sizes, all of them useful in their own way. All computers
have the power of handling letters as well as numbers.
Hence, a computer can be taught to recognize certain
combination of letters or numbers. In this way, the
computer can be trained to translate a program written with
symbols instead of numbers into the computers own
machine language. Then we can write program for the
computer using symbols instead of numbers, and have the
computer do its own translating. This makes it easier for
the programmer, because he can use letters, symbols, and
mnemonics instead of numbers for writing his programs.
The language, which substitutes letters and symbols
for the numbers in the machine language program, is called
an assembly language or symbolic language. The translator
program that translates an assembly code into the
computers machine code is called assembler. The
assembler is a system program, which is supplied by the
computer manufacturer.
Advantages of Assembly language:
1. Easier to understand and use

2. Easy to locate and correct errors


3. Easier to modify
4. No worry about addresses
5. Easily re locatable.
Limitations of assembly language
1. Machine dependent
2. Knowledge of hardware required.
3. Machine level coding.
High level languages:
While writing programs in any of the above languages, a
programmer has to remember all the operation codes of the
computer and now in detail what each code does and how it
affects the various registers of the computers. High-level
languages, instead of being machine based, are oriented
more towards the problem to be solved. These languages
enable the programmer to write instructions using English
words and familiar mathematical symbols. So it becomes
easier for his to concentrate on the logic of his problem
rather than getting involved in programming details. Every
instruction, which the programmer writes in a high-level
language, is translated into many machine language
instructions. This is one-to-many translation and not oneto-one as in the case of assembly language.
Compiler: A compiler is software that translates a
program from high level language to machine language.
Interpreter: An interpreter is software that translates a
program from high level language to machine language.
Differences between Compiler and interpreter.
Compiler
Interpreter
1. Compiler converts all
1. Interpreter converts line

statements at a time, if any


errors are there, it displays
all errors as a list.
2. After Compiling the
whole program, if the
program is error free then it
will execute the program.
3. After Compilation, it
creates an executable file,
using that executable file we
can run the program any
number of times.
4. It is fast.

by line, at the time of


interpreting any error is
there, it displays that error.
2. After interpreting the first
line, if it is error free then it
will execute that line.
3. It does not create any
executable file, every time
we need to interpret the
program.
4. It is slow.

Introduction:
==========

C is a programming language developed at AT & Ts Bell


Laboratories of USA in 1972. It was designed and written
by a man named Dennis Ritche.
Features of C
1. Portability or machine independent
2. Sound and versatile language
3. Fast program execution.
4. An extendible language.
5. Tends to be a structured language.
6. It allows recursion.

Historical developments of C:
Year
1960
1963

Language Developed by
ALGOL International
committee
CPL
Cambridge University

Remarks
Too general, too
abstract
Hard to learn,
difficult to
implement
Martin Richards at
Could deal with
Cambridge university only specific
problems
Ken Thompson at AT Could deal with
&T
only specific
problems
Dennis Ritche at AT & Lost generality
T
of BCPL and B
restored

1967

BCPL

1970

1972

Character set of the C language:


This indicates group of characters which are useful to write
the programming statements. It contains 3 parts.
1. alphabets: C language supports upper case letters(A to
Z), lower case letters from (a to z). But both are
different. C is a case sensitive language.

2. Numeric digits: C language contains the numeric


digits from 0 to 9.
3. Special Characters: The characters other than
alphabets and numeric digits are called as special
characters.
Special Character Name
+
Plus
Minus
*
Asterisk
/
Slash/ forward slash
%
Modulator (percentage)
<
Less than
>
Greater than
=
Equal to
.
Full stop
,
Comma
;
Semi colon
:
Colon

Single quotation

Double quotation
?
Question mark
$
Dollar
!
Exclamatory
|
Pipe or Bar
^
Cap
&
Ampersand
(
Left parenthesis
)
Right parenthesis
[
Open square bracket
]
Close square bracket

{
}
\

Open brace
Close brace
Back slash or Reverse slash

White space characters:


These are also called as escape sequence characters. These
are the controlling characters. These characters should start
with \ , then followed by a single character.
White space
character
name
\a
\n
\t
\b
\r
\
\
\\
\?

Usage
Bell sound or beep sound
New line
Tab space
Back space
Carriage return
Single quotation
Double quotation
For back slash
For question mark

Identifiers: It identifies a value in the memory.


Constants : It is a quantity which does not change its value
during the program execution. Constants are 4 types.
1. integer constants: These are the numbers without
decimal point or exponent.
Ex: int a=10;

2. float constants: These are the numbers with decimal


point or exponent.
Ex: float pie=3.14;
3. single character constants: It is any one of the single
character enclosed in single quotation.
Ex: char ch=a;
char section=b;
4. String constants: Group of characters enclosed with
in double quotations.
ex: char name[20]=chandra ;
char nation[10]=India;
Variables: it is a value which changes its value during
program execution. Variables are 4 types.
1.integer variables
2.float variables
3.single character variables
4.string variables.
Rules to write variable names:
1. A variable name contains maximum of 30 characters.
2. A variable name includes alphabets and numbers, but
it must start with an alphabet.
3. It cannot accept any special characters, blank spaces
except under score( _ ).
4. It should not be a reserved word.
Reserved words (or) key words

Keywords are the words whose meaning has already


been explained to the C compiler. The keywords cannot be
used as variable names. They are
auto
continue
enum
if
short
switch
volatile

break
default
extern
int
signed
typedef
while

case
do
float
long
sizeof
union

char
double
for
register
static
unsigned

const
else
goto
return
struct
void

Operators:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment or decrement operators
6. Conditional operators
1.arithmetic operators
Arithmetic operators are used to perform arithmetic
operations like addition, subtraction, multiplication,
division and remainder.
Operator
+
*
/
%

Name
Plus
Minus
Asterisk
Slash
Modulator

Meaning
Addition
Subtraction
Multiplication
Division
Remainder

Example
a+b
a-b
a*b
a/b
a%b

10

Ex:- a=10, b=3


a+b = 10+3 =13
a-b = 10 - 3 = 7
a*b = 10 * 3 =30
a/b= 10 / 3 = 3
a%b=10%3=1
b%a =3%10=3
2. Relational operators
Relational operators are used to check the relation
between two values. Using relational operators we write
relational expressions, a relational expression returns a
Boolean value either 1 or 0. 1 indicates true, 0 indicates
false.
a=10;
b=3;
Operator
<
>
<=
>=
==
!=

Name
Less than
Greater
than
Less than or
equal to
Greater
than or
equal to
Equal to
Not equal to

Example
a<b
a>b

Result
False or 0
True or 1

a<=b

False or 0

a>=b

True or 1

a= =b
a!=b

False or 0
True or 1

11

3.Logical operators: logical operators are used to


combine two or more relational expressions. C language
contains 3 logical operators.
Operator name
&&
and
||
or
!
not
&& : It is used to combine two or more relational
expressions, it returns true when all conditions are true,
other wise it returns false.
Truth Table:
Expression1 &&
expression2 result
T
T
T
T
F
F
F
T
F
F
F
F
|| : This operator is used to combine two or more
relational expressions, it returns true when any one of the
condition is true, else it returns false.
Truth table:
Expression1 || expression2 result
T
T
T
T
F
T
F
T
T
F
F
F

12

! (not): This is the negative operator, we can place the


not operator before any one of the condition. It returns
the opposite result of the condition. i.e. It converts true to
false and false to true.
Truth table:
! expression result
T
F
F
T
4. Assignment operator: C language contains equal to
(=) operator to assign a value or expression into a
variable.
Ex: a=10;
a=a+10;
C language has the following short cut assignment
operators.
+=
a=a+10 a+=10
-=
a=a-10 a-=10
*=
a=a*10 a*=10
/=
a=a/10 a/=10
%= a=a%10 a%=10
5. Increment and decrement operators:
1)++ increment operator
2) -- decrement operator
++: it is used to increment 1 to a variable.
a. pre increment( ++a)

13

ex:-

printf(%d,++a);

First the value will be incremented, and then the


new value will be printed.
2)post increment(a++)
ex:- printf(%d,a++);
First the value will be printed and then the value will
be incremented.
2) - - : It is used to decrement the value by 1.
a. pre decrement (--a)
b. post decrement (a--)
6. Conditional operators: C language contains
conditional operators(? , : ) to return a value from 2
values based on a condition.
Syn: condition ? value 1 : value 2.
If the condition is true then value1 will be returned, if the
condition is false then value2 will be returned
Ex:- a=40, b=20
Max=a>b ? a : b;
Max=40
A=40, b=20
Min=a<b ? a : b
Min contains 20
Data types:

14

Data type
signed char
unsigned char
short signed int
short unsigned
int
long signed int
long unsigned
int
Float
Double
long double

Range
-128 to +127
0 to 255
-32,768 to +32,767
0 to 65,535

Bytes
1
1
2
2

Format
%c
%c
%d
%u

-2147483648 to
+2147483647
0 to 4294967295

%ld

%lu

-3.4e38 to +3.4e38
-1.7e308 to +1.7e308
-1.7e4932 to 1.7e4932

4
8
10

%f
%lf
%Lf

structure of a C program
# include <header file name>
return type main( )
{
declaration part;
execution part;
}
A C program starts with the include statement. This
statement is used to link the header files into our program.
This statement should start with # symbol.
ex: # include <stdio.h>
stdio.h stands for standard input output . header file
A C program contains 1 or more functions from these
functions one function is the main( ).

15

ex: main( )
or
void main( )
The C compiler executes the program from the main().
main() function begins with { and ends with } . The
main() has declaration part and execution part. The
declaration part is used to define the variables and data
types.
Ex:int a,b,c;
float x,y,z;
char ch,name[20] ;
The remaining programming statements are called as
execution part. Each statement should be terminated with a
semicolon (;) . We have to write the C program in lower
case letters only.
output functions:printf( ): This function is used to display the information on
the screen. It displays the variable values and the string
values. This function belongs to stdio.h.
Syntax:printf(control string ,variable list);

16

Where control string is used to control the output.


It contains the conversion characters, white space
characters and the string. The conversion character
specifies the position and data type of a variable.
The compiler displays the common message as it
is on the screen. The variable list specifies the list of
variables to be displayed and they are separated by a
comma (,)
ex;-

printf(\n Welcome to C);


printf(\n
InterFace \n Informatics );
int a=10;
printf(\n The value of a is %d,a);
float pi=3.14;
printf(\n the value of pi is %f, pi);
char ch=y;
printf(The option is %c,ch);

char name[20]=InterFace ;
printf(\n The name is %s,name);
input functions:
scanf():- It is a input function, that is used to accept the
values into variables at the time of executing a program.
This function belongs to stdio.h .
syntax:- scanf(control string, variable list);

17

Where control string is used to control the input


format. It contains the conversion characters that specify
the data type of the variable. Where variable list specifies
one or more variables and they are separated by a comma.
Each variable should start with &, that indicates the
address of the variables. This symbol is not required for
string variables, because a string is a pointer to itself.
Example:int a;
scanf(%d, &a);
float b;
scanf(%f, &b);
char ch;
scanf(%c, &ch);
char name[20];
scanf(%s, name);
int a,b;
scanf(%d%d,&a,&b);
int a;
float b;
scanf(%d%f,&a,&b);
int a;
float b;
char ch,name[20]

18

scanf(%d%f%c%s, &a, &b, &ch, name);


Conditional statements:
if statement:
This statement is used to perform conditional
operations.
syntax1:
if(condition)
statement1;
statement2;
If the condition is true then statement1 will be executed.
If the condition is false then it will not execute the
statement1.
syntax 2:
if(condition)
{
statement1;
statement2;
}
If the condition is true then statement1 and statement2 will
be executed.
syntax 3:
if(condition)
statement1;
else
statement2;
If the condition is true then statement1 will be executed, if
the condition is false then statement2 will be executed.
syntax 4: if( condition)
{

19

statement 1;
statement 2;
}
else
{
statement 3;
statement 4;
}
If the condition is true then statement1 and statement2 will
be executed. If the condition is false then statement3 and
statement4 will be executed.
syntax 5:
Nested if
if(condition1)
Statement1;
else
if(condition2)
statement2;
else
statement3;
If the condition1 is true then statement1 will be executed
and the remaining statements will be skipped. If the
condition1 is false then it will check the condition2, if it is
true then statement2 will be executed. If both condition1
and condition2 are false then it will execute statement3.
Looping statements:
while statement:-

20

It is used to execute a set of statements repeatedly as long


as the given condition is true.
syntax:
while(condition)
{
statements; //body of while loop
}
First the condition will be tested, if it is true the body of the
while loop will be executed first time, again the control will
be transferred back to while loop. If it is false the control
will come out of the while loop.

for loop :
It is used to execute a set of statements repeatedly as long
as the given condition is true.
syntax:for(initial value ; test condition ; increment / decrement)
{
statements;
//body of for loop
}
When the for loop is executing for the very first time the
initial value will be stored in the given variable. Then the

21

condition will be tested. If it is true, the statements will be


executed. After executing the last statement the control
will return back to the for loop. Then the value will be
incremented / decremented. Again the condition is tested,
if it is false the control will come out of the for loop.
ex:for(i=1; i<=10; i++)
for( ;i<=10;i++)
for(; i<=10 ; )
for( ; ; ) //infinite loop
for(i=1,j=10;i<=10;i++,j--)
printf(\n%d %d,i,j);
for(i=1;i<=10;i++);

//body less for loop

do while loop:
This statement executes a set of statements as long as
the condition is true.
syntax: do
{
statements;
statements;
}while(condition);
This is similar to while loop, but the difference is this loop
will execute the statements at least once.
switch statement:-

22

It is used to execute one of the options from no. of


options. It is also called as multi branching statement.
switch(expression)
{
case value:
statements;
break;
case value:
statements;
default:
statements;
}
The expression value may be numeric or character
type. The switch statement evaluates the expression. It
will select one of the cases based on expression. It will
execute default statements when no case is selected.
break statement:
It is used to exit from a looping statement. We can
use this in for, while , do while, and switch statement.
ex:- break;
goto statement: It is used to transfer the control from one
place to another place in the program. It is also called as
unconditional jumping statement
syntax:
example:-

goto <label name> ;


goto print;

23

continue statement: It is used to transfer the control to the


beginning of the looping statements.
Ex: continue;
ARRAYS
An array is contiguous area in the memory referred by a
common name. The array allows the storage of a number
of data values in one variable. Each array element i.e. each
individual data item is referred by specifying the array
name followed by one or more subscripts. Each subscript
is enclosed with square brackets. Each subscript indicates
the position of the element in the array. Arrays are divided
into 2 types.
1) Single dimensional arrays. or One dimensional arrays
2) Multi dimensional arrays
1) One dimensional arrays:- The array which is using
only 1 subscript is known as one dimensional array. In One
dimensional array the elements are represented in single
row or single column.
Syntax:- Data_type variable_name[subscript]
ex:int a[10];
2) Two dimensional arrays: The array which is using
two subscripts is known as 2 dimensional array
Syntax:- Data_type variable_name[subscript][subscript]

24

ex:-

int a[2][2];

String functions:
1) strlen():- This function is used to return the length of a
string.
syntax:- strlen(string);
note:- When we are using string functions we have to
include a header file <string.h>
2) strcpy():- This function copies a string from one string
variable to another string variable.
syntax:- strcpy(target_string , source_string);
3) strcat(): (String concatenation)
This function adds a string at the end of another string
syntax:- strcat(string1,stirng2 );
4) strcmp(): (String comparison)
This compares one string with another string, if two strings
are equal this function returns 0, else it returns the numeric
difference between the non-matching characters.
Syntax:- strcmp(string1, string2);
5) stricmp(): This function is similar to strcmp, but this
function ignores the case sensitivity.
syntax:- stricmp(string1, string2);
6) strrev(): This function reverses the given string
Syntax:- strrev(string);

25

7) strupr(): This function converts the given string into


upper case(capitals)
syntax:- strupr(string)
8) strlwr(): This function converts the given string into
lower case.
syntax:- strlwr(string);
single character functions
1) toupper() :- This function converts a single character to
upper case.
syntax:- toupper(character);
2) tolower() :- This function converts a single character to
lower case.
syntax:- tolower(character);
note:- when we are using single character functions we
have to include a header file <ctype.h>
FUNCTIONS
A function is a set of statements which performs a specific
task. Functions are 2 types
1) standard functions or Library functions
2) User defined functions
1) Standard Functions:- These functions are ready made
functions available to the user.

26

Ex:- printf(), scanf(), pow(), strlen(), strcpy(), toupper(),

2) User defined functions:- These functions are created by


the user.
Ex:- address(),name(), add(a,b), area(r) , fact(n),
1. with return type with argument
Syn: return type function name (argument list);
Ex: int address(int);
2. with return type without argument
Syn : return type function name(no arguments);
Ex: int address(void);
3. without return type with argument
Syn: No return type function name (arguments );
Ex: void address (int);
4.without return type without argument
Syn: No return type function name(No arguments);
Ex: void address(void);

Uses of functions:
a. Dividing the task into sub tasks.
b. If a particular task is repeated number of times in
a program we can create a function and call that
function wherever we require.

27

c. Whenever a function is called in a statement the


control will be transferred to that called function.
After executing all statements it returns back to
the calling function.
d. The length of the source program will be reduced
by using functions at appropriate places.
e. A function can be used in other programs also.
Syntax of a function.
Return_type function_name(argument list)
{
declarations;
statements;
------------return value;
}
return statement:
It returns a value of the expression to the calling
function.
syntax:ex:-

return(expression)

return(0);
return(a);
return(a+b);
return a;
return;

28

POINTERS
A pointer is a variable which holds the address of
another variable. Since addresses are whole numbers
pointers would always contains whole numbers.
Consider the following declaration
int i=10;
The declaration tells the C compiler,
1) Reserve space in memory to hold the integer value.
2) Associate the name i with this memory location.
3) Store the value 10 at this location.
Memory map:i - location name
10 - Value at location
65524 location number or address.
Dynamic memory allocation:
malloc()
The name malloc stands for "memory allocation". The
function malloc() reserves a block of memory of specified size and
return a pointer of type void which can be casted into pointer of
any form.
Syntax of malloc()
ptr=(cast-type*)malloc(byte-size)

29

Here, ptr is pointer of cast-type. The malloc() function returns a


pointer to an area of memory with size of byte size. If the space is
insufficient, allocation fails and returns NULL pointer.
ptr=(int*)malloc(100*sizeof(int));
This statement will allocate either 200 or 400 according to size
of int 2 or 4 bytes respectively and the pointer points to the address
of first byte of memory.
calloc()
The name calloc stands for "contiguous allocation". The only
difference between malloc() and calloc() is that, malloc() allocates
single block of memory whereas calloc() allocates multiple blocks
of memory each of same size and sets all bytes to zero.
Syntax of calloc()
ptr=(cast-type*)calloc(n,element-size);
This statement will allocate contiguous space in memory for an
array of n elements. For example:
ptr=(float*)calloc(25,sizeof(float));
This statement allocates contiguous space in memory for an array
of 25 elements each of size of float, i.e, 4 bytes.
free()
Dynamically allocated memory with either calloc() or malloc()
does not get return on its own. The programmer must use free()
explicitly to release space.
syntax of free()
free(ptr);
This statement cause the space in memory pointer by ptr to be
deallocated.

30

realloc()
If the previously allocated memory is insufficient or more than
sufficient. Then, you can change memory size previously allocated
using realloc().
Syntax of realloc()
ptr=realloc(ptr,newsize);

]
STRUCTURES
Structure is a user defined data type. A structure is a
collection of variables that is referenced under a single
name. A structure contains a number of data types grouped
together. These data types may or may not be of the same
type.
Declaring a structure:struct <structure name>
{
structure element 1;
structure element 2;
--structure element n;
};
structure variables;
ex:struct student

31

{
int sno;
char name[20];
int total;
};
struct address a,b,c;
or
struct student
{
int sno;
char name[20];
int total;
}a,b,c;
Accessing structure elements:We can access the structure elements by using
structure_variable.structure_element;
ex:- a.sno, a.name, a.total
a.sno=5;
a.total=432;
STORAGE CLASSES
It tells us
1. Where the variable would be stored
2. What will be the initial value of the variable, if
the initial value is not specifically assigned?

32

3. What is the scope of the variable, i.e. in which


functions the value of the variable would be
available.
4. What is the life of the variable? i.e. how long
would the variable exist.
There are 4 storage classes in C
1) Automatic storage classes
2) register storage classes
3) static storage classes
4) external storage classes
1.automatic storage classes:1. storage - memory
2. default initial value - garbage value
3.scope - local to the block in which the variable is
defined
4.life - till the control remains with in the block in
which the variable is defined.
3. register storage classes
1. storage - CPU registers
2. default initial value - garbage value
3.scope - local to the block in which the variable is
defined
4.life - till the control remains with in the block in
which the variable is defined.

33

3.static storage classes


1. storage - memory
2. default initial value - 0
3.scope - local to the block in which the variable is
defined
4.life - value of the variable persists between
different function calls.
4. external storage classes
]1. storage - memory
2. default initial value - 0
3.scope - global
4.life - As long as the program execution does not
come to an end.
FILES
A file is a collection of information stored on a storage
device with a particular file name.
Uses of a file:1) Stores the data in the form of a file and we can retrieve it
whenever we require.
2) Using the data from the file in different programs.
Opening a file:- we can open a file by using
Syntax: - fopen(file name, opening mode);
Ex: FILE *fp;
fp=fopen(interface.txt,w);

fopen()

34

File opening modes:1)w :- Writing the data into a file. If the file already exists
its contents will be over written.
2)r:- Reading data from a file.
3)a:- Adds data into an existing file.(Appending)
4)w+ :- We can write data into a file and we can read data
from the file. If the file already exists its contents will be
over written, else creates a new file.
5)r+ :- Reading existing contents , writing new contents,
modifying existing contents of a file.
6)a+ :- Reading existing contents, appending new contents
at the end of a file.
closing a file:When the file processing is over, the file
must be closed. We can close a file by using fclose( ).
syntax:- fclose(file pointer);
ex:fclose(fp);
File functions:1)getc()
This function is used to read a character from a file.
syntax :- getc(file pointer);
ex:getc(fp);
2)putc()
This function is used to write a character into a file.
syntax:- putc(character, file pointer);
ex:putc(ch,fp);
3)fprintf():- This function writes formatted data into a file.
Syntax:- fprintf(file pointer, formatted string, list of
variables)
Ex:fp=fopen(student.dat,w);

35

fprintf (fp , %d %s %d,sno ,name, marks);


4)fscanf():- This function reads formatted data from a file.
Syntax:- fscanf(file pointer, formatted string, list of
variables)
Ex:fp=fopen(student.dat,r);
fscanf(fp , %d %s %d,&sno ,name,&marks);
COMMAND LINE ARGUMENTS.
C supports a feature that facilities the supply of
arguments to the main() function. These arguments are
supplied at the time of invoking the program. They are
typically used to pass the names of data files. For example
C:\>EXAM DATA RESULTS
Here exam is the name of the file contains the program
to be executed and data and results are the file names
passed to the program as command line arguments.
The command line arguments are typed by the user
and are delimited by a space. The first argument is always
the file name and contains the program to be executed. The
main() function which we have been using up to now
without any arguments can take two arguments as shown
below.
void main(int argc, char *argv[ ])

36

The first argument argc is known as argument counter


represents the number of arguments in the command line.
The second argument argv is known as argument vector is
an array of char type pointer that point to the command line
arguments. The size of this array will be equal to the value
of argc. For example
C:\>EXAM DATA RESULTS
The value of argc would be 3 and argv would be an
array of 3 pointers to strings as shown below
argv[0]--------- exam
argv[1]--------- data
argv[2]--------- results
Note that argv[0] always represents the command name
that invokes the program.
RANDOM ACCESS FILES (OR) BINARY FILES
ftell() :- This function tells us the current position of the
file in bytes.
Syntax:- ftell(file pointer)
ex:- n=ftell(fp);
2) rewind():- This function moves the record pointer to
the starting position of the file.
Syntax:- rewind(file pointer);
ex:rewind(fp);

37

3) fseek() :- This function moves record pointer to a


particular place in the file.
Syntax:- fseek(file pointer, offset , position);
if position is 0 i.e. from starting position (SEEK_SET)
if position is 1 i.e. from current position (SEEK_CUR)
if position is 2 i.e. from end position (SEEK_END)
offset:- long int type number or variable.
Position is the numeric value. It may be the
positive or negative. Offset may be numeric value or a
variable of long type. It indicates number of bytes to move
from position. If it is positive value to move forward,
negative value to move backward.
4) fwrite( ) This function is used to write the
information in a binary file.
Syntax: -

fwrite(address of struct variable, size of


struct variable, no. of blocks, file pointer);

ex: -

fwrite(&b, sizeof(b),1,fp);

5) fread()

This function is used to read the data from a


binary file.

Syntax:-

fread(address of struct variable, size of struct


variable , no. of blocks, file pointer);

38

ex:-

fread(&b,sizeof(b),1,fp);