Prepared By: Teh Faradilla Abdul Rahman: Student Learning Time Hours 2 Test 1 Project 3 Assignment 2
Prepared By: Teh Faradilla Abdul Rahman: Student Learning Time Hours 2 Test 1 Project 3 Assignment 2
Prepared By: Teh Faradilla Abdul Rahman: Student Learning Time Hours 2 Test 1 Project 3 Assignment 2
Introduction to C Programming
PART ONE
CSC 099
LEARNING OUTCOMES
At the end of this lesson, students should be
able to
1. illustrate the basic structure of C program
Program
Output
Structure of C Program
/*Written by: Nurul Syatrah*/
Comments
//This program is to calculate the area of a cylinder
Area_cylinder = 2*pi*radius*height;
return 0;
Return statement
}
Structure of C Program
6
Comments
Two format
Line comment
Marked by two slashes (//) at the beginning of comment
For short comment in a single line
Example: //Written by : Rosmiza Wahida
Block comment
Marked by opening token(/*) and closing token (*/)
For long comment
Example : /*This program is to calculate the area of a cylinder*/
Comments CANNOT be nested (comments inside comments)
Use : to document programs and improve program readability
Help other people read and understand others program
IGNORED by C compiler and DO NOT cause any machine-language object code to be generated.
DO NOT cause the computer to perform any action when the program is run.
CAN be put ANYWHERE in the program
Structure of C Program
7
Preprocessor Directive
Will be read first before the program is compiled
Indicated by hash sign (#) together with keyword
#include – to include the contents of another file
#define – to define global variable/constant
** No space between # and include
#include <stdio.h>
stdio.h
Is the name of the file that is to be included.
Dot h indicates the type of the file – header file
Allow C program to display output on the screen – printf() and read input from the
keyboard – scanf()
#define pi 3.142
pi - Name of constant
3.142 – value of the constant
Structure of C Program
8
main() function
C Program can have one or more functions, exactly one MUST
be main() function
int main()
int – stands for Integer. It indicates that the function sends an
integer back to operating system when it is finished
executing
main() – the name of the function
Function Body
Enclosed by a set of braces ({ })
Function contains
Local declaration
Declaration of data that will be used for the function/program
Example :
float radius, height;
Statements @ Program body
Set of instructions of what the function/program should do
Return Statement
Return value that sends back to operating system
Example :
return 0;
** 0 usually indicates that a program executes successfully
Common Programming Language
Elements
10
Syntax
Rules that must be followed when constructing a program
Lines
A “line” is a single line appear in the body of program
Statement
A complete instruction that causes the computer to perform
some action
Keywords (Reserve Words)
Words that have special meaning and used for intended
purpose
Common Programming Language
Elements
11
** C is CASE-SENSETIVE
this means that average, Average, AveraGe and averaGe
are four completely different name.
15
X *multiply // * is illegal
student_name 4300zipcode2 // can’t
_aSystemName start with number
Pi Area of circle// can’t have
space
Aminah
int // reserved word
campusCode
password43
Birth0608day
Exercise
Decide whether or not each of the following is a
valid C identifier
• _hello • km_per_hour
• Hello37 • speed!
• one+two • sensible$name
• Subject#1 • _num_incorrect
• M • 12oclock
• double • big long_name
17
Variables
Variable names correspond to locations in the computer's
memory
Every variable has a NAME, a DATA TYPE, a SIZE and a
VALUE
A variable is created via a declaration where its name
and type are specified.
Example :
int integer1, integer2, sum;
Whenever a new value is placed into a variable (through
scanf(), for example, it replaces (and destroys) the
previous value
Data Types
18
int
long int
double
long double
Type Byte Precision Range
Size
float 4 6 10-37 ..1038
Types introduced
earlier are the Ends with
number types int can_per_pack ; a
int and semicolon
double
Variable Declaration
Syntax :
data_type variable_name ;
Example
int maxItems;
float payRate;
double tax;
char code;
int a, b; // equivalent to int a; int b;
Example
Data
of Variable in Memory
25
identifier
type
Program code Memory
char blood_type ; A blood_type ;
Example:
double can_volume;
can_volume = 12 * litter_per_ounce;
double litter_per_ounce = 0.0296;
Common Error:
Using Uninitialized Variables
Using a variable without initializing it, then the prior
value will be used, yielding unpredictable result.
Example
int bottles; //forgot to initialize
int bottle_volume;
bottle_volume = bottles * 2;
It is impossible to know what value will be computed
Plausible value will happen to appear when you run the
program
Exercise
Identify valid and invalid variable name:
sum.of , 12345 , newbal , c123 , @balance
Write a declaration statement to declare the variable count
that will be used to store an integer
Write a declaration statement to declare the variable choice
that will be used to store a character
Write a declaration statements for the following :
Syntax of scanf()
scanf(FormatControlString, InputList);
Example: int number_of_student_in_a_class;
scanf(“%d”, &number_of_student_in_a_class);
** FormatControlString MUST consist of format specifiers
only
** Each element in InputList must be an ADDRESS to a
memory location for which it must be made into
** Address of memory location is specified by prefixing the
variable name with an ampersand character (&) address
operator
Common scanf() Format Specifiers
36
double height;
int year;
scanf(“%lf”, &height);
scanf(“%d”, &year);
scanf(“%lf”, height); /* This is an error!! */
Example : Inputting Multiple Values with
39
a single scanf()
int height;
char ch;
double weight;
…
scanf(“%d %c %lf”, &height, &ch , &weight);
int sum, x = 5, y = 5;
sum = x + y;
printf( "Sum is %d\n", sum );
%d means an integer will be sum specifies what integer-value will
printed be printed
float average;
average = 435 / 10;
printf( “Average is %f\n", average );
%f means decimal number will be printed
average specifies what value will be printed
Programming Example
Write a full C program that allows user to enter two
integers, calculate and display the total of the two
integers.
1 /*
2 Addition program */
3 #include <stdio.h>
4
545 int main()
6 { variables declarations
7 int integer1, integer2, sum; //
declaration
8 */
9 printf( "Enter first integer\n" ); //
10 scanf( "%d", &integer1 ); // input
teger printf(
11 */ "Enter second integer\n" ); //
prompt
12 */
scanf( "%d", &integer2 ); // Calculate sum
integer
13 sum*/= integer1 + integer2; // • 2.1 Sum
assignment
14 printf(of "Sum
sum */
is %d\n", sum ); //
print sum
print sum */
15
16 return 0; // indicate that program ended
successfully */
17}