QUAN C Objective
QUAN C Objective
-----------------------------------------Introduction to problem solving -----------------------------------------Q. What are the applications of C programming? Though C is a general-purpose programming language, it has got some special area of work now :
Device Driver / controller development programming Network / Socket programming Mobile Application Development Cryptographic programming
Q. What is complexity? An algorithm is a method for solving a class of problems on a computer and the Complexity of an algorithm is the cost, measured in running time, or storage, or whatever units are relevant, of using the algorithm to solve one of those problems. It is a relative term in comparison with other algorithms. Complexity is of two types :
Time Complexity(number of steps that it takes to solve an instance of the problem as a function of the size of the input (usually measured in bits)) Space Complexity(is a related concept, that measures the amount of space, or memory required by the algorithm.)
-----------------------------Fundamental of C
-----------------------------Q. With suitable example differentiate between a++ and ++a. a++ is the post-increment operator whereas ++a is the pre-increment operator. They behave differently when they are used in expressions on the right-hand side of an assignment statement as follows:
They can have alphabets, digits and underscore characters. They must not begin with a digit. Uppercase and lowercase letters are distinct. Keywords cannot be used as identifiers.
Q. What is the difference between a and a? a denotes a character constant whereas a denotes a string constant. In C any character enclosed within single inverted commas indicates a character constant. The maximum length of a character constant can be 1 character. Ex. a, R, 5, =. On the other hand, we can enclose any no of characters including blank spaces within double quotes. Ex. a, 123, C is a programming language. Q. What is an unsigned integer constant? What is the significance of declaring a constant unsigned
integer?
If we declare a variable as unsigned integer constant the range of permissible integer values (for a 16-bit OS) will shift from the range 32768 to +32767 to the range 0 to 65535. Declaring an integer as unsigned almost doubles the size of the largest possible value that it can otherwise take. So if we know in advance that the value stored in a given integer variable will always be positive we can declare the variable to be unsigned as unsigned int <variable_name>; Q. What is implicit and explicit data type conversion in C? Implicit type conversion : Conversion from lower data type to higher data type is done automatically. This is known as implicit data type conversion, also known as Coercion. It is an automatic type conversion by the compiler. float type data automatically gets converted into double. char and short type data automatically gets converted into int. In a mixed-type expression, data of one or more subtypes can be converted to a supertype as needed at runtime so that the program will run correctly. For example, the following is legal C language code: E.g. double d; long l; int i; if(d > i) d = i; if(i > l) l = i; if(d == l) d *= 2; Explicit type conversion : Here, the RHS data is converted to LHS data by explicit cast operation.
E.g.
Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102 here 2 double variables are first cast into int & then added up. double da = 5.5; double db = 5.5; int result = static_cast<int>(da) + static_cast<int>(db); //Result would be equal 10 and not 11
Q. Explain the difference between L-value and R-value. An Lvalue is an object locator, an expression that designates an object. Historically, L stood for left, meaning that an lvalue could legally stand on the left (the receiving end) of an assignment statement. Now only modifiable lvalues can legally stand on the left of an assignment statement. On the other hand, R-value goes to the right of the assignment operator. Q. What do you understand by the term coercion? Conversion from lower data type to higher data type is done automatically. This is known as implicit data type conversion, also known as Coercion. It is an automatic type conversion by the compiler. float type data automatically gets converted into double. char and short type data automatically gets converted into int. In a mixed-type expression, data of one or more subtypes can be converted to a supertype as needed at runtime so that the program will run correctly. For example, the following is legal C language code: E.g. double d; long l; int i; if(d > i) d = i; if(i > l) l= i; if(d == l) d*= 2;
------------------------------------Functions and statements ------------------------------------Q. What are the three conversion specifications used to read char type data? char ch; 1) scanf(%c,&ch); 2) scanf(%s,ch); 3) scanf(%d,&ch); Q. What is the significance of \v and \b escape sequences? Give example. \v is to print a Vertical Tab : printf(\v); \b is to print a backspace : printf(\b); Q. How will you convert the value of a character into ASCII? Here is the code for conversion : void main() { char ch; printf(\nEnter a character : ); scanf(%c,&ch); //read the character value printf(ASCII is : %d,ch); //print the value as integer
----------------------------------Control Flow Statement ----------------------------------Q. Differentiate between while and do-while loop
can be used as an entry-controlled loop can be used as an infinite loop(used deliberately with a conditional break) more structured looking as far as syntax is concerned can be used to substitute a recursive task
Q. Write short notes on empty loop. The loop that has no body is called an empty loop. Such as, in case of while loop an empty loop can be constructed as follows: while(condition); Also, in for statement, for(n=1;n<10000;n++); Empty loop can be used as a time delay loop, for example: x=1; while(x++<10000); Q. How can time delay loop be written in C? Give examples. A time delay loop can be written using empty loop, which is a loop that doesnt contain even a single statement in the loop body, but with a high value as condition to be false. for(n=1;n<10000;n++); However, it is totally dependent on the machine clock speed hence not a good way to apply delays. Secondly, We can use some library function like delay() or sleep() to make some useful held-up of a program for some seconds/milliseconds passed as argument to the function. Example:
-----------------------Arrays ------------------------
Q. What are the limitations of an ordinary variable? How are these removed with the use of an array? 1) For each variable we can store a single value in it. e.g. int a; If we have to store a larger collection of similar types of values, say of hundred in number, declaring that many number of variables will be too tedious, so here we have to use an array. e.g. int a1, a2, .. ,a100; //this is too long alternatively, int a[100]; 2) In case of normal variable, all the variables are not ideally located at contiguous memory locations but on the other hand in case of an array all the data of array will be placed at the contiguously in the memory. 3) In normal variables, keeping track of each & individual variable within a long list of variables is a difficult job : programmers has to keep in mind the purpose of each variable. On the other hand in case of an array this need not be cared of since the array index itself is the single point of access to an array(array-name) and an array is formed comprising of several values ideally to serve a common purpose. Q. Explain the significance of subscript in an array. An array is a collection of similar type of variables under a common name. Now, in order to refer each value of the variables we have to use some kind of index which is known as a subscript of an array. Depending on the value of the subscript we can locate a specific variable of the array & can make use of it. E.g. int arr[10]; int i=4; scanf(%d, &arr[i]); //read the 5th variable printf(%d, arr[i]); //print the 5th variable Here i is the subscript of the array. However, this can be written in another syntax too : scanf(%d, (arr+i)); //read the 5th variable printf(%d, *(arr+i)); //print the 5th variable Q. What are static initialization and dynamic initialization of array? In static initialization of array, the array elements are initialized when they are declared. The general form of initialization of array is: type <array_name>[size] = {list of values}; Example: int number[3] = {12, 345, 1}; char name[10] = { J, o, h, n, \0 }; On the other hand in dynamic initialization of array, the array elements are explicitly initialized at run time. Example: int x[10]; for( j=0; j<10; j++)
----------------------------String Manipulations ----------------------------Q. What are the difference between strlen() and sizeof()?
Q. What is the role played by atoi() in string manipulation? Give examples. It is a macro that converts a string to integer. Its declared in the header file : stdlib.h Declaration: int atoi(const char *s); Remarks: atoi() converts a string pointed to by s to int. Example: #include <stdlib.h> #include <stdio.h> void main() { int n; char *str = 12345.67; n = atoi(str); printf(string = %s integer = %d\n, str, n); } Output : string = 12345.67 integer = 12345
-----------------Functions -----------------Q. Can we pass function to other functions? If yes, how? Yes, we can pass a function to another function as argument of it. Heres the example : //Function pointer program... void young(int); void old(int); //prototype of function in which function pointer is passed as an argument void greeting(void (*)(int), int); int main(void) { int age; printf(How old are you? ); scanf(%d, &age); if(age > 30) { greeting(old, age); //call of the outer function }
--------------------------Structure & Union --------------------------Q. What are the differences between structure and union?
Q. In what all situations structures are preferred? Ordinary variables can hold one piece of information and arrays can hold a number of pieces of information of same data type. But quite often we deal with entities that are collection of dissimilar data types. For example, if we want to store data about a book which contains its name (a string), its price (a float) and In such situations if we construct individual arrays the program becomes difficult. So the convenient way is to declare a structure variable, which can contain dissimilar data types like, struct book { char name; float price; int pages; }; Q. What are self-referential structures? Lists contain a reference variable of the type itself as a member. For this reason, these data structures are frequently called self-referential or recursive data structures. The simplest type of self-referential list structure is a sequence of elements. Each element contains some data, and a reference, often called a pointer, to the next element in the list. We can do it as shown in the following example which will allow us to create an arbitrarily long list of integer values.
Enumerated data types are used with fixed set of enumerators. Once defined, this number can not be changed. Enumerators can be overridden by other values if defined so, but can not be used to hold floating point values or string values.
Q. In what sense a structure can be recursive. A structure can be recursive if it is a self-referential structure, that means: a structure having a pointer member referring to a structure of same type. E.g. struct list { int data; struct list *next; //self-referential }; Q. What are the difficulties of processing a union? In case of a union, we cannot ideally use all the data members of it at the same time since they share the same memory space in case of a union. In fact, the size of the largest data member of the union forms the size of it.
---------------------Pointers ---------------------Q. How can we pass pointers to a function? Passing pointers to a function means actually passing the address of a variable to the function. The process is called call by reference. main() { int *x = 20; change(x); printf(%d\n, x); } change(int *p) { *p = *p + 10; } Here, the address of x has been passed to the called function change() which receives the address as pointer variable *p. Q. Differentiate between pointer to a variable and pointer to a pointer. A pointer to a variable keeps the address of that variable. Ex. int a, *p; Here p is a pointer to the variable a, that is p keeps the address of a. On the other hand, pointer to a pointer means a pointer, which keeps the address of another pointer type variable. Ex. int a, *p, **p1; Here, p1 is a pointer to the pointer p, that is p1 keeps the address of p which itself is a pointer to the variable a.
10
11