Académique Documents
Professionnel Documents
Culture Documents
BASICS
Four important aspects of any language are the way it stores data, the way it
operates upon this data, how it accomplishes input and output and how it lets
you control the sequence of execution of instructions in a program.
A design is the path from the problem to a solution in code. Program Design is
both a product and a process. The process results in a theoretical framework for
describing the effects and consequences of a program as they are related to its
development and implementation.
6. What is algorithm?
9. What is coding?
Documentation explains how the program works and how to use the program.
Documentation can be of great value, not only to those involved in maintaining
or modifying a program, but also to the programmers themselves. Details of
particular programs, or particular pieces of programs, are easily forgotten or
confused without suitable documentation.
Documentation comes in two forms:
* External documentation, which includes things such as reference manuals,
algorithm descriptions, flowcharts, and project workbooks
* Internal documentation, which is part of the source code itself (essentially, the
declarations, statements, and comments)
Testing means detecting errors. Debugging means diagnosing and correcting the
root causes.
17. What is C?
Characteristics of C Language
The increasing popularity of C is due to its various desirable qualities:
*C language is well suited for structured modular programming.
* C is a robust language with rich set of built-in functions and operators.
* C is smaller which has minimal instruction set and programs written in C are
efficient and fast.
* C is highly portable (code written in one machine can be moved to other).
* C is highly flexible.
* C allows access to the machine at bit level (Low level (Bitwise) programming).
* C supports pointer implementation - extensive use of pointers for memory,
array, structures and functions.
21. How many types of constants are there? What are they?
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
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 can’t we use key word as
variable’s 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.
________________________________________________________________________
________________________________________________________________________
! – sizeof()
_________________________________________________________________________
The if statement has three basic forms: Simple if-else, Nested if and if-else if
ladder.
Whenever the expression with the operators && and || are evaluated, the
evaluation process stops as soon as the outcome, true or false is known.
Operators Type
! Logical NOT
*/% Arithmetic and modulus
+- Arithmetic
< > <= >= Relational
== != Relational
&& Logical AND
|| Logical OR
= Assignment
34. What are the different types of loop control instructions available in
C?
1. For
2. While
3. Do...while
The loop (loop control instruction) involves repeating some portion of the
program either a specified number of times or until a particular condition is being
satisfied.
If we want to take the control to the beginning of the loop, bypassing the
statements inside the loop, which have not yet been executed. The keyword
continue allows us to do this. When continue is encountered inside any loop,
control automatically passes to the beginning of the loop. A continue is usually
associated with an if.
38. What is the difference between an entry controlled loop and an exit
controlled loop?
If the condition of the loop is tested before the control enters into the loop then it
is called entry control loop (While)and if the condition is tested at exit then it is
exit controlled loop(do-while).
The control statement that allows us to make a decision from the number of
choices is called a switch, or more correctly a switch-case-default
If the break is not used in a case statement the control will not come out of the
switch and it goes executing the following cases till it encounters a break
otherwise it all the statements in the switch.
Takes the control where ever you want, is done by using goto statement. Better
don’t use it.
The big problem with goto’s is that when we do use them we can never be sure
how we got to a certain point in our code. They obscure the flow of control.
The exit ( ) function is a standard library function which terminates the execution
of the program.
There are two parameter passing mechanism available. Those are: Call by value
and Call by address
1. In the above code snippet main () is calling function because the function
reverse is called and reverse is called function
2. If a function (here reverse ()) is written below main () its prototype must be
included in the calling function otherwise it should be declared globally so that
the function call does not raise any error. It informs the compiler about the
function.
3. The parameters present in the function call are called actual parameters and
the parameters which are there in function heading are formal parameters
4. Return statement in function is the last executable statement. If we write any
statements after if those are not going to be executed
5. Return type of a function is the type of value that is returned by the function
in the present example int is the return type because reverse function returns
int.
54. What is the main difference between call by value and call by
reference (address)?
55. What is the difference among int, short and long data types?
Short is at least 2 bytes big, long is at least 4 bytes big, short is never bigger
than int’s and int’s are never bigger than longs.
Declaring an integer as unsigned almost doubles the size of the largest possible
value that it can otherwise take.
2. What will be the initial value of the variable, if initial value is not specifically
assigned? (i.e. the default initial value).
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.
Use static storage class only if you want the value of a variable to persist
between different function calls.
Use register storage class for only those variables that are being used very often
in a program. A typical application of register storage class is loop counters,
which get used a number of times in a program.
PRE PROCESSOR
# is called pre processor directive. Its job is to expand macros and files (headers)
#include "goto.c" This command would look for the file goto.c in the
current directory as well as the specified list of
directories as mentioned in the include search path
that might have been set up.
#include <goto.c> This command would look for the file goto.c in the
specified list of directories only.
They include search path is nothing but a list of directories that would be
searched for the file being included. Different C compilers let you set the search
path in different manners. If you are using Turbo C/C++ compiler then the
search path can be set up by selecting ‘Directories’ from the ‘Options’ menu. On
doing this a dialog box appears. In this dialog box against ‘Include Directories’
we can specify the search path.
Output :
Inside fun1
Inside main
Inside fun2
ARRAYS
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 student’s 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.
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.
Just passing the address of the zeroth element of the array to a function is as
good as passing the entire array to the function. It is also necessary to pass the
total number of elements in the array
Bubble sort, selection sort, insertion sort, quick sort, merge sort and heap sort.
STRING FUCTIONS
80. What is the format specifier used to print strings on to the screen?
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 Use
strlen Finds length of a string
strlwr Converts a string to lowercase
strupr Converts a string to uppercase
strcat Appends one string at the end of another
strncat Appends first n characters of a string at the end of
another
strcpy Copies a string into another
strncpy Copies first n characters of one string into another
strcmp Compares two strings
strncmp Compares first n characters of two strings
strcmpi Compares two strings without regard to case ("i"
denotes that this function ignores case)
stricmp Compares two strings without regard to case
(identical to strcmpi)
strnicmp Compares first n characters of two strings without
regard to case
strdup Duplicates a string
strchr Finds first occurrence of a given character in a string
strrchr Finds last occurrence of a given character in a string
strstr Finds first occurrence of a given string in another
string
strset Sets all characters of string to a given character
strnset Sets first n characters of a string to a given character
strrev Reverses string
STUCTURES
A structure contains a number of data types grouped together. These data types
may or may not be of the same type.
Example 1:
Struct emp
Int empno;
Float sal;
};
This is called structure definition. Memory is not allocated to the members when
the structure is defined.
Example 2:
Struct emp {
Int empno;
Float sal;
} a, b, c;
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;
Usually structure type declaration appears at the top of the source code file,
before any variables or functions are defined. In very large programs they are
usually put in a separate header file, and the file is included (using the pre-
processor directive #include) in whichever program we want to use this structure
type.
Syntax is
Structure name. field name;
struct book {
char name [20];
float price;
int pages;
} b [100];
B is array of structures where each element is nothing it is structure.
Some times like this a lot of variables (b1 to b100 for storing data about hundred
books) needed to be handled. Therefore he allowed us to create an array of
structures; an array of similar data types which themselves are a collection of
dissimilar data types.
Yes we can assign for example a, b are two structures we can do a=b provided
both structures belong to the same tag
We can use structure within a structure. But inner structure definition must be
written in above outer structure.
91. Can you send a structure to a function using call by value and call
by address?
If structure has one of its members type as its own type then it is called self
referential structure.
INPUT/OUTPUT FUNCTION:
There are numerous library functions available for I/O. These can be classified
into three broad categories:
It allows us to read characters from a string and to convert and store them in C
variables according to specified formats. The sscanf( ) function comes in handy
for in-memory conversion of characters to values. You may find it convenient to
read in strings from a file and then extract values from a string by using
sscanf( ). The usage of sscanf( ) is same as scanf( ), except that the first
argument is the string from which reading is to take place.
These functions return the character that has been most recently typed. The ‘e’
in getche( ) function means it echoes (displays) the character that you typed to
the screen. As against this getch( ) just returns the character that you typed
without echoing it on the screen.
getchar( ) works similarly and echo’s the character that you typed on the screen,
but unfortunately requires Enter key to be typed following the character that you
typed. The difference between getchar( ) and fgetchar( ) is that the former is a
macro whereas the latter is a function. Here is a sample program that illustrates
the use of these functions.
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?
• 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.
• A structure or a union can be passed by value to functions and returned by
value by functions. The argument must have the same type as the
function parameter. A structure or union is passed by value just like a
scalar variable; that is, the entire structure or union is copied into the
corresponding parameter.
The union is a structure. The main difference between structure and union is, the
size of the union is equal to the size of the largest member of the union where as
size of the structure is the sum of the size of all members of the structure. And
one more thing is that, at a time we can use one member of the union.
FILES
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( ).
To read the file’s 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.
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.
And r+, w+, a+ we can do read, write, append with these three.
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.
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.
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.
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( )
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.
Operator Meaning
~ One’s complement
>> Right shift
<< Left shift
& Bitwise AND
| Bitwise OR
^ Bitwise XOR(Exclusive
OR)
Example:
Many languages permit a programmer to specify an array size at run time. Such
languages have the ability to calculate and assign during executions, the
memory space required by the variables in the program. But c inherently does
not have this facility but supports with memory management functions, which
can be used to allocate and free memory during the program execution. The
following functions are used in c for purpose of memory management.
Function Task
Malloc Allocates memory requests size of bytes and returns a pointer to the Ist byte of allocated
space
Calloc Allocates space for an array of elements initializes them to zero and returns a pointer to the
memory
Free Frees previously allocated space
Realloc Modifies the size of previously allocated space.
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.
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));
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.
free(ptr);
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.
Basic programs:
2. Reverse of a number
3. Armstrong number
4. Reverse of a string
6. Fibonacci series
7. Whether the given number is a prime or not.
14.