Vous êtes sur la page 1sur 33

C Language Notes 201 0

1. /* */ Comment 2. Preprocessor Directive Contains information needed by the program to ensure the correct operation of Turbo Cs standard library functions. a. #include directive ex. #include stdio.h #include <stdio.h> b. #define directive ex. #define P13.1416 #define g gotoxy 3. Declaration Section syntax: <data type> <identifier> ex. Int x, y, z; float f=5.67; char g; char name[10]; int x=10; A. Data Types a. int > contain integral values only that are value that do not contain decimal places. Ranges 32768 to 32768. b. float > used for storing values containing decimal places (3.4E-38 to 3.4E+38). c. double > can store floating point numbers from 1.7E-308 to 1.7E+308 with 15 digits accuracy. d. char > can be used to contain a single character. A character constant is formed by enclosing the character with in a pair of single quote marks. e. void > valueless B. Identifiers the names that are used to reference variables, functions, labels and various other user-defined objects. case sensitive and has only 32 chars. RULE: The first character must be a letter or an underscore with subsequent characters being letters, numbers or underscore. Correct count test123 Incorrect 1count hi! there

4. The Body of the Program The body of the program starts with the reserved word main() and is enclosed with { and }. block of code is logically connected group of program statements that is treated as a unit. Is created by placing a sequence of statements between opening and closing curly ({}) braces. NOTES:

/ard 1

C Language Notes 201 0


All C keywords must be lowercase. It may not be used for any other purpose in a C program. All program statements in C must be terminated by a semicolon (;). Comments may be placed anywhere in a program and are enclosed between two markers. The starting comment marker is /* and the ending comment marker is */. CONSTANTS refers to fixed values that may not be altered by the program. Ex. char a 9 Int 10.1 123 float 123.23 double 123.23 12323333 string Mary Syntax: type variable_name = constant Example: char ch = a; int num = 0; float bal = 123.3; KEYWORDS/RESERVED WORDS auto break case char register continue default Do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

OPERATORS is a symbol that tells the compiler to perform specific mathematical or logical manipulation. a. Arithmetic Operators Operators + * / % -++ Functions subtraction addition multiplication division modulus decrementing x-- same as x=x-1 incrementing x++ same as x=x+1

Precedence of Arithmetic Operators highest ++ -- (unary) */% lowest +b. Relational Operators

/ard 1

C Language Notes 201 0


> >= < <= == != c. Logical Operators && II ! greater than greater than or equal to less than less than or equal to equal not equal and or not

d. Assignment Operator ( = ) equal sign e. Combined Operators += *= -= /= %= ex. ex. ex. ex. ex. a+=b a*=b a-=b a/=b a%=b a=a+b a=a*b a=a-b a=a/b a=a%b

THE COMMA OPERATOR It is used to string together several equations. The left side of the comma operator will always be evaluated as void. This means that the expression on the right side will become the value of the total comma separated expression. Example: x = ( y =3,y+1); y = 20; x = ( y=y-5,30/y); The value of x is 4 The value of x is 2

COMMONLY USED FORMAT SPECIFIER Code %c %d %l %f %s %o %x %% Description a single char decimal decimal decimal floating point string octal hexadecimal prints a percent sign

/ard 1

C Language Notes 201 0


BACKSLASH CODES Code \n \b \f \t \v \ \ \a \\ Meaning newline backspace formfeed horizontal tab vertical tab double quote single quote bell backslash

FORMATTED INPUT/OUTPUT FUNCTION IN C 1. printf( ) CHAR/STRING/NUMERIC OUTPUT is a function in a C system that simple prints or display print its argument at the terminal or on the CRT sreen. Limiting Float Numbers ( for printf( )) %.3f--- means 3 places after the decimal point x = 89.832211 printf(%.3f,x); output = 89.832 %8.3f -- _ _ _ _ _ _ _ _ ._ _ _ ( 8 with decimal point and 3 decimal places ) Examples: #include stdio.h main( ) { clrscr( ); printf(programming is fun\n); prinf(programming in C is even more fun\n); getche( ); } Output: programming is fun programming in C is even more fun ________________________________________________ #include stdio.h main( ) { clrscr( ); printf(Testing..,\n..1\n2\n.3\n);

/ard 1

C Language Notes 201 0


getche( ); } Output: Testing ..1 2 .3 ________________________________________________

#include stdio.h main( ) { Int sum; clrscr( ); sum=50+25; printf(the sum of 50 and 25 is %d\n,sum); getche( );

} Output: The sum of 50 and 25 is 75 #include <stdio.h> main( ) { int v1, v2+v25, sum; clrscr ( ); v1 = 50; sum = v1 + v2; printf ( The sum of the %d and %d is %d\n, v1, v2, sum); getche ( ); } Output: The sum of 50 and 25 is 75 _______________________________________________ 2. scanf( ) CHAR/STRING/NUMERIC OUTPUT Used to read virtually any type of data entered at the keyboard. Syntax : where : scanf( control string, argument list);

/ard 1

C Language Notes 201 0


control sting > Includes all the text labels, escape character and conversion specifies (format required for the desired output/input argument list > Includes all the variables to be printed in the order they are to be printed. Example: _______________________________________________ #include <stdio.h> main( ) { char my_char; clrscr( ); printf( Please type a character : ); scanf( %c,&my_char); printf( Thank You. That character was %c\n, my_char); getche( ); } _______________________________________________ UNFORMATED I/O FUNCTIONS IN C 1. getch( ) / getche( ) > single character input > waits until a key is pressed and then returns the result 2. putch( ) > single char output 3. gets ( ) > string input 4. puts ( ) > sting output CONDITIONAL STATEMENTS C supports two(2) types of conditional statement: if and switch. IF STATEMENTS The general form of the if else statement: Syntax: if (expression) statement/s; else statement/s; Note: Block of statements in C is surrounded by curly braces { } else is optional IF ELSE IF STATEMENTS A common programming construct is the if else if statement. It look like this: code)

/ard 1

C Language Notes 201 0


Syntax: if (expression) statement; else if (expression) statement; else if (expression) statement; else statement; Sample Program No. 1 /* Program to determine if a number is a positive or negative */ #include stdio.h main() { int num; clrscr(); printf(Enter an integer); scanf(%d,&num); if (num > 0) printf(%d is a positive integer,num); else printf(%d is a negative integer<,num); getch(); } Sample Program No.2 /* Program to determine if a number is odd or even*/ #include stdio.h main() { int num,rem; clrscr(); printf(Enter a number); scanf(%d,&num); rem = num % 2; if (rem == 0) printf(%d is an even number,num); else if (rem ! = 0) printf(%d is an odd number,num); getch); } Sample Program No.3 /*Program Arithmetic Operators*/ #include stdio.h main() {

/ard 1

C Language Notes 201 0


int f,s, total=0; char optr; clrscr(); printf(Enter 1st number :); scanf(%I, &f); printf(Enter the operator :); scanf(%s, &optr); printf(Enter 1st number :); scanf(%I, &s); if (optr==+) total=f+s; if (optr ==-) total=f-s; if (optr==*) total=f*s; if (optr==/) total=f/s if (optr==+ II optr==- II optr==* II optr==/) printf(%d %c %d = %d, f, optr, s, total); else printf(Operator not valid!!!); getch(); } THE CONDITIONAL (?) OPERATION The ? operator can be used to replace if/else statements of the general form If (condition) expression else expression The key restriction is that the target of both the if and the else must be a single expression not another C expression. The ? is called a ternary operator because it requires three operands and takes the general form Exp1 ? Exp2 : Exp3 where Exp1, Exp2 and Exp3 are expressions. The value of a ? expression is determined this way: Exp1 is evaluated. If true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value become the value of the expression. Format: condition ? statement : statementF;

/ard 1

C Language Notes 201 0


result = condition ? statement : statementF; Example: x = 10; y = x>9 ? 100: 200; x = 10; f (x>9) Y = 100; else Y = 200;

x = 10 x>9 ? printf(Yes): printf(No); max = (a>b) ? a : b;

SWITCH STATEMENT Is a built in multiple branch decision statement. A variable is successively tested against a list of integer or character constants. When a match is found, a statement or block statement is executed. Syntax: switch(variable) { case constant1 : statement/s; break ; case constant2 : statement/s; break; case constant3 : statement/s; break; default : statement/s; } where the default statement is executed if no matches are found. The default is optional. When matches found the statement associated with the case, is executed until the break statement is reached, or in the case of default or last case if no default is present, the end of switch statement is encountered. There are three important things to know about switch statement: 1. Switch differs from the if in that switch can only test for equality, where as the if, can evaluate relational or logical expression. 2. No two case constant in the same switch can have identical values. A switch statement endorsed by an outer switch may have case constants that are the same. 3. If char constant are used in the switch, they are automatically converted to their integer values. Switch statement is often used to process keyboard commands, such as menu selection.

/ard 1

C Language Notes 201 0


Sample Program No. 1 /* Program Stimulation*/ #include stdio.h int x; main( ) { clrscr( ); puts(Enter a number: ); scan(%d,&x); switch(x) { case 1 : puts(now, there\s); break; case 2 : puts(only but a few); case 3 : puts(good men); puts(it\s true!!!);break; default: puts(no good men at all); } getch( ); } Sample Program No. 2 /* Program Stimulation*/ #include stdio.h int x; main( ) { clrscr ( ); puts(Enter a number:); scanf(%d,&x); switch(x) { case 1 : case 2 : case 3 : pritnf(%d Indian, x); break; case 4 : case 5 : case 6 : printf (4 little 5 little 6 little Indian); break; } getch( ); }

Sample Program No. 3

/ard 1

C Language Notes 201 0


switch(optr) { case+ : total = f+s; break; case- : total = f-s; break; case* : total = f*s; break; case/ : total : f/s; break; default : printf(operator not valid); } Sample Program No. 4 /* Program that uses switch statement */ #include stdio.h main( ) { int n1, n2, choice; clrscr( ); printf(************************************\n); printf(* Main Menu *\n); printf(* *\n); printf(* [ 1 ] Addition *\n); printf(* [ 2 ] Subtraction *\n); printf(* [ 3 ] Multiplication *\n); printf(* [ 4 ] Division *\n); printf(* *\n); printf(************************************\n); printf( Enter choice: ); scanf(%d,&choice) switch(choice) { case 1 : printf(Enter first number:);scanf(%d,&n1); printf(Enter second number:);scanf(%d,&n2); printf(The sum of %d and %d is %d,n1,n2,n1+n2); break; case 2 : printf(Enter first number:);scanf(%d,&n1); printf(Enter second number:);scanf(%d,&n2); printf(The difference of %d and %d is %d,n1,n2,n1-n2); break; case 3 : printf(Enter first number:);scanf(%d,&n1); printf(Enter second number:);scanf(%d,&n2); printf(The product of %d and %d is %d,n1,n2,n1*n2); break; case 4 : printf(Enter first number:);scanf(%d,&n1); printf(Enter second number:);scanf(%d,&n2); printf(The quotient of %d and %d is %d,n1,n2,n1/n2); break; default : printf(Invalid Option); } getch ( ); }

/ard 1

C Language Notes 201 0


LOOPS Allows a set of instructions to be repeated until a certain condition is reached. FOR LOOP Syntax: for(initialization; condition; increment) statement/s; The initialization is an assignment statement that is used to set the loop-control variable. The condition is a relational expression that determines when the loop will exit by testing the loop-control variable against some value. The increment defines how the loop-control variable will change each time the loop is repeated. These three major sections must be separated by semicolons. The for loop will continue to execute as long as the condition is true. Once the condition becomes false, the program execution will resume at the statement following the for. Sample Program No. 1 /* Program on Factorial*/ #include <stdio.h> Enter a number : 5 main( ) The factorial of 5 is 120 { int f,x,ans; clrscr( ); printf(Enter a number : );scanf(%d,&f); Thefa ans=1; for(x=f; x>=1;x--) ans=x*ans; printf(The factorial of %d is %d,f,ans); getch( ); } Sample Program No. 2 /* Program Exponential */ #include <stdio.h> Enter Base : 2 main( ) Enter Exponent: 3 { The answer is 8.00 int b,e,x; double ans; char sagot; clrscr( ); printf(Enter Base : );scanf(%d,&b); printf(Enter Exponent: );scanf(%d,&e); ans=1 for(x=1;x<=e;x++) ans=b*ans ; printf(The answer is %.2,ans); getch( );

factoria

/ard 1

C Language Notes 201 0


} Sample Program No. 3 /* Displays the triangular number of a number */ what triangular number do you #include <stdio.h> want? 5 main( ) Triangular number 5 is 15 { int n,m,t_num; clrscr( ); printf(What triangular number do you want? \n); scanf(%d,&m); t_num=0; for (n=1;n<=m;n++) t_num=t_num+n; printf(Triangular number %d is %d\n,m,t_num); getch( ); }

Sample Program No. 4 /* Program Simulation */ #include <stdio.h> #define r 7 int k, k2, x, y, z; main( ) { clrscr( ); k=x=0; y=2; z=5; for (k=1;k<=r; k++) { k2=x; x+=y; y+=z; z+=5; printf(%d %d\n,k, k2); } printf(%d %d %d, x, y, z); getch( ); } Sample Program No. 5 /* Program Simulation */ #include stdio.h int x, y;

10 22 39 4 26 5 58 6 110 7 187 294 142 40

10 8 6 4 2 8 6 4 2 6 4 2 4 2 2

/ard 1

C Language Notes 201 0


main( ) { clrscr( ); x=7; y=3; x=x%y; if(x==0) printf(FINISH); else for(x=10;x>=2;x-=2) for(y=x; y>=1;y-=2) printf(%d,y); printf(\n); getche( ); } Sample Program No.6 /* Program Simulation */ #include stdio.h #define p printf #define c case int x, ctr, num; main( ) { clrscr( ) x=5; for (ctr=5;ctr<=35;ctr*=2) { switch(ctr%x) { c 3 : p(T); c 7 : p(C); c 8 : p(I);break; c 2: c 0 : p(_J);p(A); c 11: break; c 1 : p(J);break; c 4 : p(C); c 15 : p(K); c 16 : p(**); c 10 : p(_AND_); c 13 : p(J);break; default : p(I);p(L);p(_); } x++; } getch( ); }

JACK**_AND_JILL_

/ard 1

C Language Notes 201 0


WHILE STATEMENT Continues to execute as long as the condition which can be any expressions remains true. Syntax: while (expression) statement/s; Sample Program no. 1 /* Program Simulation */ #include stdio.h main( ) { int count=1 clrscr( ); while (count<=5) { printf(%d\n,count); ++count; } getche( ); }

1 2 3 4 5

Sample Program No. 2 /* This program finds the Greatest Common Divisor */ /* of two nonnegative integer values */ Please type in two nonnegative 3include <stdio.h> integers: main( ) 48 { Their Greatest Common Divisor is 4 int num1, num2, temp; clrscr( ); printf(Please type in two nonnegative integers : \n); scanf(%d %d,&num1,&num2); while (num2!=0) { temp = num 1 % num2; num 1= num 2; num 2 = temp; } printf(Their Greatest Common Divisor is %d\n,num1); getch( ); } Sample Program No. 3 /* Program to reverse the digits of a number */ #include <stdio.> main( )

/ard

Enter a number 12345 54321

C Language Notes 201 0


{ int num, right digit; clrscr( ); printf(Enter a number \n); scanf(%d,&num); while (num != 0) { rightdigit = num % 10; printf(%d,rightdigit); num = num / 10; } printf(\n); getch( ); } DO STATEMENT The program statement is executed first. Next, the expression inside the parenthesis is evaluated. If the result of the evaluation TRUE, the loop continues and program statements is once again executed. As long as evaluation of expression continues to be TRUE, program statement will be repetitively executed. When evaluation of expression proves FALSE, the loop will be terminated and next statement in the program will be executed in the normal sequential manner. Syntax: do statement/s; while (expression); Sample Program No. 1 /* Program to reverse the digit of the number */ #include <stdio.h> main( ) { int num, rightdigit; clrscr( ); printf(Enter a number \n); scanf(%d,&num); do { rightdigit = num % 10; printf(%d,rightdigit); num = num / 10; } while (num ! = 0); printf(\n); getch( ); }

Enter a number 12345 54321

/ard 1

C Language Notes 201 0


Sample Program No. 2 /* Fibonacci Sequence */ #include <stdio.h> main( ) { int a=0,b=1,c; clrscr( ); pintf(%d,a); printf(%l,b); do { c=a+b; printf(%l,c); a=b; b=c; } while(c<100); getch( ); }

0 1 1 2 3 5 8 13 21 34 55 89 144

MIDTERM QUIZ # 1 FUNCTIONS Is a subroutine that contains one or more statements and performs one or more tasks Advantages of Using a Function: 1. fits naturally in topdown approach 2. can be divided for many programmers as subtasks 3. ca easily be tested, each function can be tested individually. Syntax: type_specifier function_name(parameter_list) { body of function } where: type_specifier / return_type specifiers the type of value that the function returns using the return statement. parameter_list / function_arguement a function maybe without parameters in which case the parameter list contains only the key word void. Uses of the return Statement: 1. It causes an immediate exit from the function it is in. That is, it causes program execution to return to the calling code. 2. It can be use to return a value. Global variables

/ard 1

C Language Notes 201 0


known throughout the entire program and may be used by any piece of code. It is declared outside any function. Local variables variables that are declared inside a function. It can be referenced only by a statements that are inside the block in which the variables are declared. Formal variables variables that accept the values of the arguments of a function. TWO WAYS OF PASSING ARGUMENTS 1. Call by value this method copies the value of an argument into the formal parameter of the subroutine. changes made to the parameters of the subroutine have no effect on the variables used to call it. 2. Call by reference the address of an argument is copied into the parameter. Inside the subroutine, the address is used to access the actual argument used in the call. changes made to the parameter affect the variable used to call the routine. Sample Program No. 5 /* Program that increments a number by 3 */ #include stdio.h int inc(int x, int y) { The value of y now is 8 int ans; x+=y; ans=x; return(ans); } main( ) { int y=5; clrscr( ); printf(The value of y now is %d, inc(y,3) ); getche( ); } Sample Program No. 6

/ard 1

C Language Notes 201 0


# includestdio.h void trick(int w, int x,, int y, int z); { printf(\n%d %d %d %d, w, x, y, z); } main( ) { int w=10, x=12, y=14, z=16; clrscr( ); trick(w, x, y, z); trick(x, y, w, z); trick(y, w, z, x); trick(x, x, w, z); trick(x, y, y, w); trick(x, z, z, w); trick(x, y, w, z); getch( ); }

10 12 14 12 12 12 12

12 14 10 12 14 16 14

14 10 16 14 14 16 10

16 16 12 16 10 10 16

MIDTERM QUIZ # 2

MIDTERM EXAMINATION

FINAL PERIOD ARRAY

collection of variables of the same type that are referenced by a common name. A specific
element in an array is accessed by an Index. The lowest address corresponds to the first element and the highest address corresponds to the last element. All array have 0 as the Index Of their first element.

SINGLE DIMENSIONAL ARRAY Syntax: type arr_name[size]; where: type > type of each elements arr_name > name size > number of elements the array will hold Example:

/ard 1

C Language Notes 201 0


char p [10] > character array that has 10 elements p[0] through p[9] Int x[10] > Integer array Sample Program No. 1 /** Prints the reverse of the entered string **/ Enter a String : # Include <stdio.h> Mary main() yraM { char s[10]; Int x, ctr; clrscr(); printf(Enter a String :); gets(s); x = strien(s)-1; for (ctr=x;ctr>=0;ctr--) printf(%c,s[ctr]); getch(); } Sample Program No. 2 /* Program that determines the highest number and average of 5 numbers*/ # include <stdio.h> main( ) { Enter 5 number : int num[5],sum=0, ave, ctr, hn, x; 22334 clrscr( ); The highest noumber is 4 printf( Enter 5 numbers :\n ); The average is 2 for ( ctr = 0;ctr<=4;ctr++ ) scanf(%d,&num [ctr] ); hn=0; for ( ctr =1;ctr< =4;ctr++ ) if ( num[ctr] > hn ) hn=num[ ctr ] ; for ( ctr =0;ctr< =4;ctr++ ) sum=sum+num[ ctr ]; ave=sum/5; printf( The highest noumber is %d, hn ); printf( \nThe average is %d, ave ); getch( ); }

Sample Program No. 3 /* Arrange the number in ascending order */

/ard

Enter 5 numbers : 14352 The numbers in ascending order 12345

C Language Notes 201 0


#include <stdio.h> main( ) { int num[ 5 ], ctr1, ctr2, temp; clrscr(); printf( Enter 5 numbers :\n ); for ( ctr1=0;ctr1<=4;ctr1++ ) scanf( %d,&num[ctr1] ); for ( ctr1=0;ctr1<=3;ctr1++ ) for ( ctr2=ctr1+1;ctr2<=4;ctr2++ ) if ( num[ctr1] > num[ctr2] ) { temp = num[ctr1]; num[ctr1] = num[ctr2]; num[ctr2] = temp; } printf( The numbers in ascending order \n ); for ( ctr1=0;ctr1<=4;ctr1++ ) printf( %d,num[ctr1] ); getch( ); }

Sample Program No. 4 /* Display the number of times a number appeared */ #include <stdio.h> main() { Enter 5 numbers : int num[5], ctr, x, y; 13231 clrscr(); The numbers are : 1 appears 2 time(s) printf(Enter 5 numbers :\n); 3 appears 2 time(s) for ( ctr=0; ctr<=4; ctr++) 2 appears 1 time(s) scanf(%d,&num[ctr]); printf(The numbers are :\n); for (x=0;x<=4;x++) { ctr=1; if ((x==4)&&(num[x]>=0)) printf(%d appears %d time(s)\n, num[x], ctr); else if ( num[x]>=0 ) { for ( y=x+1;y<=4;y++) if ( num[x] == num[y] ) { num[y]=-1; ctr+=1; } printf(%d appears %d time(s)\n, num[x],ctr); }

/ard 1

C Language Notes 201 0


} getch(); } MULTIDIMENSIONAL ARRAY (TWO DIMENSIONAL ARRAY) Syntax: type arr_name[2dlm][1dlm]; column row Sample Program No.1 /*Program Simulation*/ #include<stdio.h> int t, n, num[3][3]; main() { clrscr(); for(t=0;t<=2;t++) for(n=0;n<=2;n++) num[t][n]=(2*4)+n; for(t=0;t<=2;t++) {for(n=0;n<=2;n++) printf(%d,num[t][n]); printf(\n); } Sample Program No. 2 /*Multiplication Table using Array*/ #include<stdio.h> int num [5][5],ctr,ctr2,x,y; main() { clrscr(); x=y=0; for(ctr=1;ctr<=5;ctr++) { for(ctr2=1;ctr2<=5;ctr2++) { num[x][y]=ctr*ctr2; y++; } x++; y=0; } for(ctr=0;ctr<=4;ctr++) printf(%d,num[ctr][ctr2]; printf(\n); } getch(); } Final Output:
8910 8910 8910

Final Output:

12345 246810 3691215 48121620 510152025

/ard 1

C Language Notes 201 0


Sample Program No.3 /*Sum of each row and column*/ #include<stdio.h> int num [3][3],a_col[3],a_row[3],x,y; int cols, rows; main() { clrscr(); printf(Enter Number in 3 x 3 matrix :); cols=5; rows=2; for(x=0;x<=2;x++) { gotoxy(cols,rows); scanf(%d,&num[x][y]); cols+=3; } cols=5;rows++; } for(y=0;y<=2;y++) for (x=0;x<=2;x++) { a_row[y]=a_row[y] + num [y][x]; a_col[x]=a_col[x] + num [y][x]; } cols=5; for(x=0;x<=2;x++) { gotoxy(cols,7);printf(%d,a_row[x]); gotoxy(cols,8);printf(%d,a_col[x]); cols+=3; } getch(); } String Functions

Enter Number in 3 x 3 matrix: 123 121 123 646 367

10. strcmpi( ) syntax: strcmpi(str1,str2); compare str1 to str2 Ignoring case. 11. stmcpy( ) syntax: strcpy(str1,str2,cnt);

/ard 1

C Language Notes 201 0


example strcpy(str1,Hello); strcpy(str1,str1,4); output: Hell 12. strset( ) syntax: strset(str1,chr) sets all characters in str1 to chr. example strcpy(str1,HELLO); strset(str1,E); output: EEEEE 13. strnset( ) syntax: strnset(str1,chr,cnt) example strcpy(str1, HELLO); strnset(str1,A,3); output AAALO FINAL QUIZ # 1 Pointers is a variable that holds a memory address of another object.

Syntax: type*variable > name of a pointer variable Examples: char*p; int*temp,*start;

/ard 1

C Language Notes 201 0


Pointer Operators &, * & is a unary operator that return the memory address of its operand. ex. count_addr =&count; count_addr receives the address of count. address 2000 2001 2002 variable count value 100

* is the complement of &. It is unary operator that returns the value of the variable located at the address that follows. ex. val = *count_addr; val = 100; val receives the value at address count_addr Sample Program No.1 /*Program Simulation*/ #include<stdio.h> main( ) { int *count_addr, count, val; count=100; clrscr(); count_addr= &count; val =*count_addr; printf(%d,val); getch(); }

100

Sample Program No. 2 /*Program Simulation */ #include<stdio.h> print(int *w, int x, int *z) { *w+=2; x-=1; y+=3; *z-=1;

5 8 13 18 7 7 16 17 8 13 7 17 15 6 20 7 17 7 15 7 19 16 10 14 7 7 14 19

/ard 1

C Language Notes 201 0


printf(%d %d %d %d\n, *w,x,y,*z); } main( ) { int w=5, x=8, y=13, z=18; clrscr( ); printf(%d %d %d %d\n,w,x,y,z); print(&w,x,y,&z); printf(%d %d %d %d\n,x,y,w,z); print(&y,w,z,&x); printf(%d %d %d %d\n,z,w,y,x); print(&z,z,w,&y); printf(%d %d %d %d\n,x,w,y,z); getch( ); } Sample Program No. 3 /*Program Simulation */ #includestdio.h void trick(int w, int *x, int y, int *z) { printf(\n%d %d %d %d, w, *x, y, *z); ++*x; --*z; } main( ) { int w=10, x=12, y=14, z=16; clrscr( ); trick(w, &x, y, &z); trick(x, &y, w, &z); trick(y, &w, z, &x); trick(x, &x, w, &z); trick(x, &y, y, &w); trick(x, &z, z, &w); trick(x, &y, w, &z); printf(%d %d %d %d\n, w,x,y,z); getch( ); } Sample Program No. 4 /*Program Simulation*/ #include stdio.h void trick(int w, int *x, int y, int *z) { printf(\n%d %d %d %d, w, *x, y, *z); ++*x;--*z;
10 12 14 16 13 14 10 15 15 10 14 13 12 12 11 14 13 15 15 11 13 13 13 10 13 16 9 14 9 13 17 13

/ard

8 10 12 14 11 12 12 8 11 13 7 13 11 11 7 12 14 7 11 12 11 11 11 8 11 14 7 12 7 11 15 11

C Language Notes 201 0


} main( ) { int w=8, x=10, y=12, z=14; clrscr( ); trick(w, &x, y, &z); trick(x, &y, y, &w); trick(x, &y, w, &z); trick(x, &x, w, &z); trick(y, &w, z, &x); trick(x, &z, z, &w); trick(x, &y, w, &z); printf(%d %d %d %d, w, x, y, z); getch( ); } Sample Program No. 5 / * Program Simulation * / # includestdio.h swap(int *x, int *y) { int temp; temp= *x; *x= *y; *y= temp; } main( ) { int x= 5, y= 12, z= 15; clrscr( ); swap(&z, &y); printf(%d swap(&y, &x); printf(%d swap(&z, &x); printf(%d swap(&x, &y); printf(%d swap(&y, &z); printf(%d getch( ); }

5 15 15 5 12 5 5 12 5 15

12 12 15 15 12

%d %d\n, x, y, z); %d %d\n, x, y, z); %d %d\n, x, y, z); %d %d\n, x, y, z); %d %d\n, x, y, z);

Sample Program No.6 /*Program Simulation*/ #include <stdio.h> swap(int*x, int*y) {

/ard

6 10 4 10 6 4 10 4 6 10 6 4 6 10 4

C Language Notes 201 0


int temp; temp = *x; *x = *y; *y= temp; } main() { int x=4, y=10, z=6; clrscr(); swap(&z,&x); printf(%d %d %d\n, x,y,z); swap(&y,&x); printf(%d %d %d\n, x,y,z); swap(&z,&y); printf(%d %d %d\n, x,y,z); swap(&y,&z); printf(%d %d %d\n, x,y,z); swap(&x,&y); printf(%d %d %d\n, x,y,z); getch(); } Sample Program No.7 /*Program Simulation*/ #includestdio.h balik(int*a, int b, int*c) { int d; d=2; b=*a+d; d=b+*c; *a=d; *c=b+d; } main() { int x,y,z; clrscr(); x=y=z=0; balik(&x,5,&y); printf(%d %d %d\n,x,y,z); balik(&z,y,&x); printf(%d %d %d\n,x,y,z); getch(); }
240 644

Sample Program No. 8 /*Program Simulation*/ #include <stdio.h>

/ard 1

C Language Notes 201 0


int fill_a(int *a, char b, int *c) { int d, switch; if((b==y)&&(*a==0)) { *a= *c+3; d= *c+*a; switch= 1; } else switch= 0; return(switch); }

Fill Var X?: Y -64 is filled with 3 or

main( ) { int x, y, z, sw; Fill Var X?: char ans; n-64 remains empty clrscr( ); x= y= z= 0; puts(Fill Var X?:); ans= getche( ); sw= fill_a(&x, ans, &z); if(sw==0) printf(%d remains empty, &x); else printf(\n%d is filled with %d, &x,x); getch( ); }

STRUCTURE is a collection of variables that are referenced under one name, providing a convenient means of keeping related information together. Structure elements > the variable that make up the structure. Structure declaration: Struct Struct_type_name { type element_name1; type element_name2; type element_name3; type elememt_nameN; } structure_variables;

/ard 1

C Language Notes 201 0


Interpretation: struct tells the compiler that a structure template is being defined. The declaration Id terminated by a semicolon because a structure definition is a statement. struct_type_name structure name Identified particular data structure and its type specifier. structure_variables are common separated list of variable names. Note: Either the structure type name struct_type_name or structure_variables may be omitted, but not both. Example: struct personal { char name [15]; int age; char bday[8]; int tel_no; }; struct personal p_ _data

Referencing Struct Element Individual struct elements are referenced by using the . (dot) operator, Syntax: struct_type_name.element_name; Example: p_data.tel_no=1234567 scanf(%d,&p_data.tel_no); printf(%d,p_data.tel_no); Sample Program No.1 #includestdio.h struct personal { char name [15]; Final Output:

Entry screen Name : mary Age : 22 Birthday : 082977 Tel.No. : 1234566

/ard

C Language Notes 201 0


int age; char b day[8]; int tel_no; } struct personal p_data; main() { clrscr(); puts(Entry screen\n); printf(Name :);scanf(%s,&p_data.name); printf(Age :);scanf(%d,&p_data.age); printf(Birthday :);scanf(%s,&p_data.bday); printf(Tel.No. :);scanf(%d,&p_data.tel_no); }

Sample Program No. 2 #include<stdio.h> struct grade { float mlec, mlab, flec, flab; float mid, fin; float total; }; struct grade grd; main() { Final Output:
Entry screen MidLecGrade : 90 MidLabGrade : 90 FinLecGrade : 85 FinLabGrade : 85 The total grade is 87.00 Enter Another?

clrscr(); puts(Entry screen\n); printf(MidLecGrade:);scanf(%f,&grd.mlec); printf(MidLabGrade:);scanf(%f,&grd.mlag); printf(FinLecGrade:);scanf(%f,&grd.flec); printf(FinLabGrade:);scanf(%f,&grd.flab); grd.mid = (grd.mlec*.6) + (grd.mlab*.4); grd.fin = (grd.flec*.6) + (grd.flab*.4); grd.total=(grd.mid*.4) + (grd.fin*.6); printf(\nThe total grade is %.2f,grd.total); puts(\n\nEnter Another?); ans = getch(); }while((ans==Y)II(ans==Y));

/ard 1

C Language Notes 201 0


} Array Of Structure To declare an array of structure you must first define a struct and then declare an array variable of that type. Example: Struct personal { char name [15]; int age: char bday[8]: int tel_no: }; struct personal p_data[5]: Sample Program No.1 Name Age Birthday Tel. No. : Mary : 22 : 082977 : 1234567

#include stdio.h struct personal { char name[15]: int age: char bday[8]: int tel_no: }; struct personal p_data[5]; main() { int ctr; clrscr(); puts(Entry screen\n\n); for(ctr=0;ctr<=4;ctr++) { clrscr();

/ard 1

C Language Notes 201 0


printf(Name :);scanf(%s, &p_data[ctr].name); printf(Age :);scanf(%s, &p_data[ctr].age); printf(Birthday :);scanf(%s, &p_data[ctr].bday); printf(Tel. No :);scanf(%s, &p_data[ctr].tel_no); } }

/ard 1

Vous aimerez peut-être aussi