important questions.

Question 1 : what is difference between structure and union in C?

Structure:

1. It allocates memory equal to sum of memory allocated to its each individual member.

2.each member have their own memory space.

3. structure can not  be implemented in shred memory.

4. it has less Ambiguity.

5. Self referential structure can be implemented in data structure.

6. All members if structure can be accessed at a time.

Union:

1.It allocates piece of memory that is Large enough to hold the Largest variable of type in union.

2. one block is used by all the members of union.

3. Union is the Best environment where memory is shared.

4. as memory is shared, Ambiguity is more in union.

5. self ref. union can not be implemented.

6. only one member is accessed at a time.



Question 2 :Difference betn getch(), getche(), getchar() functions

getchar
This is a standard function that gets a character from the stdin.

getch
This is a nonstandard function that gets a character from keyboard, does not echo to screen.

getche
This is a nonstandard function that gets a character from the keyboard, echoes to screen.

Use getchar if you want it to work on all compilers. Use getch or getche on a system that supports it when you want keyboard input without pressing [Enter].


Question 3 : Difference between dynamic


 and static memory allocation in C?







STATIC MEMORY 

DYNAMIC 

Memory is allocated 
before the execution 
of the program begins.
(During Compilation)

Memory is allocated during the execution of the program.

No memory allocation or deallocation actions are performed during Execution.

Memory Bindings are established and destroyed during the Execution.

Variables remain permanently allocated.

Allocated only when program unit is active.

Implemented using stacks and heaps.

Implemented using data segments.

Pointer is needed to accessing variables.

No need of Dynamically allocated pointers.

Faster execution than Dynamic.

Slower execution than static.

More memory Space required.

Less Memory space required.




Question 4 :What is difference between 


the arrow and the dot operator in c 


structures?

The arrow is used when a pointer 
variable is used to get at a member of a 
structure, and the dot operator is used 
when dealing with the real structure.

ptr->fld == (*ptr).fld
str.fld == (&str)->fld


Question 5 : What are the differences between call by value and call by reference?




Call by value: In call by value method, the called function creates a new set of variables and copies the values of arguments into them.
Example: Program showing Call by value method
void swap(int x, int y)
{ 
  int temp;
  temp = x;
  x = y;
  y = temp;
  printf("Swapped values are a = %d and b = %d", x, y);
}

void main()
{
  int a = 7, b = 4;
  swap(a, b);
  printf("Original values are a = %d and b = %d", a, b);
  printf("The values after swap are a = %d and b = %d", a, b);
}

Output:
Original Values are a = 7 and b = 4
Swapped values are a = 4 and b = 7
 
The values after swap are a = 7 and b = 4
This happens because when function swap() is invoked, the values of a and b gets copied on to x and y. The function actually swaps x and y while the original variables a and b remains intact.

Call by reference: In call by reference method, instead of passing a value to the function being called a reference/pointer to the original variable is passed.
Example: Program showing Call by reference method
void swap(int *x, int *y)
{
  int temp;
  temp = *x;
  *x = *y;
  *y = temp;
  printf("Swapped values are a = %d and b = %d", *x, *y);
}
void main()
{
  int a = 7, b = 4;
  swap(&a, &b);
  printf("Original values are a = %d and b = %d",a,b);
  printf("The values after swap are a = %d and b = %d",a,b);
}
  
Output:
  Original Values are a = 7 and b = 4
  Swapped values are a = 4 and b = 7
The values after swap are a = 4 and b = 7
This happens because when function swap() is invoked, it creates a reference for the first incoming integer a in x and the second incoming integer b in y.

question 6: what is scale factor in pointer?
Answer :

p1++ :Will cause the pointer p1 to point to the next value of its type.
Pointer value is incremented by the ‘length’ of the data type that it points to.
This length is called the scale factor.
Char 1 byte
Int 2 bytes
Floats 4bytes
Long int   4 bytes
Doubles   8 bytes
Question 8 : Explain Storage class of 'c'?
Answer :

A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program.


There are following storage classes which can be used in a C Program


auto


register


static


extern


auto - Storage Class


auto is the default storage class for all local variables.


{
            int Count;
            auto int Month;
}
The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.


register - Storage Class


register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).


{
            register int  Miles;
}
static - Storage Class


static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.



extern - Storage Class


extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.


When you have multiple files and you define a global variable or function which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to decalre a global variable or function in another files.


File 1: main.c


   int count=5;


   main()
   {
     write_extern();
   }
File 2: write.c


   void write_extern(void);


   extern int count;


   void write_extern(void)
   {
     printf("count is %i\n", count);
   }
Here extern keyword is being used to declare count in another file.


Now compile these two files as follows


   gcc main.c write.c -o write
This fill produce write program which can be executed to produce result.


Count in 'main.c' will have a value of 5. If main.c changes the value of count - write.c will see the new value