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

Slides:



Advertisements
Similar presentations
The Assembly Language Level
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Introduction to C Programming
COSC 120 Computer Programming
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Introduction to C Programming
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
C programming Language and Data Structure For DIT Students.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
CHAPTER 1: INTORDUCTION TO C LANGUAGE
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Introduction to C/C++ Programming This lecture has major focus on programming, compilation.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
C Programming language
H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It.
Creating your first C++ program
Programming With C.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Computer Programming I Hour 2 - Writing Your First C Program.
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C CMSC 104, Section 4 Richard Chang 1.
Introduction to C 2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park 1.
CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation.
CMSC 1041 Introduction to C Creating your first C program.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Introduction. Natural Languages are languages of the Humans These Languages tend to be ambiguous A sentence in natural may not have just one meaning I.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Introduction to C Programming
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
CSC201: Computer Programming
Introduction to C Language
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
Chapter 2 - Introduction to C Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Introduction to Computer Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

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

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

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.

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 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.

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.

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 A new language ``B'' a second attempt A totally new language ``C'' a successor to ``B'‘, developed by Dennis Ritchie A totally new language ``C'' a successor to ``B'‘, developed by Dennis Ritchie By 1973, UNIX OS almost totally written in ``C''. By 1973, UNIX OS almost totally written in ``C''.

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

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: DB 69 0BC0: JNB BC0: JO BC0:0015 6F DB 6F 0BC0: DB 67 0E ----> 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

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

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.

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.

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.

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.

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.

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 ; }

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 ; }

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.