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

Midterm Exam Review Slides

Fundamental of Programming (C)

Review Topics. Midterm Exam Review Slides

Lecture 03 Bits, Bytes and Data Types

211: Computer Architecture Summer 2016

Review Topics. Midterm Exam Review Slides

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

Work relative to other classes

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

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

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

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

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

Basic Types and Formatted I/O

Chapter 12 Variables and Operators

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Computing Layers. Chapter 7 Assembly Language

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

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

CSE 333 Lecture 7 - final C details

Chapter 12 Variables and Operators

PROGRAMMAZIONE I A.A. 2017/2018

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

2. Numbers In, Numbers Out

Topic 6: A Quick Intro To C

Fundamentals of Programming

CSE 303 Lecture 8. Intro to C programming

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

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

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Structured programming

Control Flow, Functions and Basic Linkage

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

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

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Introduction to C. Systems Programming Concepts

EC 413 Computer Organization

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

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

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

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

CMPE-013/L. Introduction to C Programming

n Address of a variable in memory n Allows us to indirectly access variables ! Array n A list of values arranged sequentially in memory

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

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

CS240: Programming in C. Lecture 2: Overview

Programming and Data Structures

2. Numbers In, Numbers Out

Chapter 12 Variables and Operators

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

CSE 374 Programming Concepts & Tools

Programming in C - Part 2

Intermediate Code Generation

C and Java some big differences

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

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

Programming Language Basics

CpSc 1111 Lab 4 Formatting and Flow Control

INTRODUCTION 1 AND REVIEW

Program Control Structures

CSE / ENGR 142 Programming I

C introduction: part 1

Chapter 7 Assembly Language. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University

C Language Coding Standard

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Computers Programming Course 5. Iulian Năstac

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

Programming and Data Structure

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

A Fast Review of C Essentials Part I

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

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

PRINCIPLES OF OPERATING SYSTEMS

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Chapter 3: Arrays and More C Functionality

C: How to Program. Week /Mar/05

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Introduction to the C Programming Language

Transcription:

Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified by Chris Wilcox, Yashwant Malaiya Colorado State University

C: A High-Level Language Gives symbolic names to values don t need to know register or memory location Provides abstraction of underlying hardware operations do not depend on instruction set example: a = b * c, even without multiply instruction Provides expressiveness use meaningful symbols that convey meaning simple expressions for control patterns (if-then-else) Enhances code readability Safeguards against bugs enforce rules or conditions at compile-time or run-time 2

C came from B, 3

C among programming languages 4

Compilation vs. Interpretation Different ways of translating high-level language Interpretation interpreter = program that executes program statements generally one line or command at a time limited scope of processing easy to debug, make changes, view intermediate results languages: BASIC, LISP, Perl, Java, Matlab, C-shell Compilation Compiler = program that makes an executable from code translates statements into machine language performs optimization over multiple statements change requires recompilation optimized code can be harder to debug languages: C, C++, Fortran, Pascal 5

Compilation vs. Interpretation Consider the following algorithm: Get W from the keyboard. X = W + W Y = X + X Z = Y + Y Print Z to screen. If interpreting, how many arithmetic operations? If compiling, can we simplify the computation? Yes, by analyzing the entire program, we can reduce to single arithmetic operation! 6

Compiling a C Program Compilers have multiple phases: Preprocessor macro substitution conditional compilation source-level transformations output is still C code Compiler generates machine instructions output is object file Linker combines object files (including libraries) output is executable image 7

Source Code Analysis front end Compiler 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 for efficiency very dependent on target machine Symbol Table map between symbolic names and items like assembler, but more kinds of information 8

A Simple Java Program import java.io.*; public class Simple { /* Function: main */ /* Description: counts down from user input to STOP */ public static void main(string[] args) { /* variable declarations */ public static final int STOP = 0; int counter; /* an integer to hold count values */ int startpoint; /* starting point for countdown */ /* prompt user for input */ System.out.println("Enter a positive number: "); startpoint = Integer.parseInt(in.readLine()); /* count down and print count */ for (counter=startpoint; counter>=stop; counter--) { System.out.println(counter); } }} 9

A Simple C Program #include <stdio.h> #define STOP 0 /* Function: main */ /* Description: counts down from user input to STOP */ int main(int argc, char *argv[]) { /* 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); } 10

Preprocessor Directives #include <stdio.h> Before compiling, copy contents of header file (stdio.h) into source code. Header files typically contain descriptions of functions and variables needed by the program. No restrictions, could be any C source code, including your own. #define STOP 0 Commonly called a macro, before compiling, replace all instances of string "STOP" with "0" Used for values that are constant during execution, but might change if the program is reused. (requires recompilation.) 11

Comments Begins with /*, ends with */ Can span multiple lines Several versions of C Cannot have a comment within a comment c99 allows use of single line comments: // 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 12

main Function Every C program must have a main() function: The main function contains the code that is executed when the program is run. As with all functions, the code for main lives within brackets: main() { /* code goes here */ } Legal syntax, but simplified to defer discussion of return type and command line options. 13

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). int counter; int startpoint; int is a predefined signed integer type in C. 14

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 prints the variable counter as a decimal integer, followed by a linefeed (\n). scanf("%d", &startpoint); String contains formatting directions for interpreting the type of the input. This call reads a decimal integer and assigns it to the variable startpoint. (The & determines the address of the variable.) 15

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 16

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 char is %c.\n", 43+59); produces this output: 43 is a prime number. 43 plus 59 in decimal is 102. 43 plus 59 in hex is 66. 43 plus 59 as a character is f. 17

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, pointers will be discussed later in detail. 18

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 usually controlled by makefile intermediate files -- object (.o), assembler (.s), preprocessor (.i), etc. 19

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 assembly language. Also see C Reference in Appendix D. 20

C Example #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); } 21