What is the purpose of auto keywords
Filed under: C Interview Questions, C++ Interview Questions, Microsoft Interview Questions
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
#include <stdio.h>
void main(void)
{
int var = 0; /*explicit initialization*/var += 10;
printf(“Var = %d \n”, var);
}
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:
-
-
- static to the function.
- 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;
/tmp/cckXvIiw.o(.text+0x2f): In function `main’:
: undefined reference to `j’
What is a static function in C ?
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?
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
