Académique Documents
Professionnel Documents
Culture Documents
1.1
Introduction
C can be used for systems programming (e.g. write operating systems) and
applications programming (problem solving).
Example 1
A C program that reads radius of a circle, calculates the area and writes the result.
/* calculation of area of a circle */
Comment
#include <stdio.h>
main()
Function heading
{
float radius, area;
Variable declarations
printf(Radius = ? );
scanf(%f, &radius);
area = 3.14159*radius*radius;
printf(Area = %f \n, area);
system(pause);
Output statement
Input statement
Assignment statement
Output statement
Hold output screen
Chapter 2
C Fundamentals
2.1
C Character Set
Constants
Integer Constants
Floating-Point Constants
0.
0.2
2E-5
0.2e-4 1.5e+8
Invalid constants:
5
.789e10
1,234.0
5e+10.4
4E 12
2.4.3
comma is illegal
exponent must be integer
blank space not allowed
Character Constants
String Constants
2.5
A variable is an identifier used to represent a single data item. Data assigned to the
variable can be accessed by referring to the variable name.
e.g.
a = 2;
x = 10*y + z;
a, x, y and z are variables.
An array is an identifier referring to a collection of data items that all have the same
name.
e.g.
char letter[40] = Skudai; letter is a one-dimensional character array
Array size is 40 i.e. it can store a maximum of 40 characters.
letter[0] = S, letter[1]=k, letter[2]=u, letter[3]=d, letter[4]=a, letter[5]=i,
letter[6]=null-character \0.
An array element has a unique address known as subscript or index.
2.6
Declarations
A declaration associates a group of variables with a specific data type. All variables
must be declared. e.g.
int a, b, c;
float x1, x2;
char flag, text[80];
float a;
double b;
long float c;
Single precision values (magnitudes) vary from 3.4 x 10-38 to 3.4 x 1038 with 6
significant figures
Double precision values (magnitudes) vary from 1.7 x 10-308 to 1.7 x 10308 with
18 significant figures
could be written as
char[11] = California; array size is explicitly specified.
2.7
Expressions
i=i+1
Statements
Control statements are used for logical tests, loops and branches. e.g.
Symbolic constants
#define PI 3.142
#define TRUE 1
#define FRIEND Awang
PI = 3.142
TRUE = 1
FRIEND = Awang