A Sample C Program 8 A source code file named C2_example1.c Purpose: display our university’s name and course’s name on the screen... A Sample C Program 9 A source code file named C2_e
Trang 1Chapter 2: C Program Structure and its Components
chauvtn@hcmut.edu.vn)
Trang 3References
Deitel and Harvey Deitel, Prentice Hall, 2012
– Brian W Kernighan and Dennis M Ritchie, Prentice Hall, 1988
3
Trang 4Content
Trang 5Introduction
pleasant, expressive, and versatile
language for a wide variety of programs It
is easy to learn, and it wears well as one’s experience with it grows.‖
5
versatile = able to be used for many different purposes
Trang 6Preprocessor
Library (Header: *.h)
Enhanced source code
*.h + *.c (*.cpp)
Object code
*.obj
Integrated Development Environment (IDE):
Visual Studio; Eclipse; Qt Creator; Code block; Online tool; etc
Trang 7Preprocessor
Library (Header: *.h)
Enhanced source code
*.h + *.c (*.cpp)
Object code
*.obj
Integrated Development Environment (IDE):
Visual Studio; Eclipse; Qt Creator; Code block; Online tool; etc
Trang 8A Sample C Program
8
A source code file named C2_example1.c Purpose: display our university’s name and course’s name on the screen
Trang 9A Sample C Program
9
A source code file named C2_example1.c Purpose: display our university’s name and course’s name on the screen
Trang 10A Sample C Program
10
A source code file named C2_example1 c
Purpose: display our university’s name and course’s name on the screen
The specific extension of
C source code files
Every C program requires the specific function named ―main‖ exactly
The body of the
Trang 11A Sample C Program
11
A source code file named C2_example1.c
Purpose: display our university’s name and course’s name on the screen
A directive to the C preprocessor before the program is compiled
<stdio.h> is used for standard input/output library functions such
as the output function printf() and the input function scanf()
This global definition section is used for external headers
Comments in /*…*/ or after //…
Trang 12A Sample C Program
executable program file with exactly one ―main‖
function
MAIN vs Main vs main
12
Trang 13Open bracket ―{― to start the body section
Corresponding close bracket ―}― to end the body section
int main(void){
}
Data type of the value returned by the ―main‖ function
int: a status value: 0 = no error; 1 = error
EXIT_SUCCESS = 0; EXIT_FAILURE = 1
void: no value is returned
Place for specifying the parameters of the function
Empty or void: no parameter is specified
Trang 14A Sample C Program
14
void main(){
printf( "Ho Chi Minh City University of Technology\n" );
printf( "Introduction to Computer Programming\n" );
}
statement statement
Each statement
- ended with a semicolon (;)
- stretched on multiple lines with a backslash \ at the end
- able to be grouped in the brackets {}
- not consider spaces Several statement types
- sequence: function calling, assignment, break, continue, return, …
- selection: if, if… else, switch
- repetition: for, while, do… while
Trang 16A Sample C Program
The #include directive tells the preprocessor to include the contents of another file
#include ― myHeader.h ‖
A personal header file in the current directory
#include < stdio.h >
A standard library file in the standard directory \include
The #define directive to set up symbolic replacements
The #if test at compile-time to look at the symbols in
#define and turn on and off which lines the compiler uses
16
Trang 18A Sample C Program
18
A source code file named C2_example1.c
Purpose: display our university’s name and course’s name on the screen
How to get the program run for the specified purpose? *.c => a machine code file (e.g *.exe)
Trang 19A Sample C Program
C2_example1.c
gcc –Wall C2_example1.c –o C2_example1
-Wall: display all the warning types along with the errors if any -o C2_example1: specify a name for the resulting executable program
C2_example1
19
Trang 20A Sample C Program
binaries.html
including Linux, Mac OS X (via Xcode) and
Windows—via tools like Cygwin
(www.cygwin.com) and MinGW
(www.mingw.org)
Editions, CodeBlock, Dev C++, …
20
Trang 22Coding Styles
the same purpose return the same result:
just a few lines; instead, with thousands of
Trang 23Coding Styles
the block {} that they are contained
explanation, marking, tracking, …
program development
Trang 25Coding Styles
names?
Function: your defined modular processing unit
Variable: a location in memory where a value can be stored for use by a program
Symbolic constant: constant represented as a symbol (a string) that will be replaced with the value it stands for
Trang 26Coding Styles
digits, underscore ―_‖ and starting with a letter
Function: verb, phrase
Variable: noun, lower-case letters, short for loops
Global variable: prefix g_
Data type: suffix _ptr for pointers
Symbolic constant: upper-case letters, underscores 26
Trang 28Coding Styles
28
Trang 29Coding Styles
29
Trang 31Coding Styles
to be part of your own coding habit
always well-formatted
readable, interpretable, able to be maintained, reused, and extended
http://users.ece.cmu.edu/~eno/coding/CCodingStandard.html
http://www.cs.swarthmore.edu/~newhall/unixhelp/c_codestyle.html
31
Trang 32Data and Standard Output
Function
#include <stdio.h>
A sequence of bytes flows from main memory to an output device: display screen, printer, disk drive, network connection, and so on
Normally and automatically, the standard output stream is connected to the screen
32
Examples:
Trang 33Data and Standard Output
Function
Rounding floating-point values to an indicated number
of decimal places
Aligning a column of numbers with decimal points
appearing one above the other
Right justification and left justification of outputs
Inserting literal characters at precise locations in a
Trang 34Data and Standard Output
Function
34
- format-control-string describes the output format
- other-arguments (optional) correspond to each conversion
specification in format-control-string
Each conversion specification begins with a percent sign (%) and
ends with a conversion specifier
There can be many conversion specifications in one format control
string
Examples:
The 1 st conversion specification: %5.2f => argument: x
The 2 nd conversion specification: %5.2f => argument: y
Trang 35Data and Standard Output
Function
35
format-control-string is summarized as follows:
%[flag][width][.precision]specifier specifier = d/i, u, o, x/X, f/F, e/E, g/G, a/A, c, s, p, n, % precision = number, *
width = number, *
flag = +, -, space, #, 0
Trang 36Data and Standard Output
Function
36
Trang 37Data and Standard Output
Trang 38Data and Standard Output
Function
38
Flags to supplement its output formatting capabilities,
placed immediately to the right of the percent sign
Trang 39Data and Standard Output
Function
39
Trang 40Data and Standard Output
Function
40
Trang 42Data and Standard Output
Function
42
Trang 43Data and Standard Input
Function
#include <stdio.h>
A sequence of bytes flows from an input device:
keyboard, disk drive, network connection, and so on to main memory
Normally and automatically, the standard input stream
is connected to the keyboard
43
format-control-string describes the formats of the input
other-arguments are pointers to variables in which the input will be stored
Trang 44Data and Standard Input
Function
44
The conversion specifiers used to input all types of data
Trang 45Data and Standard Input
Function
45
The conversion specifiers used to input all types of data
Trang 46Data and Standard Input
Function
46
The conversion specifiers used to input all types of data
Input data flow
from an input device
to main memory
?
Input device = keyboard
Main memory ≈ variable
Input Input
Trang 47Data and Standard Input
Function
where the input data will be stored?
Address is used!
47
Input device = keyboard
Main memory ≈ variable
Input Input
Input a string: Input
Input device = keyboard
Main memory ≈ variable
A
A
Input a character: A
Input device = keyboard
Main memory ≈ variable
-123 -123 Input an integer: -123
Fixed sizes: character = 1 byte, integer = 4 bytes, … Varying size: user-predefined
Trang 48Data and Standard Input
Function
48
Input device = keyboard
Main memory ≈ variable
Input Input
Input a string: Input
Input device = keyboard
Main memory ≈ variable
A
A
Input a character: A
Input device = keyboard
Main memory ≈ variable
-123 -123 Input an integer: -123
Fixed sizes: character = 1 byte, integer = 4 bytes, … Varying size: user-predefined
printf( ―%c‖ , aChar)
int anInteger;
… scanf( ―%d‖ , & anInteger)
printf( ―%d‖ , anInteger)
In C, a string is defined as an array (sequence) of characters The memory of this array
is referred by the address of the first character represented by the name of the array
Trang 49Data and Standard Input
Function
49
Trang 50Data and Standard Input
Function
50
Trang 51Data and Standard Input
Function
51
Trang 52Data and Standard Input
Function
52
???
Trang 53Data Processing: Simple Example
the center and a given point in a 2D space
Trang 54Data Processing: Simple Example
the center and a given point in a 2D space
on screen
54
Trang 55Data Processing: Simple Example
55
printf() scanf() sqrt() system()
stdio.h stdio.h math.h stdlib.h
Trang 56Summary
Input & Output vs Read & Write vs scanf & printf
also readable
56
Trang 57Chapter 2: C Program Structure and its Components