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

Midterm Exam Review Slides

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

211: Computer Architecture Summer 2016

Fundamental of Programming (C)

Review Topics. Midterm Exam Review Slides

Lecture 03 Bits, Bytes and Data Types

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

Review Topics. Midterm Exam Review Slides

Work relative to other classes

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

Chapter 12 Variables and Operators

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

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

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

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

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

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

Topic 6: A Quick Intro To C

CSCI 2132 Software Development. Lecture 8: Introduction to C

CSE 303 Lecture 8. Intro to C programming

Computing Layers. Chapter 7 Assembly Language

Basic Types and Formatted I/O

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

CSE 333 Lecture 7 - final C details

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

PROGRAMMAZIONE I A.A. 2017/2018

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

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

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

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

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

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

Fundamentals of Programming

Programming in C - Part 2

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

Chapter 12 Variables and Operators

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

Structured programming

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

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

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

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

2. Numbers In, Numbers Out

EC 413 Computer Organization

Computers Programming Course 5. Iulian Năstac

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

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

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

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

CS240: Programming in C. Lecture 2: Overview

Chapter 12 Variables and Operators

Programming and Data Structures

Intermediate Code Generation

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

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

C and Java some big differences

Control Flow, Functions and Basic Linkage

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

Fundamentals of Programming

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

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

Programming Language Basics

Fundamental of Programming (C)

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

INTRODUCTION 1 AND REVIEW

CpSc 1111 Lab 4 Formatting and Flow Control

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

C Language Coding Standard

Motivation was to facilitate development of systems software, especially OS development.

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

CSE / ENGR 142 Programming I

C introduction: part 1

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

CMPE-013/L. Introduction to C Programming

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

A Fast Review of C Essentials Part I

Programming and Data Structure

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

Program Control Structures

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Motivation was to facilitate development of systems software, especially OS development.

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

CSE 374 Programming Concepts & Tools

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

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

Should you know scanf and printf?

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

Transcription:

Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University C: A High-Level Language! Gives symbolic names to values dot 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 1

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 3 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! 4 2

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 Library Object Files combines object files (including libraries) output is executable image Source Code Analysis Target Code Synthesis C Source and Header Files C Preprocessor Compiler Linker Executable Image Symbol Table 5 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 for efficiency very dependent on target machine! Symbol Table map between symbolic names and items like assembler, but more kinds of information 6 3

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); 7 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); 8 4

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.) 9 Comments! Begins with /*, ends with */! Can span multiple lines! 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 10 5

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. 11 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. 12 6

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. (Don't worry about the & yet!) 13 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 14 7

Examples! This code: printf("%d is a prime number.\n", 43); printf("43 plus 59 (decimal) is %d.\n", 43+59); printf("43 plus 59 (hex) is %x.\n", 43+59); printf("43 plus 59 (char) is %c.\n", 43+59); produces this output: 43 is a prime number. 43 plus 59 (decimal) is 102. 43 plus 59 (hex) is 66. 43 plus 59 (char) is f. 15 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. 16 8

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. 17 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. 18 9