Welcome to C
CSCI 112: Programming in C
Welcome to C CSCI 112: Programming in C C is a high-level, - - PowerPoint PPT Presentation
Welcome to C CSCI 112: Programming in C C is a high-level, imperative programming language C code maps to machine instructions very efficientlyits What is C? lightweight and has wide applications Originally developed in 10973 at
CSCI 112: Programming in C
C is a high-level, imperative programming language C code maps to machine instructions very efficiently—it’s lightweight and has wide applications Originally developed in 10973 at Bell labs, used to implement the Unix OS
There are many languages which are “higher level”—why learn such an old, lower-level language?
Age--C has 30 years of maturity. It’s well understood. Many resources available. Lingua Franca—C can be used to express a ay to solve a problem in a way everyone understands Foundational—The concepts of C show up in most other languages in one form or another No bell and whistles: learning to work at such a low level will make you a better programmer. You’ll have an understanding of exactly what is happening under the hood, and when you move to a higher- level language like Java, this will help you write faster, more robust code.
macOS Windows Linux iOS SQL databases Graphics programs Most of your car
CSCI 112: Programming in C
What is hardware, what is software? Hardware is equipment used to perform computations Main memory Secondary storage Central Processing Unit (CPU) Input/output devices Software is a list of instructions
\Data in memory is composed of bits
0’s and 1’s representing base 2 numbers Called binary
Bits make up bytes (8 bits per byte
1 kilobyte = 1,000 bytes = 8,000 bits 1 megabyte = 1 million bytes Examples:
Binary Base 10 1 1 10 2 11 3 110 4 What’s the largest base-10/decimal number 4 bits can represent?
Also known as RAM Memory can be though of as a series of “cells” Each cell holds data or a program instruction
Stored program concept Programs must be loaded into the memory
executed. Data cannot be manipulated by the computer until it is first stored in memory.
Every cell has an address and contents, called a word word—a collection of bytes
32 or 64 bits (4 or 8 bytes) on modern systems
Memory cells are never empty, but their contents are meaningless until initialized (set) by the program.
Stores large amounts of data at low cost. Slower than RAM Organized into files. Can store data and programs
The Central Processing Unit is responsible for coordinating computer
It retrieves an instruction from memory, determines which data it needs, fetches that data from memory, and performs the arithmetic/logic
It then stores the result back into memory CPU’s are actually made up of several different components, including:
Arithmetic Logic Unit Registers
The CPU knows only how to read and execute machine code
Input devices allow data to be entered into memory and accessed by program
Mice Keyboard Scanner Camera
Output devices present results of program back to users
Terminal Graphics Printers
Programs access input data and write to output devices via main memory.
Which of these hardware components are necessary for a computer to
Set of instructions that tells the computer to do things. Written in a ‘programming language’ - C is one such language Programming languages range from machine languages (meaningful to the computer) to high-level languages (meaningful to the programmer) Files that contain code for a program are called 'source files' Instructions are processed one line at a time in consecutive order.
Algebraic expressions, human-readable Basic mnemonic instructions, each line represents 1 machine code instruction A binary string representing one instruction to be performed by CPU
C Code x = g + h; Assembly Language LOAD g, $1 LOAD h $2 ADD $3, $1, $2 STORE $3, x Machine Language (for ADD instruction) 01001 01011 000000010000001 000100
Don’t worry about understanding the assembly or machine code, I just want you to understand how we go from high-level C code down to machine code.
The OS is the base software, the bridge between the programs we write and the hardware that executes them It takes care of many low-level tasks:
Receives commands Manages memory and gives our programs access to it Schedules tasks, i.e. processor time Collects input Conveys output Reads and writes data to storage
We need to get our source files (C, in this case) into machine language that our CPU understands
This is called compiling We do this for each file in our program First checks for syntax errors, halts if it finds any Outputs object files-–contains machine language instructions in binary format
Next, we need to link these files together
Hooks up references from one file to another Outputs an executable that we can actually run!
Specify problem & requirements
Forces you to state the problem clearly and unambiguously, to gain a clear understanding of what is required to find the solution.
Analyze problem
Identify:
inputs (data you have to work with) outputs (the desired results) Any additional requirements or constraints on the solution
Determine format in which results are to be displayed Develop list of problem variables and their relationships The process of modeling a problem by extracting the essential variables and their relationships is called abstraction.
Design an algorithm to solve the problem.Step 1: develop a list of steps (called an algorithm) to solve the problem. Step 2: verify that the algorithm solves the problem as intended. Use top-down design (divide and conquer). List major steps (subproblems) that need to be solved. Then, solve each
solved. Can save time by doing a desk check on an algorithm by running it
Implement the algorithm.
Convert each algorithm step into one or more statements in a programming language.
Test and verify the completed program
Run the program with several different sets of input data. Try edge cases like 0, -1, and no input at all. Be creative; the more tests you try, the more likely you are to uncover bugs.
Maintain program and update as necessary