Professional Documents
Culture Documents
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.
Example
Live Demo
#include <stdio.h>
int main() {
auto int a = -28;
static int b = 8;
if(a!=0)
printf("The sum of static variable and auto variable : %d\n",(b+a));
return 0;
}
Output
Here is the output
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?
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 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.
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.
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?
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.