Tuesday, December 27, 2011

C language interview questions:



                                            
1.What is C language?
Ans: The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to many other operating systems, and is one of the most widely used programming languages. C is prized for its efficiency, and is the most popular programming language for writing system software, though it is also used for writing applications.

2.What does static variable mean?

Ans: Static variables are the variables which retain their values between the function calls. They are initialized only once their scope is within the function in which they are defined.

3.What is a pointer?
Ans: Pointers are variables which stores the address of another variable. That variable may be a scalar (including another pointer), or an aggregate (array or structure). The pointed- to object may be part of a larger object, such as a field of a structure or an element in an array.

4.What are the uses of a pointer?
Ans: Pointer is used in the following cases

i)  It is used to access array elements
ii)  It is used for dynamic memory allocation.
iii)  It is used in Call by reference
iv)  It is used in data structures like trees, graph, linked list etc.

5.What are the pointer declarations used in C? Ans:

1- Array of pointers, e.g , int *a[10]; Array of pointers to integer 2-Pointers to an array,e.g , int (*a)[10]; Pointer to an array of into

3-Function returning a pointer,e.g, float *f( ) ; Function returning a pointer to float 4-Pointer to a pointer ,e.g, int **x; Pointer to apointer to int
5-pointer to a data type ,e.g, char *p; pointer to char

6.What is Polymorphism ?

Ans: 'Polymorphism' is an object oriented term. Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate actions. In other words,
polymorphism means taking more than one form. Polymorphism leads to two important aspects in Object Oriented terminology – Function Overloading and Function Overriding.


Overloading is the practice of supplying more than one definition for a given function name in the same scope. The compiler is left to pick the appropriate version of the function or operator based on the arguments with which it is called. Overriding refers to the modifications made in the sub class to the inherited methods from the base class to change their behavior.

7.What is a structure?

Ans: Structure constitutes a super data type which represents several different data types in a single unit. A structure can be initialized if it is static or global.

8.What is a union?
Ans: Union is a collection of heterogeneous data type but it uses efficient memory utilization technique by allocating enough memory to hold the largest member. Here a single area of memory contains values of different types at different time. A union can never be initialized.

9.What are the differences between structures and union?
Ans: A structure variable contains each of the named members, and its size is large enough to hold all the members. Structure elements are of same size.

A union contains one of the named members at a given time and is large enough to hold the largest member. Union element can be of different sizes.

10.What are the differences between structures and arrays?
Ans: Structure is a collection of heterogeneous data type but array is a collection of homogeneous data types.
Array

1-It is a collection of data items of same data type. 2-It has declaration only
3-.There is no keyword.
4- array name represent the address of the starting element.

Structure

1-It is a collection of data items of different data type. 2- It has declaration and definition
3- keyword struct is used
4-Structure name is known as tag it is the short hand notation of the declaration.

11.How are pointer variables initialized?
Ans:  Pointer variable are initialized by one of the following two ways

-  Static memory allocation
-  Dynamic memory allocation

12.Difference between arrays and pointers?
Ans: - Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them

- Arrays use subscripted variables to access and manipulate data.


Array variables can be equivalently written using pointer expression.

13.Is using exit() the same as using return?
Ans: No. The exit() function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main() function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit() function are similar.

14.What is a method?
Ans: Method is a way of doing something, especially a systematic way; implies an orderly logical arrangement (usually in steps).

15.What is indirection?
Ans: If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable, or any other object in memory, you have an indirect reference to its value.

16.Why should I prototype a function?
Ans: A function prototype tells the compiler what kind of arguments a function is looking to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and that no erroneous type conversions are taking place.

17.What is static memory allocation and dynamic memory allocation?
Ans: Static memory allocation: The compiler allocates the required memory space for a declared variable.By using the address of operator,the reserved address is obtained and this address may be assigned to a pointer variable.Since most of the declared variable have static

memory,this way of assigning pointer value to a pointer variable is known as static memory allocation. memory is assigned during compilation time.

Dynamic memory allocation: It uses functions such as malloc( ) or calloc( ) to get memory dynamically.If these functions are used to get memory dynamically and the values returned by these functions are assingned to pointer variables, such assignments are known as dynamic memory allocation.memory is assined during run time.

18.Difference between an array of pointers and a pointer to an array? Ans:
Array of pointers

1- Declaration is: data_type *array_name[size];
2-Size represents the row size.
3- The space for columns may be dynamically

Pointers to an array

1-Declaration is data_type ( *array_name)[size];
2-Size represents the column size.


19.What are the differences between malloc() and calloc()? Ans: There are 2 differences.

First, is in the number of arguments. malloc() takes a single argument(memory required in bytes), while calloc() needs 2 arguments(number of variables to allocate memory, size in bytes of a single variable).
Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

20.malloc() Function- What is the difference between "calloc(...)" and "malloc(...)"? Ans:
1. calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size). malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not blocks of memory like calloc(...).
2. malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available. calloc(...) allocates an array in memory with elements initialized to 0 and returns a pointer to the allocated space. calloc(...) calls malloc(...) in order to use the C++ _set_new_mode function to set the new handler mode.

21.printf() Function- What is the difference between "printf(...
)" and "sprintf(...
)"?
Ans:  sprintf(...
) writes data to the character array whereas printf(...
) writes data to the
standard output device.







22.Compilation How to reduce a final size of executable?

Size of the final executable can be reduced using dynamic linking for libraries.

23.How would you use the functions fseek(), freed(), fwrite() and ftell()? Ans:

fseek(f,1,i) Move the pointer for file f a distance 1 byte from location i. fread(s,i1,i2,f) Enter i2 dataitems,each of size i1 bytes,from file f to string s. fwrite(s,i1,i2,f) send i2 data items,each of size i1 bytes from string s to file f. ftell(f) Return the current pointer position within file f.
The data type returned for functions fread,fseek and fwrite is int and ftell is long int.

24.What is the difference between the functions memmove() and memcpy()?

Ans: The arguments of memmove() can overlap in memory. The arguments of memcpy() cannot.

25.What is a file?
Ans: A file is a region of storage in hard disks or in auxiliary storage devices.It contains bytes of

information .It is not a data type.

26.IMP>what are the types of file?


Ans: Files are of two types

1-high level files (stream oriented files) :These files are accessed using library functions 2-low level files(system oriented files) :These files are accessed using system calls

27.IMP>what is a stream?
Ans: A stream is a source of data or destination of data that may be associated with a disk or other I/O device. The source stream provides data to a program and it is known as input stream. The destination stream eceives the output from the program and is known as output stream.

28.What is meant by file opening?
Ans: The action of connecting a program to a file is called opening of a file. This requires creating an I/O stream before reading or writing the data.

29.What is FILE?

Ans: FILE is a predefined data type. It is defined in stdio.h file.

30.What is a null pointer?
Ans: There are times when it’s necessary to have a pointer that doesn’t point to anything. The macro NULL, defined in , has a value that’s guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather than NULL.

The null pointer is used in three ways:
1) To stop indirection in a recursive data structure
2) As an error value
3)    As a sentinel value

31.In header files whether functions are declared or defined?
Ans: Functions are declared within header file. That is function prototypes exist in a header file,not function bodies. They are defined in library (lib).

32.What are the differences between malloc () and calloc ()?
Ans: Malloc Calloc 1-Malloc takes one argument Malloc(a);where a number of bytes 2-memory allocated contains garbage values

1-Calloc takes two arguments Calloc(b,c) where b no of object and c size of object 2-It initializes the contains of block of memory to zerosMalloc takes one argument, memory allocated contains garbage values.
It allocates contiguous memory locations. Calloc takes two arguments, memory allocated contains all zeros, and the memory allocated is not contiguous.

33.What are macros? What are its advantages and disadvantages? Ans: Macros are abbreviations for lengthy and frequently used statements. When a macro is called the entire code is substituted by a single line though the macro definition is of several lines. The advantage of macro is that it reduces the time taken for control transfer as in case of


function. The disadvantage of it is here the entire code is substituted so the program becomes

lengthy if a macro is called several times.

34.Difference between pass by reference and pass by value?
Ans: Pass by reference passes a pointer to the value. This allows the callee to modify the variable directly.Pass by value gives a copy of the value to the callee. This allows the callee to modify the value without modifying the variable. (In other words, the callee simply cannot modify the variable, since it lacks a reference to it.)

35.What is static identifier?
Ans: A file-scope variable that is declared static is visible only to functions within that file. A

function-scope or block-scope variable that is declared as static is visible only within that scope. Furthermore, static variables only have a single instance. In the case of function-or block-scope variables, this means that the variable is not “automatic” and thus retains its value across function invocations.

36.What are the different storage classes in C?
Ans: C has three types of storage: automatic, static and allocated.

Variable having block scope and without static specifier have automatic storage duration. Variables with block scope, and with static specifier have static scope.
Global variables (i.e, file scope) with or without the static specifier also have static scope. Memory obtained from calls to malloc(), alloc() or realloc() belongs to allocated storage class

37.Where is the auto variables stored?
Ans: Auto variables can be stored anywhere, so long as recursion works. Practically, they’re stored on the stack. It is not necessary that always a stack exist. You could theoretically allocate function invocation records from the heap.

38.Where does global, static, and local, register variables, free memory and C Program instructions get stored?
Ans: Global: Wherever the linker puts them. Typically the “BSS segment” on many platforms.

Static: Again, wherever the linker puts them. Often, they’re intermixed with the globals. The only difference between globals and statics is whether the linker will resolve the symbols across compilation units.Local: Typically on the stack, unless the variable gets register allocated and never spills.Register: Nowadays, these are equivalent to “Local” variables. They live on the stack unless they get register-allocated.

39.Difference between arrays and linked list?
Ans: An array is a repeated pattern of variables in contiguous storage. A linked list is a set of

structures scattered through memory, held together by pointers in each element that point to the next element. With an array, we can (on most architectures) move from one


element to the next by adding a fixed constant to the integer value of the pointer. With a linked list, there is a “next” pointer in each structure which says what element comes next.

40.What are enumerations?
Ans: They are a list of named integer-valued constants. Example:enum color { black , orange=4,

yellow, green, blue, violet };This declaration defines the symbols “black”, “orange”, “yellow”, etc. to have the values “1,” “4,” “5,” … etc. The difference between an enumeration and a macro is that the enum actually declares a type, and therefore can be type checked.

50.What is the difference between strings and character arrays?
Ans: A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicity specified by using the static keyword.

Actually, a string is a character array with following properties:
*   the multibyte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating
NUL character.
*  it not specified what happens if this array, i.e., string, is modified.
*  Two strings of same value[1] may share same memory area. For
example, in the following declarations: char *s1 = “Calvin and Hobbes”;

char *s2 = “Calvin and Hobbes”;
the strings pointed by s1 and s2 may reside in the same memory location. But, it is not true for the following:
char ca1[] = “Calvin and Hobbes”; char ca2[] = “Calvin and Hobbes”;

[1] The value of a string is the sequence of the values of the contained characters, in order.

51.Describe about storage allocation and scope of global, extern, static, local and register variables?
Ans:
Globals have application-scope. They’re available in any compilation unit that includes an

appropriate declaration (usually brought from a header file). They’re stored wherever the linker puts them, usually a place called the “BSS segment.”
Extern? This is essentially “global.”

Static: a variable known only in function . Stored the same place as globals, typically, but only available to the compilation unit that contains them. If they are block-scope global, only available within that block and its subblocks.
Local: Stored on the stack, typically. Only available in that block and its subblocks. (Although pointers to locals can be passed to functions invoked from within a scope where that local is valid.)


Register: A type of auto variable. “local” vs. “register.” The only difference is that the C compiler will not let you take the address of something you’ve declared as “register.”

52.What are register variables? What are the advantages of using register variables? Ans: If a variable is declared with a register storage class,it is known as register variable.The

register variable is stored in the cpu register instead of main memory.Frequently used variables
are declared as register variable as it’s access time is faster.

53.What is the use of typedef?
Ans: The typedef help in easier modification when the programs are ported to another machine.

A descriptive new name given to the existing data type may be easier to understand the code.

54.Can we specify variable field width in a scanf() format string? If possible how? Ans: All field widths are variable with scanf(). You can specify a maximum field width for a given field by placing an integer value between the ‘%’ and the field type specifier. (e.g. %64s). Such a specifier will still accept a narrower field width.

The one exception is %#c (where # is an integer). This reads EXACTLY # characters, and it is the only way to specify a fixed field width with scanf().

55.Out of fgets() and gets() which function is safe to use and why?
Ans: fgets() is safer than gets(), because we can specify a maximum input length. Neither one is completely safe, because the compiler can’t prove that programmer won’t overflow the buffer he pass to fgets ().

56.What is a modulus operator? What are the restrictions of a modulus operator?

A Modulus operator gives the remainder value. The result of x%y is obtained by (x-(x/y)*y). This operator is applied only to integral operands and cannot be applied to float or double.

57.Difference between strdup and strcpy?
Ans: Both copy a string. strcpy wants a buffer to copy into. strdup allocates a buffer using malloc().

Unlike strcpy(), strdup() is not specified by ANSI .

58.What is recursion?
Ans: A recursion function is one which calls itself either directly or indirectly it must halt at a definite point to avoid infinite recursion.

59.Differentiate between for loop and a while loop? What are it uses?
Ans: For executing a set of statements fixed number of times we use for loop while when


the number of iterations to be performed is not known in advance we use while loop.

60.What is storage class? What are the different storage classes in C?
Ans: Storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage. The storage classes in c are auto, register, and extern, static, typedef.

61.What the advantages of using Unions?
Ans: When the C compiler is allocating memory for unions it will always reserve enough room for the largest member.

62.What is the difference between Strings and Arrays?
Ans: String is a sequence of characters ending with NULL .it can be treated as a one dimensional array of characters terminated by a NULL character.

63.What is a far pointer? Where we use it?
Ans: In large data model (compact, large, huge) the address B0008000 is acceptable because in these model all pointers to data are 32bits long. If we use small data model(tiny, small, medium) the above address won’t work since in these model each pointer is 16bits long. If we are working in a small data model and want to access the address B0008000 then we use far pointer. Far pointer is always treated as a 32bit pointer and contains a segment address and offset address both of 16bits each. Thus the address is represented using segment : offset format B000h:8000h. For any given memory address there are many possible far address segment : offset pair. The segment register contains the address where the segment begins and offset register contains the offset of data/code from where segment begins.

64.What is a huge pointer?
Ans: Huge pointer is 32bit long containing segment address and offset address. Huge pointers are normalized pointers so for any given memory address there is only one possible huge address segment: offset pair. Huge pointer arithmetic is doe with calls to special subroutines so its arithmetic slower than any other pointers.

65.What is a normalized pointer, how do we normalize a pointer?
Ans: It is a 32bit pointer, which has as much of its value in the segment register as possible. Since a segment can start every 16bytes so the offset will have a value from 0 to F. for normalization convert the address into 20bit address then use the 16bit for segment address and 4bit for the offset address. Given a pointer 500D: 9407,we convert it to a 20bitabsolute address 549D7,Which then normalized to 549D:0007.

66.What is near pointer?
Ans: A near pointer is 16 bits long. It uses the current content of the CS (code segment) register (if the pointer is pointing to code) or current contents of DS (data segment) register (if the pointer is pointing to data) for the segment part, the offset part is stored in a 16 bit near pointer. Using near pointer limits the data/code to 64kb segment.



67.In C, why is the void pointer useful? When would you use it?
Ans: The void pointer is useful because it is a generic pointer that any pointer can be cast into and back again without loss of information.

68.What is a NULL Pointer? Whether it is same as an uninitialized pointer?
Ans: Null pointer is a pointer which points to nothing but uninitialized pointer may point to anywhere.

69.Are pointers integer?
Ans: No, pointers are not integers. A pointer is an address. It is a positive number.

70.What does the error ‘Null Pointer Assignment’ means and what causes this error?
Ans: As null pointer points to nothing so accessing a uninitialized pointer or invalid location may cause an error.

71.Difference between const char* p and char const* p
Ans: In const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character pointed by p but u can make ‘p’ refer to some other location. in char const* p, the ptr ‘p’ is constant not the character referenced by it, so u cant make ‘p’ to reference to any other location but u can change the value of the char pointed by ‘p’.

72.What is hashing?
Ans: To hash means to grind up, and that’s essentially what hashing is all about. The heart of a hashing algorithm is a hash function that takes your nice, neat data and grinds it into some random-looking integer. The idea behind hashing is that some data either has no inherent

ordering (such as images) or is expensive to compare (such as images). If the data has no inherent ordering, you can’t perform comparison searches.
If the data is expensive to compare, the number of comparisons used even by a binary search might be too many. So instead of looking at the data themselves, you’ll condense (hash) the data to an integer (its hash value) and keep all the data with the same hash value in the same place. This task is carried out by using the hash value as an index into an array.
To search for an item, you simply hash it and look at all the data whose hash values match that of the data you’re looking for. This technique greatly lessens the number of items you have to look at. If the parameters are set up with care and enough storage is available for the hash table, the number of comparisons needed to find an item can be made arbitrarily close to one.
One aspect that affects the efficiency of a hashing implementation is the hash function itself. It should ideally distribute data randomly throughout the entire hash table, to reduce the likelihood of collisions.
Collisions occur when two different keys have the same hash value. There are two ways to resolve this problem. In open addressing, the collision is resolved by the choosing of another position in the hash table for the element inserted later. When the hash table is


searched, if
the entry is not found at its hashed position in the table, the search continues checking until either the element is found or an empty position in the table is found.

The second method of resolving a hash collision is called chaining. In this method, a bucket or linked list holds all the elements whose keys hash to the same value. When the hash table is searched, the list must be searched linearly.

73.How can you determine the size of an allocated portion of memory?
Ans: You can’t, really. free() can , but there’s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there’s no guarantee the trick won’t change with the next release of the compiler

74.What is generic pointer in C?
Ans: In C void* acts as a generic pointer. When other pointer types are assigned to generic pointer, conversions are applied automatically (implicit conversion).

75.Are the expressions arr and &arr same for an array of integers? Ans: Yes for array of integers they are same.

76.What is Operator overloading ?
Ans: When an operator is overloaded, it takes on an additional meaning relative to a certain class. But it can still retain all of its old meanings.

Examples:
1) The operators >> and << may be used for I/O operations because in the header, they are overloaded.
2)    In a stack class it is possible to overload the + operator so that it appends the contents of one stack to the contents of another. But the+ operator still retains its original meaning relative to other types of data.

77.IMP>How pointer variables are initialized?
Ans: Pointer variables are initialized by one of the following ways. I. Static memory allocation

II. Dynamic memory allocation

78.What is static memory allocation?
Ans: Compiler allocates memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address is assigned to a pointer variable. This way of assigning pointer value to a pointer variable at compilation time is known as static memory allocation.

79.What is dynamic memory allocation?
Ans: A dynamic memory allocation uses functions such as malloc() or calloc() to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these function are assigned to pointer variables, such a way of allocating memory at run time is known as dynamic memory allocation.



80.What is the purpose of realloc?
Ans: It increases or decreases the size of dynamically allocated array. The function realloc (ptr,n) uses two arguments. The first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument specifies the new size. The size may be increased or decreased. If sufficient space is not available to the old region the function may create a new region.

81.What is pointer to a pointer?
Ans: If a pointer variable points another pointer value. Such a situation is known as a pointer to a pointer.

Example:
int *p1,**p2,v=10; P1=&v; p2=&p1;

Here p2 is a pointer to a pointer.

82.What is an array of pointers?
Ans: if the elements of an array are addresses, such an array is called an array of pointers.

83.Difference between linker and linkage?
Ans: Linker converts an object code into an executable code by linking together the necessary built in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.

84.Is it possible to have negative index in an array?
Ans: Yes it is possible to index with negative value provided there are data stored in this location. Even if it is illegal to refer to the elements that are out of array bounds, the compiler will not produce error because C has no check on the bounds of an array.

85.Why is it necessary to give the size of an array in an array declaration? Ans: When an array is declared, the compiler allocates a base address and reserves

enough space in memory for all the elements of the array. The size is required to allocate the required space and hence size must be mentioned.

86.What modular programming?
Ans: If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming.

87.What is a function?

Ans: A large program is subdivided into a number of smaller programs or subprograms. Each subprogram specifies one or more actions to be performed for the larger program. Such sub programs are called functions.

88.What is an argument?


Ans: An argument is an entity used to pass data from the calling to a called function.

89.What are built in functions?
Ans: The functions that are predefined and supplied along with the compiler are known as built-in functions. They are also known as library functions.

90.What is a preprocessor, what are the advantages of preprocessor?
Ans: A preprocessor processes the source code program before it passes through the compiler.

1- a preprocessor involves the readability of program
2- It facilitates easier modification
3- It helps in writing portable programs
4- It enables easier debugging
5- It enables testing a part of program
6- It helps in developing generalized program

91.What are the facilities provided by preprocessor? Ans:

1-file inclusion
2-substitution facility
3-conditional compilation

92.Write a program to interchange 2 variables without using the third one.

Ans:
a ^= b; ie a=a^b b ^= a; ie b=b^a; a ^= b ie a=a^b;

here the numbers are converted into binary and then xor operation is performed. You know, you’re just asking “have you seen this overly clever trick that’s not worth applying onmodern architectures and only really applies to integer variables?”

93.IMP>what are C tokens?
Ans: There are six classes of tokens: identifier, keywords, constants, string literals, operators and other separators.

94.What are C identifiers?
Ans: These are names given to various programming element such as variables, function, arrays.It is a combination of letter, digit and underscore.It should begin with letter. Backspace is not allowed.

95.Difference between syntax vs logical error? Ans:
Syntax Error

1-These involves validation of syntax of language.
2-compiler prints diagnostic message.


Logical Error

1-logical error are caused by an incorrect algorithm or by a statement mistyped in such a way
that it doesn’t violet syntax of language. 2-difficult to find.

96.What is preincrement and post increment?
Ans: ++n (pre increment) increments n before its value is used in an assignment operation or any

expression containing it. n++ (post increment) does increment after the value of n is used.

97.What is a file pointer?
Ans: The pointer to a FILE data type is called as a stream pointer or a file pointer. A file pointer points to the block of information of the stream that had just been opened.

98.How is fopen()used ?
Ans: The function fopen() returns a file pointer. Hence a file pointer is declared and it is assigned

as
FILE *fp;
fp= fopen(filename,mode);
filename is a string representing the name of the file and the mode represents: “r” for read operation
“w” for write operation “a” for append operation

“r+”,”w+”,”a+” for update operation

99.What is the difference between goto and longjmp() and setjmp()?
Ans: A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution. Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program. A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions.

When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.

However, there is a major drawback to using these functions: your program, when restored to its previously saved state, will lose its references to any dynamically allocated memory between the
longjmp() and the setjmp(). This means you will waste memory for every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be horribly inefficient. It is highly recommended that you avoid using functions such


as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming practice.

100.How is a file closed ?
Ans: A file is closed using fclose() function Eg. fclose(fp);

Where fp is a file pointer.

101.What is a random access file? Ans:

A file can be accessed at random using fseek() function fseek(fp,position,origin);
fp file pointer position number of bytes offset from origin origin 0,1 or 2 denote the beginning ,current position or end of file respectively.

102.What is the purpose of ftell ?
Ans: The function ftell() is used to get the current file represented by the file pointer. ftell(fp);

returns a long integer value representing the current file position of the file pointed by the file pointer fp.If an error occurs ,-1 is returned.

103.What is the purpose of rewind() ?
Ans: The function rewind is used to bring the file pointer to the beginning of the file. Rewind(fp);

Where fp is a file pointer.Also we can get the same effect by feek(fp,0,0);

104.Is the allocated space within a function automatically deallocated when the function returns?
Ans: No pointer is different from what it points to .Local variables including local pointers

variables in a function are deallocated automatically when function returns.,But in case of a
local pointer variable ,deallocation means that the pointer is deallocated and not the block of
memory allocated to it. Memory dynamically allocated always persists until the allocation is freed or the program terminates.

105.Discuss on pointer arithmetic? Ans:

1- Assignment of pointers to the same type of pointers. 2- Adding or subtracting a pointer and an integer. 3-subtracting or comparing two pointer.

4-incrementing or decrementing the pointers pointing to the elements of an array. When a pointer


to an integer is incremented by one , the address is incremented by two. It is done automatically
by the compiler.
5-Assigning the value 0 to the pointer variable and comparing 0 with the pointer. The pointer
having address 0 points to nowhere at all.

106.What is the invalid pointer arithmetic? Ans:

i)  adding ,multiplying and dividing two pointers.
ii)  Shifting or masking pointer.
iii)  Addition of float or double to pointer.
iv)  Assignment of a pointer of one type to a pointer of another type

107.What are the advantages of using array of pointers to string instead of an array of strings?
Ans:

i)  Efficient use of memory.
ii)  Easier to exchange the strings by moving their pointers while sorting.

108.What is the difference between text and binary modes?
Ans: Streams can be classified into two types: text streams and binary streams. Text streams are interpreted, with a maximum length of 255 characters. With text streams, carriage return/line feed combinations are translated to the newline n character and vice versa. Binary streams are uninterrupted and are treated one byte at a time with no translation of characters. Typically, a text stream would be used for reading and writing standard text files, printing output to the screen or

printer, or receiving input from the keyboard. A binary text stream would typically be used for reading and writing binary files such as graphics or word processing documents, reading mouse input, or reading and writing to the modem.

109.Are the expressions *ptr ++ and ++ *ptr same?
Ans: No,*ptr ++ increments pointer and not the value pointed by it. Whereas ++ *ptr increments the value being pointed to by ptr.

110.What would be the equivalent pointer expression foe referring the same element as
a[p][q][r][s] ?
Ans : *( * ( * ( * (a+p) + q ) + r ) + s)

111.Are the variables argc and argv are always local to main? Ans: Yes they are local to main.

112.Can main () be called recursively?
Ans: Yes any function including main () can be called recursively.

No comments:

Post a Comment