Sunday 9 February 2014

Storage classes in C

Every variable in c is declared with the following syntax:


Storage_Class Data_Type Var_name=Initial_value;

However the name of Storage_class and Initial_value are optional. That mean we can declare a variable by just providing the Data_Type and the Var_Name . In such case compiler will apply the default values for Storage_Class and Initial_Value

e.g: int n;

In this case the parameters are:
Storage_Class=auto
Data_type=int
Var_name=n
Inital_Value=Garbage

How many storage classes are there in c ?
There are four storage classes in c and those are:
1. Automatic Storage Class. Keyword auto
2. Static Storage Class.  Keyword static
3. Register Storage Class. Keyword register
4. External Storage Class.  Keyword external



Friday 31 May 2013

Difference between getch() getche() and getchar()

C language provides many ways to input a character. We can use the traditional scanf() function to input a character. However there are three special character input functions in c, these are:

1. getch(): To input a single character form user.
                            char ch;
                           ch=getch();
However this code will not show the typed character ,but still typed character is stored to ch variable;

2. getche() : Again only single character can be inputed like getch() but this time , it will echo(displays) the typed character
                           char ch;
                          ch=getche();

3. getchar():  This works similar to scanf() function, i.e. allows user to type more than one character but only the first one will be accepted as input.
                          char ch;
                        ch=getchar();

In all the three cases you can check the inputed value to ch by the statement :
printf("Character inputed is %c",ch);
or
putchar(ch);

Sunday 12 May 2013

Coupling and Cohesion

Coupling and cohesion are two terms related to modular programming.

When we face a large problem it can be divided into smaller modules which can be easily designed, coded and debugged, by a group of different peoples. It also helps in load sharing.
Now there is a standard way to divide programs into modules, it is not just the code size or Line of Codes, it refers to an independent (as much as possible) and complete set of code.
Coupling is the interrelationship or interdependence of these individual modules. Conventionally coupling is should be least or zero in a system of program or software. However it might not be possible in some cases to achieve fully.
Cohesion is the interdependence or interrelationship between the elements or sub parts of a module. The elements of a module should be strongly dependent on each other , as they are the part of same system or module.

Coupling and cohesion are the important issues which must be considered while designing a system, as it helps in up-gradation of maintenance of the system. In a loosely coupled module changes can be easily done without much affecting the other modules in the system.

Friday 10 May 2013

Alternate for fflush(stdin) in c , for c++ . Clearing Input Buffer in c++

The problem comes when a string is inputted after a number. Because the "Enter" pressed after the number still remains in the standard input buffer. In C language the problem can be solved using:
 fflush(stdin);
use it after line used to input number. 
 The above code will clear the enter from input buffer.


Similar situation comes in c++;
Well there is a way to solve this problem.

1.Create a variable of char type.
2.Call the getc() method using object of standard input class istream.


char ch;
cin.getc(ch);


However the code only removes a single character from the input buffer.!