Showing posts with label Advanced C Interview Questions and Answers. Show all posts
Showing posts with label Advanced C Interview Questions and Answers. Show all posts

Wednesday, April 6, 2016

'Extern' Storage Class Specifier

You can use extern keyword along with function declaration as well, but that's a redundant operation since functions have external linkage by default. ie; They can be accessed globally from other places.

'Typedef' Qualifier

'typedef' is not a storage class specifier, but it allows us to give an alias (new name) to existing data types (built-in or user defined datatypes). Sytax of 'typedef' is as given below,
typedef data_type alias; 
Let's consider the following example
typedef int number;
Here, we've given an alias to data type 'int' which is 'number'. So now we can use 'number' as a type in variable declaration and definition to hold an integer values. By creating an alias, we can use it as given below
number first, second;
It means 'first' and 'second' are of the 'number' data type which in turn is an integer. So, 'first' and 'second' are also integers.

Summary of Storage Class Specifiers

Table given below summarizes the storage location, initial default value, scope and life time of different storage class specifiers
Storage ClassSpace Allocated inDefault Initial ValueScopeLife
autoMemoryGarbageLocal ScopeAlive within the declared code block
registerCPU RegisterGarbageLocal ScopeAlive within the declared code block
staticMemory0Local ScopeValue retained between invocations
externMemory0Global ScopeAlive till program exit

Tuesday, April 5, 2016

'Static' Storage Class Specifier

  • Value of a 'Static' variable will be maintained across the function invocations. If a variable is declared using 'Static' qualifier at the top of the source file, then the scope of that variable is within the entire file. Files outside the declared file can't access the declared static variable. 
  • 'Static' variables declared within a function or a block, also known as local static variables, have the scope that, they are visible only within the block or function like local variables. The values assigned by the functions into static local variables during the first call of the function will persist/available until the function is invoked again.
  • 'Static' variables are by default initialized with value 0.

Let's see the effect of declaring a local and global variable with 'Static' keyword.
  1. #include <stdio.h>
  2. static int global_static_variable = 1;
  3.  
  4. static void static_variable_test() {
  5. static int local_static_variable;
  6. printf("local_static_variable:%d\t", local_static_variable);
  7. local_static_variable++;
  8. printf("global_static_variable:%d\n", global_static_variable);
  9. global_static_variable++;
  10. }
  11. int main()
  12. {
  13. static_variable_test();
  14. static_variable_test();
  15. }
  16.  
  17. Output:
  18. local_static_variable:0 global_static_variable:1
  19. local_static_variable:1 global_static_variable:2
Few points to note here are
  1. 'Static' variable got initialized with value 0 by default. (We didn't initialize the variable 'local_static_variable' with a value, but it got initialized with 0 automatically)
  2. Value of the the local variable 'local_static_variable' is retained across invocations of function 'static_variable_test()'
'Static' and Global functions/variables
In C programming language, functions are global by default. That means, by default function declared in a file can be accessed from code with in another file. But when you apply static keyword to a functions, it makes the function accessible only from that file. So if you want to restrict access to a function from code in other files, you can declare the function as static.

In the example given above, the function 'static_variable_test' will be visible only with in the file it's declared. Also, if you declare a global variable as static, code in other files cannot access the variable. In simple words, declaring a global function or variable as static gives it internal linkage.

Wednesday, March 30, 2016

Usage of Unions

The main characteristic of union is that it lets you to use same memory location for different data type. In real life C programming, you may encounter a situation where you need to store a value of say 4 byte but at different time according to situation you need to know the value of 1st byte or 3rd byte or may be 4th byte contained value. Yes there is bitwise operators to do that but if you define a union like given above, you can access any byte of value at any time without any logical or arithmetic operation.
#include<stdio.h>
int main() {
char my_char;
union my_union {
} my_union;
int my_int; float my_float;
printf("Here is the Output:\n%c\n%i\n%.3f\n", my_union.my_char, my_union.my_int, my_union.my_float );
my_union.my_char = 'A'; my_union.my_int = 57;
printf("Here is the Output:\n%c\n%i\n%.3f\n", my_union.my_char, my_union.my_int, my_union.my_float );
my_union.my_float = 101.357; printf("Here is the Output:\n%c\n%i\n%.3f\n", my_union.my_char, my_union.my_int, my_union.my_float ); return 0;
}

Thursday, March 24, 2016

Member Variables

All the subfields i.e the primitive data type variables in structures are called member variables. We can access these member variables using dot(.) operator. We can set and retrieve the member variable of student structure as shown below.
student student_1 = {0};
student_1.roll_number = 20;
strcpy(student_1.name,”Denis Ritchie”);
student_1.class = 12;
student_1.section = ‘A’;
student_1.marks = 96.9;
strcpy(student_1.school_name,”a4academics.com”);