Vous êtes sur la page 1sur 31

CSE 203G: Introduction to C

Lecture – 3

Course Teacher: Dr. Md Sadek Ferdous


Assistant Professor, CSE, SUST
E-mail: ripul.bd@gmail.com
Outline
• C program structure
• First C programs
• Variables
C program structure
• Every C program consists of one or more modules called functions
• One of the functions must be called main
• The main function is the entry point in the every C program
• The program will always begin by executing the main function, which may
access other functions
• Any other function definitions must be defined separately, either
ahead of or after main
• In later lectures we will explain what does define mean!
C program structure
• Each function must contain:
• A function heading, which consists of the function name, followed by
an optional list of arguments, enclosed in parentheses -> ()
• The arguments are symbols that represent information being passed
between the function and other parts of the program
• Arguments are also referred to as parameters
C program structure
• Each function must contain:
• A compound statement, which comprises the remainder of the
function
• Each compound statement is enclosed within a pair of braces, i.e., { }
• Within these braces
• there are (expression) statements (instructions) for declaration, assignment,
condition check, looping etc.
• and other compound statements. That is compound statements may be
nested, one within another
• Each expression statement must end with a semicolon (; )
• Statements are executed one by one
C program structure
• A C program might also contain comments
• Comments (remarks) may appear anywhere within a program, as long
as they are placed within the delimiters /* and */ (e.g., /* this is a
comment */)
• Such comments are helpful in identifying the program's principal
features or in explaining the underlying logic of various program
features
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 * /

f l o a t radius, area; / * VARIABLE DECLARATIONS * /


p r i n t f ("Radius = ? 'I); /* OUTPUT STATEMENT (PROMPT) * I
scanf ( "%f & r a d i u s ) ;
'I, I* INPUT STATEMENT * /
area = 3.14159 * radius * radius; /* ASSIGNMENT STATEMENT * /
p r i n t f ( " A r e a = % f " ,a r e a ) ; /* OUTPUT STATEMENT * /
1
• A C program is typed in lowercase or uppercase. Remember, C is case sensitive
The comments at the end of each line have been added in order to emphasize the overall program organization.
• It isNormally
customary to type ordinary statements in lowercase
a C program will not look like this. Rather, it might appear as shown below.
• Most comments are in lowercase
• Uppercase
/ * programis tonly
o c a lused
c u l a t etot hemphasise
e a r e a o f a something
circle */
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 * /

f l o a t radius, area; / * VARIABLE DECLARATIONS * /


p r i n t f ("Radius = ? 'I); /* OUTPUT STATEMENT (PROMPT) * I
scanf ( "%f & r a d i u s ) ;
'I, I* INPUT STATEMENT * /
area = 3.14159 * radius * radius; /* ASSIGNMENT STATEMENT * /
p r i n t f ( " A r e a = % f " ,a r e a ) ; /* OUTPUT STATEMENT * /
1

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 * /

f l o a t radius, area; / * VARIABLE DECLARATIONS * /


Main p r i n t f ("Radius = ? 'I); /* OUTPUT STATEMENT (PROMPT) * I
function scanf ( "%f & r a d i u s ) ;
'I, I* INPUT STATEMENT * /
area = 3.14159 * radius * radius; /* ASSIGNMENT STATEMENT * /
p r i n t f ( " A r e a = % f " ,a r e a ) ; /* OUTPUT STATEMENT * /
1

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 * /

f l o a t radius, area; / * VARIABLE DECLARATIONS * /

Compound p r i n t f ("Radius = ? 'I); /* OUTPUT STATEMENT (PROMPT) * I


scanf ( "%f & r a d i u s ) ; I* INPUT STATEMENT * /
Statement 'I,

area = 3.14159 * radius * radius; /* ASSIGNMENT STATEMENT * /


p r i n t f ( " A r e a = % f " ,a r e a ) ; /* OUTPUT STATEMENT * /
1

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 * /

f l o a t radius, area; / * VARIABLE DECLARATIONS * /

Compound p r i n t f ("Radius = ? 'I); /* OUTPUT STATEMENT (PROMPT) * I


scanf ( "%f & r a d i u s ) ; I* INPUT STATEMENT * /
Statement 'I,

area = 3.14159 * radius * radius; /* ASSIGNMENT STATEMENT * /


p r i n t f ( " A r e a = % f " ,a r e a ) ; /* OUTPUT STATEMENT * /
1
printf, scanf -> are special functions, called library functions with
The comments at the end of each line have been added in order to emphasize the overall program organization.
Normally a C program will everything inside
not look like this. () are
Rather, parameters
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
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 * /

f l o a t radius, area; / * VARIABLE DECLARATIONS * /


p r i n t f ("Radius = ? 'I); /* OUTPUT STATEMENT (PROMPT) * I
scanf ( "%f & r a d i u s ) ;
'I, I* INPUT STATEMENT * /
area = 3.14159 * radius * radius; /* ASSIGNMENT STATEMENT * /
p r i n t f ( " A r e a = % f " ,a r e a ) ; /* OUTPUT STATEMENT * /
1

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

Instruction executed Memory location allocated


to a variable X
X = 10
T
i X = 20 10
m
e
X = X +1

X = X*5
Variables in memory

Instruction executed Memory location allocated


to a variable X
X = 10
T
i X = 20 20
m
e
X = X +1

X = X*5
Variables in memory

Instruction executed Memory location allocated


to a variable X
X = 10
T
i X = 20 21
m
e
X = X +1

X = X*5
Variables in memory

Instruction executed Memory location allocated


to a variable X
X = 10
T
i X = 20 105
m
e
X = X +1

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!

Vous aimerez peut-être aussi