+ All Categories
Home > Technology > 3. chapter ii

3. chapter ii

Date post: 18-Jan-2017
Category:
Upload: chhom-karath
View: 67 times
Download: 0 times
Share this document with a friend
50
CHAPTER II C-Programming
Transcript

CHAPTER IIC-Programming

CONSTANTS AND VARIABLES

C program is made of basic elements, such as expressions, statements, statement blocks, and function blocks.

Constant is a value that never changes Variables can be used to present different values Expression: is a combination of constants,

variables, and operators that are used to denote computations. 6 An expression of a constant. i An expression of a variable. 6 + i An expression of a constant plus a variable. exit(0) An expression of a function call.

EXPRESSION

Arithmetic Operators+ Addition - Subtraction * Multiplication / Division % Remainder (or modulus)(6%4=2)

Note:2 + 3 * 10 (2+ (3 * 10))

STATEMENTS

statement is a complete instruction, ending with a semicolon. i = 1; i = (2 + 3) * 10; i = 2 + 3 * 10; j = 6 % 4;k = i + j; return 0;exit(0);printf ("Howdy, neighbor! This is my first C

program.\n");

STATEMENT BLOCKS

A group of statements can form a statement block that starts with an opening brace ({) and ends with a closing brace (}). A statement block is treated as a single statement by the C compiler. #include<stdio.h>#include<conio.h>void main(){

clrscr();int i;for(i=1;i<=20;i++){textcolor(i);cprintf("Computer Bak Touk Center\n\r");}getch();

}

ANATOMY OF A FUNCTION

standard C library functionsFunctions are the building blocks of C

programs printf() and exit()scanf()clrscr()gotoxy(x,y); textcolor();colorbackground()

ANATOMY OF A FUNCTION

Illegal Name The Rule 2 (digit) A function name cannot start with a

digit. * (Asterisk) A function name cannot start with

an asterisk. + (Addition) A function name cannot start with

one of the arithmetic signs that are reserved C keywords.

. (dot) A function name cannot start with .. total-number A function name cannot contain a

minus sign. account'97 A function name cannot contain an

apostrophe.

ANATOMY OF A FUNCTION

ANATOMY OF A FUNCTIONvoid functionName(){……………………….……………………….}Void function Name(parameter){………………………………………………}Returntype functionName(){………………………..………………………..}Returntype functionName(parmeter){………………………..………………………..}

#include<stdio.h>#include<conio.h>void info();void header(){

gotoxy(25,5);printf("Computer BakTouk Center\n");gotoxy(24,7);printf("------------------------");

}void main(){

clrscr();header();info();getch();

}

void info(){ gotoxy(25,10);

printf("Near Master sukisup\n\t\t\t Tel: 011 11 11 11");

}

ANATOMY OF A FUNCTION

#include<stdio.h>#include<conio.h>Void sum(int a,int b);void main(){

int x=100,y=34;sum(x,y);getch();

}void sum(int a,int b){

printf("100+34=%d",a+b);}

ANATOMY OF A FUNCTION#include<stdio.h>int integeradd(int x, int y){

int result;result=x+y;return result;

}int main(){

int sum;sum=integeradd(5,12);printf("5+12 =%d",sum);return 0;

}

ANATOMY OF A FUNCTION

#include<stdio.h>int integer_add(int x, int y);int main(){

int sum;sum=integer_add(5,12);printf("5+12 =%d",sum);return 0;

}int integer_add(int x, int y){

int result;result=x+y;return result;

}

#include<stdio.h>#include<conio.h>void interFace();void enterValue(float val1,float val2);void main(){

clrscr();float val1,val2;interFace();enterValue(val1,val2);getch();

}void interFace(){

textbackground(BLUE);printf("Enter value1:\t");cprintf(" \n\n\r");printf("Enter value2:\t");cprintf(" ");printf(" ");cprintf(" + \n\n\r");printf("Result :\t");cprintf(" ");

}void enterValue(float value1,float value2){

gotoxy(20,1);scanf("%f",&value1);gotoxy(20,3);scanf("%f",&value2);gotoxy(20,5);printf("%.2f",value1+value2);

}

#include<stdio.h>#include<conio.h>void interFace();float enterValue(float val1,float val2);void main(){

clrscr();float val1,val2,result;interFace();result=enterValue(val1,val2);

gotoxy(20,5);printf("%.2f",result);getch();

}void interFace(){

textbackground(BLUE);printf("Enter value1:\t");cprintf(" \n\n\r");printf("Enter value2:\t");cprintf(" ");printf(" ");cprintf(" * \n\n\r");printf("Result :\t");cprintf(" ");

}float enterValue(float value1,float value2){

gotoxy(20,1);scanf("%f",&value1);gotoxy(20,3);scanf("%f",&value2);return value1*value2;

}

MATH FUNCTIONS The sin() function The cos() function The tan() function The pow() function The sqrt() function

#include <stdio.h>#include <math.h>main(){ double x; x = 45.0; /* 45 degree */ x *= 3.141593 / 180.0; /* convert to radians */ printf("The sine of 45 is: %f.\n", sin(x)); printf("The cosine of 45 is: %f.\n", cos(x)); printf("The tangent of 45 is: %f.\n", tan(x)); return 0;}#include <stdio.h>#include <math.h>main(){ double x, y, z; x = 64.0; y = 3.0; z = 0.5; printf("pow(64.0, 3.0) returns: %7.0f\n", pow(x, y)); printf("sqrt(64.0) returns: %2.0f\n", sqrt(x)); printf("pow(64.0, 0.5) returns: %2.0f\n", pow(x, z)); return 0;}

GETTING CONTROLS The if statement The if-else statement The switch statement The break statement The continue statement The goto statement

ALWAYS SAYING "IF…" used to evaluate the conditions as well as to

make the decision whether the block of code controlled by the statement is going to be executed.

if (5>1) { statement1; statement2; . . }if (x > 0) printf("The square root of x is: %f\n", sqrt(x));

EX:#include <stdio.h> main(){ char ch; printf("Enter a character\n"); scanf("%c", &ch); if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U') printf("%c is a vowel.\n", ch); else printf("%c is not a vowel.\n", ch); return 0;}

#include<stdio.h>#include<conio.h>void main(){

clrscr();textbackground(YELLOW);cprintf(" Min and Max \n\r");textbackground(BLUE);printf("Enter value1:");cprintf(" \n\n\r");printf("Enter value2:");cprintf(" \n\n\r");printf("Enter value3:");cprintf(" \n\n\r");printf("Maximum: ");cprintf(" \n\n\r");printf("Minimum: ");cprintf(" \n\n\r");cprintf(" Max ");printf("\t");cprintf(" Min ");float val1,val2,val3;gotoxy(18,2);scanf("%f",&val1);gotoxy(18,4);scanf("%f",&val2);gotoxy(18,6);scanf("%f",&val3);float Min,Max;if(val1>=val2 && val1>=val3) Max=val1;if(val2>=val1 && val2>=val3) Max=val2;if(val3>=val1 && val3>=val2) Max=val3;gotoxy(18,8);printf("%.2f",Max);if(val1<=val2 && val1<=val3) Min=val1;if(val2<=val1 && val2<=val3) Min=val2;if(val3<=val1 && val3<=val2) Min=val3;gotoxy(18,10);printf("%.2f",Min);getch();

}

Note: strcmp-include<string.h>-Syntax: int strcmp(st1,st2);

- If return 0 means st1==st2- If return <0 means st1<st2- If return >0 means st1>st2

-Convert to Upper case//int tolower(char)//int toupper(char)#include <string.h>#include <stdio.h>#include <ctype.h>int main(void){ int length, i; char *string = "this is a string";

length = strlen(string); for (i=0; i<length; i++) { string[i] = toupper(string[i]);//tolower } printf("%s\n",string); return 0;}

NESTED IF STATEMENTS

#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(){

clrscr();textbackground(YELLOW);cprintf(" Study and Class \n\r");textbackground(BLUE);printf("Maths :");cprintf(" \n\n\r");printf("Chemistry :");cprintf(" \n\n\r");printf("Biology :");cprintf(" \n\n\r");printf("Physics :");cprintf(" \n\n\r");printf("Average :");cprintf(" \n\n\r");printf("Result :");cprintf(" \n\n\r");printf("Class :");cprintf(" \n\n\r");

cprintf(" OK ");printf("\t");cprintf(" Cancel ");

float maths,chem,phys,bio;gotoxy(18,2);scanf("%f",&maths);gotoxy(18,4);scanf("%f",&chem);gotoxy(18,6);scanf("%f",&bio);gotoxy(18,8);scanf("%f",&phys);gotoxy(18,10);printf("%.2f",(maths+chem+phys+bio)/4);

if((maths+chem+phys+bio)>=200) {gotoxy(18,12);printf("Pass");if((maths+chem+phys+bio)>=350){gotoxy(18,14);printf("A");getch();exit(0);}if((maths+chem+phys+bio)>=300){gotoxy(18,14);printf("B+");getch();exit(0);}if((maths+chem+phys+bio)>=200){gotoxy(18,14);printf("C++");getch();exit(0);} }if((maths+chem+phys+bio)<200) {gotoxy(18,12);printf("Fail");if((maths+chem+phys+bio)<100){gotoxy(18,14);printf("F");getch();exit(0);}if((maths+chem+phys+bio)<200){gotoxy(18,14);printf("E");getch();exit(0);} }getch();}

THE IF-ELSE STATEMENT

IF ELSE If(expression) {

block of statement; }

elsestatement;

SWITCH use to make unlimited decisions or choices based on

the value of a conditional expression and specified cases. Int, byte, short, char

switch (expression) { case expression1: statement1; case expression2: statement2; . . . default: statement-default;}

#include <stdio.h>main(){ int day; printf("Please enter a single digit for a day\n"); printf("(within the range of 1 to 3):\n"); day = getchar(); switch (day){ case `1': printf("Day 1\n");break; case `2': printf("Day 2\n");break; case `3': printf("Day 3\n");break; default: } return 0;}

Up = 72 Down = 80 Left =75Right=77Esc=27

THE BREAK STATEMENT You can add a break statement at the

end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed. BREAK LOOP ALSO

#include <stdio.h>main(){ int day; printf("Please enter a single digit for a

day\n"); printf("(within the range of 1 to 7):\n"); day = getchar(); switch (day){ case `1': printf("Day 1 is Sunday.\n");

break; case `2': printf("Day 2 is Monday.\n"); break; case `3': printf("Day 3 is Tuesday.\n"); break; case `4': printf("Day 4 is Wednesday.\n"); break; case `5': printf("Day 5 is Thursday.\n"); break; case `6': printf("Day 6 is Friday.\n"); break; case `7': printf("Day 7 is Saturday.\n"); break; default: printf("The digit is not within the range of 1 to 7.\n"); break; } return 0;}

MENUE SWITCH

THE CONTINUE STATEMENT Instead of breaking a loop, there are

times when you want to stay in a loop but skip over some statements within the loop. To do this, you can use the continue statement provided by C. The continue statement causes

execution to jump to the top of the loop immediately.

#include <stdio.h> main() { int i, sum; sum = 0; for (i=1; i<8; i++){ if ((i==3) || (i==5)) continue; sum += i; } printf("The sum of 1, 2, 4, 6, and 7 is: %d\

n", sum); return 0; }

DOING THE SAME THING OVER AND OVER

Looping, called iteration, is used in programming to perform the same set of statements over and over until certain specified conditions are met.The for statement The while statement The do-while statement

for (expression1; expression2; expression3)

{ statement1; statement2; . . .}

FOR LOOP

#include<stdio.h>#include<conio.h>void main(){

int i;for(i=0;i<3;){

printf(“Hello \n”); i++;}

getch();

}

#include <stdio.h> main() { int i, j; for (i=0, j=8; i<8; i++, j--) printf("%d + %d = %d\n", i, j, i+j); return 0; }

FOR LOOP

FOR LOOPfor (i=0, j=10; i<10, j>0; i++, j--){ /* statement block */}Ex:#include <stdio.h>main(){ int i, j; for (i=0, j=8; i<8; i++, j--) printf("%d + %d = %d\n", i, j, i+j); return 0;}

FOR LOOP#include <stdio.h>main(){ char c; printf("Enter a character:\n(enter x to exit)\n"); for ( c=‘’; c != ‘x’; ) { c = getc(stdin); putchar(c); } printf("\nOut of the for loop. Bye!\n"); return 0;}

THE DO-WHILE LOOP the expressions are set at the top of the

loopdo { statement1; statement2; . . .} while (expression);

THE DO-WHILE LOOP #include <stdio.h> main() { int i; i = 100; do { printf("The numeric value of %c is %d.\n", i,

i); i++; } while (i<72); return 0; }

THE WHILE LOOP while (expression) { statement1; statement2; . . .}

THE WHILE LOOP #include <stdio.h>main(){ char c; printf("Enter a character:\n(enter x to exit)\n"); while (c != `x') { c = getc(stdin); putchar(c); } printf("\nOut of the while loop. Bye!\n"); return 0; }

THE WHILE LOOP while (1) { statement1; statement2; . . .}


Recommended