Saturday 4 October 2014

Operator and Expressions

Operators and Expressions

An operator that performs a specific task and  yield the value. The variables, constants can be joined by various operators to form an expression. An operand is a data on which operator acts. Some operators require two operands.


There are many type of Operators-
  1. Arithmetic Operators
  2. Assignment Operators 
  3. Increment and Decrements Operator
  4. Relational Operator
  5. Logical Operator 
  6. Conditional Operator 
  7. Comma Operator 
  8. Sizeof Operator
  9. Other Operators 

Arithmetic Operator

Arithmetic operator are used for numeric calculations. They are of two type-
  1. Unary arithmetic operator
  2. Binary arithmetic operatoer

Unary Arithmetic Operator : In unary operator only one operand are used.
e.g. +x , -y ,+z , +r.

Binary Arithmetic Operator : Binary operator require two operands. There are five binary arithmetic operators-

Operator
Purpose


+
Addition
-
Subtraction
*
Multiplication
/
division
%
Give the remainder


Example :-

Integer Arithmetic :- When both operand are integer then the arithmetic operation with these operands is called integer arithmetic and the resulting value is always an integer. take two variable a and b. The value of a=17 and b=4, the result of the following operand are-


Expression
Result


a+b
21
a-b
13
a*b
68
a/b
4
a%b
1

After division operation the decimal part will be truncated and result is only
integer part of quotient.

#include<stdio.h>
main()
{
int a=17 , b=4;
printf("Sum =%d\n", a+b);
printf("Substract =%d\n",a-b);
printf("Product =%d\n",a*b);
printf("Quotient=%d\n,a/b);
printf("Remainder =%d\n",a%b);
}

Output-
Sum=21
Substract=13
Product=68
Quotent=4
Remainder=1


Floating point Arithmetic : When both operands are of float type then the arithmetic operation with these operands is called floating point arithmetic. Take two variable a=12.4 and b=3.1 then result of the following operations.


Expression
Result


a+b
15.5
a-b
9.3
a*b
38.44
a/b
4.0

The modules operator % cannot be used floating point number

#include<stdio.h>
main()
{
float a=12.4, b=3.8;
printf("Sum =%.2f\n",a+b);
printf("substract =%.2f\n",a-b);
printf("Product =%.2f\n",a*b);
printf("a/b =%.2f\n",a/b);
}


Assignment Operator


A value can be stored in a variable with the use of assignment operator. This assignment operator "=" is used in assignment expression and assignment statement.
The operand on the left hand side should be a variable, while the operand on the right hand side can be any variable, constant or expression. The value of right hand side operand is assigned to the left hand operand. Example --

x=8               8 is assigned to x.
y=5               5 is assigned to y
s=x+y-2        Value of expression x+y-2 is assigned to s
y=x               Value of x is assign to y.
x=y               Value of y is assigned to x

The value that is being assigned is consider as value of the assignment expression. for example =8 is an assignment expression whose value is 8.
We can multiple assignment operation also.

x=y=z=20
Here all three variable x,y,z will be assigned value 20, and the value of the variable whole expression will be 20.
if we put a semicolon after the assignment expression then it become an assignment statement  for e.g. 
x=8;
y=5;
s=x+y-2;
x=y=z=20;
when the variable on the left hand side of assignment operator also occurs on the right hand side then we can avoid writing the variable twice by using compound assignment operators. for e.g.
x=x+5
can also be written as
x+=5
here += is a compound statement
we have other compound statement-
x-=5             is equivalent                      x=x-5
y*=5             is equivalent                      y=y*5
sum/=5         is equivalent                     sum=sum/5



Increment And Decrements Operators

C has two useful operators increment(++) and decrement(--). there are unary operators because they operate on a single operand. The increment operator(++) increment the value of the variable by 1 and decrement by 1 and decrement operator (--) drecrements the value of the variable by 1.
++x    is equivalent    x=x+1
--x      is equivalent    x=x-1
These operators should be used only with variable; they can't be used with constant or expressions.
for example the expressions ++5 or ++(x+y+z) are invalid.

These operator are two type-


  1. prefix increment / decrement 
  2. Postfix increment / decrement 

  prefix increment / decrement 

In prefix,operator written before the operand e.g. ++x or --x . here first the value of the variable is incremented/decremented then the new value is used in the operation. take a variable x whose value is 3. means x=3.
The statement y=++x; means first increment the value of x by 1, then assign the value of x to y.
This single statement is equivalent to these two statements-
x=x+1;
y=x;
Hence now value of x is 4 and value of y is 4.

The statement y=--x; means first decrement the value of x by 1 then assign the value of x and y.
This statement x=x-1;
y=x;
Hence now value of x is 3 and value of y is 3.

// Program to understand the use of prefix increment/ decrements 
#include<stdio.h>
main()
{
int x=8;
printf("x=%d\t",x);
printf("x=%d\t",++x);
printf("x=%d\t,x);
printf("x=%d\t",--x);
printf("x=%d\t",x);
}

Output
x=8, x=9, x=9, x=8;x=8


Postfix Increment /Decrement

Operator is written after the operand. here first the value of variable is used in the operation and then increment/decrement is performed. take a variable x whose value is 3 means x=3;
The statement y=x++; means first the value of x is assigned to y and then x is incremented. This statement is equivalent to these two statements-
y=x;
x=x+1;
hence now value of x is 4 and value of y is 3
The statement y=x--; means first the value of x is assigned to y and then x is decremented. this statement is equivalent to these two varibale-
y=x;
x=x+1
now value of x is 3 and value of y is 4

// understood by program//

#include<stdio.h>
main()
{
int x=8;
printf("x=%d\t",x);
printf("x=%d\t",x++);
printf("x=%d\t,x);
printf("x=%d\t",x--);
printf("x=%d\t",x);
}

Output
x=8, x=8, x=9, x=9;x=8



Relational Operators

Relational operators are used to compare values of two expression depending on their relations. An expression that contains relational operators is called relational expression. If the relation is true then the value of relational expression is 1 and if the relational is false then the value of expression is 0.
The relational operators are.

Operator
Meaning


< 
Less than
<=
Less than or equal to
==
Equal to
!=
Not equal to
> 
Greater than
>=
Greater than or equal to
Two variables a=9 and b=5, and form simple relational expression with them

Expression
Relation
Value of Expression



A<b
False
0
A<=b
False
0
A==b
False
0
A!=b
True
1
a>b
True
1
A==0
False
0
B!=0
True
1

The relational operators are generally used in If.......else construct and loops. 

=Program to understand the use of relational operators.

#include<stdio.h>
main()
{
int a,b;
printf("Enter the value for a and b");
scanf("%d%d",&a,&b);
if(a<b)
printf("%d is less than %d\n",a,b);
if(a<=b)
printf("%d is less than or equal to %d\n",a,b);
if(a==b)
printf("%d is equal to %d\n",a,b);
if(a!=b)
printf("%d is not equal to %d\n",a,b);
if(a>b)
printf("%d is greater than %d\n",a,b);
}

It is important to note that the assignment operator(=) and equality (==) are entirely different. assignment operator = is used to assign the value, and == operator is used to compare the value.



Logical Or Boolean Operators

An expression that combines two or more expressions is termed as a logical expression. for combining these expressions we use logical operators. These operators return 0 for false and 1 for true. The operands may be constants, variable expressions. C has three logical operators.

Operator
Meaning


&&
AND
||
OR
!
NOT

  1. AND (&&) Operators

This operator give the net result true if both the conditions are true,otherwise the result is false.


Condition1
Condition2
Result



False
False
False
False
True
False
True
False
False
True
True
True

Lets us take three  variable a=10, b=5, c=0
if we have a logical expression 
(a==10)&&(b<a)Here both the conditions a==10 and b<a are true, and hence this whole expression is true. Since the logical operators return 1 for true hence the value of this expression is 1.


Expression

Result
Value of expression




(a==10)&&(b>a)
True && false
False
0
(b>=a)&&(b==3)
False && false
False
0
A && b
True && true
True
1
A && c
True && false
False
0


2.OR (| |) Operator :

This operator gives the net result false, if both the conditions have value false , otherwise the result is true.

Condition  1
Condition  1
Result



False
False
False
False
True
True
True
False
True
True
True
True

Let us take three variable a=10 , b=5, c=0
Consider the logical expression-
(a>=b) ||(b>15)

this gives result true because one condition is true.

Expression

Result
Value of expression




A||b
True || true
True
1
A||c
True||false
True
1
(a<9)||(b>10)
False||false
False
0
(b!=7)||c
True||false
True
1

3.Not (!) Operator

This is a unary operator and it negates the value of the condition. if the value of the condition is false then it gives the result true. if the value of the condition is true then it gives the result false.

Condition
result


False
True
True
False

Conditional Operator

Conditional operator is a ternary operator (? and :) which requires the three expression as operands. 

Syntax- (condition)? Expression 1 : Expression 2

If the condition is true then expression 1 is proceed and if condition is false then expression 2 is proceed.
for eg. if a=5 and b=6;
(a>b)?printf("the greater", a):printf("b is greater",b);

// program to print the larger of two number using conditional operator

#include<stdio.h>
main()
{
int a,b,max;
printf("enter value for a and b");
scanf("%d%d",&a,&b);
max=a>b? a:b;
printf("Larger of %d and %d is %d\n"a,b,max);
}

Output

Enter value for a and b: 12  7
Larger of 12 and 7 is 12

Comma Operator

The comma operator (,) is used to permit different expression to appear in situations where only one expression would be used. The expression are separated by the comma operator.The separated expression are evaluated from left to right . Comma Operator separated two or more variable.

a=8, b=7,c=9,a+b+c

Here 4 variable 8 is assigned to variable a, 7 is assigned to variable b, 9 is assigned to variable c and all variable is separated by comma operator.

// Program to understand the use of comma operator.

#include<stdio.h>
main()
{
int a,b,c,sum;
sum=(a=8,b=7,c=9,a+b+c);
printf("Sum=%d",sum);
}

Output

24

Sizeof Operators

Sizeof is an unary operator. This operator gives the size of its operand in the term of bytes. The operand can be a variable, constant or any datatype( int, float,char etc) for example- sizeof(int) gives the byte occupied by the int data type.

// Program to understand the sizeof operator.

#include<stdio.h>
main()
{
int var;
printf("Size of int =%d",sizeof(int));
printf("Size of float =%d",sizeof(float));
printf("Size of var =%d",sizeof(var));
printf("Size of an integer constant=%d",sizeof(45));
}

output

Size of int : 2
Size of float : 4
Size of var : 2
Size of integer Constant =2


Friday 3 October 2014

Suppression Character in Scanf()

Suppression Character in Scanf()

If we want to skip any input field then we specify * between the % sign and the conversion specification. The input field is read but its value is not assigned to any address. This character * is called the suppression character. For example-

scanf("%d %*d %d",&a, &b, &c);

Input 

a=25 , b=30 ,c=35

Here 25 is stored in a, 30 skipped and 35 is stored in the b, since no data is available for c so it has garbage value.

scanf("%d %*c %d %*c %d",&d, &m, &y);

Input :

3/1/2003

Here 3 will be stored in d, then / will be skipped , 11 will be stored in m, again / will be skipped and finally 2003 will be stored in y.

#include<stdio.h>
main()
{
int a, b, c;
printf("Enter three number:");
scanf("%d %*d %d",&a,&b,&c);
printf("%d %d %d", a,b,c);
}

Output

Enter three number: 25 ,30 ,35

25  35  25381

the variable c has garbage value.

Formatted Input and Output

Formatted Input and Output


Formatted input and output means that data is entered and displayed in a particular format. Through format specifications, better presentation of result can be obtained.

Format For Integer Input

%wd :- Here is the conversion specification character for integer value and 'w' is an integer number specifying the maximum field width of input data. if the length of input is more than this maximum field width then the value are not stored correctly. e.g.

scanf("%2d%3d",&a,&b);

"when input data length is less than the given width, then the input values are unaltered and stored in given variable."

Input
6,39

Result 

6 is stored in a and 39 is stored in b.
**************************************************************
scanf("%2d%3d",&a,&b);

Input 
26, 39

Result

26 is stored in a and 394 is stored in b.
*************************************************************

scanf("%2d%3d",&a,&b);

Input

269 , 3845

Result 

26 is stored in a and 9 is stored in b and the rest of input is ignored. the reason of ignoring that %2d and %3d , variable a range of storing the value length is 2 digit and variable b is 3 digits because %wd  is responsible for this.
*************************************************************

Format For Integer Output 

Here w is the integer number specifying the minimum field width of the output data. if the length of the variable is less than the specified field width, then the variable is right justified with leading blanks.

For example-

printf("a=%3d, b=%4d", a,b);

when the length of variable is less than the width specifier.

Value of variable

78 9

Output

here * is space
a= *78, b=***9

the width specifier of first data is 3 while there are only 2 digits in it, so there is one leading blank. The width specifier of second data is 4 while there is only 1 digits, so there are 3 leading blanks.  
******************************************************

  • when the length of the variable is equal to the width specifier

value of variable 

263   1941

Output

a=263, b=1941

  • When length of variable is more than the width specifier, than also the output is printed correctly.
value of variables-

2691   19412

Output

a=2691, b=19412

example-

#include<stdio.h>
main()
{
int a=4000, b=200, c=15;
printf("a=%d\n b=%d\n c=%d\n,a,b,c);
printf("a=%d\n b=%d\n c=%d\n,a,b,c);
}

Output of the first printf would be

a=4000
b=200
c=15

while the second output of prinf

a=4000
b=200
c=15

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


Format For Floating point Numeric Input-

%wf :- Here 'w' is the integer number specifying the total width of the input data (including the digits before and after decimal and the decimal itself ).

scanf("%3f %4f",&x , &y);

when input data length is less than the given width, value are unaltered and stored in the variables.

Input  :    5  , 5.9

Output / result: 5.0 is stored in x and 5.90 is stored in y.
********************************************************

scanf("%3f %4f",&x , &y);

When  input data length is equal to the given width, then value are unaltered and stored in the given variables.

Input  : 5.3 , 5.92

Result  :  5.3 stored in x and 5.92 is stored in y.
*******************************************************

scanf("%3f %4f",&x , &y);

When input data length is more than the given width then the given values are altered and stored in the given variable.

Input   :  5.93 ,  65.87

Result  :  5.9 is stored in x and 3.00 is stored in y. 

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


Format For Floating point Numeric Output

%w.nf :- here w is the integer number specifying the total width of the input data and n is the number of digits to be printed after decimal point. By default 6 digits are printed after the decimal. for example 

printf("x=%4.1f, y=%7.2f", x,y);

if the total length of the variable is less than the specified width 'w' then the value is right justified with leading blanks. if the number of digits after decimal is more than 'n' then the digits are rounded off.

Value of variable

a.   8, 5.9
b.   25.3,1635.92

Output :- * used like a blank space

a.  x=*8.0, y=***5.90

b.  x=25.3, y=1635.92




Method of Output

Writing Output Data-

Output data can be written from computer memory to the standard output device(monitor) using printf() library function. Output is the method to display the massage or input variable on the screen so that user cab see our program result, using printf() function.

There are two way to using printf() function in C language-
first way when we want to display only information and massage then we use the formate-

Syntax- printf(" write massage here:");
E.g.

printf('' Hi! I am sudhir Singh:");

When we want only display the massage in the program, then we write the massage in printf function in double quotes as above in the example......

the second way we want display the value of variable that is hold at the time of input means through the scanf() function. when we want to display the value of variable then use control string as used in scanf() function but major different is,in the printf function we did not use & sign with the variable because & sign used for create memory but here use only display the value of variable so only use the variable in the printf function. 

Syntax- printf("control string",variable 1, variable 2,.........);

e.g.

printf("%d%d", a,b);
here two variable a and b that is display the variables value, we did not use the & sign with the variable a and b. when we display the information then conversion specification is not used.

Example :

#include<stdio.h>
main()
{
int age;
printf("Enter any age for any one");
scanf("%d",&age);
printf("%d",age);


*******************************************************
#include<stdio.h>
main()
{
int basic=2222;
.................
printf("%d",basic);
.................
}

in this example control string contains a conversion specification %d which implies that an integer value will be displayed. The variable basic has that integer value which will be displayed as output.


*******************************************************************************
#inlude<stdio.h>
main()
{
float height=5.6;
printf("%f",height);
}

Here control string has conversion specification character %f, which means that floating point number will be displayed. The variable height has that floating point value will be displayed as output .

**************************************************************
#include<stdio.h>
main()
{
char ch='$';
printf("%c",ch);
}

In this example the control string has conversion specification character %c means that a single character will be displayed and variable ch has that character value.


**************************************************************
#include<stdio.h>
main()
{
int b=500;
float h=200.50;
char g='A';
printf("basic =%d\n hra=%f\n grade=%c\n", b,h,g);
}

Output

Basic=500
hra  = 200.50
grade=A

in this example we print the massage and value together format is above in the program, in the printf function '\n' used, '\n moves the cursor to the beginning of next line.Here we have placed a '\n' at the end of control string also. it ensure that the output of the next program starts at a new line.

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



#include<stdio.h>
main()
{
int b=500;
float h=200.50;
char g='A';
printf("basic =%d\t hra=%f\t grade=%c\n", b,h,g);
}

Output

Basic=500     hra=200.50     g=A

in this example '\t' moves the cursor to the next tab stop.
for example we can use: \b moves the cursor one position back, \r moves the cursor to the beginning of the current line and \a alert user by a beep sound.\v moves the cursor to the next vertical tab position  and \f moves the cursor to the next page.
If we want to print character like single quote(') and double quote (") or the backslash character (\), then we have to precede them by a backslash character in the format string.

For example-

printf("10\\ 04 \\1992");       then print         10 \ 04 \1992

printf(" she said,\" sudhir mad\"."); then    she said,"sudhir mad".







Wednesday 1 October 2014

Reading Data in C

Input Data in C

Input data can be entered into the memory from a standard from a standard input device(keyboard). C provide scanf() library function for entering input data. this scanf() can take all type of values ( numeric, character, string, float etc) as input.

Syntax of sacnf():  

scanf("control string/conversion specification",address1 , address2,........n);

This function should have at least two parameters, First parameter is a control string, which contains conversion specification characters. It should be within double quotes. The conversion specification character may be one or more. number of conversion specification depends on number of variable/address in the scanf ().if we use 2 address then use 2 conversion specification, three address, three conversion specification. it depends on the number of variable we want to input. the other parameter are addresses of variables. in the scanf() function at least one address should be present.The address of variable is found by preceding the variable name by an ampersand(&) sign. This sign is called address operator and it gives the starting address of the variable name in memory. A string variable is not preceded by &sign to get address. discus in String chapter.
 let's write a input code......

#include<stdio.h>
main()
{
int marks;
..............
scanf("%d",&marks);
..............
}
in above example the control string contains only one conversion specification character %d, which implies that one integer value should be entered as input. This entered value will be stored in the variable marks.

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

#include<stdio.h>
main()
{
char ch;
scanf("%c",&ch);
..............
}

here the control string contains conversion specification character %c, which means that a single character should be entered as input. This entered value will be stored in the variable ch.

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

#include<stdio.h>
main()
{
float fee;
scanf("%f",&fee);
}

here the control string contains conversion specification character %f, which means that a floating point number should be entered as input. This entered value will be stored in the variable fee.

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

#include<stdio.h>
main()
{
char str[30];
scanf("%s",str);
}

In this, String has conversion specification character %s implying that a string should be taken as input. Note that the variable str is preceded by & sign. The entered string will be stored in the variable str.

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


More than one value can also entered by scanf() function-

#inclue<stdio.h>
main()
{
int basic, da;
...........
scanf("%d%d",&basic,&da);
..........
}
in this example the control string has two conversion specification character imply that two value should be entered. These values are stored in the variable basic and da. the data can be entered with space as the delimiter.Number of   conversion specification is responsible for number of variable.

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


How to input different type of value 


#inclue<stdio.h>
main()
{
int basic;
float hra;
char grade;
................
scanf("%d %f %c",&basic,&hra,&grade);
...............
}

Here the control string has three conversion specification characters %d %f and %c, means that one integer value, one floating point value and one single character can be entered as input. These values are stored in the variables basic, hra and grade. The input data can be entered as-
1500 , 200.50, A.
When more than one value are input by scanf(),These values can be separated by whitespace character like space. tab or newline. A specific character can be also be placed between two conversion specification character.

#include<stdio.h>
main()
{
int basic ;
float hra;
scanf("%d : %f",&basic, &hra);
}
here the delimiter is colon(:). the input data can be entered as 
1500:200.5

the value 1500 is stored in variable basic and 200.50 is stored in hra.

#inclue<stdio.h>
main()
{
int basic;
float hra;
scanf("%d,%f",&basic,&hra);
}

Here the delimiter is comma (,). The input data can be enterd as-
1500, 200.40

#include<stdio.h>
main()
{
int day,month,year;
int basic;
.............
scanf("%d-%d-%d",&day,&month,&year);
scanf("$%d",&basic);
............
}
here if the data is entered as-
24-5-1992
$2000

Then 24 is stored in variable day, 5 is variable month and 1992 is stored in the variable years and 2000 stored in the variable basic.

#inclue<stdio.h>
main()
{
int x, y, z;
scanf("%d %d %d",&x ,&y,&z);
}
  if the data is entered as
12  34  56
12 is stored in x, 34 is stored in y and 56 is stored in z;

Conversion Specification and intro input/output

Input data , process data , and output process data. there are three main function of any program. The input operation involve movement of data moves from an input device (like keyboard) to computer memory, while in output operation the data moves from computer memory to the output device (monitor).  
     C language does not provide any facility for input/output operations. The input and output operation perform through a library function that are used by compiler. we will discus library function in the potion of Function. in short, "library function is the predefine function, which is written by programmer, only we call that function and use in the program". before using library function in the program. we must include header file in the program, in which specific function is stored. for example: for input and output operations we include header file <stdio.h> "stdio" stands for standard input output operation and .h stands for header file. all input output operation perform through this function. Scanf(); and printf() is the member of stdio.h library function. how to take input and output data in C language, we must know what is Conversion Specification.

Conversion Specification

The function scanf( ) and printf( ) make use of conversion specification to specify the type and size of data. Conversion specification is the way that specify and control input and output data. data type size and type depend on this. Each conversion specification must begin with a percent sign(%). some conversion specification give below-
%c              -     read/output a single character.
%d                    read/output integer value.
%f                     read/output floating point number.
%e                    read/output floating point number
%lf                    read/output long rang of float.
%h                    for short integer.
%o                    for octal integer.
%u                    an unsigned integer.

for eg. to read data .

sacnf("%d",&a);

in above example %d specify that the input data is integer that is stored in variable 'a'. & is used for address, with variable it create a memory location for variable, depend on data type. we declare float type variable then & create 4 byte on memory for variable. if we declare integer type variable then it create 2 byte memory.