You are on page 1of 3

1.What does static variable mean?

Static variables are initialized only once. The compiler persists with the variable
till the end of the program. Static variables can be defined inside or outside the
function. They are local to the block. The default value of static variables is
zero. The static variables are alive till the execution of the program.

Here is the syntax of static variables in C language,

static datatype variable_name = value;


Here,

datatype − The datatype of variable like int, char, float etc.

variable_name − This is the name of variable given by user.

value − Any value to initialize the variable. By default, it is zero.


value − Any value to UN_initialize the variable. By default, it is NON_zero(garbage
value).
The static variables are stored in the data segment of the memory. The data segment
is a part of the virtual address space of a program. All the static variables that
do not have an explicit initialization or are initialized to zero are stored in the
uninitialized data segment( also known as the BSS segment)

Here is an example of static variable in C language,

Example
Live Demo

#include <stdio.h>

int main() {
auto int a = -28;
static int b = 8;

printf("The value of auto variable : %d\n", a);


printf("The value of static variable b : %d\n",b);

if(a!=0)
printf("The sum of static variable and auto variable : %d\n",(b+a));

return 0;
}
Output
Here is the output

The value of auto variable : -28


The value of static variable b : 8
The sum of static variable and auto variable : -20

3. What is a structure?
Structure is a group of variables of different data types represented by a single
name
Declaring a structure requires a keyword struct.
An operator used in structure to access structure variable is a dot operator.
example:
We can create a structure that has members for name, id, address and age and then
we can create the variables of this structure for each
6. What are the differences between structures and arrays?

Key Differences Between Array and Structure:-

Where an Array is a collection of variables of similar data type. On the other


hand, Structure is a collection of variables of dissimilar data types.

Variable of an array are stored in a contiguous memory location whereas, the


variables in a structure may or may not be stored in a contiguous memory location.

If you want to access any variable in an array you have to access it using its
index number which shows its position in that array. If you want to access a
variable in a structure then you have to access it using structure name followed by
a dot followed by a variable name.

An operator used in Array is square bracket “[ ]” , it is used while Array


declaration and also while accessing an array variable. An operator used in
structure to access structure variable is a dot operator.

An array name is a pointer, as the name of array points to the address of a first
variable in that array. On the other hand, structure name does not point to its
first element so a structure name is not a pointer.

We can not instantiate an array whereas, we can instantiate a structure.

All elements in an array has the same size because all elements are of the same
datatype whereas, structure contain elements of dissimilar datatype hence, all
elements are of different size.

Bit-field can not be defined in an array whereas, structure allows defining bit
field in it.

Declaring array does not require any keyword. Declaring a structure requires a
keyword struct.

An array is not a user defined data type where as structure is a user-defined


datatype.

An array can be accessed faster as compared to a structure.

Searching an element in an array is faster as compared to a structure.

7. In header files whether functions are declared or defined?


A. Functions are declared within header file. That is function prototypes exist in
a header file,not function bodies. They are defined in library (lib).

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

KEY DIFFERENCES:
malloc() function returns only starting address and does not make it zero on the
other hand, calloc() function returns the starting address and make it zero.
In malloc function, number of arguments is 2 while in calloc function, number of
argument is 1.
malloc() time efficiency is higher than calloc() whereas malloc() is not secure as
compared to calloc()
malloc does not initialize memory whereas calloc performs memory initialization.
9. What are macros? What are its advantages and disadvantages?

A macro is a name given to a block of C statements as a pre-processor directive.


Being a pre-processor, the block of code is communicated to the compiler before
entering into the actual coding (main () function). A macro is defined with the
preprocessor directive, #define.

The advantage of using macro is the execution speed of the program fragment. When
the actual code snippet is to be used, it can be substituted by the name of the
macro.

The disadvantage of the macro is the size of the program. The reason is, the pre-
processor will replace all the macros in the program by its real definition prior
to the compilation process of the program.

10. Difference between pass by reference and pass by value?

You might also like