Showing posts with label Basic structure of c- programmme. Show all posts
Showing posts with label Basic structure of c- programmme. Show all posts

Wednesday, October 27, 2010

Basic structure of c- programmme



Basic structure of c- programmme



Basic structure of c- programmme

Documentation section(comment section)
Link section
Define section
Global declaration section
Main( ) ------- main function section
{

Declaration part
Executable part
}

Sub program section
Function 1
Function 2
Function 3 [ user defined functions]
--- ---- ----- ----
--- -------- -----
Function n

C program can be viewed as a group of building blocks called functions. A function is a sub routine that may include one or more statements designed to perform a specific task. To write a C program first creates function and then put them together .C program may contain one or more sections.

Documentation section :
Documentation section consists of a set of comment lines giving the name of the program, author, date and other details.

Line section :
Line section provides the instructions to the compiler to link functions from the system library.
Definition section:
Defines all symbolic constants.

Global declaration section:
There are some variables that are used in more than one function such variables are called global variables and are declared in global declaration i.e., outside of all the functions. The variables which is defined in function is called local variable(private variable).

Main function:
Declaration part
Executable part (input, arithmetic, output, control statements etc)

Sub program section :
This section contains all the user defined functions that are called in the main function. User defined functions are generally placed immediately after the main function although they may appear in any order.

Rules ;

1) each and every instruction in a C program is written in a separate statement, A complete C programme comprises a series of statement. The statements must appear in same order in which we wish them to be executed.
2) Usually all statements are entered in small case letters.
3) C has no specific rules for the position at which the statement is to be written that’s why it is often called free form language.
4) Every statement should always end with a semi colon.

Relational operators : 21/9/07
Symbol Associativity Description
> Left to right Is greater than
>= Left to right Is greater than or equal
< Left to right Is less than
<= Left to right Is less than or equal
== Is equal to
!= or <> Not equal

Note : the value of a relational expression is either non-zero
value (1) or zero value.
It is one if the specified relation is true and zero if specified relation is false.

Logical operators :

Symbol Associativity Description
&& Left to right Expressions must be satisfied
|| Left to right Any one of expression must be satisfied.
! (exclamatory) Right to left These operators reverse the value of an expression. It makes true expression as false and false expression as true.




Truth table :

Op-1 Op-2 Value of the
op1&&op2 Value of the
op1 || op2
Non-zero Non-zero Non-zero Non-zero
Non-zero zero zero Non-zero
Zero Non-zero zero Non-zero
Zero zero zero Zero


Note :
Like simple relational expressions, A logical expressions yields a value of one or zero.

6) Bit wise operators :
‘C’ as a distinction of supporting a special operator
known as operators. For manipulation of data at bit-level. These operators are used for testing the bits or shifting them from right to left.
Note: Bit wise operators maynot be applied to float or double.

Operator Meaning
&(amposand) Bitwise and
|(pipe) Bitwise or
^(carot) Bitwise exclusive or
<< Shift left
>> Shift right
Special operators :

Special operators are comma(,) operator,size of ( ) operator.
Syntax: sizeof (datatype);
Pointer operators are & and * and
Member selection operators -> and .
Example of comma operator :
X=(a-5,b=6,a+b);

Note : ANSI commity has introduce two pre processor operators string as “string zing” and “token pasting”
The operators are # and ## .

Control structures in C :-

Decision control structure or Bi directional conditional structures :-
There are three types of IF statements
1) simple if (simple conditional statements)
2) multiple if or if ladder (multiple conditional statements)
3) nested if (nested conditional statements)
C uses the keyword ‘IF’ to implement the decision control instruction the keyword if tells the compiler that what follows is the decision control instruction.
The condition following the keyword is always enclosed with a pair of parenthesis . if the condition is true the statement is executed and if the condition is false then the statement is not executed instead the program skips.

I syntax :-
If ()
{
Statements ;
}
Statements x;
Or
If ()
Statements ;
II syntax :
If ()
Statements ;
Else
Statements;
Statements x;
III syntax :
If()
{
Statements ;
}
Else
{
Statements ;
}
Statements x;


The if statement by itself will execute a single statement or a group of statements.
1) The group of statements of the if and not included else is called if block statement.
2) Similarly the statement after the else called else block statement.


Accept any two integer values and find whether the two integers are equal or not . ?

Program :

/* Equal of two numbers created on 22-09/07 */

#include
#include
main()
{
int x=0,y=0;
clrscr();
printf("enter x integer value :");
scanf("%d",&x);
printf("enter y integer value:");
scanf("%d",&y);
if(x==y)
printf("Both numbers are equal ");
else
printf("not equal");
getch();
return 0;
}


Accept any two integers and find the biggest value ? 22/9/07

Program :

/* Biggest of two numbers created on 22-09/07 */

#include
#include
main()
{
int x=0,y=0;
clrscr();
printf("enter x integer value :");
scanf("%d",&x);
printf("enter y integer value:");
scanf("%d",&y);
if(x>y)
printf("X is biggest ");
else
printf("Y is biggest");
getch();
return 0;
}

Case studies :
1) Accept any integer value and find whether the given integer is positive or negative.
program

/* To find the integer value is positive or negative */

#include
#include
main()
{
int a=0;
clrscr();
printf("enter integer value ");
scanf("%d",&a);
if(a>0)
printf("the integer value %d is positive ");
else
printf("the integer value %d is negative");
getch();
return 0;
}

2) Accept any integer value and find whether the given integer is even or odd?
Program :
/* To find the integer value is even or odd created on 23-09-07 */

#include
#include
main()
{
int x=0;
clrscr();
printf("enter integer value ");
scanf("%d",&x);
if(x%2==0)
printf("the integer value %d is an even number");
else
printf("the integer value %d is an odd number");
getch();
return 0;
}


3) Accept any alphabatical value and find whether the given alphabet is vowel or consonant ?
Program 24/09/07

/* To find the given character is vowel or consonant created on 24/9/07*/

#include
#include
main()
{
char x;
clrscr();
printf("enter a character\t");
scanf("%c",&x);
if(toupper(x)=='A'||toupper(x)=='E'||toupper(x)=='I'||toupper(x)=='O'||toupper(x)=='U')

[( or ) /* if(x=='a'||x=='A'||x=='e'||x=='E'||x=='i'||x=='I'||x=='u'||x=='U'||x=='o'||x=='O')]
*/ printf("it is a vowel");
else
printf("it is a consonant");
getch();
}
Program : 24/9/07
/* To find the character which is in upper case or lower case by using three methods */
/* created on 24/09/07 */

#include
#include
main()
{
char n;
clrscr();
printf("enter the character:\t");
scanf("%c",&n);
if(isupper(n))
printf("the given character is upper case letter");
else
printf("the given character is lower case letter");
/* SECOND METHOD

if(n>=65&&n<=90)
printf("the given character is uppercase letter");
else
printf("the given character is lowercase letter ");

THIRD METHOD

if(n>='A'&&n<='Z')
printf("the given character %c is uppercase letter");
else
printf("the given characte %c is lowercase letter");*/
getch();
return 0;
}



2) Multiple if(the if else ladder) :-
Syntax :
If()
{
Statements ;
}
Else if()
{
Statements;
}
Else if()
{
Statements;
}
- - - -- - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - -- - - -
- ------ - - - - - - - -

Else
{
Statements;
}
Statements x;

There is another way putting if’s together when multiple path decisions are involved. A multiple path decision is a chain of if’s , if the statement associated with each else is as on if.
Program :
/* ABC sales data created on 24/09/07 */

#include
#include
main()
{
int ino=0;
unsigned int qty=0;
float r=0,dp=0.00,amt=0,damt=0,net=0;
/* dp=discount percent,damt=discount amount */
char iname[15];
clrscr();
printf("enter the item number :");
scanf("%d",&ino);
printf("enter the item name :");
scanf("%s",&iname);
printf("enter the quantity of particular item : ");
scanf("%u",&qty);
printf("enter the rate of the item :");
scanf("%f",&r);
amt=qty*r;
if(amt>=20000)
dp=0.1;
else if(amt>=10000&&amt<20000 br=""> dp=0.07;
else if(amt>=5000&&amt<10000 br=""> dp=0.05;
else
dp=0.02;
damt=amt*dp;

/* {
damt=(amt*0.1);
printf("the discount of the item is gives 10percent %f" ,damt);
}
else if(amt>=10000 && amt<20000 br=""> {
damt=(amt*0.07);
printf("the discount of the item gives 7percent is %f",damt);
}
else if(amt>=5000&&amt<10000 br=""> {
damt=(amt*0.05);
printf("the discount of the item gives is 5percent is %f",damt);
}
else
{
damt=(amt*0.02);
printf("the discount of the item is gives 2percent is %f",damt);
} */
net=amt-damt;
printf("\nthe total amount is %f\n the discount received is %f \ndiscount percent is%f \n total net is %f",amt,damt,dp,net);
getch();
return 0;
}


Gotoxy(C,R) : 25/9/7
This function will help us to keep the cursor at a desired location with in the limits of text mode x-columns, y-rows(80 columns,25rows
Program :
/* To calculate the electricity bill created on 25-9-07 */

#include
#include
main()
{
int cr=0,pr=0,nou=0;
float R=0,bill=0;
char mno[5],cname[20];
clrscr();
gotoxy(32,4);
printf("ELECTRICITY BILL ");
gotoxy(60,4);
printf("DATE :25/09/2007");
gotoxy(5,5);
printf("*******************************************************************");
gotoxy(5,7);
printf("Enter meter no :");
gotoxy(50,7);
printf("Enter Cname :");
gotoxy(5,8);
printf("********************************************************************");
gotoxy(32,9);
printf("Enter CR :");
gotoxy(32,11);
printf("Enter PR :");
gotoxy(5,13);
printf("********************************************************************");
gotoxy(5,15);
printf("No.of.Units :");
gotoxy(32,15);
printf("Rate :");
gotoxy(50,15);
printf("Bill :");
gotoxy(5,17);
printf("*********************************************************************");
gotoxy(32,19);
printf("THANK Q");
gotoxy(22,7);
scanf("%s",mno);
gotoxy(64,7);
scanf("%s",cname);
gotoxy(42,9);
scanf("%d",&cr);
gotoxy(42,11);
scanf("%d",&pr);
nou=cr-pr;
if(nou>=800)
{
R=5.50;
}
else if(nou>=600&&nou<800 br=""> {
R=3.50;
}
else if(nou>=300&&nou<600 br=""> {
R=2.50;
}
else if(nou>=100&&nou<300 br=""> {
R=1.50;
}
else
{
R=1.00;
}
bill=nou*R;
gotoxy(24,15);
printf("%d",nou);
gotoxy(44,15);
printf("%f",R);
gotoxy(60,15);
printf("%f",bill);
getch();
return 0;
}





EXAMAPERS123.BLOGSPOT.COM