C Language Notes 201 0

1. /* … */ Comment 2. Preprocessor Directive Contains information needed by the program to ensure the correct operation of Turbo C’s 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

} Output: programming is fun programming in C is even more fun ________________________________________________ #include “stdio.832211 printf(“%. prinf(“programming in C is even more fun”\n).h “ main( ) { clrscr( ). printf(“Testing. getche( ).3f”._ _ _ _ _ _ _ _ . Limiting Float Numbers ( for printf( )) %.3f--. /ard 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._ _ _ ( 8 with decimal point and 3 decimal places ) Examples: #include “stdio. printf(“programming is fun”\n).x).3f -. output = 89.832 %8.3\n”).1\n…2\n….\n.means 3 places after the decimal point x = 89.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...h “ main( ) { clrscr( )..

h “ main( ) { Int sum. } Output: Testing… . clrscr( ). argument list). v1 = 50. /ard 1 . } Output: The sum of 50 and 25 is 75 #include <stdio. sum).. getche( ). scanf( ) CHAR/STRING/NUMERIC OUTPUT  Used to read virtually any type of data entered at the keyboard. sum. sum=50+25. v2. printf(“the sum of 50 and 25 is %d\n. v2+v25.1 …2 ….C Language Notes 201 0 getche( ).3 ________________________________________________ #include “stdio. } Output: The sum of 50 and 25 is 75 _______________________________________________ 2. clrscr ( ). printf (“ The sum of the %d and %d is %d\n”. Syntax : where : scanf(“ control string”.h> main( ) { int v1. getche ( ). sum = v1 + v2. v1.sum).

printf( “Please type a character : “ ). It look like this: code) /ard 1 . gets ( ) > string input 4.&my_char). else statement/s. Example: _______________________________________________ #include <stdio. That character was %c\n”. printf( “Thank You. IF STATEMENTS The general form of the if – else statement: Syntax: if (expression) statement/s. clrscr( ).C Language Notes 201 0 control sting > Includes all the text labels. getch( ) / getche( ) > single character input > waits until a key is pressed and then returns the result 2. getche( ). puts ( ) > sting output CONDITIONAL STATEMENTS C supports two(2) types of conditional statement: if and switch. scanf( “%c”. } _______________________________________________ UNFORMATED I/O FUNCTIONS IN C 1. my_char). 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. putch( ) > single char output 3.h> main( ) { char my_char. 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.

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

else printf(“Operator not valid!!!”). The ? is called a ternary operator because it requires three operands and takes the general form Exp1 ? Exp2 : Exp3 where Exp1. scanf(“%I”. &s). &optr). Exp2 and Exp3 are expressions. /ard 1 . total=0. clrscr(). then Exp3 is evaluated and its value become the value of the expression. if (optr==’/’) total=f/s if (optr==’+’ II optr==’-‘ II optr==’*’ II optr==’/’) printf(“%d %c %d = %d”.C Language Notes 201 0 int f. s. optr. f. } 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. printf(“Enter 1st number :”). getch(). scanf(“%s”. printf(“Enter 1st number :”). &f). total). Format: condition ? statement : statementF. if (optr==’*’) total=f*s. char optr. The value of a ? expression is determined this way: Exp1 is evaluated. if (optr ==’-‘) total=f-s. if (optr==’+’) total=f+s. If true. If Exp1 is false. then Exp2 is evaluated and becomes the value of the entire ? expression.s. scanf(“%I”. printf(“Enter the operator :”).

C Language Notes 201 0 result = condition ? statement : statementF. f (x>9) Y = 100. Syntax: switch(variable) { case constant1 : statement/s. A switch statement endorsed by an outer switch may have case constants that are the same. Switch differs from the if in that switch can only test for equality. x = 10 x>9 ? printf(“Yes”): printf(“No”). Switch statement is often used to process keyboard commands. SWITCH STATEMENT  Is a built – in multiple branch decision statement. /ard 1 . } where the default statement is executed if no matches are found. else Y = 200. Example: x = 10. a statement or block statement is executed. the end of switch statement is encountered. The default is optional. No two case constant in the same switch can have identical values. such as menu selection. can evaluate relational or logical expression. is executed until the break statement is reached. 2. 3. where as the if. x = 10. When a match is found. A variable is successively tested against a list of integer or character constants. max = (a>b) ? a : b. they are automatically converted to their integer values. When matches found the statement associated with the case. or in the case of default or last case if no default is present. break. break . case constant2 : statement/s. break. If char constant are used in the switch. default : statement/s. case constant3 : statement/s. y = x>9 ? 100: 200. There are three important things to know about switch statement: 1.

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

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

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

k2. getch( ). for (n=1.h> #define r 7 int k. x+=y. printf(“What triangular number do you want? \n”). z). y+=z.h” int x. t_num=0. z+=5. y.&m). clrscr( ).n++) t_num=t_num+n.m. y. z=5.k. z. y=2. printf(“%d %d\n”. x. } Sample Program No. getch( ). 4 /* Program Simulation */ #include <stdio.C Language Notes 201 0 } Sample Program No.t_num).h> want? 5 main( ) Triangular number 5 is 15 { int n. printf(“Triangular number %d is %d\n”. x. 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 . y. k=x=0. for (k=1. k2). k++) { k2=x. 3 /* Displays the triangular number of a number */ what triangular number do you #include <stdio. scanf(“%d”.t_num.m. 5 /* Program Simulation */ #include “stdio.n<=m. main( ) { clrscr( ).k<=r. } Sample Program No. } printf(“%d %d %d”.

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

h” main( ) { int count=1 clrscr( ). } printf(“Their Greatest Common Divisor is %d\n”. printf(“Please type in two nonnegative integers : \n”).&num1. 2 /* This program finds the Greatest Common Divisor */ /* of two nonnegative integer values */ Please type in two nonnegative 3include <stdio. num 1= num 2. getch( ).count).> main( ) /ard Enter a number 12345 54321 1 . 1 /* Program Simulation */ #include ‘stdio.h> integers: main( ) 48 { Their Greatest Common Divisor is 4 int num1. while (count<=5) { printf(‘%d\n”. num 2 = temp. ++count. Syntax: while (expression) statement/s.C Language Notes 201 0 WHILE STATEMENT  Continues to execute as long as the condition which can be any expressions remains true. Sample Program no.num1). clrscr( ). while (num2!=0) { temp = num 1 % num2. } Sample Program No. } 1 2 3 4 5 Sample Program No. temp. } getche( ). num2. scanf(‘%d %d”. 3 /* Program to reverse the digits of a number */ #include <stdio.&num2).

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

fits naturally in topdown approach 2. } while(c<100). It can be use to return a value. Uses of the return Statement: 1. 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. b=c.c). That is. parameter_list / function_arguement  a function maybe without parameters in which case the parameter list contains only the key word void.b). 2 /* Fibonacci Sequence */ #include <stdio. 2. } 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. each function can be tested individually.c. a=b. It causes an immediate exit from the function it is in.a). can be divided for many programmers as subtasks 3. do { c=a+b. it causes program execution to return to the calling code. ca easily be tested. pintf(“%d”. printf(“%l”.h> main( ) { int a=0.b=1. clrscr( ). getch( ). Global variables /ard 1 . printf(“%l”.C Language Notes 201 0 Sample Program No.

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

w). z). trick(x. y. x). SINGLE DIMENSIONAL ARRAY Syntax: type arr_name[size]. x. x. z. } 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. y. int z).h” void trick(int w. The lowest address corresponds to the first element and the highest address corresponds to the last element. w. A specific element in an array is accessed by an Index. w. y. z=16. All array have 0 as the Index Of their first element. w). w. x. trick(w.. clrscr( ). where: type > type of each elements arr_name > name size > number of elements the array will hold Example: /ard 1 . z). x=12. trick(x. trick(x. w. z. } main( ) { int w=10. trick(x. trick(x. int y. y. { printf(“\n%d %d %d %d”. y. z). z). z). y=14.C Language Notes 201 0 # include”stdio. trick(y. y. getch( ). w. z. int x.

x = strien(s)-1.h> Mary main() yraM { char s[10].ctr++ ) if ( num[ctr] > hn ) hn=num[ ctr ] .s[ctr]).ctr--) printf(“%c”. The average is 2 for ( ctr = 0.h> main( ) { Enter 5 number : int num[5]. printf( “The highest noumber is %d”. gets(s). ave=sum/5. for ( ctr =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.&num [ctr] ). ctr. hn. 3 /* Arrange the number in ascending order */ /ard Enter 5 numbers : 14352 The numbers in ascending order 12345 1 . hn=0. getch( ).ctr>=0. printf( “\nThe average is %d”. ave. 1 /** Prints the reverse of the entered string **/ Enter a String : # Include <stdio. printf(“Enter a String :”). } Sample Program No. for (ctr=x. ave ). hn ).ctr++ ) sum=sum+num[ ctr ]. for ( ctr =0.sum=0. ctr. x. clrscr(). getch().ctr< =4. 22334 clrscr( ). } Sample Program No.ctr<=4. The highest noumber is 4 printf( “Enter 5 numbers :\n” ).ctr++ ) scanf(“%d”. Int x.ctr< =4. 2 /* Program that determines the highest number and average of 5 numbers*/ # include <stdio.

ctr1++ ) scanf( “%d”. } printf( “The numbers in ascending order \n” ). } printf(“%d appears %d time(s)\n”. 13231 clrscr(). else if ( num[x]>=0 ) { for ( y=x+1.&num[ctr]). if ((x==4)&&(num[x]>=0)) printf(“%d appears %d time(s)\n”.ctr2++ ) if ( num[ctr1] > num[ctr2] ) { temp = num[ctr1]. getch( ). for (x=0. ctr+=1. for ( ctr1=0. ctr1.&num[ctr1] ). 3 appears 2 time(s) for ( ctr=0.ctr1++ ) for ( ctr2=ctr1+1. 4 /* Display the number of times a number appeared */ #include <stdio.h> main( ) { int num[ 5 ]. num[ctr1] = num[ctr2]. num[x].x<=4.ctr1++ ) printf( “%d”.y++) if ( num[x] == num[y] ) { num[y]=-1.ctr1<=4.x++) { ctr=1.C Language Notes 201 0 #include <stdio.y<=4.ctr2<=4. num[ctr2] = temp. num[x].ctr). for ( ctr1=0. } /ard 1 . ctr2.h> main() { Enter 5 numbers : int num[5]. x. ctr. temp.num[ctr1] ). } Sample Program No. ctr++) 2 appears 1 time(s) scanf(“%d”. The numbers are : 1 appears 2 time(s) printf(“Enter 5 numbers :\n”). for ( ctr1=0.ctr1<=4.ctr1<=3. printf(“The numbers are :\n”). clrscr(). ctr<=4. ctr). y. printf( “Enter 5 numbers :\n” ).

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

strcmpi( ) syntax: strcmpi(str1.a_col[3].x++) { gotoxy(cols.8). main() { clrscr(). } for(y=0.a_row[3].&num[x][y]). } String Functions Enter Number in 3 x 3 matrix: 123 121 123 646 367 10. cols+=3.rows++. } cols=5. for(x=0.y<=2. cols=5.printf(“%d”. rows.rows).str2). scanf(“%d”.h> int num [3][3].cnt).x++) { a_row[y]=a_row[y] + num [y][x]. } getch().y++) for (x=0. cols+=3.C Language Notes 201 0 Sample Program No. gotoxy(cols.x<=2. /ard 1 .x<=2.x++) { gotoxy(cols.  compare str1 to str2 Ignoring case. stmcpy( ) syntax: strcpy(str1. for(x=0.str2. 11.printf(“%d”.a_row[x]).3 /*Sum of each row and column*/ #include<stdio.a_col[x]). a_col[x]=a_col[x] + num [y][x]. printf(“Enter Number in 3 x 3 matrix :”). int cols.x<=2.y.x. } cols=5. rows=2.7).

chr)  sets all characters in str1 to chr.’chr’. /ard 1 .”HELLO”).’A’. Syntax: type*variable │ └—―> name of a pointer variable Examples: char*p.cnt) example strcpy(str1. strnset(str1. output: EEEEE 13. strcpy(str1.3).4). int*temp.str1.’E’).*start.C Language Notes 201 0 example strcpy(str1. example strcpy(str1. “HELLO”). output AAALO FINAL QUIZ # 1 Pointers ► is a variable that holds a memory address of another object. strset( ) syntax: strset(str1. output: Hell 12.”Hello”). strnset( ) syntax: strnset(str1. strset(str1.

val = *count_addr. It is unary operator that returns the value of the variable located at the address that follows. clrscr(). “val receives the value at address count_addr” Sample Program No. val =*count_addr. x-=1. count_addr =&count. val. * & ► is a unary operator that return the memory address of its operand. count=100. val = 100. *z-=1.h> print(int *w.C Language Notes 201 0 Pointer Operators &. 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 . ex. count. y+=3. int x. count_addr= &count. printf(“%d”.h> main( ) { int *count_addr. int *z) { *w+=2. “count_addr receives the address of count”. getch().val). ex. } 100 Sample Program No. 2 /*Program Simulation */ #include<stdio.1 /*Program Simulation*/ #include<stdio. address 2000 2001 2002 variable count value 100 * ► is the complement of &.

C Language Notes 201 0 printf(“%d %d %d %d\n”. y.x. printf(“%d %d %d %d\n”. printf(“%d %d %d %d\n”. printf(“%d %d %d %d\n”. getch( ).*z). trick(x.w.z).y.&y).y. w. 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 1 .x. &z). trick(y. ++*x. trick(x. --*z. x=12. 3 /*Program Simulation */ #include”stdio. &w).z. int y.h” void trick(int w.w. &x). clrscr( ). int *z) { printf(“\n%d %d %d %d”.x. x=8. ++*x.y.w.y. &z). trick(x. &x.w. &z). w. w.z. *w.x. 4 /*Program Simulation*/ #include “stdio.y. trick(w. y.y. z.y. y.&z). printf(“%d %d %d %d\n”. y.&x).h” void trick(int w. &w. } main( ) { int w=5.w.z.w. printf(“%d %d %d %d\n”. w. } Sample Program No. trick(x.x). int *z) { printf(“\n%d %d %d %d”. *x. w. int y. &y.z). *x. z=16. print(&w. y=13. } main( ) { int w=10. *z).z). print(&y. &w). w.x. int *x. } Sample Program No. z=18. z. &y. clrscr( ). &z). int *x. *z). &z.z). trick(x. &x. getch( ).--*z.x. &y. print(&z. y=14.

} Sample Program No. *y= temp. *x= *y. y. trick(x. &y). x=10. } main( ) { int x= 5. &x. &z). temp= *x. x. y. %d %d\n”. z).6 /*Program Simulation*/ #include <stdio.h> swap(int*x. &y. printf(“%d swap(&z. z). z. y. &y. &w). 5 / * Program Simulation * / # include”stdio. x. w. y. z). %d %d\n”. z. &z). x. w. z). &y).h” swap(int *x. &w. z=14. int*y) { /ard 6 10 4 10 6 4 10 4 6 10 6 4 6 10 4 1 . y=12. trick(y. &z). printf(“%d swap(&x. clrscr( ). x. int *y) { int temp. x. &x. swap(&z. y. %d %d\n”. trick(x. &w). trick(x. Sample Program No. %d %d\n”. getch( ). printf(“%d getch( ). trick(x. &x). &x). &y. z= 15. z). trick(x. &z). y. y. &x). printf(“%d swap(&y. clrscr( ). printf(“%d %d %d %d”. y= 12. x. trick(w. w. w.C Language Notes 201 0 } main( ) { int w=8. } 5 15 15 5 12 5 5 12 5 15 12 12 15 15 12 %d %d\n”. z). y. &z. printf(“%d swap(&y. &z).

clrscr().z).h” balik(int*a.z.5.&z).x. printf(“%d %d %d\n”.y. x.y.y. x=y=z=0.&x).z). } 240 644 Sample Program No. printf(“%d %d %d\n”. swap(&z. } Sample Program No.y. x. int b. swap(&x. swap(&y. balik(&z. printf(“%d %d %d\n”. } main() { int x=4. swap(&y. *y= temp.x.&y). printf(“%d %d %d\n”.7 /*Program Simulation*/ #include”stdio. x. getch().z).z). 8 /*Program Simulation*/ #include <stdio. getch(). printf(“%d %d %d\n”.z). clrscr(). } main() { int x. int*c) { int d.y. x. printf(“%d %d %d\n”.h> /ard 1 . temp = *x.z). *a=d. d=b+*c.&x). *x = *y. y=10.&y). balik(&x.y. d=2. *c=b+d.&x). x.z).C Language Notes 201 0 int temp. swap(&z. b=*a+d.&y).y. printf(“%d %d %d\n”. z=6.y.y.

x). int *c) { int d. providing a convenient means of keeping related information together. } STRUCTURE  is a collection of variables that are referenced under one name. sw= fill_a(&x. return(switch). &x. Structure elements > the variable that make up the structure. n-64 remains empty clrscr( ). } structure_variables. } else switch= 0. if(sw==0) printf(“%d remains empty”. switch. sw. y. } Fill Var X?: Y -64 is filled with 3 or main( ) { int x.C Language Notes 201 0 int fill_a(int *a. puts(“Fill Var X?:”). ans. z. char b. getch( ). Fill Var X?: char ans. … type elememt_nameN. if((b==’y’)&&(*a==0)) { *a= *c+3. else printf(“\n%d is filled with %d”. type element_name3. &x). switch= 1. Structure declaration: Struct Struct_type_name { type element_name1. ans= getche( ). type element_name2. x= y= z= 0. d= *c+*a. /ard 1 . &z).

tel_no).&p_data. (dot) operator.element_name. Syntax: struct_type_name.h” struct personal { char name [15]. Sample Program No. Example: struct personal { char name [15].tel_no=1234567 scanf(“%d”.  The declaration Id terminated by a semicolon because a structure definition is a statement. char bday[8]. }. printf(“%d”. int tel_no. Note: Either the structure type name struct_type_name or structure_variables may be omitted. but not both. int age. struct_type_name  structure name  Identified particular data structure and its type specifier. Example: p_data.p_data.1 #include”stdio. : 1234566 /ard 1 . Final Output: Entry screen Name : mary Age : 22 Birthday : 082977 Tel. structure_variables  are common separated list of variable names.No.tel_no).C Language Notes 201 0 Interpretation: struct  tells the compiler that a structure template is being defined. struct personal p_ _data Referencing Struct Element  Individual struct elements are referenced by using the .

4).total). float mid. printf(“MidLecGrade:”). printf(“Name :”).scanf(“%d”.flec*. struct grade grd. :”). flec.grd.name).scanf(“%s”. }while((ans==’Y’)II(ans==’Y”)).bday).flab*.fin = (grd. main() { clrscr().4).6). char b day[8].mid*.scanf(“%f”.h> struct grade { float mlec.&p_data.C Language Notes 201 0 int age.4) + (grd.scanf(“%d”. printf(“Birthday :”).flab).2f”. grd. printf(“Age :”). printf(“Tel. }.mlec*.scanf(“%s”.6) + (grd. grd.fin*. printf(“FinLabGrade:”).scanf(“%f”. ans = getch().tel_no).&grd.&p_data.scanf(“%f”.&p_data.mlec). flab.&p_data.flec).6) + (grd.mlag). grd.mlab*. mlab. printf(“MidLabGrade:”).No. fin. puts(“Entry screen\n). puts(“Entry screen\n”). puts(“\n\nEnter Another?”).&grd. printf(“FinLecGrade:”). main() { Final Output: Entry screen MidLecGrade : 90 MidLabGrade : 90 FinLecGrade : 85 FinLabGrade : 85 The total grade is 87.&grd.total=(grd. } Sample Program No.mid = (grd.scanf(“%f”. printf(“\nThe total grade is %. } struct personal p_data.age). float total.00 Enter Another? clrscr(). 2 #include<stdio. /ard 1 .&grd. int tel_no.

Example: Struct personal { char name [15]. struct personal p_data[5].1 Name Age Birthday Tel. int age: char bday[8]: int tel_no: }.ctr<=4.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. struct personal p_data[5]: Sample Program No. clrscr(). for(ctr=0. No.ctr++) { clrscr().h” struct personal { char name[15]: int age: char bday[8]: int tel_no: }. main() { int ctr. : Mary : 22 : 082977 : 1234567 #include “stdio. /ard 1 . puts(“Entry screen\n\n”).

bday). &p_data[ctr].scanf(“%s”. printf(“Birthday :”).scanf(“%s”.age).C Language Notes 201 0 printf(“Name :”).name). } } /ard 1 .scanf(“%s”.tel_no). printf(“Age :”). printf(“Tel.scanf(“%s”. No :”). &p_data[ctr]. &p_data[ctr]. &p_data[ctr].