26
Anatomy of the Anatomy of the simplest C Program simplest C Program Sen Zhang Sen Zhang SUNY Oneonta SUNY Oneonta Oneonta, NEW YORK Oneonta, NEW YORK © 2004 -@2005 © 2004 -@2005

Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Embed Size (px)

Citation preview

Page 1: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Anatomy of the simplest C Anatomy of the simplest C ProgramProgram

Sen ZhangSen Zhang

SUNY OneontaSUNY OneontaOneonta, NEW YORKOneonta, NEW YORK

© 2004 -@2005© 2004 -@2005

Page 2: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Outline of the courseOutline of the course PreliminaryPreliminary Basic lexical elements of CBasic lexical elements of C OperatorsOperators The declaration of variablesThe declaration of variables Basic Data TypesBasic Data Types Input/OutputInput/Output Control StructuresControl Structures FunctionsFunctions ArraysArrays PointersPointers Character StringsCharacter Strings StructuresStructures File ProcessingFile Processing moremore

Page 3: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Computer LanguageComputer Language

first generationfirst generation: machine language : machine language second generationsecond generation: assembly language : assembly language third generation:third generation: high-level programming high-level programming

languages, such as C, C++, and Java. languages, such as C, C++, and Java. Fouth generation: usually used to access Fouth generation: usually used to access

databases.databases. fifth generationfifth generation: languages used for artificial : languages used for artificial

intelligence and neural networks. intelligence and neural networks.

Page 4: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

How to control the hardwareHow to control the hardware The operations of the hardware of a computer are controlled by The operations of the hardware of a computer are controlled by

machine language, i.e. chip signals. Each type of CPU has its own machine language, i.e. chip signals. Each type of CPU has its own machine hard-wired (yes, hard-wired, not hardware) language. machine hard-wired (yes, hard-wired, not hardware) language.

Machine languages consist entirely of binary representations and Machine languages consist entirely of binary representations and are almost impossible for “normal” human beings to read and are almost impossible for “normal” human beings to read and write. Only specially trained people can understand chip write. Only specially trained people can understand chip languages. languages.

They are purely zeros and ones.They are purely zeros and ones. 11111000110001010101 11111000110001010101 Can you understand the meaning of the above binary string? Can you understand the meaning of the above binary string?

Machine can, actually that is all the hardware can understand.Machine can, actually that is all the hardware can understand.

Page 5: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Assembly languagesAssembly languages

Assembly languages have the same structure and set Assembly languages have the same structure and set of commands as machine languages, but they enable of commands as machine languages, but they enable a programmer to use symbol names instead of pure a programmer to use symbol names instead of pure binary numbers. binary numbers.

Therefore, assembly languages are more human being Therefore, assembly languages are more human being friendly. In the early days of programming, all friendly. In the early days of programming, all programs were written in assembly language. programs were written in assembly language. Programmers still use assembly language when speed Programmers still use assembly language when speed is essential or when they need to perform an is essential or when they need to perform an operation that isn't possible in a high-level language. operation that isn't possible in a high-level language.

Page 6: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

HistoryHistory

Assembly language is tediously long and error Assembly language is tediously long and error prone. High-level languages were then invented to prone. High-level languages were then invented to mitigate such issues.mitigate such issues.

A new language ``B'' a second attempt. 1970. A new language ``B'' a second attempt. 1970. A totally new language ``C'' a successor to ``B'‘, A totally new language ``C'' a successor to ``B'‘,

developed by Dennis Ritchie - 1972developed by Dennis Ritchie - 1972 By 1973, UNIX OS almost totally written in ``C''. By 1973, UNIX OS almost totally written in ``C''.

Page 7: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

HistoryHistory

C is the most famous high level structural C is the most famous high level structural language! language!

Rapid growth lead to incompatible variations.Rapid growth lead to incompatible variations. In 1989, the In 1989, the ANSI C ANSI C standard was approvedstandard was approved

Page 8: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

E:\sen\cousin3\UFS040~1>debug t1.exe (we can do a debug demo!)

-u

0BC0:0000 0E PUSH CS

0BC0:0001 1F POP DS

0BC0:0002 BA0E00 MOV DX,000E

0BC0:0005 B409 MOV AH,09

0BC0:0007 CD21 INT 21

0BC0:0009 B8014C MOV AX,4C01

0BC0:000C CD21 INT 21

0BC0:000E 54 PUSH SP

0BC0:000F 68 DB 68

0BC0:0010 69 DB 69

0BC0:0011 7320 JNB 0033

0BC0:0013 7072 JO 0087

0BC0:0015 6F DB 6F

0BC0:0016 67 DB 67

0E ----> 00001110

Binary machine language, Instructions CPU will acceptAnd execute!

Assembly language!

1. address offset in memory2. First generation language, Machine code in Hexadecimal

We do not want to speak machine language, nor assembly language,(although, they are even easier to study, but tedious to use.)instead we want to speak our own language, English style langauge in particular, which the inventors of high level programming languages happened to speak at that time.

second generation

Page 9: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

C program development cycleC program development cycle

Source File pgm.c

Program Object Code File pgm.o

Executable File pgm.exe

Preprocessor

Modified Source Code in RAM

Compiler

Linker

Other Object Code Files (if any)

Editor

Debug or refine

Page 10: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

A programmer uses a A programmer uses a text editor text editor to create or modify to create or modify files containing C code.files containing C code.

Code is also known as Code is also known as source codesource code.. A file containing source code is called a A file containing source code is called a source filesource file. .

The common practice is that we use “.c” as a C source The common practice is that we use “.c” as a C source file’s extension name. file’s extension name.

After a C source file has been created, the programmer After a C source file has been created, the programmer must must invoke the C compilerinvoke the C compiler (which bridging two (which bridging two language worlds, i.e. translates C languages to object language worlds, i.e. translates C languages to object codes or machine languages,) before the program can codes or machine languages,) before the program can be be executedexecuted ( (runrun).).

How to write a C file.How to write a C file.

Page 11: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Using the C Compiler at SUNY OneontaUsing the C Compiler at SUNY Oneonta

Invoking the compiler is system dependent.Invoking the compiler is system dependent.

Different compliers can be used on different Different compliers can be used on different operating system platforms or in different operating system platforms or in different environments.environments.

At Oneonta, we are talking about BCC5.5 which is At Oneonta, we are talking about BCC5.5 which is integrated to TextPad Editor environment on integrated to TextPad Editor environment on Windows system. However, you can use BCC5.5 Windows system. However, you can use BCC5.5 directly in DOS command line window without directly in DOS command line window without integrated with TextPad.integrated with TextPad.

Page 12: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

3 Stages of Compilation3 Stages of Compilation

Stage 1: Stage 1: PreprocessingPreprocessing

• Performed by a program called the Performed by a program called the preprocessorpreprocessor • Modifies the source code (in RAM) according to Modifies the source code (in RAM) according to

preprocessor directives (preprocessor preprocessor directives (preprocessor commandscommands) embedded in the source code.) embedded in the source code.

• Strips comments and white spaces from the codeStrips comments and white spaces from the code

• The original source code as stored on disk is The original source code as stored on disk is notnot modified.modified.

Page 13: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

3 Stages of Compilation (con’t)3 Stages of Compilation (con’t)

Stage 2: Stage 2: CompilationCompilation

o Performed by a program called the Performed by a program called the compilercompilero Translates the preprocessor-modified source code into Translates the preprocessor-modified source code into

object code or object code or executable code executable code (machine code).(machine code).o Checks for Checks for syntax errorssyntax errors and and warningswarningso Saves the translated result (object code or machine code) Saves the translated result (object code or machine code)

to a disk fileto a disk fileo If any compiler errors are received, no object code file If any compiler errors are received, no object code file

will be generated.will be generated.o An object code file An object code file willwill be generated if only warnings, not be generated if only warnings, not

errors, are received.errors, are received.

Page 14: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

3 Stages of Compilation (con’t)3 Stages of Compilation (con’t)

Stage 3: Stage 3: LinkingLinking

o Combines the program object code with other Combines the program object code with other object code to produce the executable file.object code to produce the executable file.

o The other object code can come from the The other object code can come from the Run-Run-Time LibraryTime Library, other libraries, or object files that , other libraries, or object files that you have created.you have created.

o Saves the executable code to a disk file. If any Saves the executable code to a disk file. If any linker errors are received, no executable file will linker errors are received, no executable file will be generated.be generated.

Page 15: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

The first C ProgramThe first C Program /* Filename: helloOneonta.c/* Filename: helloOneonta.c Author:Author: Sen Zhang Sen Zhang Date written: Aug/22/2004Date written: Aug/22/2004 Description: This program prints the greeting Description: This program prints the greeting

“Hello, Oneonta!” “Hello, Oneonta!” */*/ #include <stdio.h>#include <stdio.h> int main ( )int main ( ) {{ printf (“Hello, Oneonta!\n”) ;printf (“Hello, Oneonta!\n”) ; return 0 ;return 0 ; }}

Page 16: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Anatomy of a C ProgramAnatomy of a C Program program header commentprogram header comment

preprocessor directives (if any)preprocessor directives (if any)

int main ( )int main ( ) {{ statement(s);statement(s); return 0 ;return 0 ; }}

Page 17: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Program Header CommentProgram Header Comment

A comment is descriptive text in a program A comment is descriptive text in a program which is intended only for humans reading the which is intended only for humans reading the program, but not for compilers translating the program, but not for compilers translating the comment itself. comment itself.

A comment is marked specially using special A comment is marked specially using special characters so that it will be ignored when the characters so that it will be ignored when the program is to be compiled. These special program is to be compiled. These special characters are called characters are called comment delimiters.comment delimiters.

Page 18: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Two ways of commentingTwo ways of commenting There are two ways of commenting in C. One is used for There are two ways of commenting in C. One is used for

commenting a single line, and the other one is used for commenting a single line, and the other one is used for commenting a block of code. commenting a block of code. The one we use for commenting a line is a double slash (//). The one we use for commenting a line is a double slash (//). The other one, however, will comment a block of code. It means that it The other one, however, will comment a block of code. It means that it

will start commenting the code until it reaches its terminator. So it has will start commenting the code until it reaches its terminator. So it has its own BEGIN and END. We use /* to BEGIN commenting, and */ to its own BEGIN and END. We use /* to BEGIN commenting, and */ to END commenting.END commenting.

Usually should not be nested.Usually should not be nested. The program header comment usually, if not always, comes The program header comment usually, if not always, comes

first.first. Look at the class web page for the required contents of our Look at the class web page for the required contents of our

header comment.header comment.

Page 19: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Preprocessor DirectivesPreprocessor Directives Lines that begin with a # in column 1 are Lines that begin with a # in column 1 are

called called preprocessor directivespreprocessor directives ( (commandscommands). ). They are not C statements! No ending They are not C statements! No ending semicolons!!! semicolons!!!

Example: the Example: the #include <stdio.h>#include <stdio.h> directive directive causes the preprocessor to include a copy of causes the preprocessor to include a copy of the standard input/output header file the standard input/output header file stdio.h stdio.h at this point in the code.at this point in the code.

This header file was included because it This header file was included because it contains information about the printf ( ) contains information about the printf ( ) function that is used in this program.function that is used in this program.

Page 20: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

Head filesHead files

A text file that contains declarations used by a A text file that contains declarations used by a group of functions or programs. group of functions or programs.

Also known as an include file. Also known as an include file.

Page 21: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

int main ( )int main ( ) Every program must have a Every program must have a functionfunction called called

mainmain. This is where program execution begins.. This is where program execution begins. main() is usually placed in the source code file main() is usually placed in the source code file

as the first function for readability, but actually as the first function for readability, but actually even physically it is not the first function in a even physically it is not the first function in a file, it still is the entry point to any program.file, it still is the entry point to any program.

The The reserved wordreserved word “int” indicates that main() “int” indicates that main() returnsreturns an integer value. an integer value.

The parentheses following identifier “main” The parentheses following identifier “main” indicate that it is a function, not a variable.indicate that it is a function, not a variable.

Page 22: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

The Function BodyThe Function Body

A left brace (curly bracket) -- A left brace (curly bracket) -- { { -- begins -- begins the the bodybody of every function. A of every function. A corresponding right brace -- corresponding right brace -- }} -- ends the -- ends the function body.function body.

The style is to place these braces on separate The style is to place these braces on separate lines and to indent the entire function body 3 lines and to indent the entire function body 3 to 5 spaces.to 5 spaces.

Page 23: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

printf (“Hello, Oneonta!\n”) ;printf (“Hello, Oneonta!\n”) ;

This line is a C This line is a C statementstatement.. It is a It is a callcall to the function to the function printf ( )printf ( ) with a with a

single single argument (parameter)argument (parameter), namely the , namely the stringstring “Hello, Oneonta!\n”. “Hello, Oneonta!\n”.

Even though a string may contain many Even though a string may contain many characters, the string itself should be characters, the string itself should be thought of as a single unit.thought of as a single unit.

Notice that \n is a special character to Notice that \n is a special character to indicate that a newline will be output.indicate that a newline will be output.

Page 24: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

StatementsStatements

Notice also that this line ends with a Notice also that this line ends with a semicolon. semicolon.

All statementsAll statements in C end with a semicolon.in C end with a semicolon. A program statement is like a sentence in A program statement is like a sentence in

English, except that it is terminated by a semi-English, except that it is terminated by a semi-colon, instead of a full stop.colon, instead of a full stop.

A statement can be an variable declaration, A statement can be an variable declaration, assignment, a function call, a return or usually assignment, a function call, a return or usually a combination of them.a combination of them.

Page 25: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

return 0 ;return 0 ; Because function main() is defined to return an Because function main() is defined to return an

integer value, there must be inside the function body integer value, there must be inside the function body a statement that indicates what this value is.a statement that indicates what this value is.

The statementThe statementreturn 0 ;return 0 ;

indicates that main() returns a value of zero toindicates that main() returns a value of zero tothe operating system.the operating system.

A value of 0 indicates that the program successfully A value of 0 indicates that the program successfully terminated execution. You can use other integer terminated execution. You can use other integer value to indicate failure event whenever appropriate.value to indicate failure event whenever appropriate.

Page 26: Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004 -@2005

SummarizeSummarize

An English paper is a sequence of statements An English paper is a sequence of statements that have to follow the rules of English that have to follow the rules of English language.language.

A C program is a sequence of statements that A C program is a sequence of statements that have to be formed in accordance with the have to be formed in accordance with the predefined syntax of C language.predefined syntax of C language.