Home Other Languages C Basic C Questions - 2

Login Form




Basic C Questions - 2 Print E-mail

 

1. printf() Function- What is the difference between "printf(...)" and "sprintf(...)"?

sprintf(...) writes data to the charecter array whereas printf(...) writes data to the standard output device.

 

2. What will be printed as the result of the operation below:

main()
{
char *p1;
char *p2;

p1=(char *)malloc(25);
p2=(char *)malloc(25);

strcpy(p1,”Cisco”);
strcpy(p2,“systems”);
strcat(p1,p2);

printf(“%s”,p1)
;
}

Answer: Ciscosystems

 

3. What will be printed as the result of the operation below:

#define swap(a,b) a=a+b;b=a-b;a=a-b;
void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %d\n”,x,y)
; swap2(x,y);
printf(“%d %d\n”,x,y)
; }

int swap2(int a, int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;
}


as x = 5 = 0×0000,0101; so x << 0100 =" 20;">7gt; 2 -> 0×0000,0001 = 1. Therefore, the answer is 5, 20 , 1

the correct answer is
10, 5
5, 10

Answer: 10, 5

 

4. Which bit wise operator is suitable for turning off a particular bit in a number?

The bitwise AND operator, again. In the following code snippet, the bit number 24 is reset to zero.
some_int = some_int & ~KBit24;

 

5. What are the different storage classes in C?

C has three types of storage: automatic, static and allocated.

Variable having block scope and without static specifier have automatic storage duration.

Variables with block scope, and with static specifier have static scope. Global variables (i.e, file scope) with or without the the static specifier also have static scope.

Memory obtained from calls to malloc(), alloc() or realloc() belongs to allocated storage class.

 

6. What will be printed as the result of the operation below:

main()
{
int x=5;
printf(“%d,%d,%d\n”,x,x<<2,>>2)
;
}

Answer: 5,20,1

 

7. Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?

a[i] == *(a+i)a[i][j] == *(*(a+i)+j)a[i][j][k] == *(*(*(a+i)+j)+k)a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l)

 

8. malloc() Function- What is the difference between "calloc(...)" and "malloc(...)"?

1. calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size).

malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not blocks of memory like calloc(...).

2. malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available.

calloc(...) allocates an array in memory with elements initialized to 0 and returns a pointer to the allocated space. calloc(...) calls malloc(...) in order to use the C++ _set_new_mode function to set the new handler mode.

 

9. Linked Lists -- Can you tell me how to check whether a linked list is circular?

Create two pointers, and set both to the start of the list. Update each as follows:

while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circularn");
}
}

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it’s either 1 or 2 jumps until they meet.

 

10. Difference between const char* p and char const* p

in const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character pointed by p but u can make ‘p’ refer to some other location.

in char const* p, the ptr ‘p’ is constant not the character referenced by it, so u cant make ‘p’ to refernce to any other location but u can chage the value of the char pointed by ‘p’.