Académique Documents
Professionnel Documents
Culture Documents
Lecture – 3
C program structure
These program components will be discussed in much greater detail later in this book. For now, the
reader should be concerned only with an overview of the basic features that characterize most C programs.
EXAMPLE 1.6 Area of a Circle Here is an elementary C program that reads in the radius of a circle, calculates its
area and then writes the calculated result.
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * / / * T I T L E (COMMENT) * /
#include <stdio.h> / * LIBRARY F I L E ACCESS * /
main ( ) / * FUNCTION HEADING * /
C program structure
These program components will be discussed in much greater detail later in this book. For now, the
reader should be concerned only with an overview of the basic features that characterize most C programs.
EXAMPLE 1.6 Area of a Circle Here is an elementary C program that reads in the radius of a circle, calculates its
Top comment is usually
area and then used
writes the to identify
calculated result.the purpose of the program
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * / / * T I T L E (COMMENT) * /
#include <stdio.h> / * LIBRARY F I L E ACCESS * /
main ( ) / * FUNCTION HEADING * /
The comments at the end of each line have been added in order to emphasize the overall program organization.
Normally a C program will not look like this. Rather, it might appear as shown below.
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * /
Comments (remarks) may appear anywhere within a program, as long as they are placed within the
delimiters / * and * / (e.g., / * t h i s i s a comment */). Such comments are helpful in identifying the
program's principal features or in explaining the underlying logic of various program features.
C program structure
These program components will be discussed in much greater detail later in this book. For now, the
reader should be concerned only with an overview of the basic features that characterize most C programs.
EXAMPLE 1.6 Area of a Circle Here is an elementary C program that reads in the radius of a circle, calculates its
Top comment is usually
area and then used
writes the to identify
calculated result.the purpose of the program
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * / / * T I T L E (COMMENT) * /
#include <stdio.h> / * LIBRARY F I L E ACCESS * /
main ( ) / * FUNCTION HEADING * /
The comments at the end of each line have been added in order to emphasize the overall program organization.
Normally a C program will not look like this. Rather, it might appear as shown below.
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * /
Comments (remarks) may appear anywhere within a program, as long as they are placed within the
delimiters / * and * / (e.g., / * t h i s i s a comment */). Such comments are helpful in identifying the
program's principal features or in explaining the underlying logic of various program features.
C program structure
These program components will be discussed in much greater detail later in this book. For now, the
reader should be concerned only with an overview of the basic features that characterize most C programs.
EXAMPLE 1.6 Area of a Circle Here is an elementary C program that reads in the radius of a circle, calculates its
Top comment is usually
area and then used
writes the to identify
calculated result.the purpose of the program
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * / / * T I T L E (COMMENT) * /
#include <stdio.h> / * LIBRARY F I L E ACCESS * /
main ( ) / * FUNCTION HEADING * /
The comments at the end of each line have been added in order to emphasize the overall program organization.
Normally a C program will not look like this. Rather, it might appear as shown below.
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * /
Comments (remarks) may appear anywhere within a program, as long as they are placed within the
delimiters / * and * / (e.g., / * t h i s i s a comment */). Such comments are helpful in identifying the
program's principal features or in explaining the underlying logic of various program features.
C program structure
These program components will be discussed in much greater detail later in this book. For now, the
reader should be concerned only with an overview of the basic features that characterize most C programs.
EXAMPLE 1.6 Area of a Circle Here is an elementary C program that reads in the radius of a circle, calculates its
Top comment is usually
area and then used
writes the to identify
calculated result.the purpose of the program
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * / / * T I T L E (COMMENT) * /
#include <stdio.h> / * LIBRARY F I L E ACCESS * /
main ( ) / * FUNCTION HEADING * /
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * /
Comments (remarks) may appear anywhere within a program, as long as they are placed within the
delimiters / * and * / (e.g., / * t h i s i s a comment */). Such comments are helpful in identifying the
program's principal features or in explaining the underlying logic of various program features.
C program structure
These program components will be discussed in much greater detail later in this book. For now, the
reader should be concerned only with an overview of the basic features that characterize most C programs.
EXAMPLE 1.6 Area of a Circle Here is an elementary C program that reads in the radius of a circle, calculates its
area and then writes the calculated result.
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * / / * T I T L E (COMMENT) * /
#include <stdio.h> / * LIBRARY F I L E ACCESS * /
main ( ) / * FUNCTION HEADING * /
The comments at the end of each line have been added in order to emphasize the overall program organization.
Normally a C program will not look like this. Rather, it might appear as shown below.
/ * program t o c a l c u l a t e t h e a r e a o f a c i r c l e * /
C program structure
#include <stdio.h>
void main()
{
printf ("Hello, World! \n") ; Output statement
}
C program structure
#include <stdio.h>
void main()
{ A variable
int num ;
Input statement
scanf ("%d", &num) ;
printf (“No. of students is %d\n”, num) ; Output statement
}
C character set
• The C language alphabet
• Uppercase letters ‘A’ to ‘Z’
• Lowercase letters ‘a’ to ‘z’
• Digits ‘0’ to ‘9’
• Certain special characters:
! # % ^ & * ( )
- _ + = ~ [ ] \
| ; : ‘ “ { } ,
. < > / ? blank
• A C program should not contain anything else
Variables
• Very important concept for programming
• An entity that has a value and is known to the program by a
name
• Can store any temporary result while executing a program
• Can have only one value assigned to it at any given time
during the execution of the program
• The value of a variable can be changed during the execution
of the program
Variables
• Variables stored in memory
• Remember that memory is a list of storage locations, each
having a unique address
• A variable is like a bin
• The contents of the bin is the value of the variable
• The variable name is used to refer to the value of the variable
• A variable is mapped to a location of the memory, called its
address
Example
#include <stdio.h>
void main( )
{
int x;
int y;
x=1;
y=3;
printf("x = %d, y= %d\n", x, y);
}
Variables in memory
X = X*5
Variables in memory
X = X*5
Variables in memory
X = X*5
Variables in memory
X = X*5
Variables in memory
X = 20
X
Y=15 20
X = Y+3 ? Y
Y=X/6
Variables in memory
X = 20
X
Y=15 20
X = Y+3 15 Y
Y=X/6
Variables in memory
X = 20
X
Y=15 18
X = Y+3 15 Y
Y=X/6
Variables in memory
X = 20
X
Y=15 18
X = Y+3 3 Y
Y=X/6
Data types
• Each variable has a type, indicates what type of values the variable
can hold
• Four common data types in C
• int - can store integers (usually 4 bytes)
• float - can store single-precision floating point numbers (usually 4 bytes)
• double - can store double-precision floating point numbers (usually 8 bytes)
• char - can store a character (1 byte)
Data types
• Must declare a variable (specify its type and name) before
using it anywhere in your program
• All variable declarations should be at the beginning of the
main() or other functions
• A value can also be assigned to a variable at the time the
variable is declared
int speed = 30;
char flag = ‘y’;
Variable names
• Sequence of letters and digits
• First character must be a letter or ‘_’
• No special characters other than ‘_’
• No blank in between
• Names are case-sensitive (max and Max are two different names)
• Examples of valid names:
• i rank1 MAX max Min class_rank
• Examples of invalid names:
• a’s fact rec 2sqroot class,rank
Variable names
• Sequence of letters • Valid identifiers • Invalid identifiers
and digits X 10abc
• First character must be abc my-name
a letter or ‘_’ simple_interest “hello”
a123
• No special characters simple interest
LIST
other than ‘_’ (area)
stud_name
• No blank in between Empl_1 %rate
• Names are case- Empl_2
sensitive avg_empl_salary
The lecture slides can be found in the following location!