Sunday, 29 June 2014

Use Increment and Decrement Operator in C Programming

In programming it may happen that you need to increase or decrease value of a variable by one. Increment and decrement operator are used for that purpose. Let’s take “a” as an integer variable, in Increment Operator it is written as “a++” or “++a” which have same mining as “a=a+1”. In Decrement Operator it is written as “a--” or “--a” which have same mining as “a=a-1”.
Now when we write “++a” called as Prefix Increment and for “a++” it is called as Postfix Increment. Prefix Increment make immediate increment of variable value where postfix make increment in next call.
Same way when we write “--a” called as Prefix Decrement and for “a--” it is called as Postfix Decrement. Prefix Decrement make immediate decrement of variable value where postfix make decrement in next call.
Now let’s see the example
#include<stdio .h>
void main()
{
    int a;
    a=1;//lets take value of a as 1
    printf("The Value Of a is %d\n",a++);
    printf("The Value Of a is %d\n",a);
    a=1;//reset the value of a
    printf("The Value Of a is %d\n",++a);
    printf("The Value Of a is %d\n",a);
    a=1;//reset the value of a
    printf("The Value Of a is %d\n",a--);
    printf("The Value Of a is %d\n",a);
    a=1;//reset the value of a
    printf("The Value Of a is %d\n",--a);
    printf("The Value Of a is %d\n",a);
}
To Know how increment operator works we must see output
The Value Of a is 1
The Value Of a is 2
The Value Of a is 2
The Value Of a is 2
The Value Of a is 1
The Value Of a is 0
The Value Of a is 0
The Value Of a is 0
Here for “a++” the value is not increased in first call but in next call it’s increase by 1. Now for “++a“ the value is increased in first call and in next call it show same result.
Here for “a--” the value is not decreased in first call but in next call it’s decrease by 1. Now for “--a“ the value is increased in first call and in next call it show same result.

Friday, 27 June 2014

Know About Switch Case Statement and Break keyword in C programming

In programming it may happen that program control have to make a decision from number of choices. This problem may solve by using “if else” control statement but for more easy work we go for “switch case” control statement.

Here when we run a program containing “switch case” first an integer or a character expression is given to switch. Now here switch is connected with more than one case. The case have a default constant expression that is an integer or character. Now switch checks that which expression is match with given expression. If it match with any case then control pass to that case then it will complete execution. Remember that expression must be in integer.
If no case match then there is another case which is called “default” case, which will execute.
Now let’s see an example
#include<stdio.h>
void main()
{
    int a;
    printf("enter a number within 1 to 5\n");
    scanf("%d",&a);
    switch(a)
    {
        case 1:
        {
            printf("your entered number is 1");
            break;
        }
        case 2:
        {
            printf("your entered number is 2");
            break;
        }
        case 3:
        {
            printf("your entered number is 3");
            break;
        }
        case 4:
        {
            printf("your entered number is 4");
            break;
        }
        case 5:
        {
            printf("your entered number is 5");
            break;
        }
        default:
        {
            printf("you enter wrong value");
        }
    }
}
In above example we found “break” keyword. If we don’t write the “break” then other cases after target are also be printed. But we don’t want that. So we write “break” after every case. But we don’t write break after default because it is the last case. “break” actually break the control path and return it to main path.
Now Let’s see the output of this example
enter a number within 1 to 5
4
your entered number is 4

Use of if else statement in C programs

“if”, “else”, and “else if” are called decision control instructions. In programming it may happen sometime that it have to make decision. This decision types are true of false. For example if condition is true then flow of control goes one way if false then goes to another way. It is important that coder write the instructions in the way that it will works perfectly. Now using “if”, “else” we can execute single statement or a group of statements.

Let’s see the example
#include<stdio.h>
void main()
{
    int a,b;
    printf("Enter The Value of a\n");
    scanf("%d",&a);
    printf("Enter The Value of b\n");
    scanf("%d",&b);
    if(a>b)
    {
        printf("a is bigger than b");
    }
    else if(b>a)
    {
        printf("b is bigger than a");
    }
    else
    {
        printf("a and b is equal");
    }
}
In above example we ask for two value form user and want to see which one is bigger. Here first “if” statement check whether value inside variable “a” is bigger than value inside “b” or not. If condition is true then it will print “a is bigger than b”. If condition become false then it goes to “else if” statement. Here it will check the condition whether value inside variable “a” is smaller than value inside “b” or not. If condition is true then it will print "b is bigger than a". If this condition is false too then control goes to last “else” statement. Now here coder pass the default statement, as here if a>b and b>a become false then the last condition left that is a=b. for this we can write “else if (a==b)” but as we know that it is the last condition left we pass it through default condition statement that is “else”. Note that we do not write any condition in “else” statement.
Now let’s see the output for above program
Enter The Value of a
20
Enter The Value of b
56
b is bigger than a

Thursday, 26 June 2014

Know About Local Variable and Global Variable

There are two type of Declaration for variable, depending upon type and place of declaration we call it
1. Global variable
2. Local Variable
Global variable is a variable which is declared outside of any function inside that program. The special thing is that these variables can be accessed from any were. Values are inserted in these variables can globally accessed.
Local variables is a variable which is declared inside of any function. Here the value of that variable is only accessed by that function.
Now let’s see the example
#include<stdio.h>
int a=12;
int b=10;
void main()
{
    printf("value of a %d\n",a);
    printf("value of b %d\n",b);
    add();
}
int add()
{
    int sum;
    sum=a+b;
    printf("The Value of Sum is %d",sum);
}
Here “a” and “b” are global variables. They can access by any function, for example math operation done on “a” and “b” inside “add()” function also value of “a” and “b” printed from “main()” function.
Here “sum” is a local variable. It only can accessed by function “add()”. If we try to call “sum” variable from “main()” function then it will cause errors.
Now let’s see the output of this program
value of a 12
value of b 10
The Value of Sum is 22

Wednesday, 25 June 2014

Types of functions, with arguments and without argument

Types of functions
There are mainly two types of functions. Function without arguments and Function with arguments. The main difference is Functions without argument is call a function without parameters where Function with arguments is call a function with parameters.
Now Let’s see the example of function without argument
#include<stdio.h>
void without();
void main()
{
    without();
}
void without()
{
    int a,b,c;
    a=34;
    b=56;
    c=a+b;
    printf("Value of C is %d",c);
}
Here no parameters are passed through “without()” function. All calculation dun inside function “without()” is declared within it. Output of this program is
Value of C is 90
Now let’s see an example of function with argument
#include<stdio.h>
void with(int,int);
void main()
{
    with(34,56);
}
void with(int a,int b)
{
    int c;
    c=a+b;
    printf("Value of C is %d",c);
}
Here parameters are passed through “with()” function. The value is declared in “main()” function and they are automatically addressed to variable of function “with()”  simultaneously after that they are executed. The order you maintain in parameters same way they are stored in variable. For example as here a=34 and b=56.
Let’s see the output of this program
Value of C is 90

Use Functions in C language

Function is a self-contained block of statement that perform a coherent task of some kind. In programming languages function played an important role. Here in every program we use “main()” function. When compiler start compiling if first find out “main()”. If any other function is called from “main()” only then that function will be executed.

Now there are some readymade library functions are also available in C language that is “printf()”, “scanf()”. To use these function we are using header files. For example if we want to use “printf()” in c language. Then we must use “#include<stdio.h>”. Same for C++ language if we want to use “cout” then header file “iostrem” is important.
Now here we also can create our own functions too. Why it is important? May be you need to compute same operation many time. Now if we write the code without using function then it will increases the size of the code. So if we call a function from main function then and put all operation inside that called function then size of code is much less and complexity is also decreases.
Now there is three important stapes to create a function.
1. Function Prototype Declaration
2. Function Call
3. Function Definition
we will understand these stages by example
#include<stdio.h>
void you();//Function Prototype Declaration
void main()
{
    you();//function Call
    printf("I am in main Now\n");
}
void you()//function definition
{
    printf("I am in Your Function\n");
}
Here “you ()” is a user defined function. Now when this function is called from “main()” function the control pass to “you ()” function and activity of main function is suspended. After execution of “you()” function is complicated the control back to “main()” function and it start execution.
Now Let’s see the output of this program
I am in Your Function
I am in main Now
Now what if you call that function after “printf()”  in program. Let me write code for you
#include<stdio.h>
void you();//Function Prototype Declaration
void main()
{
     printf("I am in main Now\n");
     you();//function Call
}
void you()//function definition
{
    printf("I am in Your Function\n");
}
Let see the output of this program now
I am in main Now
I am in Your Function
Function is start execution from where you call it. The statement in program must executed in the same order in which you want them to executed.
Now how function call work
In programming “main()” function is executed first. If you declare a function which is not connected with “main()” will not executed. For example you may declare functions “fun1()”, “fun2()” and “fun3()”. “fun1()” called from “main()” and “fun2()” called from “fun1()”. After execution of “fun2()” complicated the control back to “fun1()” then “main()”. Here “fun3” is never executed because it is not connected from “main()”.
Now let’s see the example
#include<stdio.h>
void fun1();
void fun2();
void fun3();
void main()
{
    fun1();
    printf("I am in main Now\n");
}
void fun1()
{
    printf("I am in fun1\n");
    fun2();
}
void fun2()
{
    printf("I am in fun2\n");
}
void fun3()
{
    printf("I am in fun3\n");
}
As we can see the “fun3()” is not connected with “main()”. Let’s see the output now
I am in fun1
I am in fun2
I am in main Now
So “fun3()” is declared but not executed.

Monday, 23 June 2014

Use printf() and scanf() function in c language

The most important thing in programming is input and output. A suitable program get user input dynamically. Suppose you create a program that calculate sum of two number. Then you compile it and generate .exe file.
Input Output

 Now the problem is if users can’t input the value they want then this program calculate same calculation again and again which have no mining. So we need to get input from users. Now in c language we use “scanf()” function to get data from users through keyboard. “scanf()” is a counter part of “printf()” function. As we know that “printf()” print the value as output where as “scanf()” get the value for calculation. Both function “printf()” and “scanf()” belongs to same header file that is standard input output - “stdio.h”.
Now let’s write the code for it  
#include<stdio.h>
void main()
{
    int a,b,c;
    printf("Enter the value of a \n");
    scanf("%d",&a);
    printf("Enter the value of b \n");
    scanf("%d",&b);
    c=a+b;
    printf("The sum of a and b is %d",c);
}
The above program is about sum of two inputs. “a” and “b” hold the value of inputs, and “c” hold the sum of two numbers by “+” operation.
Now when we printing these values inside “printf()”, we use format specifier. For each data type have it’s own format specifier. For example integer format specifier is %d, floating point specifier is %f. if we want to write variable value inside “printf()” then we need to write these format : “printf(“format specifier”, variable name);”
in “scanf()” function “&” before variable is must. “&” work as a “address of operator” in c language. It gives the location number used by the variable in memory. When we enter any input value, the value is stored in the location that variable addressed.
Now let’s see the output of these program
Enter the value of a
12
Enter the value of b
16
The sum of a and b is 28
Now what if we want to get both input in same line, to describe these we will write a program
#include<stdio.h>
void main()
{
    int a,b,c;
    printf("Enter the value of a and b\n");
    scanf("%d %d",&a,&b);
    c=a+b;
    printf("The sum of a and b is %d",c);
}
As we see the format in these program that in a single “scanf()” we can get more than one input value. As many input we want we must write format specifier for each and every variable.
Now let’s see the output of above program
Enter the value of a and b
23 12
The sum of a and b is 35
As we can see here that when we entering the value inside the program, each value must be separated by space or enter or tabs.

Saturday, 21 June 2014

Know about Data type, Keyword, Constant and Variables in C and C++

In computer programs we do lots of calculations. To execute these calculations we need memory space which can hold that value. To identify that name we will use a name which will address that memory location. The name is called Variable, if “a” is a name of memory location then “a” is a variable. 
Now for Constant definition we will say that Constant is an entity that doesn’t change, where variable is an entity that may change. For example if we put a value in a memory location then that value is a constant. Suppose variable “a” holds value 25, then 25 is a constant here. If we replace 25 with 45 then value that variable holds changed to 45, but 25 is still 25.

There are mainly three types of Primary Constants. These are Integer Constant, Real Constant, and Character Constant. Now an integer constant holds integer values, which can either positive or negative.  For examples we will say that 2, 3, -5. The allowable range for integer constant is -32768 to 32767. Real constant also called as floating point constants. For example we say 2.5, 35.700, 120.532, and 9.00. The allowable range for Real constant is -3.4e38 to 3.4e38. 
Now particular type of constants only holds by particular type of variables. These variables types are defined by Data types. If “a” is a variable and we want to make it integer type, then declare this as “int a”. There are many type of data types, some common type of data types are int, float, char.   
Keyword are “Reserved Words” whose meaning is already been explained in compilers. Keyword are used in programming for making proper instructions. Now a variable name can’t be a keyword. 
There are total 32 keywords in C compiler. Each keywords have a special mining. Data types are also keyword. Keywords are:
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while 
Now let me write a code for Data type, Keyword, Constant and Variables  
#include < stdio.h >
void main()
{
    int a=10;
    float b=20.3;
    char c='A';
    double const d=16;
    printf("The Value of a is %d\n",a);
    printf("The Value of b is %.2f\n",b);
    printf("The Value of c is %c\n",c);
    printf("The Value of d is %lf\n",d);
Here in these program “int”, “float”, “char”, “double” are data types. “const” is a keyword which says that value in variable “d” will not change during program, and “a”, “b”, “c”, “d” is variables. 
Now when we printing these values inside “printf()”, we use format specifier. For each data type have it’s own format specifier. For example integer format specifier is %d, floating point specifier is %f. if we want to write variable value inside “printf()” then we need to write these format : “printf(“format specifier”,variable name);” Now let’s see the output for these code
The Value of a is 10
The Value of b is 20.30
The Value of c is A
The Value of d is 16.000000
Now let me write same problem in C++ language. 
#include < iostream >
using namespace std;
int main()
{
    int a=10;
    float b=20.3;
    char c='A';
    double const d=16;
    cout< <"The Value of a is"< <a< <"\n";
    cout< <"The Value of b is"< < < <"\n";
    cout< <"The Value of c is"< <c< <"\n";
    cout< <"The Value of d is"< <d< <"\n";
    return 0;
}
As we can see here that format specifier is not important in C++. We can write variable directly. Now let see the output 
The Value of a is 10
The Value of b is 20.30
The Value of c is A
The Value of d is 16

First C++ Program, Print Hello World in C++

C++ is a general purpose programming language. When you write a code for C++ remember to save it with .cpp file extension where .c file extension will work with C language. Now C++ is more flexible and efficient then C. In the first stage we will write a program about how to print something in C++ language. We will print simple “Hello World!” .Now let’s write the code and then we will know more about it
#include <iostream >
using namespace std;
int main()
{
    cout<<"Hallo World!";
    return 0;
}
Now let me describe this code. Here "#include" is called Processor Directive and " iostream " is a header file for C++. A Header file contain the description about functions it holds. Now Here “using namespace std;” is written for avoiding name space collision. It is done by localizing names of identifier. “std” is the name space.  
Here “main()” is the main function. Every program must hold not more than one main function. Execution of the program start from main function. The open close bracket with main function “()” is called parenthesis. Here “int main()” says that the main function is an integer type. An integer type function must return something, but in this program we are not going to return some value so we write “return 0” for that.
The open close bracket after main “{}” which will says that the body of main function is written inside this brackets.
Now inside the body we found that “cout < <”Hello World”;” where cout is a standard C++ function. This function is work for printing some outputs. If you want to use “cout” write it in this format – “cout<<”Your Text Here”;” then any this you write replacing “Your Text Here” will be printed.
Now we know the basic of C++, Let us print the output of above program.
Hello world!
So you successfully run your first stage of coding.

Friday, 20 June 2014

First C Programming, Print Hello World In C language

First stage of C programming is to print some thing. first you need to open your c editor. If you r using Windows 7 then  codeblock is best way to write c and c++ code. We are going to print "Hello World" in C language. Now let's write the code for it
#include<stdio.h>
#include<conio.h>
void main()
{
    printf("Hello World!");
}
The above Program is written in C language, Before we see the output let me describe the code here. In program languages we pass many instructions. This instructions will run in different statements. For example the instruction above "  printf("Hello World"); " says print "Hello World". All statements are separated by ";".
Here "main()" is a function. A function is nothing but a set of statement. Now every program contain not more then one main function. Besides "main()" function here in C language we use readymade function. Here "printf()" is another function. "printf()" will print any thing written inside it. The way of using "printf()" is "printf("Your Text Here"); ".
To use "printf()" you must use "#include<stdio.h>" where  "#include" is called Processor Directive and "Stdio.h" is a header file. "Stdio.h" means Standard Input Output Header File. Header files contain the description about that function it holds. There are so may other header files, for example "conio.h" is another header file.
Now Let us see what will be the output of above program.
Hello World!
So you successfully run your first stage of coding.