Appendix A
Quick Guide to C
/* Start of multiple line comment
Quick Quide to C (up to loops)
*/
// start of a single line comment
// Preprocessor Directives before the main function
#include <stdio.h>
#include <conio.h>
#include <math.h> // to use sin(), cos(), sqrt(), pow(), exp(), log() ...
#include <ctype.h> // to use toupper(), …
#define PI 3.1416
// main function (a simple skeleton) // Declaration of variables
void main (void) int age;
{ // Declare variables int num, size;
// Input float weight, price;
// Calculation char letter, key;
// Output
} // end main
// Use printf( ) and scanf( ) to prompt for input
printf("Enter an integer: ");
scanf("%d", &num); // use address operator '&'
printf("Enter 2 real numbers: ");
scanf("%f %f", &x, &y); // use , to separate addresses
// Use printf( ) to output messages and values
printf("A simple program to find area\n"); // use double quotations " "
printf("%d years old = %d days old.\n" , age , day); // take care of , and ;
printf("The floating-point number is %f\n", x );
printf("The weight correct to 2 decimal places is %.2f kg\n", weight );
// Assignment statements and arithmetic operators
half = 0.5; // do not use 1/2
oneThird = 1.0/3;
sum = x+y;
product = x*y*z ;
xSquare = x*x ;
radian = x*PI/180;
quotient = 9/5; // integer division yields 1
remainder = 9%5; // modulus division yields 4
realNumber = 9.0/5; // floating point division yields correct value 1.8
z = x*y/(x + y) ; //(product of x and y) divided by (sum of x and y)
w = (x+y)/(x*y) ; //(sum of x and y) divided by (product of x and y)
// math functions must have arguments enclosed in brackets ( )
y = 0.5 * a * b * sin(x); // x is in radian.
y = 0.5 * a * b * sin(x*PI/180); // x is in degree
cosineSquare = cos(x)*cos(x); // x is in radian.
xCube = pow(x, 3) ; // x is raised to the power of 3
area = sqrt( s*(s-a)*(s-b)*(s-c) ); // use * for multiplication
m = exp( (n-1)/n * log(p) ) ;
COMPRO COMMON TEST Page 1/ 2
// if-else may be used with simple conditions like: (x > y) (x < y ) (x = = y) (x >= y) (x <= y) (x != y)
if ( x > y ) // cannot end with ;
{
printf("%d is greater than %d\n", x, y);
} // end if
else
{
printf("%d is less than or equal to %d\n", x, y);
} // end else
// if-else may be used with compound // if-else may be used with compound
conditions conditions
// Ex (1) : use AND logic operator && // Ex (2) : use OR logic operator ||
if (angle>0 && angle<180) if (angle<=0 || angle>=180)
{ {
} }
else else
{ {
} }
// Use of while loop // Use of while loop
// Ex (1): Print "Hello" on 3 lines // Ex (2): Print all integers from 1 to 10
i = 1; i = 1;
while ( i <= 3 ) while ( i <= 10 )
{ {
printf("Hello\n"); printf("%5d\n", i );
i = i + 1; i = i + 1;
} // end while } // end while
// Use of for loop // Use of for loop
// Ex (1): Print "Hello" on 3 lines // Ex (2): Print all integers from 1 to 10
for ( i = 1; i <= 3; i = i+1 ) for ( i = 1; i <= 10; i = i+1 )
{ {
printf("Hello\n"); printf("%5d\n", i );
} // end for } // end for
COMPRO COMMON TEST Page 2/ 2