Professional Documents
Culture Documents
C Programming
Language
Syllabus
E.g.: Constant
E.g.:
int fahr, celcius;
int lower, upper, step;
The Program Structure
Includes declarations for the
standard input/output library
of procedures.
Read: Hash-include
Comment
Pre-processor*
Declaration
Keywords
Statements
Comments
Pre-Processor Directive
Preprocessor
Analyze source file before compiling
Process pre-processor directive
Remove comments
Divide program into tokens
conio.h ????
Comment Special Character
\n newline
\t tab
\r carriage return
\f form feed
\v vertical tab
\b backspace
\ double quote (\ acts as an escape mark)
\nnn octal character value
Exercise 1
2) My
First
Program C
* Using 1 command printf*
Identifiers
Identifiers are used to name :
Constant
Variables
Function names
Labels
General form :
<data_type> <variable_name>
e.g : int a;
e.g : int a,b,c;
Data Types
2 types:
Example: A , *, z
Syntax: char z
Exercise 3
? total -12
? Name Pdaus
? Wide 3.4E+38
? Radius 1.7E+308
? Age 12
Declaring of Constant
Values cannot be changed during execution of
program
Type of constants:
character, integer, floating point, string
General form:
const <data_type> <variable>
#define <variable>
const double PI
#define Nilai
Keywords
Reserved word
Predefined by compiler libraries
Cannot be used as identifiers
Must be written in lowercase letters
Instruction set
Reserved Words
Identifiers which have standard, predefined meaning
Sequence of instructions
Branching or looping or Function calls
Start with a command word and ends
with ;
Compounded by curly brackets { }
Comments
Pre-process
/* converts distance from miles to kilometres */ Constant
directive
# include <stdio.h> macro
# include <math.h>
Main function # define KMS_PER_MILE 1.609 Reserved
main ( ) word
Start program {
float miles;
printf (enter the distance in miles);
Start scanf (%f, &miles);
identifier kms=KMS_PER_MILE * miles;
printf (That equals %f kilometres \n, kms);
getch();
}
End program
C Operators
x+=3.0 x=x+3.0;
voltage/= sqrt(2); voltage=voltage/sqrt(2);
i-- i=i 1
i++ i=i + 1
(i is incremented AFTER I has been used)
++i i incremented before i is used
--i i decrease before i is used
Relational Operators
C Operator Meaning
< less than
> greater than
=< less than or equal to
=> greater than or equal to
Examples:
C Operator Meaning
&& Logical AND
|| Logical OR
! Logical negation
Logical Operators
For logical AND (&&)
Function Description
sqrt (x) Square root of
exp (x) Exponential function ex
log (x) Natural logarithm of x
abs (x) Absolute value of x
pow (x) X raised to power y (xy)
sin (x) Trigonometric sine of x (in radian)
cos (x) Trigonometric cose of x (in radian)
Order in Expressions
B - Brackets
O - Order (exponents)
D - Division
M - Multiplication
A - Addition
S - Subtraction
1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
Example
The following example illustrates the two methods for variable initialization:
#include <stdio.h>
main ( ) {
int sum=33;
float money=44.12;
char letter;
double pressure;
letter=E ; /* assign character value */
pressure=2.01e-10; /*assign double value */
printf(value of sum is %d\n,sum);
printf(value of money is %f\n, money);
printf(value of letter is %c\n, letter);
printf(value of pressure is %e\n, pressure);
}