Bài giảng Introduction to Computer Programming (C language) - Chapter 2: C Program Structure and its Components - Võ Thị Ngọc Châu
Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering
Chapter 2: C Program
Structure and its Components
Introduction to Computer Programming
(C language)
TS. Võ Thị Ngọc Châu
2017 – 2018, Semester 2
Course Content
C.1. Introduction to Computers and
Programming
C.2. C Program Structure and its
Components
C.3. Variables and Basic Data Types
C.4. Selection Statements
C.5. Repetition Statements
C.6. Functions
C.7. Arrays
C.8. Pointers
C.9. File Processing
2
References
[1] ―C: How to Program”, 7th Ed. – Paul
Deitel and Harvey Deitel, Prentice Hall, 2012.
[2] “The C Programming Language”, 2nd Ed.
– Brian W. Kernighan and Dennis M. Ritchie,
Prentice Hall, 1988
and others, especially those on the Internet
3
Content
Introduction
A Sample C Program
Coding Styles
Data and Standard Output Function
Data and Standard Input Function
Data Processing: Simple Example
Summary
4
Introduction
―In our experience, C has proven to be a
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.‖
[2], Brian W. Kernighan and Dennis M. Ritchie
5
versatile = able to be used for many different purposes
Introduction
Library
(Header: *.h)
Library
(Object code: *.lib; *.dll; *.so)
Design of
program
Executable
Program
Preprocessor
Linker
Editor
Compiler
Source code Enhanced source code
Object code
*.obj
*.h + *.c
(*.cpp)
*.h + *.c (*.cpp)
gcc; g++
Integrated Development Environment (IDE):
Visual Studio; Eclipse; Qt Creator; Code block; Online tool; etc
6
Programming tasks using the C language
Introduction
Library
ader: *.h)
Library
(Object code: *.lib; *.dll; *.so)
Design of
program
Executable
Program
processor
Linker
Editor
Compiler
Source code Enhanced source code
Object code
*.obj
*.h + *.c
(*.cpp)
*.h + *.c (*.cpp)
gcc; g++
Integrated Development Environment (IDE):
Visual Studio; Eclipse; Qt Creator; Code block; Online tool; etc
7
Programming tasks using the C language
A Sample C Program
A source code file named C2_example1.c
Purpose: display our university’s name
and course’s name on the screen
8
A Sample C Program
A source code file named C2_example1.c
9
A Sample C Program
Every C program requires the specific
function named ―main‖ exactly.
The body of the
―main‖ function is
enclosed in the
brackets {…}.
The body of the
The specific
extension of
C source
A source code file named C2_example1.c
―main‖ function is
made to serve the
specific purpose.
code files
Purpose: display our university’s name
and course’s name on the screen
10
A Sample C Program
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 //…
A source code file named C2_example1.c
Purpose: display our university’s name
and course’s name on the screen
11
A Sample C Program
File name: following the naming conventions
supported by the OS with the .c extension
Not include some special symbols: *, /, \, …
Avoid using special symbols even if they are allowed
Prefer descriptive names
The ―main‖ function
All the files and libraries are compiled into a single
executable program file with exactly one ―main‖
function.
The starting point for the execution of a C program
Note: The C language is case-sensitive.
MAIN vs. Main vs. main
12
A Sample C Program
The ―main‖ function
void main(){
Open bracket ―{― to start the body section
…
…
Corresponding close bracket ―}― to end the body
}
section
int main(void){
Place for specifying the parameters of the function
Empty or void: no parameter is specified
}
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
13
A Sample C Program
The ―main‖ function
void main(){
statement
statement
printf("Ho Chi Minh City University of Technology\n");
printf("Introduction to Computer Programming\n");
}
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
14
- repetition: for, while, do… while
A Sample C Program
In addition to ―main‖, there are keywords/reserved words
in the C language.
[1], p. 42
15
A Sample C Program
The global definition section
Lines beginning with #
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
Function prototypes to validate function calls
Global variable declarations
16
A Sample C Program
Comments
/*…*/: multi-line comment
//…: single-line comment
A Sample C Program
How to get the program run for the specified purpose? *.c => a machine code file (e.g. *.exe)
A source code file named C2_example1.c
Purpose: display our university’s name
and course’s name on the screen
18
A Sample C Program
Edit:
C2_example1.c
Compile:
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
Run:
C2_example1
19
A Sample C Program
GCC = GNU Compiler Collection
The free GNU C++: gcc.gnu.org/install/
binaries.html
GCC is available for most platforms,
including Linux, Mac OS X (via Xcode) and
Windows—via tools like Cygwin
(www.mingw.org)
Free IDEs: Micrsoft Visual Studio Express
Editions, CodeBlock, Dev C++, …
20
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Introduction to Computer Programming (C language) - Chapter 2: C Program Structure and its Components - Võ Thị Ngọc Châu", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_introduction_to_computer_programming_c_language_ch.pdf

