Welcome to C CSCI 112: Programming in C C is a high-level, - - PowerPoint PPT Presentation

welcome to c
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Welcome to C

CSCI 112: Programming in C

slide-2
SLIDE 2

What is 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

slide-3
SLIDE 3

Why learn C?

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

slide-4
SLIDE 4

The World is Run By C

– macOS – Windows – Linux – iOS – SQL databases – Graphics programs – Most of your car

slide-5
SLIDE 5

System Architecture

CSCI 112: Programming in C

slide-6
SLIDE 6

Hardware & Software

– 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

slide-7
SLIDE 7

Memory

What is data?

– \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

  • n modern systems)

– 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?

slide-8
SLIDE 8

Memory

Main Memory

– 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

  • f the computer before they can be

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.

slide-9
SLIDE 9

Memory

Secondary Storage

– Stores large amounts of data at low cost. – Slower than RAM – Organized into files. – Can store data and programs

slide-10
SLIDE 10

CPU

– The Central Processing Unit is responsible for coordinating computer

  • perations and performing arithmetic and logic operations on data.

– It retrieves an instruction from memory, determines which data it needs, fetches that data from memory, and performs the arithmetic/logic

  • peration on that data.

– 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

slide-11
SLIDE 11

Input/Output

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

slide-12
SLIDE 12

Which of these hardware components are necessary for a computer to

  • perate?
slide-13
SLIDE 13

Software

What is a program?

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

slide-14
SLIDE 14

Software

High Level Languages Assembly Language Machine Language

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

slide-15
SLIDE 15

Software

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.

slide-16
SLIDE 16

Software

The Operating System

– 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

slide-17
SLIDE 17

Software

Compilation and Linking: From High-Level languages to Machine Code

– 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!

slide-18
SLIDE 18

Software Development Method

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

slide-19
SLIDE 19

Software Development Method

– 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

  • subproblem. Once they are all solved, the original problem will be

solved. – Can save time by doing a desk check on an algorithm by running it

  • n paper in the way a computer would.
slide-20
SLIDE 20

Software Development Method

– 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