Professional Documents
Culture Documents
PF Lecture 4
PF Lecture 4
INTRODUCTION TO C
1
Chapter 4 Objectives
4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
4.5 Summary
Chapter 4 4.1 Introduction
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.1 Introduction
o Syntax
o Keywords
o Programmer-Defined Identifiers
o Operators
o Punctuation
o Variables
• This chapter will describe the element within the examples of the
C programming code.
Chapter 4 Contents
4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
4.5 Summary
Chapter 4 4.2 Structure of C Program
#include <stdio.h>
#include <conio.h>
My First C++ 0 signifies that the program exited without any error to
Program the OS while returning any other value shows that
some error have occured in your program.
I'm 33 years old
Chapter 4 4.2 Structure of C Program
Program execution
4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
4.5 Summary
Chapter 4 4.3 Programming Style
Introduction
4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
(a) Printing a line of text
(b) Adding two integers
(c) Multiplying some values
4.5 Summary
Chapter 4 4.4 A Simple C Program
(a) Printing a line of text
• We begin by writing a simple C program.
• The program and its screen output on screen are shown in
the following figure.
1 // Program_01.c
2 // A first program in C
3 #include <stdio.h>
4
5 // function main begins program execution
6 int main( void )
7 {
8 printf( "Welcome to C!\n" );
9 } // end function main
Welcome to C!
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Comments
Comments
Preprocessor
Preprocessor
directive
directive
Blank
Blanklines
linesand
and Escape
Escape
white space
white space sequences
sequences
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Special Characters
Character Name Meaning
/* Slash star Beginning of a comment
/* */ Star slash End of a comment
# Pound sign Beginning of preprocessor
directive
< > Open/close brackets Enclose filename in #include
( ) Open/close Used when naming a function
parentheses
{ } Open/close brace Encloses a group of statements
Comment
• Example:
/* Program_4.1.c
A first program in C */
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Preprocessor Directive
• Example:
#include <stdio.h>
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Function
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
An Output Statement
• Line 8 instructs the computer to print on the screen the string
of characters marked by the double quotes “m” marks.
• A string is sometimes called a character string, a message or
a literal.
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Escape Sequences
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Tips: .
Put comment in
your program and
save your file as
Activity_4.1.c
Chapter 4 4.4 A Simple C Program
(b) Adding two integers
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
1 // Program_03.c
2 // Addition program
3 #include <stdio.h>
4
5 // function main begins program execution
6 int main( void )
7 {
8 int integer1; // first number to be entered by user
9 int integer2; // second number to be entered by user
10 int sum; // variable in which sum will be stored
11
12 printf( "Enter first integer\n" ); // prompt
13 scanf( "%d", &integer1 ); // read an integer
14
15 printf( "Enter second integer\n" ); // prompt
16 scanf( "%d", &integer2 ); // read an integer
17
18 sum = integer1 + integer2; // assign total to sum
19
20 printf( "Sum is %d\n", sum ); // print sum
21 } // end function main
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
1 // Program_03.c
2 // Addition program
3 #include <stdio.h> Variables
4 Variables
with
with
5 valid identifier
6 int main( void ) valid identifier
7 {
8 int integer1; Prompt
Prompt
9 int integer2; messages
messages
10 int sum;
11
12 printf( "Enter first integer\n" ); The
Theinput
input
13 scanf( "%d", &integer1 ); statements
statements
14
15 printf( "Enter second integer\n" );
16 scanf( "%d", &integer2 );
17
sum = integer1 + integer2; Assignment
Assignment
18
statements
statements
19 Printing
20 printf( "Sum is %d\n", sum ); Printing
statements
statements
21 } _____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Variables:
int integer1;
int integer2;
int sum;
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Prompting Messages:
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
• The statement scanf (line 13) obtain the value from the user.
• The computer then assigns this number, or value, to the
variable integer1.
scanf("%d", &integer1);
Format
Formatspecifier
specifier Address
Addressoperator
operator
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Printing:
Format
Formatspecifier
specifier Address
Addressoperator
operator
Notice
Noticethat
thatthe
theformat
formatspecifier
specifierfor
foran
aninteger
integerisisthe
thesame
sameinin
both printfand
bothprintf andscanf
scanf
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
Tips: .
Put comment in
your program and
save your file as
Activity_4.2.c
Chapter 4 4.4 A Simple C Program
(c) Multiplying some values
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
1 // Program_04.c
2 // Multiplication program: Calculate a cylinder’s volume
3 #include <stdio.h>
4
5 // function main begins program execution
6 int main( void )
7 {
8 int high, radius; // the values to be entered by user
9 double volume; // variable for result to be stored
10 const double pi = 3.14; // pi value always 3.14
11 char letter = 'B'; // label of the cylinder
12
13 printf( "\nEnter the high: " ); // prompt
14 scanf( "%d", &high ); // read an integer
15
16 printf( "Enter the radius: " ); // prompt
17 scanf( "%d", &radius ); // read an integer
18
19 // assign result to volume
20 volume = pi * high * radius * radius;
21
22 printf("\nVolume for cylinder %c is %f\n\n”, letter ,volume);
23 } // end function main
Chapter 4 4.4 A Simple C Program
1 // Program_04.c
2 // Multiplication program: Calculate a cylinder’s volume
3 #include <stdio.h>
4 Integer
Integer Floating
Floatingpoint
point
5 data types
data types data types
data types
6 int main( void )
7 {
8 int high, radius;
9 double volume;
10 const double pi = 3.14;
11 char letter = 'B';
12 Character
13 printf( "\nEnter the high: " ); Character
14 scanf( "%d", &high );
data
datatypes
types
Constant
15Constant
16 printf( "Enter the radius: " );
17 scanf( "%d", &radius );
18 Output:
19
20 volume = pi * high * radius * radius;
21
22 printf("\nVolume for cylinder %c is %f\n\n”, letter ,volume);
23 }
Chapter 4 4.4 A Simple C Program
Integer Data Types:
high = 15;
radius = 15;
Chapter 4 4.4 A Simple C Program
• E notation:
3.14159e-1 6.25e-5
Constants:
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
• Example: • Syntax
:
#include <stdio.h>
#define NAME value
#define PI 3.14
CODE: MEMORY
:
char letter;
letter = 'C'; letter
67
Chapter 4 4.4 A Simple C Program
Important:
Important:Note
Notethat
thatthe
thevalue
valueofofaacharacter
characterisisenclosed
enclosed
ininsingle
singlequotes
quotesmarks.
marks.Example:‘B’
Example:‘B’
Printing:
• For printing the output (Line 22), we need to use the correct
format specifier for:
• char data type, letter
• double data type, volume.
Format
Formatspecifier
specifier
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 4.4 A Simple C Program
http://3.bp.blogspot.com/-5b2Smx0fx5E/TrGT3SrXa1I/AAAAAAAAAH4/SsnplVh7agk/s1600/Format+Specifications+in+C+programming+language.bmp
Chapter 4 4.4 A Simple C Program
Solution 4.3:
Activity_4.3.c
Hint:
Hint:Need
Needtoto
write
write%.2f
%.2f
Chapter 4 4.4 A Simple C Program
• C is case sensitive.
• printf() is NOT the same as Printf().
• All C commands (functions) are lowercase.
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4 Contents
4.1 Introduction
4.2 Structure of C Program
4.3 Programming Style
4.4 A Simple C Programs
4.5 Summary
Chapter 4 4.5 Summary
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Chapter 4
Self-Reviews
Chapter 4 Self-Review
Exercise 4.2: Calculate and display the power (watts) and the
voltage (volts) when enter TWO set of floating point
values contain the current (amps) and resistance
Tips: .
(ohms) from the keyboard.
Put comment in
your program and The result values should be displayed with a
save it file as precision of 3. You can only declare 4 variables.
Exercise_4.2.c