Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004"— Presentation transcript:

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

2 Outline of the course Preliminary Preliminary Basic lexical elements of C Basic lexical elements of C Operators Operators The declaration of variables The declaration of variables Basic Data Types Basic Data Types Input/Output Input/Output Control Structures Control Structures Functions Functions Arrays Arrays Pointers Pointers Character Strings Character Strings Structures Structures File Processing File Processing more more

3 Computer Language first generation: machine language first generation: machine language second generation: assembly language second generation: assembly language third generation: high-level programming languages, such as C, C++, and Java. third generation: high-level programming languages, such as C, C++, and Java. Fouth generation: usually used to access databases. Fouth generation: usually used to access databases. fifth generation: languages used for artificial intelligence and neural networks. fifth generation: languages used for artificial intelligence and neural networks.

4 How to control the hardware 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 hard-wired (yes, hard-wired, not hardware) language. 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 hard-wired (yes, hard-wired, not hardware) language. Machine languages consist entirely of binary representations and are almost impossible for “normal” human beings to read and write. Only specially trained people can understand chip languages. Machine languages consist entirely of binary representations and are almost impossible for “normal” human beings to read and write. Only specially trained people can understand chip 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? Machine can, actually that is all the hardware can understand. Can you understand the meaning of the above binary string? Machine can, actually that is all the hardware can understand.

5 Assembly languages Assembly languages have the same structure and set of commands as machine languages, but they enable a programmer to use symbol names instead of pure binary numbers. Assembly languages have the same structure and set of commands as machine languages, but they enable a programmer to use symbol names instead of pure binary numbers. Therefore, assembly languages are more human being friendly. In the early days of programming, all programs were written in assembly language. Programmers still use assembly language when speed is essential or when they need to perform an operation that isn't possible in a high-level language. Therefore, assembly languages are more human being friendly. In the early days of programming, all programs were written in assembly language. Programmers still use assembly language when speed is essential or when they need to perform an operation that isn't possible in a high-level language.

6 History Assembly language is tediously long and error prone. High-level languages were then invented to mitigate such issues. Assembly language is tediously long and error prone. High-level languages were then invented to 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'‘, developed by Dennis Ritchie - 1972 A totally new language ``C'' a successor to ``B'‘, developed by Dennis Ritchie - 1972 By 1973, UNIX OS almost totally written in ``C''. By 1973, UNIX OS almost totally written in ``C''.

7 History C is the most famous high level structural language! C is the most famous high level structural language! Rapid growth lead to incompatible variations. Rapid growth lead to incompatible variations. In 1989, the ANSI C standard was approved In 1989, the ANSI C standard was approved

8 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 accept And execute! Assembly language! 1. address offset in memory 2. 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

9 C 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

10 A programmer uses a text editor to create or modify files containing C code. A programmer uses a text editor to create or modify files containing C code. Code is also known as source code. Code is also known as source code. A file containing source code is called a source file. The common practice is that we use “.c” as a C source file’s extension name. A file containing source code is called a source file. The common practice is that we use “.c” as a C source file’s extension name. After a C source file has been created, the programmer must invoke the C compiler (which bridging two language worlds, i.e. translates C languages to object codes or machine languages,) before the program can be executed (run). After a C source file has been created, the programmer must invoke the C compiler (which bridging two language worlds, i.e. translates C languages to object codes or machine languages,) before the program can be executed (run). How to write a C file.

11 Using 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 operating system platforms or in different environments. Different compliers can be used on different operating system platforms or in different environments. At Oneonta, we are talking about BCC5.5 which is integrated to TextPad Editor environment on Windows system. However, you can use BCC5.5 directly in DOS command line window without integrated with TextPad. At Oneonta, we are talking about BCC5.5 which is integrated to TextPad Editor environment on Windows system. However, you can use BCC5.5 directly in DOS command line window without integrated with TextPad.

12 3 Stages of Compilation Stage 1: Preprocessing Performed by a program called the preprocessor Performed by a program called the preprocessor Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code. Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code. Strips comments and white spaces from the code Strips comments and white spaces from the code The original source code as stored on disk is not modified. The original source code as stored on disk is not modified.

13 3 Stages of Compilation (con’t) Stage 2: Compilation o Performed by a program called the compiler o Translates the preprocessor-modified source code into object code or executable code (machine code). o Checks for syntax errors and warnings o Saves the translated result (object code or machine code) to a disk file o If any compiler errors are received, no object code file will be generated. o An object code file will be generated if only warnings, not errors, are received.

14 3 Stages of Compilation (con’t) Stage 3: Linking o Combines the program object code with other object code to produce the executable file. o The other object code can come from the Run- Time Library, other libraries, or object files that you have created. o Saves the executable code to a disk file. If any linker errors are received, no executable file will be generated.

15 The first C Program /* Filename: helloOneonta.c /* Filename: helloOneonta.c Author: Sen Zhang Author: Sen Zhang Date written: Aug/22/2004 Date written: Aug/22/2004 Description: This program prints the greeting “Hello, Oneonta!” Description: This program prints the greeting “Hello, Oneonta!” */ */ #include #include int main ( ) int main ( ) { printf (“Hello, Oneonta!\n”) ; printf (“Hello, Oneonta!\n”) ; return 0 ; return 0 ; }

16 Anatomy of a C Program program header comment program header comment preprocessor directives (if any) preprocessor directives (if any) int main ( ) int main ( ) { statement(s); statement(s); return 0 ; return 0 ; }

17 Program Header Comment A comment is descriptive text in a program which is intended only for humans reading the program, but not for compilers translating the comment itself. A comment is descriptive text in a program which is intended only for humans reading the program, but not for compilers translating the comment itself. A comment is marked specially using special characters so that it will be ignored when the program is to be compiled. These special characters are called comment delimiters. A comment is marked specially using special characters so that it will be ignored when the program is to be compiled. These special characters are called comment delimiters.

18 Two ways of commenting 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 block of code. 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 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 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 END commenting. 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 its own BEGIN and END. We use /* to BEGIN commenting, and */ to END commenting. Usually should not be nested. Usually should not be nested. The program header comment usually, if not always, comes first. The program header comment usually, if not always, comes first. Look at the class web page for the required contents of our header comment. Look at the class web page for the required contents of our header comment.

19 Preprocessor Directives Lines that begin with a # in column 1 are called preprocessor directives (commands). They are not C statements! No ending semicolons!!! Lines that begin with a # in column 1 are called preprocessor directives (commands). They are not C statements! No ending semicolons!!! Example: the #include directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. Example: the #include directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. This header file was included because it contains information about the printf ( ) function that is used in this program. This header file was included because it contains information about the printf ( ) function that is used in this program.

20 Head files A text file that contains declarations used by a group of functions or programs. A text file that contains declarations used by a group of functions or programs. Also known as an include file. Also known as an include file.

21 int main ( ) Every program must have a function called main. This is where program execution begins. Every program must have a function called main. This is where program execution begins. main() is usually placed in the source code file as the first function for readability, but actually even physically it is not the first function in a file, it still is the entry point to any program. main() is usually placed in the source code file as the first function for readability, but actually even physically it is not the first function in a file, it still is the entry point to any program. The reserved word “int” indicates that main() returns an integer value. The reserved word “int” indicates that main() returns an integer value. The parentheses following identifier “main” indicate that it is a function, not a variable. The parentheses following identifier “main” indicate that it is a function, not a variable.

22 The Function Body A left brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- ends the function body. A left brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- ends the function body. The style is to place these braces on separate lines and to indent the entire function body 3 to 5 spaces. The style is to place these braces on separate lines and to indent the entire function body 3 to 5 spaces.

23 printf (“Hello, Oneonta!\n”) ; This line is a C statement. This line is a C statement. It is a call to the function printf ( ) with a single argument (parameter), namely the string “Hello, Oneonta!\n”. It is a call to the function printf ( ) with a single argument (parameter), namely the string “Hello, Oneonta!\n”. Even though a string may contain many characters, the string itself should be thought of as a single unit. Even though a string may contain many characters, the string itself should be thought of as a single unit. Notice that \n is a special character to indicate that a newline will be output. Notice that \n is a special character to indicate that a newline will be output.

24 Statements Notice also that this line ends with a semicolon. Notice also that this line ends with a semicolon. All statements in C end with a semicolon. All statements in C end with a semicolon. A program statement is like a sentence in English, except that it is terminated by a semi- colon, instead of a full stop. A program statement is like a sentence in English, except that it is terminated by a semi- colon, instead of a full stop. A statement can be an variable declaration, assignment, a function call, a return or usually a combination of them. A statement can be an variable declaration, assignment, a function call, a return or usually a combination of them.

25 return 0 ; Because function main() is defined to return an integer value, there must be inside the function body a statement that indicates what this value is. Because function main() is defined to return an integer value, there must be inside the function body a statement that indicates what this value is. The statement The statement return 0 ; indicates that main() returns a value of zero to the operating system. A value of 0 indicates that the program successfully terminated execution. You can use other integer value to indicate failure event whenever appropriate. A value of 0 indicates that the program successfully terminated execution. You can use other integer value to indicate failure event whenever appropriate.

26 Summarize An English paper is a sequence of statements that have to follow the rules of English language. An English paper is a sequence of statements that have to follow the rules of English language. A C program is a sequence of statements that have to be formed in accordance with the predefined syntax of C language. A C program is a sequence of statements that have to be formed in accordance with the predefined syntax of C language.


Download ppt "Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004"

Similar presentations


Ads by Google