Index: Rejoi - e by V.Sampurnakumar
Index: Rejoi - e by V.Sampurnakumar
Index: Rejoi - e by V.Sampurnakumar
SAMPURNAKUMAR
Page
Index
Rejoi-C-e
03
09
3. Pre-processor
11
4. Arrays
12
5. String Functions
14
6. Structures
15
7. I/O Function
17
8. Files
19
21
23
27
C Language Material:
Page 1
DO NOT COPY.
For THIS MATERIAL Call -9705551427
Rejoi-C-e
By V.SAMPURNAKUMAR
Page
e.
Compiling
Program
b. Design (Algorithm)
and
Running
the
c. Coding
g. Maintenance
Rejoi-C-e
By V.SAMPURNAKUMAR
Page
An algorithm must be: Definite, Finite, Precise and Effective and Implementation
independent ( only for problem not for programming languages).
Rejoi-C-e
By V.SAMPURNAKUMAR
Page
Rejoi-C-e
By V.SAMPURNAKUMAR
Page
Escape sequences are non printable characters, which begin with backward slash
and followed by one or more special characters. The frequently used escape
sequences are given below:
Horizontal tab (\t)
Form feed (\f)
Vertical tab (\v)
Back Space (\b)
Carriage return (\r)
Back Slash (\\)
New line (\n)
Null (\0)
20. Define a constant?
A constant is an entity that doesnt change. A constant in C refers to the fixed
values that do not change during the execution of a program.
There are two types of constants:
Symbolic constants and Constant variables, also called read-only variables.
21. How many types of constants are there? What are they?
There are mainly two types of constants
a. Primary (integer, float, char)
b. Secondary (array, pointer, structure, union)
22. Define the term Variable.
Variable is a named memory location. It can store one value at a time. Every
time new value is assigned, the old value is overwritten
23. Define datatype.
Data types are used to indicate the type of value represented or stored in a
variable, the number of bytes to be reserved in memory, the range of values that
can be represented in memory, and the type of operation that can be performed
on a particular data item.
24. Define the term key word. Why cant we use key word as
variables name?
Keywords are the words whose meaning has already been explained to the C
compiler (or in a broad sense to the computer). The keywords cannot 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.
25. What is an identifier?
Identifiers are names given to various programming elements such as variables,
constants, and functions. It should start with an alphabet, followed by the
combinations of alphabets and digits. No special character is allowed except
underscore (_). An Identifier can be of arbitrarily long. Some implementation of C
recognizes only the first eight characters and some other recognize first 32
characters.
Page 5
DO NOT COPY.
For THIS MATERIAL Call -9705551427
Rejoi-C-e
By V.SAMPURNAKUMAR
Page
26. How many no. of keywords are there in C language? What are the
different control structures available in C?
There are 32 keywords available in C language. The decision control statements
are: if statement, if-else statement and the conditional operator.
27. What are the different operators available in c?
The following are the different operators available in C:
Arithmetic operators
Assignment operators
Relational operators
Unary operators
Logical operators
Bitwise operators
Page 6
DO NOT COPY.
For THIS MATERIAL Call -9705551427
Type
Logical NOT
Arithmetic and modulus
Arithmetic
Relational
Relational
Logical AND
Logical OR
Assignment
PRE PROCESSOR
61. What is a pre processor?
It is a program that processes our source program before it is passed to the
compiler.
#include <goto.c>
}
void fun2 ( )
{
printf (\nInside fun2);
}
Output
:
Inside fun1
Inside main
Inside fun2
ARRAYS
68. What is the need for going an array?
If there are situations in which we would want to store more than one value at a
time in a single variable.
Ex. For example, suppose we wish to arrange the percentage marks obtained by
100 students in ascending order. In such a case we have two options to store
these marks in memory:
1. Construct 100 variables to store percentage marks obtained by 100 different
students, i.e. each variable containing one students marks.
2. Construct one variable (called array or subscripted variable) capable of storing
or holding all the hundred values.
Obviously, the second alternative is better. A simple reason for this is, it would be
much easier to
handle one variable than handling 100 different variables.
69. What is an array?
An array is a collection of similar elements. The first element in the array is
numbered 0, so the last element is 1 less than the size of the array. An array is
also known as a subscripted variable. Before using an array its type and
dimension must be declared.
70. Is there any boundary check available for arrays in c?
No, there is no boundary checking for arrays in c.
71. Discuss the call by value and call by reference with arrays.
In the call by value we pass values of array elements to the function, whereas in
the call by reference we pass addresses of array elements to the function.
72. How a pointer is associated to an array?
Ex.
Int a [10];
Int *p;
p=a;
p [1] is same as a [1]
Here in the above snippet p is pointer and it is pointing to an array called a; we
can access the whole array with the help of the pointer
STRING FUCTIONS
79. What is a string?
A string constant is a one-dimensional array of characters terminated by a null
(\0). For example,
char name [ ] = {H, 'A', 'E', 'S', 'L', 'E', 'R', '\0} ;
or
char name [] =UGY /* \0 is appended by c*/
The terminating null (\0) is important, because it is the only way the functions
that work with a string can know where the string ends.
80. What is the format specifier
The %s used in printf ( ) is a format specification for printing out a string. The
same specification can be used to receive a string from the keyboard.
81. What are the different pre defined string functions available in C?
Function
strlen
strlwr
strupr
strcat
strncat
Use
Finds length of a string
Converts a string to lowercase
Converts a string to uppercase
Appends one string at the end of another
Appends first n characters of a string at the end of
another
Copies a string into another
Copies first n characters of one string into another
Compares two strings
Compares first n characters of two strings
Compares two strings without regard to case ("i"
denotes that this function ignores case)
Compares two strings without regard to case
(identical to strcmpi)
Compares first n characters of two strings without
regard to case
Duplicates a string
Finds first occurrence of a given character in a string
Finds last occurrence of a given character in a string
Finds first occurrence of a given string in another
string
Sets all characters of string to a given character
Sets first n characters of a string to a given character
Reverses string
strcpy
strncpy
strcmp
strncmp
strcmpi
stricmp
strnicmp
strdup
strchr
strrchr
strstr
strset
strnset
strrev
STUCTURES
82. Why do we use structure?
A structure contains a number of data types grouped together. These data types
may or may not be of the same type.
83. Define structure.
Structure is collection of variables of different data type name under single
entity.
84. What are the terms associated with structure?
Example 1:
Struct emp
{
Int empno;
Char ename [20];
Float sal;
};
This is called structure definition. Memory is not allocated to the members when
the structure is defined.
Struct emp a; a is structure variable of tag emp;
Memory is allocated to the members when the structure is instantiated.
In the above structure the total memory allocated to it is 2+20+4 i.e. 26
Example 2:
We can instantiate a structure at the time of definition it self
Struct emp {
Int empno;
Char ename [20];
Float sal;
} a, b, c;
a, b, c are structure variables
Example 3:
See these examples to have a better idea about declarations
struct book
{
char name;
float price;
int pages;
};
struct book b1, b2, b3;
Is same as...
struct book
{
char name;
float price;
int pages;
} b1, b2, b3;
Or even...
struct {
char name;
float price;
int pages;
} b1, b2, b3;
INPUT/OUTPUT FUNCTION:
93. What are the different I/O functions available?
There are numerous library functions available for I/O. These can be classified
into three broad categories:
Console I/O functions
File
I/O functions
macro whereas the latter is a function. Here is a sample program that illustrates
the use of these functions.
99. What is union?
A "union declaration" specifies a set of variable values and, optionally, a tag
naming the union. The variable values are called "members" of the union and
can have different types. Unions are similar to "variant records" in other
languages.
A variable with union type stores one of the values defined by that type. The
same rules govern structure and union declarations. Unions can also have bit
fields.
Members of unions cannot have an incomplete type, type void, or function type.
Therefore members cannot be an instance of the union but can be pointers to
the union type being declared.
A union type declaration is a template only. Memory is not reserved until the
variable is declared.
If a union of two types is declared and one value is stored, but the union is
accessed with the other type, the results are unreliable. For example, a union of
float and int is declared. A float value is stored, but the program later accesses
the value as an int. In such a situation, the value would depend on the internal
storage of float values. The integer value would not be reliable.
100. What are the similarities between union and structure?
Structures and unions share the following characteristics:
Their members can be objects of any type, including other structures and
unions or arrays. A member can also consist of a bit field.
The only operators valid for use with entire structures and unions are the
simple assignment (=) and sizeof operators. In particular, structures and
unions cannot appear as operands of the equality ( == ), inequality (!=),
or cast operators. The two structures or unions in the assignment must
have the same members and member types.
FILES
102. How do you open a file?
Before we can read (or write) information from (to) a file on a disk we must open
the file. To open the file we have called the function fopen( ).
103. How do you read file content?
To read the files contents from memory there exists a function called fgetc( ).
ch = fgetc ( fp ) ;
fgetc( ) reads the character from the current pointer position, advances the
pointer position so that it now points to the next character, and returns the
character that is read, which we collected in the variable ch.
104. What does a fputc() do?
The fputc( ) function is similar to the putch( ) function, in the sense that both
output characters. However, putch( ) function always writes to the VDU, whereas,
fputc( ) writes to the file.
105. What are the different file opening modes?
Read(r), write(w), append(a) modes
And r+, w+, a+ we can do read, write, append with these three.
106. How do you close a file?
By using fclose() we can close a opened file.
107. What is binary file?
A binary file is merely a collection of bytes. This collection might be a compiled
version of a C program (say PR1.EXE), or music data stored in a wave file or a
picture stored in a graphic file. A very easy way to find out whether a file is a text
file or a binary file is to open that file in Turbo C/C++. If on opening the file you
can make out what is displayed then it is a text file, otherwise it is a binary file.
108. Why do use fprintf()?
The only function that is available for storing numbers in a disk file is the
fprintf( ) function.
Text and characters are stored one character per byte, as we would expect.
Numbers are stored as strings of characters.
109. Why do we go for a binary file?
If large amount of numerical data is to be stored in a disk file, using text mode
may turn out to be inefficient. The solution is to open the file in binary mode and
use those functions (fread( ) and fwrite( ) which are discussed later) which store
the numbers in binary format. It means each number would occupy same
number of bytes on disk as it occupies in memory.
110. Why fread and fwrite are more important?
Any database management application in C must make use of fread( ) and fwrite(
) functions, since they store numbers more efficiently, and make writing/reading
of structures quite easy. Note that even if the number of elements belonging to
the structure increases, the format of fread( ) and fwrite( ) remains same.
111. Explain rewind(),ftell(),fseek().
The rewind( ) function places the pointer to the beginning of the file, irrespective
of where it is present right now.
The fseek( ) function lets us move the pointer from one record to another.
fseek ( fp, -recsize, SEEK_CUR ) ;
If we wish to know where the pointer is positioned right now, we can use the
function ftell( ). It returns this position as a long int which is an offset from the
beginning of the file. The value returned by ftell( ) can be used in subsequent
calls to fseek( )
112. What are command line parameters?
The arguments that we pass on to main( ) at the command prompt are called
command line arguments. The function main( ) can have two arguments,
traditionally named as argc and argv. Out of these, argv is an array of pointers to
strings and argc is an int whose value is equal to the number of strings to which
argv points. When the program is executed, the strings on the command line are
passed to main.
113. What are the bitwise operators?
Operator
~
>>
<<
&
|
^
Meaning
Ones complement
Right shift
Left shift
Bitwise AND
Bitwise OR
Bitwise XOR(Exclusive
OR)
*/
Task
Malloc
Calloc
Free
Realloc
According to the conceptual view the program instructions and global and static
variable in a permanent storage area and local area variables are stored in
stacks. The memory space that is located between these two regions in available
for dynamic allocation during the execution of the program. The free memory
region is called the heap. The size of heap keeps changing when program is
executed due to creation and death of variables that are local for functions and
blocks. Therefore it is possible to encounter memory overflow during dynamic
allocation process. In such situations, the memory allocation functions
mentioned above will return a null pointer.
119. How to allocate a block of memory?
A block of memory may be allocated using the function malloc. The malloc
function reserves a block of memory of specified size and returns a pointer of
type void. This means that we can assign it to any type of pointer. It takes the
following form:
ptr=(cast-type*)malloc(byte-size);
ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an
area of memory with size byte-size.
Example: x=(int*)malloc(100*sizeof(int));
120. How to allocate multiple blocks of memory?
Calloc is another memory allocation function that is normally used to request
multiple blocks of storage each of the same size and then sets all bytes to zero.
The
general
form
of
calloc
is:
ptr=(cast-type*) calloc(n,elem-size);
The above statement allocates contiguous space for n blocks each size of
elements size bytes. All bytes are initialized to zero and a pointer to the first byte
of the allocated region is returned. If there is not enough space a null pointer is
returned.
121. How to release the used space?
Compile time storage of a variable is allocated and released by the system in
accordance with its storage class. With the dynamic runtime allocation, it is our
responsibility to release the space when it is not required. The release of storage
space becomes important when the storage is limited. When we no longer need
the data we stored in a block of memory and we do not intend to use that block
for storing any other information, we may release that block of memory for
future use, using the free function.
free(ptr);
ptr is a pointer that has been created by using malloc or calloc.
122. How to alter the size of allocated memory?
The memory allocated by using calloc or malloc might be insufficient or excess
sometimes in both the situations we can change the memory size already
allocated with the help of the function realloc. This process is called reallocation
of memory. The general statement of reallocation of memory is :
ptr=realloc(ptr,newsize);
This function allocates new memory space of size newsize to the pointer variable
ptr ans returns a pointer to the first byte of the memory block. The allocated new
block may be or may not be at the same region.
SORTING TECHNIQUES:
123. What is sorting algorithm?
A sorting algorithm is an algorithm that puts elements of a list in a certain order.
124. Name different types of sorting algorithms.
In this table, n is the number of records to be sorted. The columns "Average" and
"Worst" give the time complexity in each case, under the assumption that the
length of each key is constant, and that therefore all comparisons, swaps, and
other needed operations can proceed in constant time. "Memory" denotes the
amount of auxiliary storage needed beyond that used by the list itself, under the
same assumption. These are all comparison sorts.
Name
Average
Worst
Memory
Bubble sort
O(n2)
O(n2)
O(1)
Selection sort
O(n2)
O(n2)
O(1)
Inserting sort
O(n2)
O(n2)
O(1)
Merge sort
O(n logn)
O(n logn)
O(n)
Quick sort
O(n logn)
O(n2), O(log n)
Heap sort
O(n logn)
O(n logn)
O(1)
Let us take the array of numbers "5 1 4 2 8", and sort the array from
lowest number to greatest number using bubble sort algorithm. In each
step, elements written in bold are being compared.
First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ) Here, algorithm compares the first two
elements, and swaps them.
(15428) (14528)
(14528) (14258)
( 1 4 2 5 8 ) ( 1 4 2 5 8 ) Now, since these elements are already in
order, algorithm does not swap them.
Second Pass:
(14258) (14258)
(14258) (12458)
(12458) (12458)
(12458) (12458)
Now, the array is already sorted, but our algorithm does not know if it is
completed. Algorithm needs one whole pass without any swap to know it
is sorted.
Third Pass:
(12458) (12458)
(12458) (12458)
(12458) (12458)
(12458) (12458)
Finally, the array is sorted, and the algorithm can terminate.
Algorithm for bubble sort:
procedure bubbleSort( A : list of sortable items ) defined as:
for each i in 1 to length(A) do:
for each j in length(A) downto i + 1 do:
if A[ j -1 ] > A[ j ] then
swap( A[ j - 1], A[ j ] )
end if
end for
end for
end procedure
126. Explain Selection sort.
Selection sort is a simple sorting algorithm that improves on the performance of
bubble sort. It works by first finding the smallest element using a linear scan and
swapping it into the first position in the list, then finding the second smallest
element by scanning the remaining elements, and so on. Selection sort is unique
compared to almost any other algorithm in that its running time is not affected
by the prior ordering of the list: it performs the same number of operations
because of its simple structure. Selection sort requires (n - 1) swaps and hence
(n) memory writes. However, Selection sort requires (n - 1) + (n - 2) + ... + 2 +
1 = n(n - 1) / 2 = (n2) comparisons. Thus it can be very attractive if writes are
the most expensive operation, but otherwise selection sort will usually be
outperformed by insertion sort or the more complicated algorithms.
25
25
12
12
12
12
25
22
22
22
22
25
11
64
64
64
larger lists. This method is much more efficient than the bubble sort, though it
has more constraints.
Pseudo code of insertion sort:
insertionSort(array A)
for i = 1 to length[A]-1 do
begin
value = A[i]
j = i-1
while j >= 0 and A[j] > value do
begin
A[j + 1] = A[j]
j = j-1
end
A[j+1] = value
end
128. Explain Merge sort.
Merge sort takes advantage of the ease of merging already sorted lists
into a new sorted list. It starts by comparing every two elements (i.e., 1
with 2, then 3 with 4...) and swapping them if the first should come after
the second.
It then merges each of the resulting lists of two into lists of four, then
merges those lists of four, and so on; until at last two lists are merged into
the final sorted list.
Of the algorithms described here, this is the first that scales well to very
large lists, because its worst-case running time is O(n log n).
Example:
Once the data list has been made into a heap, the root node is
guaranteed to be the largest element. When it is removed and
placed at the end of the list, the heap is rearranged so the largest
element remaining moves to the root.
Using the heap, finding the next largest element takes O(log n)
time, instead of O(n) for a linear scan as in simple selection sort.
This allows Heap sort to run in O(n log n) time.