10/15/08

DECLARE A VARIABLE

When we want to declare any variable, then we decide that what is a data type of the variable? And how it should declare? Variable can eight characters long only.

HEADER FILES

Conio.h

It is a library file which is included in the program this is used to use

Clrscr ( )

getch ( )

getchar ( ) etc.

We do not need to include this file in C program. It is necessary for C++ programs. Conio stands for CONsole Input Output header file.

Math .h

This file is used for mathematical functions. These functions are like excel functions:-

Eg. pow( ), sqrt ( ), sin ( ), tan ( ) etc.

TYPE CASTING




TYPE CASTING

It is used to convert on data at a time. It is required to used different type of constants and variables in expression then we have convert that expression into another data type, but certain rules are used during conversion of data type the computer consider one operand at a time involving two operands. We can have two types of type casting.

1. Implicit

2. Explicit

Implicit

When we are converting any data type which contains shorter values then along data type that conversion is called implicit or in simple language we can say when user do not need to convert any data type into another data type and it is automatically done through computer system that is called implicit type conversion. The conversion is not transparent to the user or the programmer and is done by the compiler. This converts narrow type to wider type so that use can avoid the information to the system.

#include

void main ()

{

float c,f;

clrscr ();

printf ("Enter the value of celcius: ");

scanf ("%f",&c);

f=9.0/5.0*c+32;

printf ("\Fehranite is %.2f",f);

getch ();

}

Explicit

Explicit type casting is done by the programmer manually. When the programmer wants a result of an expression to be in particular type, then we use explicit type casting. This type casting is done by casting operators ‘( )’ and data type.

#include

void main ()

{

float c,f;

clrscr ();

printf ("Enter the value of celcius: ");

scanf ("%f",&c);

f=(float) 9/5*c+32;

printf ("\Fehranite is %.2f",f);

getch ();

}

MACRO

Macro is a substitution of a string that is placed in a program. It is placed by the definition when program is compiled and we have to use (#) symbol in front of this. We can have different macros like:-

#define, #if, #else.

It is different from preprocessor directive, because pre processor directive is used to link files with the object code and macro is used to define any string and we always or maximum use #define macro.

#include

#define wait getch ( )

# define flot float

void main ( )

…….

…….

flot f,c;

…….

…….

…….

…….

wait;

}

OPERATORS

for beginners...

OPERATORS

Every language contains variable and data types and also support the concept of operators. C language has over forty operators, but hardly all they are used. These are divided onto nine categories:-

(1) Arithmetic

(2) Assignment

(3) Logical

(4) Relational

(5) Modulas

(6) Increment / Decrement

(7) Conditional

(8) Bit wise

(9) Special


Arithmetic Operators (+ - * /)

These are the common operators used to perform any arithmetic calculations. These are basically used on minimum two variables or constants. These operators contain +, -, *, /.

Assignment Operators

These operators are used to assign any value to the variable or constant. The main assignment operator is ‘=’.

We have some short assignment operators. Suppose a=10







Logical operators

These are used to test more than one condition or we can say conditional expression, so that you can make a decision on the basis of that condition. These operators are






Relational operators

These operators are also called comparison operators. You can use relational operators, whenever you want to compare two quantities and variables or constants and you want to take decision on the basis of that condition. Then these operators are used.

We have following types of operators:-

>

<

>=

<=

==

!=

Modulas Operator

This operator is used to find out the reminder of any value.

R=25%10 R=5

Increment / Decrement operators

These operators are also called unary operators. These operators are used when we want to perform any action on single operand. These operators are used when we want to increase or decrease value of any variable by one.

Increment operator is ++

Decrement operator is

We have two types of unary operators

Post fix

This operator is used to assign any value of variable to another variable according to use requirement. In this operator firstly the value of a variable is copied to another variable and then the value is increased or decreased.

a=10

a++ a=11

Pre-fix

This operator is used to assign value to any variable according to user requirement, so that the same value is copied to another variable. Firstly the value of variable is increased or decreased and then it is copied to any other variable.

a=10

++a a=11

Conditional operators

These operators are also called ternary operators. These are used to check any condition or expression and can be used to compare two conditional expressions and produce a result if the condition is true or false. Operators used for conditional operators are “?” and “:”

Syntax:-

(expression / condition)? True statement (if) : false statement (else)

Bit wise operators

C is known as assembly language because it provides us bit wise operators, which are normally associated with assembly language programming. C language has a powerful set of special operators which are capable of manipulating data at bit level. These are as follows:-

1) & - Bit wise (and)

2) ! - Bit wise (or)

3) ^ - Bit wise (exponent)

4) << - Shift left

5) >> - Shift right

Special operators

Special operators are provided by C. these operators are as follows:-

  1. commas operators (,)
  2. pointer operators (*, &)
  3. sizeof ( )


Relational operators

These operators are also called comparison operators. You can use relational operators, whenever you want to compare two quantities and variables or constants and you want to take decision on the basis of that condition. Then these operators are used.

We have following types of operators:-

>

<

>=

<=

==

!=

Modulas Operator

This operator is used to find out the reminder of any value.

R=25%10 R=5

Increment / Decrement operators

These operators are also called unary operators. These operators are used when we want to perform any action on single operand. These operators are used when we want to increase or decrease value of any variable by one.

Increment operator is ++

Decrement operator is

We have two types of unary operators

Post fix

This operator is used to assign any value of variable to another variable according to use requirement. In this operator firstly the value of a variable is copied to another variable and then the value is increased or decreased.

a=10

a++ a=11

Pre-fix

This operator is used to assign value to any variable according to user requirement, so that the same value is copied to another variable. Firstly the value of variable is increased or decreased and then it is copied to any other variable.

a=10

++a a=11

Conditional operators

These operators are also called ternary operators. These are used to check any condition or expression and can be used to compare two conditional expressions and produce a result if the condition is true or false. Operators used for conditional operators are “?” and “:”

Syntax:-

(expression / condition)? True statement (if) : false statement (else)

Bit wise operators

C is known as assembly language because it provides us bit wise operators, which are normally associated with assembly language programming. C language has a powerful set of special operators which are capable of manipulating data at bit level. These are as follows:-

1) & - Bit wise (and)

2) ! - Bit wise (or)

3) ^ - Bit wise (exponent)

4) << - Shift left

5) >> - Shift right

Special operators

Special operators are provided by C. these operators are as follows:-

  1. commas operators (,)
  2. pointer operators (*, &)
  3. sizeof ( )

CONTROL STATEMENTS

These statements are used to control the flow of program by using these statements. We can jump from one statement to another statement in C language. We can have four types of control statements:-
  1. decision making
  2. case control statement or switch statement
  3. looping / iteration statement
  4. jump / branching statement

Decision making

These statements are used when we want to take any decision according to the condition or user requirement. These statements work on a particular condition. These conditions are used by using ‘if’ statement. We can have four types of conditional statements

  1. if
  2. if-else
  3. if – else if
  4. nested if

if

if statement is simple decision making statement, which is used to take any decision when the condition is true.

if (statement)

{

Statement;

}

if (expression / condition)

Statement;

If-else

This statement is used to make decision in C language. If the given condition is true then the cursor will move to the true portion else it will move to the false portion.

if (condition)

{

Statement;

}

else

{

Statement;

}

If else-if

This is another control statement which is used to take any decision according to the given condition. If the condition is true then the cursor will move to the true portion and if the condition is false the cursor will move to the false or else-if portion and then checks another condition. If this condition is true then it will move and run the second true portion and if the condition is false then cursor will move the next portion of if condition which is another else-if or else portion.

if (condition)

{

Statement;

}

else if (condition)

{

Statement;

}

else if (condition)

{

Statement;

}

else

{

Statement;

}

NESTED IF

for beginners...

NESTED IF

If within if is called nested if and it is used when we have multiple conditions to check and when any if condition contains another if statement then that is called ‘nested if’.

If the external condition is true, then the internal if condition or condition are executed and if the condition is false then the else portion is executed of the external if statement

SYTAX

if (condition)

{

…….

…….

}

if (condition)

{

…….

…….

}

else if (condition)

{

…….

…….

}

else if (condition)

{

…….

…….

}

else

{

…….

……. }

}

else

{

…….

…….

}


SWITCH CASE / SELECT CASE

These statements are used with the replacement of if-else and these statements are used when we have multiple options or choices. These choices can be limited and when we use switch case we can use only integer or character variable as expression.


#include

void main()

{

int,ch qty;

long tb,dis,nb;

clrscr();

printf("1.BPL\n2.Onida\n3.Sony\n4.Samsung\n5.LG\n");

printf("\nEnter Your Choice: ");

fflush(stdin);

scanf("%d",&ch);

printf("Enter Qty: ");

scanf("%d",&qty);

switch(ch)

{

case 1:tb=(long)qty*10000;

printf("\nBPL is %ld",tb);

break;

case 2:tb=(long)qty*12000;

printf("\nOnida is %ld",tb);

break;

case 3:tb=(long)qty*11500;

printf("\nSony is %ld ",tb);


break;

case 4:tb=(long)qty*11000;

printf("\nSamsung is %ld ",tb);

break;

case 5:tb=(long)qty*13000;

printf("\nLG is %ld ",tb);

break;

default:

printf("Wrong Choice...");

}

dis=(tb*10)/100;

nb=tb-dis;

printf("\nDiscount is %ld",dis);

printf("\nNet bill is %ld",nb);

getch();

}

LOOPING





These statements are used to control the flow of the program and repeat any process multiple times according to user’s requirement. We can have three types of looping control statements.

1. While

2. Do-while

3. For

While

It is an entry control loop statement, because it checks the condition first and then if the condition is true the statements given in while loop will be executed.

SYNTAX:-

Initialization;

while (condition)

{

Statements;

Incremental / decrement;

}

Do-while

Do-while loop is also called exit control loop. It is different from while loop because in this loop the portion of the loop is executed minimum once and then at the end of the loop the condition is being checked and if the value of any given condition is true or false the structure of the loop is executed and the condition is checked after the completion of true body part of the loop.

SYNTAX:-

Initialization

do

{

Statement;

Increment / decrement;

}

while (condition)

For loop

It is another looping statement or construct used to repeat any process according to user requirement but it is different from while and do-while loop because in this loop all the three steps of constructions of a loop are contained in single statement. It is also an entry control loop. We can have three syntaxes to create for loop:-


for (initialization;

Test condition; Increment / decrement)

{

Statement;

}


for (; test condition; increment / decrement)

{

Statement;

}


for (; test condition;)

{

Statement;

Increment / decrement

}



#include

void main ( )

int a;

clrscr ( );

for (a=1; a<=10; a++)

{

printf (“%d \n”,a);

}

getch ( );

}

NESTED LOOP

These loops are the loops which contain another looping statement in a single loop. These types of loops are used to create matrix. Any loop can contain a number of loop statements in itself. If we are using loop within loop that is called nested loop. In this the outer loop is used for counting rows and the internal loop is used for counting columns

SYNTAX:-

for (initializing ; test condition ; increment / decrement)

{

statement;

for (initializing ; test condition ; increment / decrement)

{

body of inner loop;

}

statement;

}

PROGRAM

#include

void main ( )

{

int i, j;

clrscr ( );

for (j=1; j<=4; j++)

{

for (i=1; i<=5; i++)

{

printf (“*”)

}

printf (“\n”);

}

getch ( );

}

OUTPUT OF THIS PROGRAM IS


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

Continue

This statement is used within the body of the loop. The continue statement moves control in the beginning of the loop means to say that is used to skip any particular output.

Example:

WAP to print series from 1 to 10 and skip only 5 and 7.

#include

void main ( )

{

int a;

clrscr ( );

for (a=1; a<=10; a++)

{

if (a= =5 | | a= = 7)

continue;

printf (“%d \n”,a);

}

getch ( );

}

Break

This statement is used to terminate any sequence or sequence of statement in switch statement. This statement enforce indicate termination. In a loop when a break statement is in computer the loop is terminated and a cursor moves to the next statement or block;

Example:

WAP to print series from 1 to 10 and break on 5

#include

void main ( )

{

int a;

clrscr ( );

for (a=1; a<=10; a++)

{

if (a= =5)

break;

printf (“%d \n”,a);

}

getch ( );

}


Goto statement

It is used to alter or modify the normal sequence of program execution by transferring the control to some other parts of the program. the control may be move or transferred to any part of the program when we use goto statement we have to use a label.



WAP TO FIND IF NUMBER=10 THEN GOOD ELSE BAD

#include

void main ()

{

int a;

clrscr ();

printf ("Enter any Number: ");

scanf ("%d",&a);

if (a= =10)

goto g;

else

goto b;

g:

printf ("Good");

goto end;

b:

printf ("Bad");

goto end;

end:

getch ();

}


WAP TO PRINT SERIES FROM 1 TO 10

#include

void main ()

{

int a;

clrscr ();

a=1;

check:

if (a< =10)

goto inc;

else

goto end;

inc:

printf (“%d \n”,a);

goto check;

end:

getch ( );

}

JUMPS STATEMENTS

These are also called as branching statements. These statements transfer control to another part of the program. When we want to break any loop condition or to continue any loop with skipping any values then we use these statements. There are three types of jumps statements.

1. Continue

2. Break

3. Goto

STRING

String are the combination of number of characters these are used to store any word in any variable of constant. A string is an array of character. It is internally represented in system by using ASCII value. Every single character can have its own ASCII value in the system. A character string is stored in one array of character type.

e.g. “Ram” contains ASCII value per location, when we are using strings and then these strings are always terminated by character ‘\0’. We use conversion specifier %s to set any string we can have any string as follows:-

char nm [25]

When we store any value in nm variable then it can hold only 24 character because at the end of the string one character is consumed automatically by ‘\0’.

1. strlen - string length

2. strcpy - string copy

3. strcmp - string compare

4. strups - string upper

5. strlwr - string lower

6. strcat - string concatenate

7. strstr - string string

ARRAYS

Arrays are widely used data type in ‘C’ language. It is a collection of elements of similar data type. These similar elements could be of all integers, all floats or all characters. An array of character is called as string whereas and array of integer or float is simply called as an array. So array may be defined as a group of elements that share a common name and that are defined by position or index. The elements of an arrays are store in sequential order in memory.

WAP to input your name and display it

#include

void main ( )

{

char nm [15];

clrscr ( );

printf (“enter your name”);

gets (nm);

printf (“name is %s”,name);

getch ( );

}

DECLARE ARRAYS

  1. An array must be declared with type of variable or data type
  2. An array contains same type of data
    e.g. – int a [10]
  3. The name of array cannot be same as that of any other variable within the function.
  4. The size of array is specified using sub-script notation (index)
  5. THE sub-script indicates that how many elements are to be allocated to an array.
  6. A sub-script used to declare an array is called dimension