C Languagenotes
C Languagenotes
C Languagenotes
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
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
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
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
#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
/ard 1
/ard 1
/ard 1
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
/ard 1
/ard 1
factoria
/ard 1
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 8 6 4 2 8 6 4 2 6 4 2 4 2 2
/ard 1
JACK**_AND_JILL_
/ard 1
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
/ard 1
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
/ard 1
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
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
/ard
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
Final Output:
/ard 1
10. strcmpi( ) syntax: strcmpi(str1,str2); compare str1 to str2 Ignoring case. 11. stmcpy( ) syntax: strcpy(str1,str2,cnt);
/ard 1
/ard 1
* 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
/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
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);
/ard
6 10 4 10 6 4 10 4 6 10 6 4 6 10 4
/ard 1
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
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:
/ard
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
#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
/ard 1