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

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102

-----------------------------------------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. How is source program converted to executable program in C?

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:

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102


a = 5; b = ++a; // a=6, b=6. Again, a = 5; b = a++; // a=6, b=5. A pre-increment operator first adds 1 to the operand and then the result is assigned to the variable on left. On the other hand, a postfix operator first assigns the value to the variable on the left and then increments the operand. Q. What are the naming rules of an identifier? C identifiers enforce the foll owng rules:

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

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102

----------------------------------Control Flow Statement ----------------------------------Q. Differentiate between while and do-while loop

Q. Write briefly the areas of application of for statement. For 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 ------------------------

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102

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()?

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102

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 }

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102


else { greeting(young, age); //call of the outer function } return 0; } //definition of the outer function void greeting(void (*fp)(int), int k) { fp(k); } //inner function definition void young(int n) { printf(Being only %d, you sure are young.\n, n); } //inner function definition void old(int m) { printf(Being already %d, you sure are old.\n, m); } Q. What is the role of library function? In writing a program we need library functions such as printf(), scanf() etc. that perform fundamental operations in a computer, such as getting input from the user, printing output to the screen etc. These library functions have been already designed, coded and tested so that we may use them in creating our own functions. To make programming platform independent the compiler designers have included libraries of such primitive functions. We can use these functions in our programs without bothering as to how they are implemented on a particular platform. Q. Is prototyping mandatory in C? Justify your answer. Yes. Prototyping is mandatory in C. Either for User-defined or Library functions prototyping is must. It is basically the declaration or signature of the function. For library functions, prototyping is implicitly done when we include the respective header file. E.g. a) User-defined functions : int add(int, int); //Prototype of function add() int add(3, 5); //Call b) Library functions : #include<stdio.h> //Prototype for printf() is implicitly included in the code printf(\nHello); //Call Q. What do you mean by call by value and call by reference? In call by value, only the values of the variables are passed to the called function. So any changes made to the called function does not effect in the calling function. On the other hand, in call by reference the address of the variables are passed. So if we make any change in the called function, that changes also reflect in the calling function. Q. What are the conditions to perform recursion? In order to solve a problem recursively, two conditions must be satisfied: 1. The problem must be written in recursive form.

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102


2. The problem statement must include a stopping condition. Q. What are the advantages of user-defined functions? Advantages of user-defined function : Writing functions prevents rewriting the same code over and over. By using functions it becomes easier to write programs and keep track of what they are doing Debugging / Modification / Maintenance becomes easier. Code abstraction is implemented. Top Down approach is realized. Q. What are the advantages and disadvantages of recursion? The main disadvantage is : often the algorithm may require large amounts of memory if the depth of the recursion is very large. Complexity increases with the usage of system stack, both Time and Space-wise, than a iterative version of the same task. On the other hand as advantage, it has been claimed that recursive algorithms are visibly simple, easier to understand because the code is shorter and is closer to a mathematical definition. Q. Why the parameters given within a function prototype are called dummy parameters? At the time of call & definition of a function, the actual & formal parameters are mentioned respectively. Whereas during the declaration / prototyping of a user-defined function, only the data-types of the arguments are mentioned, not the actual data. Therefore, these parameters are called dummy parameters. Q. Differentiate between a function prototype and function definition. Function prototype: Function prototype is the declaration of the function which consists of the Function name Number of arguments & their types Return type E.g. int add(int, int); Function definition : Function definition consists of : Body / code-details of the function followed by : Function name Formal parameters to the function Return type E.g. int add(int x, int y) { } Function definition : Function definition consists of : Body / code-details of the function followed by : Function name Formal parameters to the function Return type E.g. int add(int x, int y) { return x+y; } Q. What are the different storage classes available in C. 1) Automatic: auto storage is automatically allocated on function/block entry and automatically freed when the function/block is exited. 2) Optimization Hint: register register provides a hint to the compiler that you think a variable will be frequently used.

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102


3) Static Storage: static if used inside a block or function, the compiler will create space for the variable which lasts for the life of the program. 4) External References: extern If a variable is declared (with global scope) in one file but referenced in another, the extern keyword is used to inform the compiler of the variables existence. Q. What do you understand by #define? The #define statement is a precompiler directive: it changes the source code before its compiled. In a #define directive, the string following the word #define, up to the first blank, is replaced by the model statement that follows, if any. In practice, #defines are used for simple text substitution, as macros, or as input to conditional compiler directives. Each #define will stay in effect until a matching #undef statement or the end of the program. Its considered good style to use all UPPERCASE for a #define. E.g. #define TRUE 1 #define FALSE 0 #define TABLESIZE 100

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

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102


struct list { int data; struct list *next; //self-referential }; Q. What are the restrictions on processing of enumerated data type?

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

Programming in C : BCA 104 / BSCIT 102/ MCA 102/ MSCIT 102


Q. List the applications of pointers in C. Call a function where arguments are passed by address Self-referential structure implementation (Linked list) Dynamic memory allocation Used to directly manipulate memory or memory-mapped devices. Q. Under what condition a pointer variable can point to another pointer variable? A pointer variable can point to another pointer variable when Left Hand Side pointer variable of the assignment is a pointer to pointer of similar data type. E.g. int p1,*p2,**p3; p1 = 100; p2 = &p1; now we can assign the address of p1 which is itself a pointer variable to p3. p3 = &p2;

Unit 10 File Handeling


Q. What are the advantages of data file over other data types? Advantages : 1) Data file provides repository of several data items, whereas Data type defines the domain or range for values of Data. 2) A Data file can hold data of several Data types but the reverse is NOT possible. Q. What is the significance of the constant EOF in a C program? EOF is a constant, which indicates that end of file has been reached. This is inserted beyond the last character in the file. The reading should be terminated when EOF is encountered. EOF is very important in testing the end-of-file condition. Any attempt to read past the end of file might either cause the program to terminate with an error or result in an infinite loop.

11

You might also like