Title: Comments in a program
1Comments in a program
comments
/ program listing with comments Author
John Dale / void main( ) Int Num1, Num2
// declaration of two integer Int Sum Sum
Num1 Num2 // performing the sum of
// Num1 and Num2
// and assign the result to Sum
comments
2preprocessor statements So far we have seen that
to produce a executable program. The program file
has to be compiled then linked.
Executable file
Source code file
preprocessor
compiler
linker
But Actually, before the compiling and the
linking stage, the source code is passed through
the preprocessor
the preprocessor executes preprocessing
statements stated in you source code program
3Preprocessing statements meant to make the
program easier to read, easier to modify, and
easier to transfer to different computer systems
Preprocessor statements are not like C commands,
they don't have semicolons at the end of each
line, and they all begin with the character.
define syntax define identifier
expression (or constants) define substitutes
each occurrence of idntifier by expression in
the program
define PI 3.14159 //the preprocessor
will replace every occurrence of PI by 3.14159
4preprocessor
define PI 3.14159 void main ( ) float
Radius, Area, Circumference Radius 50.5
Circumference 2PIRadius Area
PIRadiusRadius
void main ( ) float Radius, Area,
Circumference Radius 50.5 Circumference
2 3.14159Radius Area 3.14159
RadiusRadius
if you want to change the precision of PI in your
code, you have only to change one line
5Another Example
define MSG This a very
long..... Message Void main()
Printf(MSG) .. .. printf(MSG)
6The include statement syntax include
file_name include ltfile_namegt
The include preprocessor statement includes the
content of the file named file_name into your
program file
define PI 3.14159 defne Radius 2.5 void
main( ) float Area Area
PIRadiusRadius
CircleFile.cpp
Myprog.cpp
define PI 3.14159 defne Radius 2.5
include CircleFile void main( ) float
Area Area PIRadiusRadius
7Normally the command include is used with files
containing preprocessor statements, functions
and global variables. Such files are called
header files and usually their names ends with
extension .h e.g stdio.h
you can include more than one header file in your
program.
When the header file is in the same directory
than your program file, then include is used
with
When the header file is located in a standard
directory then include is used with lt gt
Example include ltstdio.hgt stdio.h is a
header file containing functions allowing writing
and reading data from the standard input/output
devices (keyboard and screen)
includeltmath.hgt maths.h is a header file
containing predefined functions that carry out
specific mathematical tasks.
8String Manipulation
A number of functions are available in C for
manipulating strings. These function are defined
in the standard library string.h
String length To find the length of a string (
not including the \0 c character at the end) ...
Length strlen(string)
includeltstdio.hgt includeltstring.hgt void main
( ) char Name Paul int Length
Length strlen(Name) printf(,Length) //
4 is printed
9Copying one string into another strcpy(string_dest
ination, string_source) copies the content of
string_source into string_destination the
string_destination should be large enough to
receive string_source
includeltstdio.hgt includeltstring.hgt void main
( ) char Name 20 char ClientName
David Dale strcpy(Name, ClientName)
printf(Name) // David Dale is printed
10Appending one string to another strcat(string_1,
string_2) concatenate the contents of string_2
on to the end of string_1
includeltstdio.hgt includeltstring.hgt void
main ( ) char Name 20 char FirstName
DAVID char LastName BROWN
strcpy(Name, FirstName) strcat(Name, LastName)
printf(Name) // DAVID BROWN is
printed
11Write a program that reads separately your name
and surname and put them into a single string
which has to be displayed
includeltstdio.hgt includeltstring.hgt define
SIZE 20 void main ( ) char Name SIZE
char FirstName SIZE char LastName SIZE
printf (Type your first name )
gets(FirstName) printf (Type your last name
) gest(LastName) strcpy(Name, FirstName)
strcat(Name, LastName) printf(Name) //
DAVID BROWN
12Structure
Name John Surname Dale Age 36 Weight 75
Kg Height 1.75 m
This is some information data about an
individual All the data is related to a single
person Rather then associating a variable for
each data, we want to encapsulate all the data
into a single variable. The variable tyoe should
also permit access to each data
Can we use arrays ?
No, because the elements of an array should be
all of the same type, which is not the case here.
C provides an appropriate aggregate type that can
hold together data of different type this
aggregate type is called structure
137.4.1 Structure definition
A structure is a composite of components which
are distinct and that can be of different type.
Each component has a name which is used to select
it from the structure. A structure is a
convenient way of grouping several pieces of
related information together
There are several ways to define a structure.The
more usual one is to use a typedef statement,
located after the include and the define
preprocessing statement.
include define typedef // define
structure here void main ( )
14Example of a structure definition
Name John Surname Dale Age 36 Weight 75
Kg Height 1.75 m
typedef struct char Name20 char
SurName20 int Age float Weight
float Height Person
Using typedef, we have defined a new structure
called Person. Name, SurName, Age, Weight and
Height are the members of the structure
Person Person can be considered as a new type.
That means that we declare and use variables of
type Person
Person Player
Player is variable of type Person, The variable
Player will therefore have the members Name,
SurName.Height
157.4.2 Accessing and using member of the
structure To refer to one member of structured
variable, we use the following
syntax variable_name. member_name where
variable_name is the name of the structured
variable member_name is the name of
the member
includeltstring.hgt typedef struct char
Name20 char SurName20 int Age
float Weight float Height Person
Person Player strcpy(Player.Name ,John)
strcpy(Player.Surname ,Dale) Player.Age 22
Player.Weight 70 Player.Height 1.75
16Exercise define a structure called Date
associated to the date, where the members are
day, month and year. Assign the current date to a
variable of type Date and display the current
date.
includeltstdio.hgt typedef struct int Day
char Month10 int Year Date
main ( ) Date Today Today.Day
28 strcpy(Today.Month , November ) Today.Year
2000 printf(Today\s date is d s d,
Today.Day, Today.Month, Today.Year)
17 Array of Structures You can define arrays which
elements are of structure type
define NAMEMAX 20 typedef struct
// definition of a
structure called Student char NameNAMEMAX
// and having Name and Surname as char
SurnameNAMEMAX // members Student
Define an array called Class, where each element
is defined to be of type Student
Student Class100 // denotes an array of 100
element of type Student int i 3 Student i
// denotes the 4th element of the array a
variable of type Student Student i .Name //
denotes a string of character which may be
assigned, compared and
output using the usual string operators, Student
i .Name0 // denotes a single character
value
18How to initialize array elements ?
Class 0 .Name John
Class 0 .Surname Dale
Class 1 .Name David
Class 1 .Surname Brown
Printing the names of the 10 first students in
the array
int i for (i0 ilt 10 i) printf(Name s
\n Surname s \n,Name, Surname )
19Nested structures It is possible to define
structures that contain another structure as a
member. Ex
define NAMEMAX 20 typedef struct int Day
char Month10 int Year Date
typedef struct
char NameNAMEMAX char
SurnameNAMEMAX Date
DateofBirth // DateofBirth is a memner of type
Date Student
20(No Transcript)