Vous êtes sur la page 1sur 6

Chapter 1

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>

Library file access

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

Valid characters are:


Lowercase a to z, uppercase A to Z, digits 0 to 9, and special characters
+ - * / = % & # ! ? ^ ~ \ | < > ( ) [ ] { } : ; . , - (blank space)
Most compilers accept @ and $
Escape sequences (e.g. \b, \n, \t) represent a single character
2.2

Identifiers and Keywords

Identifiers are names given to variables, functions and arrays.


Identifiers consist of letters and digits BUT first character MUST be a letter.
Uppercase and lowercase letters accepted common to use lowercase.
Case sensitive lowercase is not equivalent to uppercase (e.g. ABC is
different from abc).

Underscore character (considered a letter) is usually included in the middle of


an identifier (e.g. tax_rate, area_of_circle).
Identifier may begin with an underscore (e.g. _temperature, _area), but is
rarely done in practice.
Identifiers can vary from 8 to 31 (typical) characters in length.
Go for shortest identifier length.

Keywords or reserved words have standard, predefined meanings. They cannot be


used as programmer-defined identifiers.
e.g.
auto extern sizeof else int
float while void etc.
keywords are all lowercase.
2.3
Data types
There are 4 basic data types:
int
integer quantity
char
single character
float
floating-point number (with decimal point and/or exponent)
double
double-precision floating-point number
2.4

Constants

There 4 constants: integer constants, floating-point constants, character constants and


string constants.
For integer and floating-point constants (numeric-type) these rules apply:
No commas and blank spaces within the constants
Constant can be preceded by a minus (-) sign
The value cannot exceed specified minimum and maximum bounds
2.4.1

Integer Constants

Made up of integer-valued numbers; any combination of digits from 0 to 9.


Example of invalid decimal integer constants:
12,400
comma is not allowed
30.5
decimal point not allowed
10 200
blank space not allowed
124-900
hyphen is illegal
0123
first digit cannot be 0
2.4.2

Floating-Point Constants

Base-10 number with either a decimal point and/or exponent.


Valid constants:

0.

0.2

2E-5

0.2e-4 1.5e+8

Invalid constants:
5

requires decimal point or exponent

.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

A character constant is a single character, enclosed in apostrophes (single quotation


marks). Example:
B

Character constants have integer values refer to ASCII character set.


A
65
x
120
3
51
?
63

32
Escape sequences begin with a backslash (\) followed by one or more special
characters. Example:
Escape sequence
ASCII value
backspace
\b
008
horizontal tab
\t
009
vertical tab
\v
011
new line
\n
010
quotation mark
\
034
apostrophe
\
039
backslash
\\
092
null
\0
000
\ 0 is the null character used to indicate end of string. Escape sequences are
viewed as single characters.
2.4.4

String Constants

Consist of any number of consecutive characters (including none), enclosed in


(double) quotation marks. Example
green
123-900
$1.99
2*i/j

To display this string on the computer screen :


To continue, press the RETURN key
The command is
printf(\tTo continue, press the \RETURN\ key\n);
with special escape sequences \t \ and \n

a null character \0 is automatically placed at the end of every string constant


which is not visible when the string is displayed.

2.5

a character constant is not equivalent to a single-character string constant. (e.g.


A is nor equal to A).
a single-character string constant has ASCII value.
Variables and Arrays

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;

a, b and c declared as integer variables


x1, x2 declared as floating-point variables
flag is char-type variable, text is an 80-element chartype array.

a is floating-point single precision variable


b is floating-point double precision variable
c is floating-point double precision variable

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

Initial values can be assigned to declared variables. e.g.


int c = 12;
c is integer variable, initial value 12
char star=*; star is char-type variable, initial value character *
double y = 0.2e-6; y is double-precision variable, initial value 0.2 x 10-6.
char text[ ] = Califonia; text is an 11-element char-type array. Array size
determined by the compiler as 11.

could be written as
char[11] = California; array size is explicitly specified.
2.7

Expressions

An expression represents a single data item or combination of data items


interconnected by one or more operators. e.g.
a+b
x=y
x<y
x==y
+ +i
2.8

i=i+1

Statements

A statement causes the computer to take action. Three classes of statements


expression statements, compound statements and control statements.

An expression statement consists of an expression followed by a semicolon


a = 10;
c = a + b;
++i;
;

nothing is done, a null statement

A compound statement is made of several statements enclosed within {


It does not end with a semicolon. e.g.
{
pi = 3.13;
circ = 2*pi*radius;
area = pi*radius*radius:
}

Control statements are used for logical tests, loops and branches. e.g.

while (count <= n) {


scanf(%f, &x);
sum += x;
+ +count;
}
2.9

Symbolic constants

A symbolic constant is a name typed in CAPITAL that represents a numeric constant,


a character constant or a string constant. e.g.

#define PI 3.142
#define TRUE 1
#define FRIEND Awang

PI = 3.142
TRUE = 1
FRIEND = Awang

Usually defined at the beginning of a program.

Vous aimerez peut-être aussi