Introduction Programming with C CSCI 112, Spring 2015 Patrick - - PowerPoint PPT Presentation
Introduction Programming with C CSCI 112, Spring 2015 Patrick - - PowerPoint PPT Presentation
Introduction Programming with C CSCI 112, Spring 2015 Patrick Donnelly Montana State University Introduction What is Computer Programming? Programming with C (CSCI 112) Spring 2015 2 / 15 Introduction What is Computer Programming?
Introduction
What is Computer Programming?
Programming with C (CSCI 112) Spring 2015 2 / 15
Introduction
What is Computer Programming?
- problem-solving or “puzzle solving”
Programming with C (CSCI 112) Spring 2015 2 / 15
Introduction
What is Computer Programming?
- problem-solving or “puzzle solving”
- translation of a problem to an executable computer program
Programming with C (CSCI 112) Spring 2015 2 / 15
Introduction
What is Computer Programming?
- problem-solving or “puzzle solving”
- translation of a problem to an executable computer program
- designing, writing, testing, debugging, and maintaining the
source code of computer program
Programming with C (CSCI 112) Spring 2015 2 / 15
Introduction
What is Computer Programming?
- problem-solving or “puzzle solving”
- translation of a problem to an executable computer program
- designing, writing, testing, debugging, and maintaining the
source code of computer program
- combination of high-level design and low-level implementation
details
Programming with C (CSCI 112) Spring 2015 2 / 15
Stages of Problem Solving
Goal: use a computer to solve a problem Typical stages of building a solution:
1 Specify the problem 2 Analyze the problem 3 Design an algorithm to solve the problem 4 Implement the algorithm (write the program) 5 Test and verify the completed program 6 Maintain and update the program
Programming with C (CSCI 112) Spring 2015 3 / 15
Low Level vs High Level Languages
High Level Languages
- Uses algebraic expressions and symbols from natural languages.
- These languages are human-friendly and farther from machine
language.
- Examples: C, Java, Python
Assembly Language
- Uses mnemonic codes to represent machine language instructions.
- This is a low-level language, very close to machine language.
Machine Language
- A collection of 0’s and 1’s that can be interpretted by the processor.
- There is a different machine language for every processor family.
Programming with C (CSCI 112) Spring 2015 4 / 15
Code Examples
C Code Assembly Code Machine Code (Hex Representation)
Programming with C (CSCI 112) Spring 2015 5 / 15
Review: Bits and Bytes
BIT
- Name comes from BInary digiT.
- It is either a 0 or a 1.
Byte
- A collection of bits big enough to represent a single character.
- Typically bytes consist of 8 bits.
Example
10110101 11010010 10010100 10100110 11010010 10100110 A B C D B D
Programming with C (CSCI 112) Spring 2015 6 / 15
Review: Memory Cells
There are many different types of memory available in a computer system. Regardless
- f the type, we can view the memory as an
array of addressable cells.
- A memory cell is an individual storage
space in memory.
- The address of a memory cell can be
thought of as an index into this memory array.
- The contents of a memory cell is the
data stored at that location.
Programming with C (CSCI 112) Spring 2015 7 / 15
Review: Memory Cells
Every memory cell always has content.
- The content of a cell can either be a
program instruction or data.
- We as programmers are required to
correctly interpret memory cell contents.
Example Cell Contents
- 285
- -29.6
- 0.02
- T
- $
- MOV
Programming with C (CSCI 112) Spring 2015 8 / 15
C Language
Designed by: Dennis Richie Appeared in: 1972 Bell Labs Paradigm: imperative Extension: .c, .h Influenced by: ALGOL, Assembly, PL/I, FORTRAN Features: powerful set of operators poor type checking fast run time performance useful for lower-level systems programming unchecked access to computer memory Domains: designed as a systems language used in many application areas Contributions: syntax influence is pervasive
Programming with C (CSCI 112) Spring 2015 9 / 15
C gcd Function
int gcd_iter(int u, int v) { int t; while (v) { t = u; u = v; v = t % v; } return u < 0 ? -u : u; /* abs(u) */ }
Programming with C (CSCI 112) Spring 2015 10 / 15
The C Programming Language
Features
- Low-level access to computer memory via machine addresses.
- Low-level (bit-wise) programming available.
- Good compilers for all platforms.
- Libraries provided by American National Standards Institute (ANSI).
Implications
- The resulting machine code is compact, efficient, and natively run.
- Programs can be extremely fast, and as a result is the most
commonly used language for writing system software.
- C places a lot of trust in, and provides freedom to, programmers.
- C does not protect you from yourself, so be careful!
Programming with C (CSCI 112) Spring 2015 11 / 15
What C Does Not Do
- No Objects (but they are in C++)
- No native support for multithreading and networking
(although there are popular libraries for these).
- No standard libraries for graphics and other (now common)
programming needs.
- Limited error detection.
- No garbage collector.
- No bounds checking of arrays and allocated memory segments.
- No standards for exception handling.
Programming with C (CSCI 112) Spring 2015 12 / 15
Compiling
Compilation: The process of translating the source code (high-level) into executable code (machine-level). Source File: A file containing the program code. A compiler turns the Source File into an Object File. Object File: A file containing machine language instructions. A linker turns the Object File into an Executable File. Executable File: This is the binary file readable by the machine. This is what is typically referred to as the “program”.
Programming with C (CSCI 112) Spring 2015 13 / 15
The Compiler
Integrated Development Environment (IDE)
- IDE’s allow you to edit code and compile in the same environment.
- Editing/debugging features to make programming easier.
- Commercial IDEs like Visual Studio and Borland can be expensive.
- There are open source projects like Eclipse and Netbeans that
usually work quite well.
- We will not use an IDE for this course.
The GCC Compiler
- We will be using the GCC command line tool for this course.
- GCC is a free and extremely powerful C compiler.
- It has been optimized for years and is quite simple to use.
Programming with C (CSCI 112) Spring 2015 14 / 15
Programming with C (CSCI 112) Spring 2015 15 / 15