Smile

Thursday, September 29, 2011

Control Statements

By default the instructions in a program are executed sequentially.
Many a times, we want a set of instructions to be executed in one
situation, and an entirely different set of instructions to be
executed in another situation. This kind of situation is dealt in
C programs using a decision control instruction. As mentioned
earlier, a decision control instruction can be implemented in C using:
(a) The if statement

(b) The if-else statement

(c) The conditional operators

(d) The else-if ladder (e) The switch statement


The if Statement
Like most languages, C uses the keyword if to implement the
decision control instruction. The general form of if statement looks
like this:
Syntax
if ( this condition is true )
{
execute these statements;
}

Flow Diagram of if statement

The if-else Statement
This is also said to be two way decision control statements. The general form of if statement looks like this:
Syntax
if ( this condition is true )
{
execute these statements ;
}
else
{
execute these statements;
}

Flow Diagram of
if-else statement




Conditional operator (or) Ternary Operator

The conditional operator is unusual in that it takes three operands. The syntax of this operator is like this:

Condition ? Expression1 : Expression2;

You can think of the conditional operator as if it were a function that works like this:
if ( Condition )

return Expression1;
else
return Expression2;

The Condition expression must evaluate to true or false. If it is true Expression1 is evaluated and its value is returned. If Condition is false Expression2 is evaluated and its value is returned. Both Expression1 and Expression2 must be the same type (or convertible to the same type).

Example
int a=101,b=50,c;
c=a>b?10:20;
The else if ladder
This is also said to be two way decision control statements. The general form of if statement looks like this:
Syntax
if ( this condition is true )
{
execute these statements ;
}
else if ( this condition is true )
{
execute these statements ;
}
else if ( this condition is true )
{
execute these statements ;
}
.
.
.
else
{
execute these statements ;
}

Flow Diagram of else-if ladder

Thursday, August 25, 2011

C program Examples

Example-1

C Program for converting temperature from Celsius to Fahrenheit

#include<stdio.h>
#include<conio.h>

void main()

{

float c,f;

clrscr();

printf("Enter the temperature in celsius:");

scanf("%f",&c);

f=c*9/5+32;

printf("Temperature in Fahrenheit=%.2f",f);

getch();

}

Example-2

C Program for printing grade to the student based on the percentages given below

>75-------> A+

70-75-----> A

60-70----->B

below 60-->C

#include<stdio.h>
#include<conio.h>

void main()

{

int m,p,c,sum;

float avg;

clrscr();

x: printf("Enter the marks Maths Physics Chemistry:");

scanf("%d%d%d",&m,&p,&c);

if(m<0||m>100||p<0||p>100||c<0||c>100)

goto x;

sum=m+p+c;

avg=(float)sum/3;

printf("Maths=%d\nPhysics=%d\nChemistry=%d\nSum=%d\nAverage=%.2f\n",m,p,c,sum,avg);

m<35||p<35||c<35?printf("Fail"):avg>75?printf("Grade=A+"):avg>70?printf("Grade=A"): avg>60?printf("Grade=B"):printf("Grade=C");

getch();

}

Example-3

C Program for calculating income tax based on salary of the employee details given below
Salary----->tax%
----------------------
>500000-------> 10%
300000-500000 -->7%
100000-300000 --->5%
50000-100000 ---->3%
<50000 ---------->0%

#include<stdio.h>
#include<conio.h>

void main()

{

float inc,t,sal;

clrscr();

printf("Enter the salary:");

scanf("%f",&sal);

t=sal>500000?10:sal>300000?7:sal>100000?5:sal>50000?3:0;

inc=sal*t/100;

printf("Tax=%.2f\nIncome tax=%.2f",t,inc);

getch();

}


Wednesday, August 17, 2011

How to Execute a C program

To write a C program, we have Borland Turbo C editor

Borland Turbo C editor

Compile-------> ALT + F9 or F9

Run-----------> CTRL + F9

Output-------> ALT + F5

Trace--------> F7

Sunday, August 14, 2011

Sample C program


Explanation
---------------
//
This is used for single line comment. This portion will not be executed.

/* */
This is used for multiple line comments. This portion will not be executed.

#include<stdio.h> or #include"stdio.h"

#-----> preprocessor directive
stdio.h----> standard input output header file
If header file included between < > , C linker searches header file in predefined library section.
If header file included between " " , C linker searches header file in both predefined library section and user defined library section.

#include<conio.h> or #include"conio.h"
This is console input output header file.

void main()
void--------> This is return type of function means nothing to return.
main()------> This is main function. The execution of C program starts from here. This is mandatory. Without this program returns error.

{ }
Body of the function to be executed. The portion enclosed between {} is said to be block.



Important functions for sample C program

1. printf()
-------------
This function is used to display the messages,variables etc on the output screen or console.
Syntax
-------
printf("messages in double quotes",var1,var2,var3,..............);

2. scanf()
------------
This function is used to read the variables explicitly at run time.
Syntax
-------
scanf("format specifiers",&var1,&var2,........);

3. clrscr()
------------
This function is used to clear the output screen.
Syntax
-------
clrscr();

4. getch()
------------
This function is used to read a single character. This is mainly used to display the output without termination.
Syntax
-------
getch();

Sunday, June 26, 2011

Expressions in C & Precedence and associativity of Operators

Expressions in C are basically operators acting on operands. Statements like a = b + 3, ++z and 300 > (8 * k) are all expressions. Strictly speaking, even a single variable or constant can be considered an expression. You have seen several expressions in the previous C tutorial on Operators in which the examples involved expressions

Precedence and Associativity

When an expression can be interpreted in more than one way, there are rules that govern how the expression gets interpreted by the compiler. Such expressions follow C's precedence and associativity rules. The precedence of operators determine a rank for the operators. The higher an operator's precedence, the higher “binding” it has on the operands.

For example, the expression a * b + c can be interpreted as (a * b) + c or a * (b + c), but the first interpretation is the one that is used because the multiplication operator has higher precedence than addition.

Associativity determines the grouping of operations among operators of the same precedence. In the expression a * b / c, since multiplication and division have the same precedence we must use the associativity to determine the grouping. These operators are left associative which means they are grouped left to right as if the expression was (a * b) / c.The operators' order of precedence from highest to lowest and their associativity is shown in this table:

Identifier, constant or string literal, parenthesized expression

[] func( arglist ) . -> ++ --

Left associative

++ -- & * + - ~ ! sizeof

Right associative

(type-name)

Right associative

* / %

Left associative

+ -

Left associative

<< >>

Left associative

< <= > >=

Left associative

== !=

Left associative

&

Left associative

^

Left associative

|

Left associative

&&

Left associative

||

Left associative

?:

Right associative

= *= /= %= += -= <<= >>= &= ^= |=

Right associative

,

Left associative


Example #1
int i=2,j=5,k=6,g;
g = i+j*k;

In the above example the evaluation will be taken place in the following way
There are two operators + * Among these two * is having highest precedence. So j*k is evaluated first 5*6=30 Then addition i+30=2+30=32. Final value of g is 32.

Example #2
int x=5,y=30,z=10,w;
w=x*y/z;

Here In the above example * / both are having same precedence. So evaluation is done in left associativity. x*y is done first 5*30=150 then 150/z = 150/10 = 15. The value of w is 15.

Example #3
int i=2,j=5,k=6,g;
g = (i+j)*k;

Here In the above example
There are three operators + * () Among these three () is having highest precedence. So
i+j is evaluated first 2+5 = 7 Then 7*k = 7*6 = 42. So final value of g is 42.