CNOTES

Share Embed Donate


Short Description

c...

Description

C NOTES c /*

C LANGUAGE

INTRODUCTION TO C: ~~~~~~~~~~~~~~~~~~~ C was originally developed in 1970's by Dennis Ritche at Bell Telephone laborataries (INC). For the purpose of to implementing the unix operating system. It is an out growth two earlier language called BCPL & B. CURRENT USES OF C: ~~~~~~~~~~~~~~~~~~ The C language is used for developing following application, 1.Database system 2.Graphics packages 3.Spreadsheets 4.CAD/CAM application 5.Word processor 6.Office automation 7.Scientiffic & Engineering application C is an uncontrollable language.It has, *Graphical package(games) *Stock information *Spreadsheet(Excel-Information maintanance) *CNC Proogram (C language) We can maintain the information also a file. Office organisation software computerised, Scientiffic engineering algorithm programs. Foxpro--> Database Management System. Three levls: 1.Low level(0 & 1 binary) 2.High level(keyword as keyword) 3.Medium level(0 & 1 and keyword) C is medium level language. ~~~~~~~~~~~~~~~~~~~~~~~~~~~

CSC-AVINASHI & GANAPATHY

1

C NOTES

DATA TYPES & CODES: ~~~~~~~~~~~~~~~~~~~ Data type Code ========= ====

Size ====

Integer

%d

2 bytes

Long integer

%ld

4 bytes

Char

%c

1 byte

Float

%f

4 bytes

Double

%lf

8 bytes

String

%s

-

DATA TYPE: ~~~~~~~~~~ string(more than alphabets) character(single alphabet) integer(number) long integer(crore values) float(decimal values) double(long decimal values) FORMAT OF C PROGRAM: ~~~~~~~~~~~~~~~~~~~~ //HEADER FILE DECLARATION PART. #include #include

\\ printf and scanf functions \\ clrscr and getch functions

//----------MAIN PROGRAM STARTS-----------void main() \\ function which the operating system will start { clrscr(); \\ clear screen //----------DECLARATION SECTION-----------int a,b,c;

CSC-AVINASHI & GANAPATHY

2

C NOTES //-----------ASSIGNMENT SECTION-----------printf("Enter the value of a and b:"); scanf("%d%d",&a,&b); //-----------COMPALICATION SECTION--------c=a+b; printf("Addtion of two numbers is :%d",c); //-----------END OF THE PROGRAM----------getch(); } Meaning: --->standard input output header file This header file contains with printf and scanf functions. --->console input output header file This header file contains with clrscr and getch functions. void main()->This is the function which the operating system will start execution each and every program must have main function. clrscr()---->clear screen clears the current text window and places the curser in the upper left hand corner.(at position 1,1) VARIABLE DECLARATION: ~~~~~~~~~~~~~~~~~~~~~ All variables used in C language must declare. C variable declarations includes with name of the variable and its data type. Variables are used to fold given input. PRINTF FUNCTION: ~~~~~~~~~~~~~~~~ The printf function print stringsall output screen.

CSC-AVINASHI & GANAPATHY

any

set

characters

or

numerics

3

or

C NOTES SCANF FUNCTION: ~~~~~~~~~~~~~~~ The scanf function reads values from output buffer and stored it at supplied address. &---->address of int a,b,c

a=10

b=10

GETCH() FUNCTION: ~~~~~~~~~~~~~~~~~ This function used to get a single character from the output screen. ARITHMETICAL OPERATORS: ~~~~~~~~~~~~~~~~~~~~~~~~ Operator purpose + addtion *

multiplication

-

subtraction

/

divison

%

module

1 // EXAMPLE PROGRAM FOR ARITHMETIC OPERATION: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include #include void main() { clrscr(); int a,b,c,d,e; float f; printf("Enter the value for a and b"); scanf("%d%d",&a,&b); c=a+b; d=a*b; e=a-b; f=a/b; printf("\t\t\t ADDTION IS:%d \n",c); printf("\t\t\t MULTIPLICATION IS:%d \n",d); printf("\t\t\t SUBTRACTION IS:%d \n",e); printf("\t\t\t DIVISON IS :%f \n",f);

CSC-AVINASHI & GANAPATHY

4

C NOTES getch(); } 2 // EXAMPLE FOR MODULE FUNCTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Module means remaining values. #include #include void main() { clrscr(); int wd,rd,mon,dy; printf("Enter the number of days:"); scanf("%d",&dy); wd=dy/7; mon=dy/30; rd=dy%30; printf("WEEKS:%d \n",wd); printf("MONTHS:%d \n",mon); printf("REMAINING DAYS: %d \n",rd); getch(); } 3 // EXAMPLE PROGRAM FOR AREA OF TRIANGLE: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include #include void main() { clrscr(); int b,h,area; printf("Enter the breath:"); scanf("%d",&b); printf("Enter the height:"); scanf("%d",&h); area=0.5*b*h; printf("Area of the triangle:%d",area); getch(); } 4.EXAMPLE PROGRAM TO KNOW SIZE OF THE DATATYPE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include

CSC-AVINASHI & GANAPATHY

5

C NOTES #include void main() { clrscr(); printf("INTEGER : %d BYTES \n",sizeof(int)); printf("FLOAT : %d BYTES \n",sizeof(float)); printf("DOUBLE : %d BYTES \n",sizeof(double)); printf("CHARACTER : %d BYTES \n",sizeof(char)); printf("CHARACTER : %d BYTES \n",sizeof(char[20])); getch(); } CONTROL STRUCTURES: ~~~~~~~~~~~~~~~~~~~ The control structures are used to redirect the sequential flow of control through the program to desired locations. IF ELSE CONDITION: ~~~~~~~~~~~~~~~~~~~ syntax for if and else: ~~~~~~~~~~~~~~~~~~~~~~~~ if(condition) statment 1; else statment 2; In the above structure staement 1 will be executed conditionis true otherwise statement 2 will be executed.

if

the

If more than one statement is to be executed for a condtion then wecan group them in a single group by enclosing them within {}. The normal statement is called as a simple statement and the statements enclosed within brackets is called as compound statement. 5 //EXAMPLE PROGRAM FOR IF ELSE: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include #include void main() { clrscr(); int a,b; printf("Enter the value for a and b:");

CSC-AVINASHI & GANAPATHY

6

C NOTES scanf("%d%d",&a,&b); if(a>b) { printf("A is greater:%d",a); } else { printf("B is greater:%d",b); } getch(); } 6 // EXAMPLE PROGRAM FOR IF ELSE: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include #include void main() { clrscr(); char name[20]; int age,wt; printf("Enter the name:"); scanf("%s",name); printf("Enter the age :"); scanf("%d",&age); if(age>18) { printf("%s you are eligible to vote",name); } else { wt=18-age; printf("%s you just wait for %d years",name,wt); } getch(); } 7 //EXAMPLE PROGRAM FOR IF ELSEIF ELSE: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include #include void main() { clrscr();

CSC-AVINASHI & GANAPATHY

7

C NOTES int a,b,c; printf("Enter the value for a,b and c:"); scanf("%d%d%d",&a,&b,&c); if(a>b && a>c) { printf("A is greater %d",a); } else if(b>c && b>a) { printf("B is greater %d",b); } else { printf("C is greater %d",c); } getch(); } 8 //EXAMPLE PROGRAM FOR IF ELSEIF ELSE: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } else { printf("Result:FAIL \n"); } if(avg>40 && avg=40 && m2>=40 && m3>=40) { printf("Result:PASS \n { printf("Grade:D"); } else if(avg>55 && avg65 && avg0) { printf("Given no is positive:%d",num); } else { goto csc; } getch(); } 10 // EXAMPLE PROGRAM FOR GOTO STATEMENT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include #include void main() { clrscr(); int num; char ch; csc: printf("Enter any number: \n"); scanf("%d",&num); if(num>0) { printf("The given no is positive \n"); } else if(num
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF