C INTERVIEW QUESTIONS Part 6

Question: How can you determine the maximum value that a numeric variable can hold?
Answer : For integral types, on a machine that uses two’s complement arithmetic (which is just about any machine you’re likely to use), a signed type can hold numbers from –2(number of bits – 1) to +2(number of bits – 1) – 1. An unsigned type can hold values from 0 to +2(number of bits) – 1. For instance, a 16-bit signed integer can hold numbers from –2^15 (–32768) to +2^15 – 1 (32767).

Question :Can a variable be both const and volatile?
Answer : Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. For instance, in the example in
FAQ 8, the timer structure was accessed through a volatile const pointer. The function itself did not change the value of the timer, so it was declared const. However, the value was changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order.

Question: when should the volatile modifier be used?
Answer : The volatile modifier is a directive to the compiler’s optimizer that operations involving this variable should not be optimized in certain ways. There are two special cases in which use of the volatile modifier is desirable. The first case involves memory-mapped hardware (a device such as a graphics adaptor that appears to the computer’s hardware as if it were part of the computer’s memory), and the second involves shared memory (memory used by two or more programs running simultaneously).

Most computers have a set of registers that can be accessed faster than the computer’s main memory. A good compiler will perform a kind of optimization called “redundant load and store removal.” The compiler looks for places in the code where it can either remove an instruction to load data from memory because the value is already in a register, or remove an instruction to store data to memory because the value can stay in a register until it is changed again anyway.

If a variable is a pointer to something other than normal memory, such as memory-mapped ports on a
peripheral, redundant load and store optimizations might be detrimental. For instance, here’s a piece of code that might be used to time some operation:


time_t time_addition(volatile const struct timer *t, int a)
{
int n;
int x;
time_t then;
x = 0;
then = t->value;
for (n = 0; n < x =" x">value - then;
}

In this code, the variable t->value is actually a hardware counter that is being incremented as time passes. The function adds the value of a to x 1000 times, and it returns the amount the timer was incremented by while the 1000 additions were being performed. Without the volatile modifier, a clever optimizer might assume that the value of t does not change during the execution of the function, because there is no statement that explicitly changes it. In that case, there’s no need to read it from memory a second time and subtract it, because the answer will always be 0. The compiler might therefore “optimize” the function by making it always return 0.

If a variable points to data in shared memory, you also don’t want the compiler to perform redundant load and store optimizations. Shared memory is normally used to enable two programs to communicate with each other by having one program store data in the shared portion of memory and the other program read the same portion of memory. If the compiler optimizes away a load or store of shared memory, communication between the two programs will be affected.

Question: When should the register modifier be used? Does it really help?
Answer : The register modifier hints to the compiler that the variable will be heavily used and should be kept in the CPU’s registers, if possible, so that it can be accessed faster.

There are several restrictions on the use of the register modifier.

First, the variable must be of a type that can be held in the CPU’s register. This usually means a single value of a size less than or equal to the size of an integer. Some machines have registers that can hold floating-point numbers as well.

Second, because the variable might not be stored in memory, its address cannot be taken with the unary & operator. An attempt to do so is flagged as an error by the compiler. Some additional rules affect how useful the register modifier is. Because the number of registers is limited, and because some registers can hold only certain types of data (such as pointers or floating-point numbers), the number and types of register modifiers that will actually have any effect are dependent on what machine the
program will run on. Any additional register modifiers are silently ignored by the compiler.

Also, in some cases, it might actually be slower to keep a variable in a register because that register
then becomes unavailable for other purposes or because the variable isn’t used enough to justify the overhead of loading and storing it.

So when should the register modifier be used? The answer is never, with most modern compilers. Early C compilers did not keep any variables in registers unless directed to do so, and the register modifier was a valuable addition to the language. C compiler design has advanced to the point, however, where the compiler will usually make better decisions than the programmer about which variables should be stored in registers.

In fact, many compilers actually ignore the register modifier, which is perfectly legal, because it is only a hint and not a directive.

Question:What is a const pointer?
Answer : The access modifier keyword const is a promise the programmer makes to the compiler that the value of a variable will not be changed after it is initialized. The compiler will enforce that promise as best it can by not enabling the programmer to write code which modifies a variable that has been declared const.

A “const pointer,” or more correctly, a “pointer to const,” is a pointer which points to data that is const
(constant, or unchanging). A pointer to const is declared by putting the word const at the beginning of the pointer declaration. This declares a pointer which points to data that can’t be modified. The pointer itself can be modified. The following example illustrates some legal and illegal uses of a const pointer:

const char *str = “hello”;
char c = *str /* legal */
str++; /* legal */
*str = ‘a’; /* illegal */
str[1] = ‘b’; /* illegal */



Question: What is the difference between goto and longjmp() and setjmp()?
Answer :A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution.

Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.

A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions.

When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.

However, there is a major drawback to using these functions: your program, when restored to its previously saved state, will lose its references to any dynamically allocated memory between the longjmp() and the setjmp(). This means you will waste memory for every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be horribly inefficient. It is highly recommended that you avoid using functions such as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming


Question: Whats is structure padding?Say a given structure
Struct{
int a;
char c;
float d;
}
the size of structure is 7 here.
But structure padding is done what will be the size of the struct?Will it change and how?How to avoid this?is it necessary?
Answer : Integers and floats :compilers will try to place these variables at addresses which are in multiples of 2 or 4(in 16-bit system) now in this case 1 byte can be padded...we can store integers and floats at start to avoid padding

Question :How to type a string without using printf function?
Answer : //printing a string without printf#includeint main(){ char *str="shobhit"; while((*str)!=NULL) { putchar(*str); str++; } return 0;}

Question: How to write a C program to find the power of 2 in a normal way and in single step?
Answer : U can take logarithm base 2, and check the result is in interger form or floating point form, u can check whether it is power of 2 or not.

Question :How to break cycle in circular single link list?
Answer :we can delete an intermediate one

Question :What does it mean-

a[i]=i+i
Answer :a[i]=i+i;

its just simple... an assignment statement.

an i'th element of array a (i.e.,) a[i] is going to have a value i+i;

eg; lets i=3 means

a[3]=3+3;

a[3]=6;

Question: Between a long pointer and a char pointer, which one consumes more memory? Explain
Answer: Both will consume same amount of memory. why because they means long or char pointer always stores the address of the character or long integer .

Question: What is wrong with the following c prog??
char *s1 = "hello";
char *s2 = "world";
char *s3 = strcat(s1, s2);

Please provide me explanations??
Answer: Since what is present in memory beyond "United" is not known and we are attaching "Front" at the end of "United", thereby overwriting something, which is an unsafe thing to do.
Question: In c , main() is a function . and where is defined main() in c. bcz every function has three parts.
1>. decleration
2>. definition.
3>. Calling
Answer: Declaration is not needed if method is defined before calling. main() method is called by the OS when the program is run. So, it has only a definition..

Question: How can we open a image file through C program
Answer :In C, generally we can open files having text format...

other types of files can be opened in binary format only using

file *fp;

fp=fopen("filename","rb+");// where b stands for binary format

Question: What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?
Answer: #define NULL 0

#define NULL_PTR (void *)0

NULL_PTR has pointer context, while NULL is a normal value.

Question :How can you calculate number of nodes in a circular Linked List?
Answer :struct node

{ int data;

struct node *next;

};

i write just function here

int count(struct node *pp)

{ struct node *start;

int count=0;

start=pp->next;

while(start->next!=pp)

{ start=start->next;

count++; }

return count;

}

Question :Can we use string in switch statement?
Answer : We cannot use a string in switch statement nor can we use

a floating point in switch statement. all we can use in a switch statement is a character and integer. Also we cannot use statements like

i<10 in case statements.This is the reason why switch can't replace IF statements although it allows us to make several choices depending upon the condition.

Question: Output of this Programme please??

main()
{

int a[]={2,4,6,8,10};
int i;
change(a,5);
for(int i=0;i<=4;i++)
printf("\n %d", a[i]);
}

change(int *b,int n)
{

int i;
for(i=0;i *(b+i) = *(b+i)+5;
}

sytaxis correct?? was asked i a test

Answer :I think the syntax is incorect in the for loop of the change function, it sould have atleast a closing ')'.

Secondly, if the function defination is given after the function calling, then the proper prototype of the function must be declared before the calling of the function, other wise compiler declares a default prototype by considering each parameters as well as the return type as integer, which leads into the compiler error "type mismatch in redeclaration of the function".

Question: How to swap the content of two variables without a temporary variable
Answer: void swap (int a, int b)

{

a =a+b;

b=a-b;

a=a-b;

}

Question: How do you write a C program which can calculate lines of code but not counting comments?
Answer :Using file concept with Command line arguments.declare a variable (lcnt) used to count the no of lines.Open a file in read made and then using while loop check the condition for not equal to EOF.Later using if condition check check for new line and increment the variable for counting the lines.

Then using while,check for the character '/','*' (as the comments start with these characters) and end with ('*' and '/').if condition of this is true then break and come out of the block else increment the line.

Question: main(int x).............

explaination on arguments passed thr' main
Answer : The main function can have the command linre arguments like in this syntax main(int x)......

the main function can have two arguments

1. int x : tell the number of arguments in the main function that are given on the command line

2. char *array[] :it is an array of pointers to the string and specify the file names thay you want to pass on the command line.