Sunday, 8 January 2012

Constants

Summary: in this tutorial, you will learn how use C constants in your code to make your code more readable and easy to maintain.
Introduction to C constants
C constants using C preprocessor
There are a lot of cases you want to use a constant in your code for example to calculate the square of a circle you code as follows:
square = 3.14159 * diameter;
In the statement, the constant 3.14159 represents the constant PI. Instead of putting constant in literal form, we can use symbolic constant as below:
square = PI * diameter;
As you see, the C constants make your code readable and the symbolic constants tell you not only the value but also its meaning. In addition, if a constant is used in several places, it is better to use symbolic constant because when you want to change its value you just have to change it in one place that define the constant, see the below example:
1
total =  net + TAXRATE * net;
Later on if the tax rate changes, you only have to change it in where it is defined.But how do you define symbolic C constants? The first idea of defining a C constant is using C preprocessor as follows:
#define PI 3.14159
#define TAXRATE 0.1
By doing this, when your program compiled, the constant values 1.14159 and 0.1 will be substituted everywhere you used PI and TAXRATE. This feature is known as compile time substitution. Those defined constants are called manifest constants because when you run the program, all the substitutions have already been made.
Note that there is no equal sign between constant’s name and value and there is no semicolon (;) at the end of the statement.
It is highly recommended that you use constant name is upper case so when you see it in code you know that it is a constant. The formula of defining a constant is as follows:
#define CONSTANT_NAME value
C constants using const keyword
C90 officially added a second way to create symbolic constants using the constkeyword to declare a constant. This approach is more flexible that using C preprocessor #define such as you can use keyword const to declare an array, pointer…etc.Once the value of a constant using const keyword has been initialized, its value cannot be changed through the program. See the below example:
const double PI = 3.14159;
const double TAXRATE = 0.1;

Saturday, 7 January 2012

Operators in C

C supports various operators to perform different kind of operations. 
With C operators, you can do arithmetic, compare data, modify variables, combine relationship logically... etc. C operators operates on one or more operands to produce a value.
Operators taking just one operand are called unary operators.
Operators requiring two operands are called binary opeartors.
Data itself is called as operands.
Types of operators are
1)Arithmetic Operators
supports almost common arithmetic operators such as +,-,* , / .     
            Continue reading.......
2)Modulus Operator
Modulus operator %.It is an unary operator..... 
              Continue reading.......
3)Unary Operators
Increment and Decrement Operators are called Unary Operators.....
.          Continue reading.......
4)Relational Operators
C provides you a list of relational opeartors to allows you to compare data. The relational operators enable you to check whethere two variables or expressions are equal, not equal, which one is greater or less than other......
             Continue reading.....
5)Logical Operators
C logical operators enable to combine relational operators or logical operators into one boolean result. C supports the negation (!), logical AND (&&), and logical OR (||).....
             Continue reading.....
6)Bitwise Operators
C provides you six bitwise operators including AND, inclusive OR, left shift, right shift and one's complement for bit manipulation.......
             Continue reading.....
7)Assignment Operators
C assignment operators are used to assigned the value of a variable or expression to a variable..... 
             Continue reading.....
8)Conditional Operator
C ternary operator is sometimes called conditional operator or trinary operator because it is a shorthand of combination of the if-else and return statement and has three operands........ 
             Continue Reading......

Conditional Operator

Conditional operator is sometimes called tenary operator or trinary operator because it is a shorthand of combination of the if-else and return statement and has three operands. 
The syntax of C ternary operator is as follows:
condition ? expression 1 : expression 2
C first evaluates the condition. Based on the return value of the condition the second or third operand is evaluated:
If the condition is evaluated to true (1), only the second operand (expresion 1) is evaluated. Then the operator return the value of the expression 1.
If the condtion is evaluated to false (0), only the third operand (expression 2) is evaluated. The value of the expression 2 is returned.
The ternary operator can be rewritten as the if-else and return statement as follows
if(condition)
   return expression1;
else
   return expression2;
Example of C Ternary Operator
In this example, we are going to write an inline function that uses C ternary operator to find the minimum number between two integers.
/*Purpose: A program demonstrates C ternary operator */
#include <stdio.h>
#include <stdlib.h>
/* Find min of two integers
   @param x
   @param y
   @return minimum value of x and y */
inline int min(int x, int y) { return x <= y ? x : y; }
int main(int argc, char** argv) 
{
    int x = 10;
    int y = 15;
    printf("x=%d,y=%d\n", x, y);
    printf("min(x,y)=%d\n", min(x, y));
    return (0);
}
x=10,y=15
min(x,y)=10

Assignment Operators

C assignment operators are used to assigned the value of a variable or expression to a variable. 
The syntax of assignment operators is:
var = expression;
var = var;
Beside = operator, C programming language supports other short hand format which acts the same assignment operator with additional operator such as +=, -=, *=, /=, %=.
var +=expression; //is same as
var = var + expression;
Each assignment operator has a priority and they are evaluated from right to left based on its priority. 
Here is assignment operator and its priority: =, +=, -=, *=, /=, %=.
A simple C program to demonstrate assignment operators:
#include <stdio.h>
/* a program demonstrates C assignment operator */
void main(){
    int x = 10;
    /* demonstrate = operator */
    int y = x;
    printf("y = %d\n",y);
    /* demonstrate += operator */
    y += 10;
    printf("y += 10;y = %d\n",y);
    /* demonstrate -= operator */
    y -=5;
    printf("y -=5;y = %d\n",y);
    /* demonstrate *= operator */
    y *=4;
    printf("y *=4;y = %d\n",y);
    /* demonstrate /= operator */
    y /=2;
    printf("y /=2;y = %d\n",y);
}
Here is the output:
y = 10
y += 10;y = 20
y -=5;y = 15
y *=4;y = 60
y /=2;y = 30
host gator coupon