Sei sulla pagina 1di 15

C INTERVIEW QUESTIONS WITH ANSWERS . Q: What does static variable mean? A: They are stored in memory.

Default initial value is zero. Their scope is local to the block in which the variable is defined & value of the variable persists between different function calls. Avoid using static unless we need it. Because their values are kept in memory when the variables are not active, which means they take up space in memory that could otherwise used by other variables. Q: What is a pointer? A: Pointer is an address variable which contains address of another variable. Pointer operators are * and &. int pointer contains address of int quantity, float contains float quantity, same holds good for each data type even for user defined data types. Maximum size of pointer is 2 bytes. Ex of pointer declaration: int a=27; int *p; p=&a; OR int *p=&a; Q: What is a structure? A: A structure is a collection of one or more variables, possibly of different data types, grouped together under a single name for convenient handling. Q: What are the differences between structures and arrays? A: In array we can store variables of same data type while in structure we can store variables of different data types. In C++ structure, we can add function also to the structure. Q: In header files whether functions are declared or defined? A: Functions are declared in header files & defined in the program. This can be explained as if we use printf() without including stdio.h then it will show complile time error as function should have prototype. With this function prototype compiler came to know which type of functions we r using. Thats why function prototype should be given before using predefined or userdefined functions. Q: What are the differences between malloc() and calloc()? A: Both these functions are declared in alloc.h & are used to dynamic memory allocation of a variable. Difference is that: malloc() takes only

one argument while calloc() will take two arguments. Ex: Suppose we have to dynamically alloc memory for 10 int variables then it can be done as: int *p=(int *)malloc(n * 2) where n=10 & 2=sizeof(int) int *p=(int *)calloc(10,2) Q: What are macros? What are its advantages and disadvantages? A: Macros are preprocessor directives. They get replaced in source code before compilation. Advantages are: We can replace any valid C statement or condition in user own words. We can do File inclusion also. We can do conditional compilation. We can pass arguments also in Macros. They can be used anywhere inside the program. Disadvantages are: Macro will replace macro template with its macro expansion without checking any error. If there r number of occurrence of macros then it will increase the size of program. Q: Difference between pass by reference and pass by value? A: When we have argument as variable or any constant then function is termed as function pass by value. When we have address of variable as an argument in the function we call the function by reference. Ex: int x=27; void func(x) pass by value void func(27) pass by value void func(&x) pass by reference Q: What is static identifier? A: As explained above. Q: Where are the auto variables stored? A: They are stored in memory & default initial value is garbage value. Q: Where does global, static, local, register variables, free memory and C Program instructions get stored? A: Refer let us C. Q: Difference between arrays and linked list? A: Main difference is that array are static in nature. Once we declare it we cant modify its size. Also inserting or deleting elements in array is difficult. Array have fixed size thats why if at runtime, we want to increase or decrease its size, is not possible. While linked list are

dynamic in nature they dynamically allocate memory for variables. Their size can be decided at runtime. Inserting & deleting element is also easy. One more difference is that searching a particular element is quite easy as it would use array index number while linked list will search the element from the start of list even thought the position of element is last. Thats why traversing takes more time. Q: What are enumerations? A: An enumerated data type is user defined data type which provides a way for attaching name to numbers. Syntax is similar to structure. The enum keyword automatically enumerated the list of variables by assigning value 0,1,2,3. enum shape{circle, square, rectangle) here value of circle=0, square=1, rectangle=2 enum{red, green=7, blue=10}; here value of red=0, green=7, blue=10 We cant use float values in enum, only int values are allowed. Q: Describe about storage allocation and scope of global, extern, static, local and register variables? A: Refer let us C. Q: What are register variables? What are the advantage of using register variables? A: A: Refer let us C. Q: What is the use of typedef? A: typedef keyword is used to rename the data type. Ex: typedef int INTEGER; So instead of int a we have to use INTEGER a; Q: Can we specify variable field width in a scanf() format string? If possible how? A: As in following code: void main() { float f; printf(Enter value of f : ); scanf(%3f,&f); printf(f : %f,f); } If we enter f=12345.6789 it will give result as f=123

Q: Out of fgets() and gets() which function is safe to use and why? A: gets() receives a string from the keyboard. More exactly, gets() gets a new line (\n) terminated string of characters from the keyboard and replaces the \n with \0; Ex: char[10] s; gets(s); fgets() used to read a string from the file. fgets() takes three arguments. First is the address where the string is stored, second I the maximum length of the string which will prevent fgets() from reading in too long a string and overflowing the array. Third one is File pointer. If all lines from file have been read & we attempt to read one more line, in that case fgets() returns null. It also retains \n at the end of string & appends null byte to string to mark the end of string. Syntax: fgets(char *s, int n, FILE *fp) Thats why fgets() is more safer. Q: Difference between strdup and strcpy? A: strcpy() is defined in string.h copies one string into another. Syntax: strcpy(char *destination, char * source); Strdup() is also defined in string.h which will copy the string to newly created location. In strdup() makes a duplicate of source string , obtaining space with a call to malloc. Allocated space is (strlen(source string) +1) byte. Also we have to free this string when it is not used by calling free. Thats why we have to add alloc.h while using strdup(). Ex: char *dupstr=strdup(char * sourcestr); Q: What is recursion? A: A function is called recursive if a statement within the body of a function calls the same function. It is the process of defining something in terms of itself. Q: Differentiate between a for loop and a while loop? What are it uses? A: Simple Question. Q: What are the different storage classes in C? A: auto, register, static & extern are different storage classes. Q: Write down the equivalent pointer _expression for referring the same element a[i][j][k][l]? A: *(*(*(*(a+i)+j)+k)+l) Q: What is difference between Structure and Unions?

A: Declaration syntax is same foe both difference is that structure members have contiguous memory location while union members share common memory location having the size equal to variable size whose size is maximum than other variables. Q: What the advantages of using Unions? A: Unions are very useful while interacting with the hardware of the computer. Q: What are the advantages of using pointers in a program? A: Pointers will increase the execution speed. Using pointers we can acess array as they are correlated to each other. Also pointer can return more than one value from function. From called function we can change value of any variable of calling function using pointer. Q: What is the difference between Strings and Arrays? A: String is a character array. Array is defined as collection of similar data types under single variable name. They are also used as indexed variables as they r accessed by their indexes. So there is no difference between string & array. As string is another name for char array. Q: In a header file whether functions are declared or defined? A: Already explained. Q: How structures are passed & return from the function? A: Structures are user defined data types. So it is possible to pass structure as an argument or functions can return structures. Following example shows how structures are passed as an argument in function : #include<stdio.h> #include<conio.h> void show(struct emp); struct emp { int age; char name[20]; float salary; }e1={21,"Himanshu",27000};

void main() { clrscr(); show(e1); getch(); } void show(struct emp e1) { printf("Age : %d \n",e1.age); printf("Name : %s \n",e1.name); printf("Salary : %f \n",e1.salary); } Following example show how functions return the structure: #include<stdio.h> #include<conio.h> struct emp show(); struct emp { int age; char name[20]; float salary; }; void main() { struct emp e1; clrscr(); e1=show(); clrscr(); printf("Age : %d \n",e1.age); printf("Name : %s \n",e1.name); printf("Salary : %f \n",e1.salary); getch(); } struct emp show() {

struct emp e1; printf("Enter the age : "); scanf("%d",&e1.age); printf("Enter the name : "); scanf("%s",e1.name); printf("Enter the salary : "); scanf("%f",&e1.salary); return e1; } Q: What is argc & argv? A: Command Line Argument, argc stands for argument count & argv stands for argument vector. So, c for count & v for vector. Q: How u will convert this binary digit 1101100100111100 to other system & which will be easier? It is easy to convert binary number into octal number or hexadecimal number system. For conversion follow this number charts for octal & hexdecimal number. To convert into octal number, group the numbers from right to left each consisting of 3 numbers. So for given example: Binary Number: 1101100100111100 Grouping: 1 101 100 100 111 100 Well here the first digit is 1, so make it 001. Grouping: 001 101 100 100 111 100 Octal conversion: 1 5 4 4 7 4 So Octal conversion for 1101100100111100 = 154474 Similarly for hexadecimal conversion, group the numbers from right to left each consisting of numbers. Then follow the table. U will get as: 1101 1001 0011 1100 D 9 3 C So hexadecimal conversion = D93C

000 001 010 011 100 101 110 111 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 9 A B C D E F

Q: Which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an unsigned int or char by 1? A: Left shift will multiply the unsigned & int datatype. For char it will double the ascii value of character. E.g. left shift of A (ascii value=65) will give u character of acii value 130. Q: Are command line args are local? A: Command line arguments are local to main(). The explaination can be given as, by default program name is the first argument, therefore argc cannot be less than 1, by default it is1. Though for different programs argc=1 by default, argv is different as explained earlier. So command line arguments are local to main(). Q: Do structure contains pointer to itself? A: In linked list structure contain pointer to itself. Such pointers are called as self referential pointers. e.g

struct emp { int age; struct emp *next; }; Here struct emp *next is a pointer to structure itself & it points to next structure element. Q: What is a far pointer? where we use it? A: A far pointer is always is treated as a 32 bit pointer & contains both a segment & an offset address. The total memory (1 mb) I divided into a number of units each comprising 65536 (64 kb) locations. Each such unit is called as segment. Each segment always begins at a location number, which is exactly divisible by 16. The segment register contains the address were a segment begins, where as the offset register contains the offset of the data/code from where the segment begins. e.g. if segment register=2 & offset register=5 ten address of data/code is calculated as : (16 * 2) + 5 = 32 + 5 = 37. Declaration : char *far=0xB0008000 here segment register=0xB000 & offset register=8 Generally we r using pointers are called as near pointers of 16 bits. Also 8088 b& 8086 chips (of the old PC & XT) operate only in real mode, and so can address only 1024 KB or 1MB memory. So if u want to use the memory beyond 64KB near pointers are not sufficient, to isolate this problem we have far & huge pointers. Q: How will you declare an array of three function pointers where each function receives two ints and returns a float? A: float ( * ( * arr_fptr[3])(int 1, int 2) ) Q: What is a NULL Pointer? Whether it is same as an uninitialized pointer? A: For each pointer type ( like say a char pointer ) C defines a special pointer value that is guaranteed not to point to any object or function of that type. Usually the null pointer constant is used for representing a null pointer is the integer 0. Uninitialized pointer is not same as null pointer. Q: What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?

A: A NULL macro is used to represent the null pointer in source code. It has a value 0 associated with it. A null pointer is a pointer, which doesnt point anywhere. The ASCII NUL character has all its bits as 0 but doesnt have any relationship with the null pointer. The null string is another name for empty string . Q: What does the error 'Null Pointer Assignment' mean and what causes this error? A: The null assignment error is generated only in small & medium memory models which occurs when program changes the bottom of data segment. In Borlands C compiler, Borland places four zero bytes at the bottom of the data segment, follwed by Borlands copyright notice Borland C++ - Copyright Intl.. In small memory models null pointer points to DS:0000. Thus assigning a value to the memory referenced by this pointer will overwrite the first zero byte in the data segment. At the program termination, the copyright banner & four zeroes are checked & if either has been modified, the null pointer assignment error is generated. Q: Write a program for palindrome checking? A: There r two approaches to solve this problem. First one using functions available in string.h (strrev() & strcmp()) & second one is without using it. First approach by using strrev() & strcmp() from string.h : #include<stdio.h> #include<conio.h> #include<string.h> void main() { char *str1; char *str2; int x; clrscr(); printf("Enter the string : "); gets(str1); str2=strrev(str1); printf("Reverse String : %s \n",str2); x=strcmp(str1,str2); if(x==0) printf("%s is palindrome \n",str1);

else printf("%s is not palindrome \n",str1); getch(); } Second approach by using ASCII concept : #include<stdio.h> #include<conio.h> void main() { char str1[20]; char str2[20]; int i,j; int x,y; clrscr(); printf("Enter the string : "); gets(str1); for(i=0;str1[i]!='\0';i++); i--; for(j=0;i>=0;j++,i--) str2[j]=str1[i]; str2[j]='\0'; printf("Reverse string : %s \n",str2); for(i=0,j=0;str1[i]!='\0';i++,j++) { x=str1[i]; y=str2[j]; if((x-y)!=0) break; } if(x==y) printf("%s is palindrome \n",str1); else printf("%s is not palindrome \n",str1); getch(); } Q: How would you dynamically allocate a one-dimensional and twodimensional array of integers? A: Array elements have contiguous memory location. Similar holds true for two-dimensional array also. So there is no such arrangement that array elements are stored in row & column in memory. Therefore

dynamic allocation for one-dimensional & two-dimensional array of integers is same. Example of dynamic allocation is in preceding question. Q: How can you increase the size of a dynamically allocated array? A: #include<stdio.h> #include<conio.h> #include<alloc.h> void main() { int i,n,*p; clrscr(); printf("How many number of elements you want to enter? "); scanf("%d",&n); p=(int *)malloc(n*2); if(p==NULL) { printf("Memory allocation unsuccessful"); exit(); } for(i=0;i<n;i++) { printf("Enter the element %d : ",(i+1)); scanf("%d",(p+i)); } printf("You had entered the follwing elements : \n"); for(i=0;i<n;i++) printf("Element %d is %d \n",(i+1),*(p+i)); getch(); } Q: How can you increase the size of a statically allocated array? A: I think its impossible to increase the size of statically allocated memory. Otherwise we can use vector for static declaration which can grow or shrink in size as needed. Q: When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?

A: realloc() function used to move a particular block through n bytes. So we have to pass size of blcok & number of bytes. If block points to null it will work same as malloc() or calloc(). Q: Which function should be used to free the memory allocated by calloc()? A: free() from malloc.h Q: How much maximum can you allocate in a single call to malloc()? A: Not more than 64KB in small & tiny memory models. Q: How do you declare the following: A: An array of three pointers to chars => char *arr[3] An array of three char pointers => char *arr[3] A pointer to array of three chars => char *p=&arr[3] A pointer to function which receives an int pointer and returns a float pointer =>(float *)*funcptr(int *) A pointer to a function which receives nothing and returns nothing => void (*funcptr)() Q: Which header file should you include if you are to develop a function which can accept variable number of arguments? A: We have to include stdarg.h to use va_arg(), va_list() & va_start(). Which bit wise operator is suitable for putting on a particular bit in a number? => Bitwise AND operator is used to check whether a particular bit is ON or OFF. It is also used to turn OFF a particular bit in a number. Q: Write a program to compare two strings without using the strcmp() function. A: #include<stdio.h> #include<conio.h> void main()

{ char *str1; char *str2; int x,i; clrscr(); printf("Enter the first string : "); gets(str1); printf("Enter the second string : "); gets(str2); for(i=0;str1[i]!='\0';i++) { x = str1[i] - str2[i]; if(x!=0) break; } if(x==0) printf("Strings are equal"); else printf("Strings are not equal"); getch(); } Q: How would you dynamically allocate a one-dimensional and twodimensional array of integers? A: Array elements have contiguous memory location. Similar holds true for two-dimensional array also. So there is no such arrangement that array elements are stored in row & column in memory. Therefore dynamic allocation for one-dimensional & two-dimensional array of integers is same. Example of dynamic allocation is in preceding question. Q: How can you increase the size of a dynamically allocated array? A: #include<stdio.h> #include<conio.h> #include<alloc.h> void main() { int i,n,*p; clrscr(); printf("How many number of elements you want to enter? "); scanf("%d",&n); p=(int *)malloc(n*2);

if(p==NULL) { printf("Memory allocation unsuccessful"); exit(); } for(i=0;i<n;i++) { printf("Enter the element %d : ",(i+1)); scanf("%d",(p+i)); } printf("You had entered the follwing elements : \n"); for(i=0;i<n;i++) printf("Element %d is %d \n",(i+1),*(p+i)); getch(); } Q: How can you increase the size of a statically allocated array? A: I think its impossible to increase the size of statically allocated memory. Otherwise we can use vector for static declaration which can grow or shrink in size as needed.

Potrebbero piacerti anche