Professional Documents
Culture Documents
C Chotha
C Chotha
C Chotha
Function 1
Function 2
-
-
Function n
K. M. Nasim [Rusho] EEE’08 -2-
Write the process of executing a C program?
Ans:
Executing a program written in C involves a series of steps-
1. Creating the program.
2. Compiling the program.
3. Linking the program with functions that are needed from the C library.
4. Executing the program.
What is meant by program? What are the happenings after program execution?[02]
Ans:
Program: A set of instructions, which are used to perform a certain job, is called a computer program.
Keywords Identifiers
I. Keywords serve as basic building blocks for I. Identifiers usually used as a link between two
program statements. keywords.
II. All keywords must be written in lowercase. II. Both uppercase and lowercase letters are
permitted.
III. The underscore character is not permitted III. The underscore character is also permitted in
in keywords. identifiers.
IV. Keywords are usually system-defined IV. Identifiers are user-defined names and
characters. consists of a sequence of letters and digits,
with a letter as a first character.
CONSTANTS
Conditions:
1. Variables must begin with a letter.
2. The length of the variables should not be more than eight characters because many compilers
treat the first eight characters as significant.
3. Uppercase and lowercase characters are significant.
4. Variable should not be a keyword.
5. White space is not allowed.
Data types:
C language is rich in data types. ANSI C supports three classes of data types:
1. Primary data type
2. User-derived data type
3. Derived data type
data-type v1,v2,….vn
Scanf(“control string”,&variable1,&variable2,…..);
K. M. Nasim [Rusho] EEE’08 -5-
What is meant by storage class of variable? Name the storage class specifications
included in C.[05]
Ans:
Storage class of variable: Variables in C can have not only data type but also storage class
that provides information about their location and visibility. The storage class decides the portion
of the program within which the variables are recognized.
Expression statement: An expression represents a single data item such as a number or a character.
The expression may consist of a single entity, such as a constant, variable, an array element or a
reference to a function. It may also consist of some combination of such entities, inter connected by one
or more operators. An expression statement consists of an expression followed by a semicolon.
Example: c=a+b;
It is used in the place of if-else statement when comparison take place between two things.
Describe two different ways to utilize the increment and decrement operators. How do the
two methods differ?[05]
Ans:
The increment and decrement operators can be utilized in two different ways. Depending on whether the
operator is written before or after that means-
i. ++i or --i
ii. i++ or i--
If the operator proceeds the operand (++i or –i), then the operands will be altered the value before it is
utilized.
On the other hand, if the operator proceeds the operand (i++ or i--), then the operands will be altered the
value after it is utilized.
type variable-name[size]
What are the differences between library functions and user-defined functions?[08]
Ans:
The differences between library functions and user-defined functions are-
2. Library functions are added to C language 2. After creating an user defined function it is
automatically. added to the memory.
How can a function be called? From what points of a program can a function be called?
Explain with appropriate example. [04, 05, 06, 08]
Ans:
When the function encounters a function call, the control is transferred to the function which has
been called. This function is then executed line by line as described and a value is returned when a
returned statement is encountered. This value is assigned to calling function. A function can not be used
on the right side of an assignment statement.
mul(a,b)=15; Is invalid
The function can be called in program after the main function. Except the starting point, there are no
pre-determined relationships, rules of precedence or hierarchies among the functions that make up a
complete program. The functions can be placed at any order. A called function can be placed either
before or after the calling function. However it is the usual practice to put all the called functions at the
end.
K. M. Nasim [Rusho] EEE’08 - 11 -
Example:
main()
{printline();
printf(“Function Call\n”);
printline();}
printline()
{int i;
for(i=1;i<40;i++)
printf(“-“);
printf(“\n”);}
--------------------
Output: Function Call
--------------------
Here, after left brace printline() is the function call & it works after closing the first right brace.
What is meant by local variables and global variables? Difference between them.[07]
Ans:
Local variables/Automatic/Internal: The variables which are created when the function is called and
destroyed automatically when the function is existed are called automatic/internal/local variables.
Global/External variables: The variables that are both alive and active throughout the entire program
are known as external/global variables.
K. M. Nasim [Rusho] EEE’08 - 12 -
Differences between global variables and local variables:
i. Are used in more than one function in the i. Are used in only one function.
program.
ii. Are declared before the main() function. ii. Are declared inside the main() function.
iii. Meaningful throughout the program. iii. Meaningless beyond the considered function.
iv. Change in it affects the whole program. iv. Change in it only affect the function.
v. Cannot be used by the same name in v. Can be used by same name in different
different functions. functions.
Resister variables: The variables which are kept in one of the machine’s resister, instead of keeping in
the memory, are called resister variables.
What are the differences between resister access and memory access?
Ans:
The main difference between resister access and memory access is, resister access is much faster
than a memory access, keeping the frequently accessed variables (loop control) in the register will lead a
faster execution of programs.
Define structure[06]. What are the differences between structure and array?
Ans:
Structure: Structure is a method of packing data of different types. A structure is a convenient method of
handling a group of related data items of different data types.
struct tag_name
{
data-type member 1;
data-type member 2;
---------------------------
---------------------------
data-type member n;
};
struct tag-name member 1; member 2;………. member n;
Define union. What are the differences between structure and union?[05,06,07]
Ans:
Union: Unions contain members whose individual data types may differ from one another. The members
that compose a union all share the same storage area within the computers memory.
The differences between structure and union: In structures, each member has its own storage
location. On the other hand all the members of a union use the same location. While structure enables
us treat a number of different variables stored at different in memory, a union enables us to treat the
same space in memory as a number of different variables.
General format:
`
struct tag-name
{
data-type name1: bit-length;
data-type name2: bit-length;
----------
----------
data-type nameN: bit-length;
}
K. M. Nasim [Rusho] EEE’08 - 14 -
Define pointer. What are the reasons/advantages for using pointer?[05,07]
Ans:
Pointer: A pointer is nothing but a variable that contains an address which is a location of another
variable in memory.
Reasons for using pointer:
Reasons for using pointer are-
i. Pointers are more efficient in handling arrays and data tables.
ii. Pointers can be used to return multiple values from a function via function arguments.
iii. Pointer enables us to access a variable that is defined outside the function.
iv. Pointers reduce the length and complexity of a program.
v. They increase the execution speed.
vi. Pointers allow C to support dynamic memory management.
data_type *pt_name;
quantity --------variable
17 ---------value
5000 ---------address
p=&quantity;
K. M. Nasim [Rusho] EEE’08 - 15 -
What is the relationship between the data item represented by a variable v and the
corresponding variable pv?[03]
Ans:
The variable ‘v’ represents the data items stored in v’s memory cell. The corresponding pointer ‘pv’
represents the address of the memory cell where ‘v’ is stored. So we have
pv= &v
Where &v is the address of v, the value of v is represented in terms of pointer variable by *pv, where * is
indirection operator.
Address of v Value of v
pv v
What is meant by call by reference and call by value (Distinguish between them)?[07]
Ans:
Call by reference: The process of calling a function using pointers to pass the address of variable is
known as call by reference.
Call by value: The process of calling a function using pointers to pass the actual value of variables is
known as call by value.
What are the major problems of the console oriented I/O operations?[04]
Ans: Using functions such as scanf and printf to read and write data are console oriented I/O functions.
There are two major problems of the console oriented I/O operations -
I. It becomes cumbersome and time consuming to handle large volumes of data through terminals.
II. The entire data is lost when either the program is terminated or the computer is turned off.
What is the purpose of the ‘putchar’ function? How is it used within a C program? Compare with
the ‘getchar’ function.[05]
Ans: ‘putchar’ is an analogous function for writing characters one at a time to the terminal. It takes the
form as shown below-
putchar(variable_name);
Where variable name is a type char variable containing a character
Example: answer=’Y’;
putchar(answer);
One the other hand, reading a single character can be done by using the function ‘getchar’. It takes the
following form-
variable_name=getchar();
Variable name is a valid C name that has been declared as char type.
What is meant by EOF? What happens when an EOF is encountered when reading
characters with the ‘getchar’ function?[05]
Ans:
EOF: EOF means end of file marker. When end of file has been reached the getc will return the end of
file marker is known as EOF.
When it is encountered while reading character with the getchar function, then the reading is terminated.
There are four library routines known as “memory management function” that can be used for
allocating and freeing memory during program execution. These functions help to build complex
application programs that use the available memory intelligently.
K. M. Nasim [Rusho] EEE’08 - 17 -
Malloc:[08]
Allocates requested size of bytes and returns a pointer to the first byte of the allocated space.
Calloc:[08]
Allocates space for an array of elements, initializes them to zero and returns a pointer to the memory.
Free function:[08]
Frees previously allocated space.
Realloc:
Modifies the size of previously allocated space.
Append mode: The file is opened with the current contents safe. A file with the specified name is
created if the file does not exist.
Read mode: If it exists, then the file is opened with the current contents safe otherwise an error occurs.
The limitation of these functions is that they can handle only one character at a time.
K. M. NASIM [RUSHO]
EEE’08