Thursday, 28 April 2011

C Tutorial Chapter 2


Hello friends i hope you read my previous tut on basics of C, if you haven’t than please first read it Smiley

In this tut we will learn about :

  • Instructions in C
  • Assignment Operators
  • Type Conversion
  • Operator Precedence in C
  • Data Types in Detail

So lets start Wink

In our previous C tut we had seen different types of constants, variables and keywords. Before discussing anything else let me first tell you about primary datatypes in C.
The Primary Datatypes in C are :
A.   int : it represents whole numbers within the range -32768 to +32767
B.   float : it represents real numbers within the range -3.4e38 to +3.4e38
C.   char : it represents all valid ASCII characters
2.1 Instructions in C

The next logical step is to learn how constants, keywords, variables and datatypes are combined to form instructions. There are basically four types of instructions in C.
2.1.1   Type Declaration Instruction2.1.2   Arithmetic Instruction2.1.3   Input/Output Instruction2.1.4   Control Instruction

Now in this chapter we will discuss only Type Declaration Instruction and Arithmetic Instruction as it is part of every C program. We will dealt with other two types in relevant chapters Wink
2.1.1 Type Declaration Instruction

This instruction is used to declare the type of variables used in C program. Any variables used in
the program must be declared before using it in any statement. The type declaration is usually written at the beginning of the program.

Examples :

int ivar ;      /* declares a variable ivar of type int */

float ah_rocks   /* declares a variable ah_rocks of type float */

char ah      /* declares a variable ah of type char */
2.1.2 Arithmetic Instructions

Arithmetic Instructions are combination of variables operators (described next) and constants.

Here is a simple program which covers both the instructions discussed above.

Code:
/ * A program explaining arithmetic instruction and type declaration instruction */

#include
#include
void main()
{
clrscr();
int x;   /* Variable Declaration */
float y; /* Variable Declaration */

x=20;  /*  Assigning a constant value of 20 to variable x using assignment operator ‘=’ */

y = x+3.14; /* Combination of variables, operators (=,+) and constants */

printf(“The Value obtained is : “);
printf(“%f”, y); /* Read Note */
 
getch();
} /* Program end */

Output is :

Quote
The Value obtained is : 23.14

Note :

%f :  is called as format specifier and it is used with pritnf() to display float values
%d : is used with printf() to display integer values
%c : is used to display character values
P.S : We will deal with format specifier in detail in coming chapters Smiley so don’t worry Wink

***************

2.2 Operators

Operators in C are classified into

Assignment Operator
Arithmetic Operator
Relational Operator
Logical Operator
2.2.1 Assignment Operator

The assignment operator is a single equal sign(=).
This operator is used to assign values to variables.

Example :

int x; /* declares a variable x of type int */
x=30 /*assigns a constant value of 30 to x */
2.2.2 Arithmetic Operators

Arithmetic operators are used in mathematical expressions (arithmetic instructions). Just as they are used in algebra.

The following table lists the arithmetic operators:

Sr. NoOperatorDescriptionType
1+AdditionUnary Binary
2-SubtractionUnary Binary
3*MultiplicationBinary
4/DivisionBinary
5%ModulusBinary
6++IncrementUnary
7--DecrementUnary
8+=Addition AssignmentBinary
9-=Subtraction AssignmentBinary
10*=Multiplication AssignmentBinary
11/=Division Assignment   Binary
12%=Modulus AssignmentBinary

Note :

   
a.) Unary operators are the operators which perform operations on one operand.
   
b.) Binary operators are the operators which perform operation on two operands.
   
c.) Ternary operators are the operators which perform operations on three operands.


2.2.2.1 The Basic Arithmetic Operators (+,-,/,*)

The Basic Arithmetic Operators (+,-,/,*) all behave as you would expect for all
Data types.

Code:
/* Program explaining basic arithmetic operators */

#include
#include
void main()
{
clrscr();
int x,y; /* multiple variable declaration */
int addresult,subresult,divresult,mulresult; /* multiple variable declaration */
x=100; /* assignment */
y=25; /* assignment */
addresult = x + y;
subresult = x-y;
mulresult = x* y;
divresult = x/y;

printf(“Addition Result : %d \n”,addresult); / * see we use %d to print integer so it means here where we wrote %d we want a integer and to tell compiler which result we want at the place of %d we write the int we already declared here in this case addresult */
printf(“Subtraction Result %d \n”,subresult);
printf(“Multiplication Result %d \n”,muresult);
pritnf(“Division Result %d \n”,divresult);
getch();
}

Output :

Quote
Addition Result : 125
Subtraction Result : 75
Multiplication Result : 2500
Division Result : 4

Note : ‘\n’ is called an escape sequence and it is generally used to add a line after displaying.
2.2.2.1 The Modulus Operator (%)

The operator returns remainder of the division operation.
It can only be applied to integer data type.

Code:
/* program showing the use of modulus operator */

#include
#include
void main()
{
clrscr();
int x,y,mod_result;
x=42;
y=10;
mod_result=x%y;
printf(“The result of modulus operation is %d”, mod_result);
getch();
}

Output :

Quote
The result of modulus operation is 2

2.2.2.3 Arithmetic Assignment Operators (+=, -=, /=, %=)

C provides special operators that can be used to combine an arithmetic operation with an assignment operator. Consider a statement x=x+4, now the same statement can be written as x+=4. Both of them adds 4 to the variable x.

Example :

A-=1           /* same as a=a-1 */
y*=4           /* same as y=y*4 */
z%=10   /* same as z=z%10 */

Code:
/* program explaining arithmetic assignment operators */

#include
#include
void main()
{
clrscr();
int a,s,m,d,mod;
a=10;
s=20;
m=30;
d=40;
mod=55;
a+=10;
s-=5;
m*=3;
d/=10;
mod%=10;
printf(“Value of a=a+10 is %d \n”,a);
printf(“Value of s=s-5 is %d \n”,s);
printf(“Value of m=m*3 is %d \n”,m);
printf(“Value of d=d/10 is %d \n”,d);
printf(“Value of mod=mod%10 is %d \n”,mod);
getch();
}

Output :

Quote
Value of a=a+10 is 20
Value of s=s-5 is 15
Value of m=m*3 is 90
Value of d=d/10 is 4
Value of mod=mod%10 is 5

Note : The Arithmetic assignment operators not only save you a bit of typing but also it is implemented more efficiently.
2.2.2.4 Increment and Decrement Operators

The ++ amd – are the increment and decrement operators of C. The increment operator increases its operand by
one and only one and the decrement operator decreases its operand by one and only one.

In the prefix form, the operand is incremented (or decremented) before the value is obtained for the use in the expression. In the postfix form, the previous value is obtained for the use in the expression and the operand is modified.

Example :

x=43;
y=++x;

In this case y is set to 43, because the increment occurs
before x is assigned to y.
Thus y=++x is equivalent to
x=x+1
y=x;

However, when written like this,
x=42;
y=x++;

The value of x is obtained before the increment operator is executed, so the value of y is
42.
In this case y=x++ is equivalent to

y=x;
x=x+1

Code:
/* program demonstrating the increment operator */

#include
#include
void main()
{
clrscr();
int a=1;
printf(“a=%d \n”,a);
printf(“a=%d \n”,a++);
printf(“a=%d \n”,++a);
printf(“a=%d \n”,++a);
printf(“a=%d \n”,a++);
printf(“a=%d \n”,a);
getch();
}

Output :

Quote
a=1
a=1
a=3
a=4
a=4
a=5

Note : The decrement operator works in the same manner.

***************


This is not the full chapter 2 i am lil busy will post the rest part in 1-2 days Smiley

please give your suggestions and try to correct me if i am somewhere wrong Smiley

Thanks

No comments:

Post a Comment