Here you will find more than 50000 job interview questions

Job Questions Search Engine


Sponsored Links

What is the purpose of auto keywords

Not much. It declares an object with automatic storage duration. Which means the object will be destroyed at the end of the objects scope. All variables in functions that are not declared as static and not dynamically allocated have automatic storage duration by default.

For example
int main()
{
int a; //this is the same as writing “auto int a;”
}

What are automatic variable, static variable and global variable in C

September 2, 2010 by bigboss · Leave a Comment
Filed under: C Interview Questions 
Source: google knowledge
A auto or local variable is a variable which is defined within the scope of a function. This sort of variables are also called automatic variables. Automatic variables cannot retains it’s value once the function returns. That mean the scope and life of an automatic variable is within the function it’s defined in. Automatic variables are initialised by 0 automaticallu by the compiler at the time of compilation if those are not explicitly initialised by the programmer. Now an automatic/local variable is reinitialized everytime the function is called and destroyed once the function returns. Lets have an example:

#include <stdio.h>
void main(void)
{
int var = 0; /*explicit initialization*/var += 10;
printf(“Var = %d \n”, var);
}

‘var’ is an automatic variable and is initialized to 0 at the beginning of the programm. Then it’s incremented by 10 and it’s value is printed. Well, as an automatic variable cannot retain it’s value once the function returns, everytime the function is called memory would be set aside for ‘var’ in the stack of the function and ‘var’ would be reinitialized to 0.

Now imagine a condition when you want to keep track of an event occurring frequently in the system. What you want to do is to call a function, increment the previous value of the event-occurring-counter, print it’s value and then return. To accomplish this you need to have a variable which can retain it’s value even after the function returns so that in the next call to the function the previous value of the counter could be incremented by one and so on. A static variable does the trick for you. A static variable can be of two types in nature:

      1. static to the function.
      2. static to the file.

The above discussed static variable is of the first type.

Well a static variable which is defined inside a function is not destroyed once the function returns and hence retains it value even after the function returns. The scope of this sort of static variable is within the function.A static variable of this sort cannot be called from outside the function. So it’s scope is just inside the function body.But even if it’s scope happens to be within the function it’s life is throughout the program execution which means that the variable still is in memory.

A static variable can retain it’s previous value because it is not allocated from the stack. It’s allocated from the data section of the memory. Uninitialized static variables are allocated form BSS(Block Started Symbol) section of the memory and initializes to 0 by default.

Now lets discuss about the second type of static variable i.e. static variables defined inside a file but outside any function. These type of static variables are global to all the function in the particular file it is defined in i.e. any function can access the variable but any function outside the file cannot access it. These sort of variables cannot be ’extern’ -ed even. The following example show hows a gcc compiler throws error when you try to “extern” a static variable defined in a file.

/*Illegal use of static variable*/
/*File: test1.c*/
#include<stdio.h>
#include “header.h”
int i = 100;
int main() {
printf(“This is the scope of the global variable:%d\n”,i);
foo();
foo();
printf(“This is not a scope of static variable:%d\n”,j);
return 0;
}
/*File: test2.c*/
extern int i;
static int j = 200;
int foo() {
printf(“This is the scope of the global variable: %d\n”,i);
j++;
printf(“in func foo() the scope of static j is: %d\n”,j);
return 0;
}
/*File: header.h*/
extern int foo();
extern int j;

Error:

/tmp/cckXvIiw.o(.text+0x2f): In function `main’:
: undefined reference to `j’

What is a static function in C ?

September 2, 2010 by bigboss · Leave a Comment
Filed under: C Interview Questions 

A static function is one with local linkage – ie it isn’t visible outside the current compilation unit (the current .c or .cpp file).

Static function restrict visibility of the function to the translation unit in which it’s declared. Functions are implicitly declared as extern by default, which means they’re visible across translation units.

Or you can say that  ….      A static function has a name visible in that translation unit only. A
normal function has a name visible across the entire program.

For example:

If these three function are declared in say test1.c
void foo ( void ){}

extern void bar ( void ){}
static void baz ( void ){}

then you will have following behaviour for the above function
int main ( void )
{

foo(); /* OK: foo is extern by default */
bar(); /* OK: bar is explicitly extern */
baz(); /* Wrong: baz isn’t visible in this translation unit */
}

By default every function is extern and static function is opposite to it

What are Macros in C?

August 11, 2010 by bigboss · Leave a Comment
Filed under: C Interview Questions 

Macros are preprocessors which are small code of snippets which are replaced in the code AS IT IS before compilation of the program preprocessors and prototypes are the definition of a function it is also one kind of preprocessor

  • Categories

    |
  • Tags

    ADO.NET Questions Algorithm Questions ASP.NET Questions auto_ptr Binary tree questions C++ Constructor Interview Questions C++ Questions CISCO Exams Questions Common Interview Questions Core Java Interview Questions Csharp Questions datastructure questions Delphi 6 find command gdb interview questions grep interview questions IBM certification exams questions Infosys Puzzles Java Struts Linked List Problem Linux Command Questions List Manager Interview Questions Markov Algorithm memory leakage mysql Interview Questions Normalization Oracle Application Developer Certification Exam Interview Questions Oracle Questions Perl Questions PHP Questions Pointers Interview Questions PostgreSQL Database Questions pthread interview questions Smart Pointer Solaris Interview Questions SQL SERVER Interview Questions STL STL Map Symbian OS Tricky Interview Questions Unix Interview Questions unix shell Vector Windows OS Questions