C Programming theory Questions and answers – Part 5

201. What is the difference between exit() and return() in c?
Answer : Exit() is a system call which terminates current process. Exit() is not an instruction of c language. Whereas, return() is a c language instruction/statement and it returns from the current function (I.E. Provides exit status to calling function and provides control back to the calling function).

202. What is the difference between memcpy() & strcpy() functions in c?
Answer : Memcpy() function is used to copy a specified number of bytes from one memory to another. Whereas, strcpy() function is used to copy the contents of one string into another string.
Memcpy() function acts on memory rather than value. Whereas, strcpy() function acts on value rather than memory.

203. What is the difference between memcpy() & memmove() functions in c?
Answer : Memcpy() function is is used to copy a specified number of bytes from one memory to another.
Memmove() function is used to copy a specified number of bytes from one memory to another or to overlap on same memory.
Difference between memmove() and memcpy() is, overlap can happen on memmove(). Whereas, memory overlap won’t happen in memcpy() and it should be done in non-destructive way.

204. Is there any inbuilt library function in C to remove leading and trailing spaces from a string? How will you remove them in C?
Answer : There is no inbuilt function to remove leading and trailing spaces from a string in c. We need to write our own function to remove them. We need to check first non-space character in given string. Then, we can copy that string from where non space character is found. Then, we can check whether any spaces are available in copied string from the end of the string. If space is found, we can copy ‘\0’ in that space until any character is found. Because, ‘\0’ indicates the end of the string. Now, we have removed leading and trailing spaces in a given string.

205. Can array subscripts have negative value in C ?
Answer : No. Array subscripts should not have negative value. Always, it should be positive.

C Programming theory Questions and answers – Part 4

151. What is an endless loop?
Answer : An endless loop can mean two things. One is that it was designed to loop continuously until the condition within the loop is met, after which a break function would cause the program to step out of the loop. Another idea of an endless loop is when an incorrect loop condition was written, causing the loop to run erroneously forever. Endless loops are oftentimes referred to as infinite loops.

152. Is this program statement valid ?
Answer : Assuming that INT is a variable of type float, this statement is valid. One may think that INT is a reserved word and must not be used for other purposes. However, recall that reserved words are express in lowercase, so the C compiler will not interpret this as a reserved word.

153. What are formal parameters?
Answer : In using functions in a C program, formal parameters contain the values that were passed by the calling function. The values are substituted in these formal parameters and used in whatever operations as indicated within the main body of the called function.

154. Describe how arrays can be passed to a user defined function
Answer : One thing to note is that you cannot pass the entire array to a function. Instead, you pass to it a pointer that will point to the array first element in memory. To do this, you indicate the name of the array without the brackets.

155. How do you convert strings to numbers in C?
Answer : You can write you own functions to do string to number conversions, or instead use C’s built in functions. You can use atof to convert to a floating point value, atoi to convert to an integer value, and atol to convert to a long integer value.

C Programming theory Questions and answers – Part 3

101. What is a far pointer? Where we use it?
Answer : 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.

102. Describe the order of precedence with regards to operators in C.
Answer : Order of precedence determines which operation must first take place in an operation statement or conditional statement. On the top most level of precedence are the unary operators !, +, – and &. It is followed by the regular mathematical operators (*, / and modulus % first, followed by + and -). Next in line are the relational operators <, <=, >= and >. This is then followed by the two equality operators == and !=. The logical operators && and || are next evaluated. On the last level is the assignment operator =.

103. What are binary trees?
Answer : Binary trees are actually an extension of the concept of linked lists. A binary tree has two pointers, a left one and a right one. Each side can further branch to form additional nodes, which each node having two pointers as well.
104. What is wrong with this program statement? void = 10;
Answer : The word void is a reserved word in C language. You cannot use reserved words as a user-defined variable.

105. What are global variables and how do you declare them?
Answer : Global variables are variables that can be accessed and manipulated anywhere in the program. To make a variable global, place the variable declaration on the upper portion of the program, just after the preprocessor directives section.

C Programming theory Questions and answers – Part 2

51. What is dynamic memory allocation?
Answer : 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.

52. What is a function?
Answer : 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.

53. What is an argument?
Answer : An argument is an entity used to pass data from the calling to a called function.

54. what is the similarity between a Structure, Union and enumeration?
Answer : All of them let the programmer to define new data type.

55. Difference between array and pointer?
Answer : Array
a – Array allocates space automatically
b – It cannot be resized
c – It cannot be reassigned
d – sizeof (arrayname) gives the number of bytes occupied by the array.
a – Explicitly assigned to point to an allocated space.
b – It can be sized using realloc()
c – pointer can be reassigned.
d – sizeof (p) returns the number of bytes used to store the pointer variable p.

C Programming theory Questions and answers – Part 1

1. Explain what is pointer?
Answer : Pointers is a variable that stores or points the address of another variable. A Pointer in C is used to allocate memory dynamically, i.e. at run time. The pointer variable can be of any of the data type such as int, float, char, double, short etc.

2. What are header files and what are its uses in C programming?
Answer : Header files are also known as library files. They contain two essential things: the definitions and prototypes of functions being used in a program. Simply put, commands that you use in C programming are actually functions that are defined from within each header files. Each header file contains a set of functions. For example: stdio.h is a header file that contains definition and prototypes of commands like printf and scanf.

3. What is the difference between structured oriented, object oriented and non-structure oriented programming language?
Answer : Structured oriented programming language
In this type of language, large programs are divided into small programs called functions.
Prime focus is on functions and procedures that operate on data
Data moves freely around the systems from one function to another
Program structure follows “Top Down Approach”
Object oriented programming language
In this type of language, programs are divided into objects
Prime focus is on the data that is being operated and not on the functions or procedures
Data is hidden and cannot be accessed by external functions
Program structure follows “Bottom UP Approach”
Non-structure oriented programming language
There is no specific structure for programming this language.

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

5. What is the difference between an enumeration and a set of pre-processor # defines?
Answer : There is hardly any difference between the two, except that #defines has a global effect (throughout the file) whereas an enumeration can have an effect local to the block if desired. Some advantages of enumeration are that the numeric values are automatically assigned whereas in #define we have to explicitly define them. A disadvantage is that we have no control over the size of enumeration variables.