C Chotha

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

K. M.

Nasim [Rusho] EEE’08 -1-


EEE-151
Fundamentals of Computing
K. M. Nasim [Rusho]
EEE’08
Write down the importance of C or the characteristics of C? [08]
Ans:
The importance of C :
1. It is a robust language.
2. It is well suited for writing both system software and business packages.
3. Programs written in C are efficient and fast.
4. C is highly portable.
5. C language is well suited for structured programming.

Write the format of simple C program.


Ans:
main() Function name
{ Start of program
---------------
--------------- Program statement
---------------
} End of program

Write the basic structure of C program. [04,06,07]


Ans:
A C program can be considered as a group of building blocks called functions. A function is a subroutine
that may include one or more statements designed to perform a specific task. To write C program, we
first create functions and then put them together. A C program may contain sections shown below:

Documentation section (Name)


Link section (Header files)
Definition section (Symbolic constants)
Global declaration section
main() Function section
{
Declaration part
Executable part
}
Subprogram section (User Defined functions)

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.

The happenings after program execution:


1. A set of information called input data will be entered and stored in a place of computer memory.
2. The input data will be processed to produce certain desired results known as output.
3. The output will be then printed or displayed on the monitor.

What is the purpose of a compiler to execute a program?[07]


Ans:
Compiler plays main role to execute a program. Compiler translates the source program instructions
into a form that is suitable for execution by the computer. The translation is done after examining each
instruction for its correctness. If everything is alright, then compiler translates the program & it is stored
which is object code. If any error is occurred, then they are listed and compilation ends right there. The
errors should be corrected and the compilation is done again.
So a compiler check the instructions of a program whether it is right or wrong. If wrong, then we get
a chance to see it and correct it by compiler to execute a program.

What is meant by top down strategy programming?[02]


Ans:
Top down strategy programming: It is essential that the overall program strategy be completely
mapped before any of the detail programming. This detail map associated with the individual program
statements is generally known as top-down strategy programming.

Character set in C:[04]


The characters that can be used to form words, numbers and expressions depend upon the computer on
which the program is run. The characters in C are grouped into the following categories:
1. Letters
2. Digits
3. Special characters
4. White spaces

What do you mean by C tokens?[08] Classify it.


Ans:
C tokens: In a C program smallest individual units are known as C tokens. C programs are written using
these tokens and the syntax of the language.

C has six types of tokens:


K. M. Nasim [Rusho] EEE’08 -3-
C TOKENS

Keywords Identifiers Constants Strings Special symbols Operators


float main -15.5 “ABC” [] +,-,*,/
while amount 100 “year” {}

What is keywords and Identifiers? Differentiate between them.[04,08]


Ans:
Keywords: Keywords are a kind of C word. All keywords have standard and predefined meanings that
cannot be changed. Keywords serve as basic building blocks for program statements.
Identifiers: Identifiers refer to the names of variables, functions and arrays. These are user defined
names and consist of a sequence of letters and digits.

Difference between keywords and identifiers:

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.

What are the restrictions applied to the use of keywords?[03]


Ans:
1. Keywords must be written in lower case.
2. They can’t be used as identifiers.

What do you mean by constants? Classify it.


Ans:
Constants: Constants in C refer to fixed values that do not change during the execution of a program.
Constants can be classified as follows:

CONSTANTS

Numeric Constants Character Constants

Integer Constants Real Constants Single Character Constants String Constants


5,10 5.5,10.6
K. M. Nasim [Rusho] EEE’08 -4-
Definitions:
Single character constants: A single character constant contains a single character enclosed within a
pair of single quote marks.

String constants: A string constant is a sequence of characters enclosed in double quotes.

What do you mean by Variables? Write down its conditions.


Ans:
Variables: A variable is a data name that may be used to store a data value. A variable may take
different values at different time during execution.

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.

Which of the followings are invalid variables name and why?[07]


Ans:
i. RUET -Valid
ii. 3rdct -Valid
iii. $EEE -Invalid -Dollar sign is illegal.
iv. Row Total -Invalid -Blank space is not permitted.

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

Why declaration of variables is needed?


Ans:
Declaration of variables is needed for-
1. It tells the computer what the variable name is.
2. It specifies what type of data the variable will hold.

General format of declaration of variables:

data-type v1,v2,….vn

General format of scanf:

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.

Storage class and their meaning:


1. Auto: Local variable known to only to the function in which it is declared.
2. Static: Local variable which exists and retains its value even after the control is transferred
to the calling function.
3. Extern: Global variables, known to all functions in file.
4. Register: Local variable which is stored in the register.

What is operator? What are the operators used in C (or classification)?[04]


Ans:
Operator: An operator is a symbol that tells the computer to perform certain mathematical or logical
calculation. Operators are used in programs to manipulate data and variables.

The operators used in C:

1. Arithmetic operators (+,-,*,/,%)


2. Relational operators (<,<=,>,>=,==,!=)
3. Logical operators (&&,||,!)
4. Assignment operators (=)
5. Increment and decrement operators (++,--)
6. Conditional operators (? :)
7. Bitwise operators (&,^,<<,>>)
8. Special operators (comma operator, size of operator)

What is meant by unary operator?[03]


Ans:
Unary operator: C includes a class of operators that act upon a single operand to produce a new value.
Such operators are known as unary operators. They are-
i. +,-
ii. ++/--
iii. size of operator
iv. !(logical not)
v. cast operator

What is an operand? What is the relationship between operators and operands?


Ans:
Operands: The data lines on which operators act upon are called operands. Some operators require two
operands. A few operator permit only single variable as operands.
For example- “a+b”. In this example a & b are operands and + is an operator.
K. M. Nasim [Rusho] EEE’08 -6-
The relationship between operators and operands: Every operator is related to operand or operands.
Operator takes place between or beside the operands and make an expression.
c=a+b is an expression.
In short, certain combination of operators and operands form certain expression and that carries out
certain actions.

What is meant by assignment and expression statement?[05]


Assignment statement: A statement causes the computer to carry out some actions. It is consist of
assigning values of variables. Values can be assigned to variables using the assignment operator =. As
follows-
variable_name=constant

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;

Describe conditional operator briefly.[04]


Ans:
conditional operator: The operator which is useful to make two way decisions and is a combination of ?
and : is called as a conditional operator. This take three operands. The general form of using conditional
operator,

conditional expression ? expression 1 : expression 2

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.

What is meant by operator precedence and associativity?[04]


Ans:
Operator precedence: Each operator has a precedence associated with it. Operator precedence
describes the order in which C reads expressions. There are distinct levels of precedence and an
operator may belong to one of these levels. The operators at the higher level of precedence are
evaluated first.
Associativity: The operators of the same precedence are evaluated either from left to right or from right
to left, depending on the level. This is known as the associativity property of an operator.
K. M. Nasim [Rusho] EEE’08 -7-
What are the differences between ++m and m++?[07]
Ans:
While ++m and m++ mean the same thing when they form statements independently, they behave
differently when they are used in expressions on the right hand side of an assignment statement. For
example consider,
m=5;
y=++m;
In this case, the value of y and m would be 6.
Again, if we consider,
m=5;
y=m++;
Here the value of y would be 5 and m would be 6.

What are the advantages of short hand assignment operator?


Ans:
The advantages of short hand assignment operator are-
i. It becomes easier to write.
ii. Statement is more concise and easier to read.
iii. Statement is more efficient.

What is meant by compound statement [08] How compound statement is written?[08]


Ans:
Compound statement: Generally one test condition is used in a for loop. When we use more
than one test condition in a for loop, then we call the statement as compound statement.
The writing method of compound statement:
for(initialization;(test condition 1 && test condition 2);increment/decrement)

What is meant by array?[05] Types of arrays.


Ans:
Array: An array is a group of related data items that share a common name. It is a fixed size sequenced
collection of elements of the same data type.
Types of arrays:
i. One dimensional array
ii. Two dimensional array
iii. Multi dimensional array

What is meant by one-dimensional array? Declaration and initialization of it.


Ans:
One dimensional array: A list of items can be given one variable name using only one subscript and
such a variable is called single subscript variable is or one-dimensional array.

Declaration of one-dimensional array:

type variable-name[size]

Initialization of one-dimensional array:

static type array-name[size]={list of values};


K. M. Nasim [Rusho] EEE’08 -8-
What is meant by two-dimensional array? Declaration and initialization of it.
Ans:
Two-dimensional array: Two-dimensional arrays are used where a table of values will have to be
stored.

Declaration of two-dimensional array:

type array_name [row_size] [column_size];

Initialization of two-dimensional array:

static int table [2][3]={0,0,0,1,1,1};

Explain the advantages of two-dimensional array over one-dimensional array.[05]


Ans: The advantages are given below-
i. Two matrix can be added
ii. Two matrix can be subtracted
iii. Two matrix can be multiplied
iv. All the table values data can be solved by using two-dimensional array easily
v. The inverse of matrix can be determined.

What are the disadvantages or drawbacks of initialization of arrays in C?[06]


Ans:
There are two disadvantages-
1. There is no convenient way to initialize only selected elements.
2. There is no shortcut method for initializing a large number of array elements.

Write down the conditions for being array.


Ans;
Conditions for being array:
i. Consists a group of elements.
ii. The elements are stored consecutively.
iii. Each of the elements can be used where as it is needed.

In what way does an array variable differs from an ordinary variable?[05]


Ans:
Difference between ordinary variable and array variable:
An ordinary variable is an identifier that is used to represent some specified types of information
within a designated portion of the program. It is used to store a single data of different types.
But an array variable is used to store a set of similar data where each data is of same size.

What is function? Classify it.


Ans:
Function: A function is a self contained program segment that carries out some specific well-defined
tasks.
C functions can be classified into two categories;
i. Library functions
ii. User-defined functions
K. M. Nasim [Rusho] EEE’08 -9-
Are the library functions actually a part of C language? Explain How are they usually
packaged with in a compiler?[04]
Ans:
Actually library functions are not a part of the C language. Because these functions carry out
various commonly used operations or calculations. Some functions return a data items to their access
point; others indicate whether a condition is true or false by returning a 1 or a 0 respectively; still others
carry out specific operations or data items but do not return anything. Features which tend to be
computer-dependent are written as library function. Library functions that are functionally similar are
usually grouped together as object programs in separate library files. These library files are supplied as a
part of each C compiler. All C compiler contain similar groups of library functions, though they lack
precise standardization. Thus there may be same variation in the library functions that are available in
different versions of the language.

What are the differences between library functions and user-defined functions?[08]
Ans:
The differences between library functions and user-defined functions are-

Library functions User-defined functions


1. The functions which are made by the writers 1. The functions which are made by the users of
of C language are called library functions. Ex- C language in different circumstances are called
printf, scanf etc. user-defined functions.

2. Library functions are added to C language 2. After creating an user defined function it is
automatically. added to the memory.

What are User-defined Functions? What are the advantages of it?[04,06,07]


Ans:
User-defined Functions: The functions that are defined by the user in a C program are known as user-
defined functions. These functions work in sub-program section.

The advantages of user-defined functions are given below:

i. It facilitates top-down modular programming.


ii. The length of a source program can be reduced by using functions at appropriate places.
iii. It is easy to locate and isolate a faulty function for further investigation.
iv. A function may be used by many other programs. This means that a C programmer can build on
what others have already done, instead of starting from scratch.

What is meant by multifunction program?


Ans:
Multifunction program: The program containing many functions which are interacted through function-
calls and forms an executable object code, is called a multifunction program.
K. M. Nasim [Rusho] EEE’08 - 10 -
Write down the general format of C function/ user-defined function.
Ans:

function-name (argument list)


argument declaration;
{local variable declaration;
statement-1;
---------------
---------------
---------------
return(expression);
}

Write down the category of functions.


Ans: A function depending on whether arguments are presented or not and whether a value is returned
or not can be classified as follows-
i. Functions with no arguments and no return values
ii. Functions with arguments and no return values
iii. Functions with arguments and one return value
iv. Functions with no arguments but return a value
v. Functions that return multiple values

What is meant by actual and formal arguments (Distinguish between them)?[07]


Ans:
Actual argument: The arguments contained by function-calls are known as actual argument.
Formal argument: The argument contained by called-function is known as formal argument.

What are function call and called function?


Ans:
Function call: A function can be called by simply using the function name in a statement. This is called
function call. [06]
Called function: The function which is called by another function by function call is known as called
function.

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 recursion?[04]


Ans:
Recursion: When a called function in turn calls another function, a process of chaining occurs. This
special case of process is known as recursion.

Explain the characteristics of the recursive function.[06]


Ans: Recursive functions can be effectively used to solve problems where the solution is expressed in
terms successively applying the same solution to subsets of the problem. When we write recursive
functions, we must have an if statement somewhere to force the function to return without recursive call
being executed. Otherwise, the function will never return.

What is meant by variable? Write down the types of variables.


Ans:
Variables: A variable is a data name that may be used to store a data value. A variable may take
different values at different time during execution.

Variables are 4 in types:


i. Automatic/Internal/Local variables
ii. External/Global variables
iii. Static variables
iv. Register variables
v.

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:

Global Variables 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.

What is meant by static and register variables?


Ans:
Static variables: The type of variable which is initialized only once and whose value persists (fixed) until
the end of the program.

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 advantages of resister variable?[08]


Ans:
1. Resister access is much faster than a memory access,
2. Leads to faster execution of a program.

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.

Differences between structure and array:


Array can be used to represent a group of data items that belong to the same type such as
int or float. On the other hand structure can be used to represent a collection of data items of different
types using a single name such as int and float. Structures help to organize complex data in a more
meaningful way.
K. M. Nasim [Rusho] EEE’08 - 13 -
Declaration of structure variable: [05]
Ans:

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.

What are the differences between multitasking & multiprogramming?[07]


Ans: Multitasking is the ability of a computer to handle a number of tasks or jobs simultaneously while
multiprogramming is the capacity to run or handle several programs at the same time. Again in
multiprogramming the processor is idle. During execution of one process it go for execution of another
program but multitasking is a combination of both multiprogramming and time sharing means in this, it
will execute more than one process simultaneously.
The work of CPU is multitasking.

What is meant by bit field?[07] Write down its general format.


Ans:
Bit field: A bit field is a set of adjacent bits whose size can be from 1 to 16 bits in length. A word can be
divided into a number of bit fields. The name and size of bit fields are defined using a structure.

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.

Declaring pointer variable:

data_type *pt_name;

Explain pointer is a variable.


Ans:
The computer’s memory is a sequential collection of storage cells or bytes. Each cell has a number
called address associated with it. Whenever a variable is declared, the system allocates an appropriate
location to hold the value of the variable which is unique. Pointer indicates the address of the location
which has a own number. As different variables have different address, the pointers of the variables are
also different, therefore pointer also acts as a variable.
Consider an example:
int quantity = 17;
Let us suppose the system has allocated the variable quantity at address location 5000. In this case the
pointer to the variable quantity is 5000.

quantity --------variable
17 ---------value
5000 ---------address

How we can determine the address of a variable?


Ans:
This can be done with the help of the operator & available in C. The operator & immediately preceding a
variable returns the address of the variable associated with it. For example the statement,
P=&quantity;
would assign the address 5000(the location of quantity) to the variable p. The & operator can be
remembered as ‘address of’.

Initializing pointer variable:

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 meant by file? [04]Which are included in basic file operation?


Ans:
File: A file is a place on the disk where a group of related data is stored.

Basic file operation includes-


i. naming a file
ii. opening a file
iii. reading data from a file
iv. writing data to a file
v. closing a file

What are the ways to perform the file operation in C?


Ans: There are two distinct ways to perform file operations in C-
1. Low-level I/O operation (Uses UNIX system calls)
2. High-level I/O operation (Uses in C’s standard I/O library)

What are the purposes of the following functions.


Ans:
fopen() - Creates a new file for use[07]
Open an existing file for use
fclose() - Closes a file which has been opened for use[07]
getc() - Reads a character from a file
putc() - Writes a character to a file
K. M. Nasim [Rusho] EEE’08 - 16 -
fprintf() - Writes a set of data values to a file
fscanf() - Reads a set of data values from a file
getw() - Reads an integer from a file[07]
putw() - Writes an integer to a file
fseek() - Sets the position to a desired point in the file[07]
ftell() - Gives the current position in the file
rewind() - Sets the position to the beginning of the file

What are the primary advantages of using a data file?[07]


Ans:
Advantages of using a data file:
1. It becomes cumbersome and time consuming to handle large volumes of data through terminals.
Use of data file can solve this.
2. The entire data is lost when either the program is terminated or the computer is turned off. It can
be solved by using data file.

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.

What is meant by dynamic memory allocation?[04,08] How it helps to build complex


program?[04]
Ans:
Dynamic memory allocation: The process of allocating memory at runtime is called dynamic memory
allocation.

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.

Write the distinctions among write, append and read mode.[03,04]


Ans:
Write mode: A file with the specified name is created if the file does not exist. The contents are deleted
if the file already exists.

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.

What are the uses and limitations of getc & putc?[04]


Ans: The simplest file I/O functions are getc and putc which handle one character at a time. Assume
that a file is opened with mode “w” and file pointer fp1. The statement: putc(c,fp1);
writes a single character contained in the char c to the file associated with FILE *fp1.
Similarly, getc is used to read a c character from a file. The statement: c=getc(fp2);
would read a character from the file whose file pointer is fp2.

The limitation of these functions is that they can handle only one character at a time.

K. M. NASIM [RUSHO]
EEE’08

You might also like