C
c faqs, c interview questions, c language programming skills, c language experts.
What is a void pointer?
Dec 9th
A void pointer is a C convention for a raw address. The compiler has no idea what type of object a void Pointer really points to. If you write int *ip; ip points to an int. If you write void *p; p doesn’t point to a void! In C and C++, any time you need a void pointer, you can use another pointer type. For example, if you have a char*, you can pass it to a function that expects a void*. You don’t even need to cast it. In C (but not in C++), you can use a void* More >
What is a pointer?
Dec 9th
A pointer is a special variable in C language meant just to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as ‘*’,'%’ operators. It follows a special arithmetic called as pointer arithmetic.
A pointer is declared as:
int *ap;
int a = 5;
In the above two statements an integer a was declared and initialized to 5. A pointer to an integer with name ap was declared.
Next before ap is used
ap=&a;
This operation would initialize the declared pointer to int. The pointer ap is now said to point to a.
Operations on More >
Which one is equivalent to multiplying an unsigned int by 2, left shifting by 1 or right shifting by 1?
Dec 9th
Left shifting of an unsigned integer is equivalent to multiplying an unsigned int by 2.
Eg1: 14<<1;
consider a number 14—–00001110 (8+4+2)is its binary equivalent left shift it by 1————–00011100(16+8+4) which is 28.
Eg2: 1<<1;
consider the number as 1—00000001(0+0+1).
left shift that by 1————00000010(0+2+0) which is 2. left shift by 1 bit of a number=2*number
left shift by 1 bit of 2*number=2*2*number left shift by n bits of number=(2^n)*number
Program: Program to illustrate left shift and right shift operations.
#include<stdio.h>
int main(void) {
int x=10,y=10;
printf(“left shift of 10 is %d \n”,x<<1);
printf(“right shift of 10 is: %d \n”,y>>1); return 0;
}
Output:
left shift of 10 is 20 right shift of 10 is: More >
Write down the equivalent pointer expression for referring the same element as a[i][j][k][l]. Explain.
Dec 9th
Consider a multidimensional array a[w][x][y][z].
In this array, a[i] gives address of a[i][0][0][0] and a[i]+j gives the address of a[i][j][0][0]
Similarly, a[i][j] gives address of a[i][j][0][0] and a[i][j]+k gives the address of a[i][j][k][0]
a[i][j][k] gives address of a[i][j][k][0] and a[i][j][k]+l gives address of a[i][j][k][l]
Hence a[i][j][k][l] can be accessed using pointers as *(a[i][j][k]+l)
where * stands for value at address and a[i][j][k]+l gives the address location of a[i][j][k][l].
Program: Example program to illustrate pointer denotation of multi-dimensional arrays.
#include<string.h> #include<stdio.h> int main() {
int a[3][3][3][3];
//it gives address of a[0][0][0][0] .
printf(” \n address of array a is %u”, a);
printf(“\n address of a[2][0][0][0] is %u , ” “given by a[2] More >
What is difference between for loop and while loop in C language?
Dec 9th
for loop: When it is desired to do initialization, condition check and increment/ decrement in a single statement of an iterative loop, it is recommended to use ‘for’ loop.
Syntax:
for(initialisation;condition;increment/decrement) {
//block of statements
increment or decrement }
Program: Program to illustrate for loop
#include<stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
//print the number
printf(“\n %d”, i);
}
return 0;
}
Output:
1
2 3 4 5
Explanation:
The loop repeats for 5 times and prints value of ‘i’ each time. ‘i’ increases by 1 for every cycle of loop.
while loop: When it is not necessary to do initialization, condition check and increment/decrement in a single statement of an iterative loop, More >
What is the difference between the functions strdup and strcpy in C?
Dec 9th
strcpy function: copies a source string to a destination defined by user. In strcpy function both source and destination strings are passed as arguments. User should make sure that destination has enough space to accommodate the string to be copied.
‘strcpy’ sounds like short form of “string copy”.
Syntax:
strcpy(char *destination, const char *source);
Source string is the string to be copied and destination string is string into which source string is copied. If successful, strcpy subroutine returns the address of the copied string. Otherwise, a null pointer is returned.
Program: Example program.
#include<stdio.h>
#include<string.h> int main() {
char myname[10];
//copy contents to myname strcpy(myname, “nodalo”); //print the string
puts(myname);
return More >
What are header files? Are functions declared or defined in header files?
Dec 9th
Functions and macros are declared in header files. Header files would be included in source files by the compiler at the time of compilation.
Header files are included in source code using #include
directive.#include<some.h> includes all the declarations present in the header file ‘some.h’.
A header file may contain declarations of sub-routines, functions, macros and also variables which we may want to use in our program. Header files help in reduction of repetitive code.
Syntax of include directive:
#include<stdio.h> //includes the header file stdio.h, standard input output header into the source code Functions can be declared as well as defined in header files. But it is recommended More >
Write a program in C that returns 3 numbers from a function. A function in C can return only one value. If we want the function to return multiple values, we need to create a structure variable, which has three integer members and return this structure.
Dec 9th
Program: Program with a function to return 3 values .
#include <stdio.h>
//sample structure which has three integer variables. struct sample {
int a, b, c; };
//this is function which returns three values. struct sample return3val(void) {
struct sample s1;
s1.a = 10;
s1.b = 20; s1.c = 30;
//return structure s1, which means return s1.a ,s1.b and s1.c return s1;
}
int main(void) {
struct sample accept3val;
//three values returned are accepted by structure accept3val.
accept3val = return3val();
//prints the values
printf(” \n %d”, accept3val.a); printf(“\n %d”, accept3val.b); printf(” \n %d”, accept3val.c); getchar();
}
Output:
10
20
30.
Explanation:
In this program, we use C structure to return multiple values from a function.
Here we have a structure holding three int More >
What is the value of sizeof(a) /sizeof(char *) in C code snippet below char *a[4]={“sridhar”,”raghava”,”shashi”,”srikanth”}; explain
Dec 9th
Explanation:
Here a[4] is an array which holds the address of strings. Strings are character arrays themselves.
Memory required to store an address is 4 bits. So memory required to store 4 addresses is equal to 4*4=16 bits.
char *; is a pointer variable which stores the address of a char variable.
So sizeof(char *) is 4 bits. Therefore sizeof(a) /sizeof(char *) = 16/4 = 4 bytes.
(i)What are the differences between the C statements below: char *str = “Hello”; char arr[] = “Hello”; (ii)Whether following statements get complied or not? Explain each statement. arr++; *(arr + 1) = ‘s’; printf(“%s”,arr);
Dec 9th
(i) char *str=”Hello”;
“Hello” is an anonymous string present in the memory. ‘str’ is a pointer variable that holds the address of this string.
char arr[]=”Hello”;
This statement assigns space for six characters: ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0′ . ‘arr’ is the variable name assigned to this array of characters.
str[4] and arr[4] also have different meanings.
str[4]: adds 4 to the value of ‘str’ and points to the address same as value of str + 4.
arr[4]: points to the fourth element in array named ‘arr’.
(ii) ‘arr’ is variable name of an array. A variable name cannot be incremented or decremented. Hence arr++ is an More >
