1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

Lecture 2 c program structure and its components

57 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề C Program Structure and its Components
Người hướng dẫn TS. Võ Thị Ngọc Châu
Trường học Ho Chi Minh City University of Technology
Chuyên ngành Computer Science and Engineering
Thể loại Giáo trình
Năm xuất bản 2017 – 2018
Thành phố Ho Chi Minh City
Định dạng
Số trang 57
Dung lượng 2,23 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Chapter 2: C Program Structure and its Components

chauvtn@hcmut.edu.vn)

Trang 3

References

Deitel and Harvey Deitel, Prentice Hall, 2012

– Brian W Kernighan and Dennis M Ritchie, Prentice Hall, 1988

3

Trang 4

Content

Trang 5

Introduction

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 6

Preprocessor

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 7

Preprocessor

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 8

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

Trang 9

A 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 10

A 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 11

A 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 12

A Sample C Program

executable program file with exactly one ―main‖

function

 MAIN vs Main vs main

12

Trang 13

Open 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 14

A 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 16

A 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 18

A 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 19

A 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 20

A 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 22

Coding Styles

the same purpose return the same result:

just a few lines; instead, with thousands of

Trang 23

Coding Styles

the block {} that they are contained

explanation, marking, tracking, …

program development

Trang 25

Coding 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 26

Coding 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 28

Coding Styles

28

Trang 29

Coding Styles

29

Trang 31

Coding 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 32

Data 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 33

Data 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 34

Data 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 35

Data 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 36

Data and Standard Output

Function

36

Trang 37

Data and Standard Output

Trang 38

Data and Standard Output

Function

38

Flags to supplement its output formatting capabilities,

placed immediately to the right of the percent sign

Trang 39

Data and Standard Output

Function

39

Trang 40

Data and Standard Output

Function

40

Trang 42

Data and Standard Output

Function

42

Trang 43

Data 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 44

Data and Standard Input

Function

44

The conversion specifiers used to input all types of data

Trang 45

Data and Standard Input

Function

45

The conversion specifiers used to input all types of data

Trang 46

Data 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 47

Data 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 48

Data 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 49

Data and Standard Input

Function

49

Trang 50

Data and Standard Input

Function

50

Trang 51

Data and Standard Input

Function

51

Trang 52

Data and Standard Input

Function

52

???

Trang 53

Data Processing: Simple Example

the center and a given point in a 2D space

Trang 54

Data Processing: Simple Example

the center and a given point in a 2D space

on screen

54

Trang 55

Data Processing: Simple Example

55

printf() scanf() sqrt() system()

stdio.h stdio.h math.h stdlib.h

Trang 56

Summary

 Input & Output vs Read & Write vs scanf & printf

also readable

56

Trang 57

Chapter 2: C Program Structure and its Components

Ngày đăng: 11/04/2023, 18:55

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN