Thursday, 9 October 2014

Trap in operating system

Q. What is trap and an example of trap which we use in daily life?

Ans. Trap is a type of interrupt which is used to communicate with processor. It can be hardware or software either. Generally it is a hardware interrupt with manual access. 

Main thing is that the priority of Trap is always higher than the any kind of other interrupt . If once a trap is occurred processor will execute the instruction corresponding to that trap in any kind of situation.

Ex: The example of a trap which we use in our daily life is the power button in laptops. In whatever condition if we push that button it shutdown the pc.

Wednesday, 8 October 2014

Macros

Q. Why we use macros?
Ans. First of all What is macros?

Macros are used to define something before the execution starts and its values are never changed during the execution.

Ex:  in c/c++

#define max_size 100
#ifndef
#include<something.h>
#end if

Imagine the scenario you write a program where you assume the size of the input to 1000 at max and u use every where 1000 . Then some one asked you to modify that program for 10000 elements then you have to go to all places where you write that 1000 and replaced it by 10000 instead of that we can use macros we define variable in starting outside the main function and use that variable in our function if some one asked to change the size we change only that variable and the changes at all the places are taken care of automatically.

Macros are predefine variables or function. Compiler replaces all the macros in its pre processing with its corresponding values in the entire program.



Explain public static void main(string [] args)

Q. The question comes here is why java's main function has all the keyword like public, static  and void?

Ans. First start with public is because of scope of the main function so that it can be called out of the class . If it would be private or something else compiler can not invoke the main function out of the class.

Second static is because when main function is called there will be no object of the class so if we want to call a method of a class without initializing the object of that class then we have to make that method static. Static function can be called without initializing the object of that class.

Void is the return type of main function we can not change it because main function will not return anything in java. If it would then to whom it will return something?? Think about it......

And the argument is the array of string objects which it expects.

Argument to main function in C/C++

Q. What is the use of passing argument to main function?
Ans.
      First of all we can define main function in three different format in c/c++.
      int main(int argc,char * argv[]);
      int main(void);
      int main();

Instead of int return type we can have any other pre defined data type as return type of main function.

We start with the first prototype, here main function excepts two arguments one is of type int and other is of type array of pointers. First of all these arguments are used for passing the input together when we start executing the program. We don't have to pass the first argument it automatically counts how many argument we have passed while executing the program and that will be stored in argc.

Second argument argv[] is array of character pointers which stores the inputs which we have sent with execution. for ex. argv[0] will have first argument and argv[1] will have second argument .


In the second prototype it does not except any argument if we passes a argument the compiler will give an error.

Third one is similar to the second one but the difference is that in third one we can pass as many argument we want it will not give an error but in the second one it will give an error while we pass an argument to main function using terminal (linux users).

Two main function in java

Q. The question is that can we have two main functions in java?

Ans. Yes, We can have two main function in java but the function's prototype should be different.

 For example we can have two functions like-
public static void main(string[] args);

and second one may be

public static void main(int a);


The reason for this is that when compiler/interpreter starts executing it starts with finding a function whose prototype is similar to the first one i have written have which takes argument as a array of string objects. It does not consider the second main function because it has different argument then the one which is needed. So we can have two main functions with two different arguments and one of them should be similar to the first one.

Header files

The question always comes to our mind is why we include header files?

Ans:  The answer is simple The function which we use can be define separately in .h files. .h is the extension of header files which contains the prototype of the function and also may contain the definition but for reason of abstraction we prefer to write prototype and definition in different file prototype in .h but not necessary can be written with some other extension also.

Then we write the definition to .c file compile those files make their object files and include header file to our main file and put both .h and .c file to the same folder then use the functions written in that header file. Some header files are already in-built which we can directly use like #include<stdio.h> it is a standard input output file which we can use for printf and scanf functions etc.

The advantage is our source code file's size is less. And if we want to give someone which functions are available we can give the header .h file only. No need to give .c file we can give object file so he/she will not need to understand how the implementation is happening.

Tuesday, 7 October 2014

Increment operator in c

Are you ready for some serious brain storming?
Here it goes my first post.

1. What would be the output of this program while running with the gnu gcc compiler?


#include<stdio.h>

int main(int argc, char * argv[]){

      int z=2;
      int y=2;
   
      z=z++;
      y=++y;
      printf("%d  %d",z,y);
 return 0;
}

Please Before proceed to answer just think for a minute.
Think ::::


This Space is left intentionally.









Although this is undefined behavior due to absence of sequence point. I will define sequence point in my future posts. But here I am presenting one of the reasons of getting this output.




Answer is  : 2 3

Reason is : For understanding the reason behind this we have to understand the execution sequence of instruction z=z++;

For evaluation of a expression compiler generates a temporary variable and evalute the expression and store the value in the temporary variable which in the given expression is 2 because postfix operator executes after the expression is evaluated. So till now the temporay variable has value 2 
and now z becomes three due to posfix operator. And then the temporary variables value is assigned to the variable which is on the left side of the = operator which here is z itself so value 2 is assigned to z again.
Finally the value of z becomes 2.

Similarly for the value of y first expression is evaluated and in this pre increment is also evaluated so value of y becomes 3 and the value of temporary variable is also becomes 3 and now this temporary variable's value is assigned to y.
Finally the value of y becomes 3.

Related Posts

Related Posts Plugin for WordPress, Blogger...