Thursday, July 26, 2018

C Questions And Answers – 2018B2

There are many commonly asked questions regarding C programming language. Below are some collected such question-answer examples. The questions are usually related with 32-bit system, Turbo C IDE in windows or GCC under Linux environment [not always].

For more such examples, click C_Q&A label.

-------------------------------------------------------------------------------------------------

 

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 ("circular");

}

}

 

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, its either 1 or 2 jumps until they meet.

 

-------------------------------------------------------------------------------------------------

 

"union" Data Type What is the output of the following program? Why?

 

#include

 

main() {

typedef union {

int a;

char b[10];

float c;

}

Union;

Union x,y = {100};

x.a = 50;

strcpy(x.b,"hello");

x.c = 21.50;

printf("Union x : %d %s %f n",x.a,x.b,x.c);

printf("Union y : %d %s %f n",y.a,y.b,y.c);

}

 

-------------------------------------------------------------------------------------------------

 

What does static variable mean?

 

There are 3 main uses for the static.

 

1. If you declare within a function: It retains the value between function calls

 

2.If it is declared for a function name: By default function is extern.so it will be visible from other files if the function declaration is as static.it is invisible for the outer files

 

3. Static for global variables: By default we can use the global variables from outside files If it is static global that variable is limited to within the file

 

-------------------------------------------------------------------------------------------------

 

Advantages of a macro over a function?

 

Macro gets to see the Compilation environment, so it can expand ____TIME__ __FILE__ #defines. It is expanded by the preprocessor.

 

For example, you can’t do this without macros

#define PRINT(EXPR) printf( #EXPR “=%d\n”, EXPR)PRINT( 5+6*7 )

// expands into printf(”5+6*7=%d”, 5+6*7 );

 

You can define your mini language with macros:

#define strequal(A,B) (!strcmp(A,B))

 

Macros are necessary evils of life. The purists don’t like them, but without it no real work gets done.

 

-------------------------------------------------------------------------------------------------

 

What are the differences between malloc() and calloc()?

 

There are 2 differences.

 

First, is in the number of arguments. malloc() takes a single argument(memory required in bytes), while calloc() needs 2 arguments(number of variables to allocate memory, size in bytes of a single variable).

 

Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

 

-------------------------------------------------------------------------------------------------

 

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 static specifier also have static scope.

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

 

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

No comments:

Post a Comment