Chapter 11 Introduction to Programming in C

Similar documents
Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

COSC121: Computer Systems: Runtime Stack

Lecture 5: C programming

Introduction to C Part 1 HLL s and the Basic Syntax. (Ch11 & 12)

Problem Solving in C. Stepwise Refinement. ...but can stop refining at a higher level of abstraction. Same basic constructs. as covered in Chapter 6

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

printf( Please enter another number: ); scanf( %d, &num2);

Fundamental of Programming (C)

CSCI 2132 Software Development. Lecture 8: Introduction to C

LECTURE 2. Compilers and Interpreters

8/16/12. Computer Organization. Architecture. Computer Organization. Computer Basics

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

CpSc 1111 Lab 4 Formatting and Flow Control

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Basic Types and Formatted I/O

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Control Flow, Functions and Basic Linkage

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Chapter 1. Preliminaries

C - Basics, Bitwise Operator. Zhaoguo Wang

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

Structured programming

PROGRAMMAZIONE I A.A. 2017/2018

EC 413 Computer Organization

211: Computer Architecture Summer 2016

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

EL2310 Scientific Programming

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Lecture 03 Bits, Bytes and Data Types

Chapter 3: Arrays and More C Functionality

Computer Basics 1/24/13. Computer Organization. Computer systems consist of hardware and software.

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

CS240: Programming in C. Lecture 2: Overview

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

Computer Components. Software{ User Programs. Operating System. Hardware

Software Lesson 1 Outline

Fundamentals of Programming

INTRODUCTION 1 AND REVIEW

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

2. Numbers In, Numbers Out

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Topic 6: A Quick Intro To C

Computer Basics 1/6/16. Computer Organization. Computer systems consist of hardware and software.

Computer Components. Software{ User Programs. Operating System. Hardware

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

Have examined process Creating program Have developed program Written in C Source code

Chapter 1 Preliminaries

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

Programming and Data Structures

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Low-Level Languages. Computer Programs and Programming Languages

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic Assignment and Arithmetic Operators

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

History of C Programming Language. Why Name C was given to Language? Summary of C Programming Language History. C Programming Language Timeline:

Design & Implementation Overview

Introduction Presentation A

2. Numbers In, Numbers Out

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Computers Programming Course 5. Iulian Năstac

C and Programming Basics

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Chapter 12 Variables and Operators

Chapter 1. Preliminaries

User Defined Functions 2 Outline

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Fundamentals of Programming Session 4

Computer Software: Introduction

CS Prof J.P.Morrison

Program Control Structures

Högnivåspråk och översättning

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Fundamentals of Programming

Introduction to C. Systems Programming Concepts

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Compilers and interpreters

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

An Introduction to Python (TEJ3M & TEJ4M)

Programming for Engineers Introduction to C

Fundamental of Programming (C)

CS102: Variables and Expressions

3/13/2012. ESc101: Introduction to Computers and Programming Languages

Introduction to Computer Engineering. CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Transcription:

Chapter 11 Introduction to Programming in C

C: A High-Level Language Gives symbolic names for containers of values don t need to know which register or memory location Provides abstraction of underlying hardware operations do not depend on instruction set example: can write a = b * c, even though LC-3 doesn t have a multiply instruction (can be implemented as an invisible subroutine) Provides expressiveness use meaningful symbols that convey meaning simple expressions for common control structures Enhances code readability Safeguards against bugs can enforce rules or conditions at compile-time or run-time 11-2

Compilation vs. Interpretation Different ways of translating high-level language Interpretation interpreter = program that executes program statements generally one line/command at a time less efficient than compilation easy to debug, make changes, view intermediate results languages: BASIC, LISP, Perl, Matlab, C-shell Compilation translates statements into machine language does not execute, but creates executable program more efficient than interpretation change requires recompilation can be harder to debug, since executed code may be different languages: C, C++, Fortran, Pascal 11-3

src: upwork.com 11-4

Compilation vs. Interpretation Interpretation/Compilation Hybrid Java Java language is compiled into JVM (Java Virtual Machine) byte code. The JVM byte code can be either interpreted or compiled into native machine code by a JIT (Just in Time) or AOT (Ahead of Time) compiler. Python Python program is converted into byte code like Java language PVM (Python Virtual Machine) for CPython (the reference implementation of Python) and JVM for Jython. The byte code can be either interpreted or compiled into native machine code by a JIT (Just in Time) or AOT (Ahead of Time) compiler. 11-5

Compilation vs. Interpretation Consider the following simple program: Get W from the keyboard. X = W + W Y = X + X Z = Y + Y Print Z to screen. If interpreting, how many arithmetic operations occur? If compiling, we can analyze the entire program and possibly reduce the number of operations. Can we simplify the above algorithm to use a single arithmetic operation? 11-6

Compiling a C Program Entire mechanism is usually called the compiler Preprocessor macro substitution conditional compilation source-level transformations Compiler output is still C generates object file Linker machine instructions combine object files (including libraries) into executable image Library Object Files Source Code Analysis Target Code Synthesis C Source and Header Files C Preprocessor Compiler Linker Executable Image Symbol Table 11-7

Compiler Source Code Analysis front end parses programs to identify its pieces variables, expressions, statements, functions, etc. depends on language (not on target machine) Code Generation back end generates machine code from analyzed source may optimize machine code to make it run more efficiently very dependent on target machine Symbol Table map between symbolic names and items like assembler, but more kinds of information 11-8

A Simple C Program #include <stdio.h> #define STOP 0 /* Function: main */ /* Description: counts down from user input to STOP */ main() { /* variable declarations */ int counter; /* an integer to hold count values */ int startpoint; /* starting point for countdown */ /* prompt user for input */ printf("enter a positive number: "); scanf("%d", &startpoint); /* read into startpoint */ /* count down and print count */ for (counter=startpoint; counter >= STOP; counter--) printf("%d\n", counter); } 11-9

Preprocessor Directives #include <stdio.h> Before compiling, copy contents of header file (stdio.h) into source code. Header files typically contain constants, descriptions of functions, and variables needed by the program. no restrictions -- could be any C source code #define STOP 0 Before compiling, replace all instances of the string "STOP" with the string "0" Called a macro Used for values that won't change during execution, but might change if the program is reused. (Must recompile.) 11-10

Comments Begins with /* and ends with */ Can span multiple lines Cannot have a comment within a comment Comments are not recognized within a string example: "my/*don't print this*/string" would be printed as: my/*don't print this*/string As before, use comments to help reader, not to confuse or to restate the obvious 11-11

main Function Every C program must have a function called main(). This is the code that is executed when the program is run. The code for the function lives within brackets: main() { } /* code goes here */ 11-12

Variable Declarations Variables are used as names for data items. Each variable has a type, which tells the compiler how the data is to be interpreted (and how much space it needs, etc.). int counter; int startpoint; int is a predefined integer type in C. 11-13

Input and Output Variety of I/O functions in C Standard Library. Must include <stdio.h> to use them. printf("%d\n", counter); String contains characters to print and formatting directions for variables. This call says to print the variable counter as a decimal integer, followed by a linefeed (\n). scanf("%d", &startpoint); String contains formatting directions for looking at input. This call says to read a decimal integer and assign it to the variable startpoint. (Don't worry about the & yet.) 11-14

More About Output Can print arbitrary expressions, not just variables printf("%d\n", startpoint - counter); Print multiple expressions with a single statement printf("%d %d\n", counter, startpoint - counter); Different formatting options: %d decimal integer %x hexadecimal integer %c ASCII character %f floating-point number 11-15

Examples This code: printf("%d is a prime number.\n", 43); printf("43 plus 59 in decimal is %d.\n", 43+59); printf("43 plus 59 in hex is %x.\n", 43+59); printf("43 plus 59 as a character is %c.\n", 43+59); produces this output: 43 is a prime number. 43 + 59 in decimal is 102. 43 + 59 in hex is 66. 43 + 59 as a character is f. 11-16

Examples of Input Many of the same formatting characters are available for user input. scanf("%c", &nextchar); reads a single character and stores it in nextchar scanf("%f", &radius); reads a floating point number and stores it in radius scanf("%d %d", &length, &width); reads two decimal integers (separated by whitespace), stores the first one in length and the second in width Must use ampersand (&) for variables being modified. (Explained in Chapter 16.) 11-17

Compiling and Linking Various compilers available cc, gcc includes preprocessor, compiler, and linker Lots and lots of options! level of optimization, debugging preprocessor, linker options intermediate files -- object (.o), assembler (.s), preprocessor (.i), etc. 11-18

Remaining Chapters A more detailed look at many C features. Variables and declarations Operators Control Structures Functions Data Structures I/O Emphasis on how C is converted to LC-3 assembly language. Also see C Reference in Appendix D. 11-19

꼭기억해야할것 Interpretation vs. Compilation and its hybrids High-Level Programming Language provides a high-level abstraction of machine language variables (registers and memory locations) operations (arithmetic / logic instructions) control structures (branch / jump instructions) - sequence - conditional - iterative 11-20