CO1003_Chapter 2_C Program Structure and its Components
CO1003_Chapter 2_C Program Structure and its Components
Chapter 2: C Program
Structure and its Components
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
gcc; g++
Integrated Development Environment (IDE):
Visual Studio; Eclipse; Qt Creator; Code block; Online tool; etc
Programming tasks using the C language 6
Introduction
Design of Library Library
program (Header: *.h) (Object code: *.lib; *.dll; *.so)
gcc; g++
Integrated Development Environment (IDE):
Visual Studio; Eclipse; Qt Creator; Code block; Online tool; etc
Programming tasks using the C language 7
A Sample C Program
}
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 14
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
17
A Sample C Program
How to get the program run for the specified purpose? *.c => a machine code file (e.g. *.exe)
C2_example1.c
🞐 Compile:
🞐 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.cygwin.com) and MinGW
(www.mingw.org)
🞐 Free IDEs: Micrsoft Visual Studio Express
Editions, CodeBlock, Dev C++, …
20
Coding Styles
Which
(A)
one do
you
prefer?
(B)
(C) 21
Coding Styles
🞐 The three different formatted examples for
the same purpose return the same result:
■ Classified
■ Consistent
■ Agreements if any
🞐 Less error-prone
24
Coding Styles
🞐 Naming conventions
■ What can you name in a C program except file
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
28
Coding Styles
🞐 How to make the following program clearer?
29
Coding Styles
■ http://www.cs.swarthmore.edu/~newhall/unixhelp/c_codestyle.html
31
Data and Standard Output
Function
🞐 Standard output function: printf()
■ #include <stdio.h>
Examples:
32
Data and Standard Output
Function
🞐 Formatting capabilities of the printf 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
line of output.
■ Representing floating-point numbers in exponential
format.
■ Representing unsigned integers in octal and
hexadecimal format.
■ Displaying all types of data with fixed-size field widths
and precisions.
33
Data and Standard Output
Function
Examples:
%[flag][width][.precision]specifier
precision = .number, .*
width = number, *
flag = +, -, space, #, 0
35
Data and Standard Output
Function
36
Data and Standard Output
Function
c Display a character.
s Display a sequence of characters until a terminating null \0
character is encountered.
37
Data and Standard Output
Function
Flags to supplement its output formatting capabilities,
placed immediately to the right of the percent sign
38
Data and Standard Output
Function
39
Data and Standard Output
Function
40
41
Data and Standard Output
Function
42
Data and Standard Input
Function
🞐 Standard input function: scanf()
■ #include <stdio.h>
■ Input the data from the standard input stream
🞐 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.
44
Data and Standard Input
Function
The conversion specifiers used to input all types of data
45
Data and Standard Input
Function
The conversion specifiers used to input all types of data
Input
Main memory ≈ variable ? 46
Data and Standard Input
Function
🞐 How to refer to the place in main memory
where the input data will be stored?
■ Memory is addressable contiguously.
■ Address is used!
Input A -123
Main memory ≈ variable Main memory ≈ variable Main memory ≈ variable
Varying size: user-predefined Fixed sizes: character = 1 byte, integer = 4 bytes, …47
Data and Standard Input
Function
Input a string: Input Input a character: A Input an integer: -123
Input A -123
Main memory ≈ variable Main memory ≈ variable Main memory ≈ variable
49
Data and Standard Input
Function
🞐 Put them altogether
50
Data and Standard Input
Function
🞐 Input a date in the form of: dd/mm/yyyy
51
Data and Standard Input
Function
🞐 Input a date in the form of: dd Month yyyy
???
52
Data Processing: Simple Example
🞐 Program for computing the distance between
the center and a given point in a 2D space
■ Input: x, y as a coordinate of a given point
■ Output: the distance along with the given point
on screen
y
(3, 2)
0 x
Distance (i.e. the length of
the blue line) = 3.605551 53
Data Processing: Simple Example
🞐 Program for computing the distance between
the center and a given point in a 2D space
■ Input: x, y as a coordinate of a given point
■ Output: the distance along with the given point
on screen
54
Data Processing: Simple Example
printf() stdio.h
🞐 Function calling: scanf() stdio.h
sqrt() math.h
system() stdlib.h 55
Summary
■ Input & Output vs. Read & Write vs. scanf & printf
56
Chapter 2: C Program Structure
and its Components
57