Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Similar documents
EL2310 Scientific Programming

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 2, Part I Introduction to C Programming

Programming I Laboratory - lesson 01

EL2310 Scientific Programming

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

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

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

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Instructor. Mehmet Zeki COSKUN Assistant Professor at the Geodesy & Photogrammetry, Civil Eng. (212)

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Programming for Engineers Introduction to C

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Week 2: Data and Output

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Work relative to other classes

BLM2031 Structured Programming. Zeyneb KURT

EL2310 Scientific Programming

Some Computer Preliminaries

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Full file at C How to Program, 6/e Multiple Choice Test Bank

Fundamentals of Programming Session 4

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 1 & 2 Introduction to C Language

A Fast Review of C Essentials Part I

Lecture 2 Tao Wang 1

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #03 The Programming Cycle

C Language, Token, Keywords, Constant, variable

Section 2.2 Your First Program in Java: Printing a Line of Text

IS12 - Introduction to Programming. Lecture 7: Introduction to C

C: How to Program. Week /Mar/05

UEE1302 (1102) F10: Introduction to Computers and Programming

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

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

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

1.1 Introduction to C Language. Department of CSE

Chapter 2 - Introduction to C Programming

SWEN-250 Personal SE. Introduction to C

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

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

Lesson 1: Hello, world! Line by line explanation

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

A college level course syllabus can be constructed around this book and its examples, suitable for both hardware and software engineers.

Algorithms and Programming I. Lecture#12 Spring 2015

LECTURE 02 INTRODUCTION TO C++

CMPE-013/L. Introduction to C Programming

Topic 6: A Quick Intro To C

Introduction to the C Programming Language

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

ECE 471 Embedded Systems Lecture 4

Your first C and C++ programs

Software Lesson 1 Outline

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Chapter 1 INTRODUCTION

Lecture 2: C Programming Basic

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Section 2.2 Your First Program in Java: Printing a Line of Text

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CSc Introduction to Computing

Introduction to Computer Systems

Reviewing all Topics this term

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Intermediate Programming, Spring 2017*

Lectures 5-6: Introduction to C

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Problem Solving With C++ Ninth Edition

Computer Programming

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

INTRODUCTION 1 AND REVIEW

Lectures 5-6: Introduction to C

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

Preview from Notesale.co.uk Page 6 of 52

Tutorial No. 2 - Solution (Overview of C)

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 2. Lexical Elements & Operators

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

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Full file at

CS Prof J.P.Morrison

EC 413 Computer Organization

Typescript on LLVM Language Reference Manual

Chris Riesbeck, Fall Introduction to Computer Systems

A Fast Review of C Essentials Part II

Variables and literals

Lecture Set 2: Starting Java

Lecture 03 Bits, Bytes and Data Types

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

Transcription:

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Topics History of Programming Languages Compilation Process Anatomy of C CMSC 104 Coding Standards

Machine Code In the beginning, people wrote programs in machine code (i.e., binary) Error prone Not exactly fun Nobody does this anymore Machine code (obviously) only ran on specific machines 01111100111000110001100111010110 01111101000001000010000111010110 01111100011001110100001000010100 01001110100000000000000000100000

Assembly As a shortcut, programmers created pneumonic words for certain binary patterns Computer converts these words to binary code Still not fun, but better than nothing Still specific to particular machines Still in use today -- including by your friendly lecturer! MULLW R7,R3,R3 MULLW R8,R4,R4 ADD R3,R7,R8 BLR

High-Level Languages Fortran - scientific computing Oldest programming language still in use (1957) Lisp - based on lambda calculus Second oldest language still used today (1958) S1 = 3.0 Language no longer tied to S2 = 4.0 specific machine H2 = (S1 * S1) + (S2 * S2) (defun sumsquare (s1 s2) (+ (* s1 s1) (* s2 s2)))

C Derived from language B, 1972 Fairly efficient, close to the machine Language that Linux is written in Most widely used computer language today Portable assembly int sumsquare(int s1, int s2) { return ((s1*s1) + (s2*s2)); }

Why Different Computer Languages? Each language designed to meet certain goal(s): Speed Size (in bytes of memory) Ease of learning Ease of writing

Compilation Process In the end, computers still operate in binary Programmers use a text editor, such as nano, to create a source file containing source code They then run a compiler Compiler reads source file Converts source code within into machine code

Sample C Program Source File #include <stdio.h> int main(void) { printf("hello, world!\n"); return 0; } (This is a valid, syntactically and semantically correct C program.)

Actual File Contents #include <stdio.h> int main(void) { printf("hello, world!\n"); return 0; } ASCII encoding 0x23 0x69 0x6e 0x63 0x6c 0x75 0x64 0x65 0x20 0x3c 0x73 0x74 0x64 0x69 0x6f 0x2e...

Compilation Stage 1: Preprocessing Performed by preprocessor Performs textual substitutions, in RAM, according to preprocessor directives Does not modify original source code on disk Include files have filenames that end in.h More about include files over the course of semester

Compilation Stage 2: Translation Performed by compiler Translates preprocessor-modified code into object code Displays syntax errors [Can] save syntactically correct object code to object file Object files filenames end in.o

Compilation Stage 3: Linking Performed by linker Combines ( links together ) all object files to produce executable Displays semantic errors Can add object code from runtime libraries Saves executable to disk

Invoking the UMBC Compiler Suppose previous program was saved to file named foo.c -Wall means show To run preprocessor and compiler: all warnings gcc -Wall foo.c -c -o foo.o To run linker: gcc foo.o -o foo To execute program:./foo Note the./ Or combine these two steps: gcc -Wall foo.c -o foo

Compilation Summary you, the programmer foo.c preprocessed code in RAM text editor preprocessor foo.o foo compiler linker shell foo program is executed

Anatomy of a C Program #include <stdio.h> int main(void) { printf("hello, world!\n"); return 0; } 1. Copy and paste the above into a file named foo.c 2. Compile it: gcc -o foo foo.c 3. Run program:./foo

Anatomy of a C Program Preprocessor directive #include <stdio.h> int main(void) { printf("hello, world!\n"); return 0; } Entry point into program (i.e., algorithm name) Algorithm contents Boundary of algorithm

Another C Program /** * This is my second program. **/ #include <stdio.h> int main(void) { printf("will\nit\nblend?"); return 0; }

Another C Program /** * This is my second program. **/ #include <stdio.h> int main(void) { printf("will\nit\nblend?"); return 0; } Comment This printf line displays something to the screen

Comments Descriptive text used to aid the reader to understand contents of program Ignored by preprocessor (and compiler and linker) Begin with /* (forward slash, star) and ends upon next instance of */ (star, forward slash) All of your homework submissions must have file header comments

Preprocessor Directives Lines that begin with a # #include <stdio.h> means include contents of a special file named stdio.h at this point in code This is needed for the pre-made printf algorithm

int main(void) {... } Every algorithm in C must have a name This function is named main, takes 0 (void) inputs, and has 1 output (an integer) Every C program must have one, and only one, main() function

Function Bodies Beginning of function denoted by left brace ( { ) End of function denoted by right brace ( } ) By convention, statements within a function are indented by a tab See course website for other acceptable indentation styles

printf("hello, World!\n"); A single C statement Calls pre-made C algorithm (i.e., function) named printf with a single argument (i.e., input) That argument is the string Hello, World!\n Every C statement ends with a semicolon

"Hello, World!\n" String Sequence of characters enclosed by double-quotes Backslash introduces an escape sequence \n represents a newline (as if hitting Enter key) So what does "Will\nit\nblend?" represent?

return 0; Another C statement Because it is a statement, it must end with a semicolon Sets output of this function to the value zero Recall that main() function has a single integer output For this class, your main() always returns 0

General Layout of a C Program 1. File header comment 2. Preprocessor directive(s) 3. int main(void) { 3.1.statement 1; 3.2.statement 2;... 3.3.return 0; 4. }

File Header Comment At top of every source file Comment that contains: Your name and email address If a project, your partner s name and email address as well A description of what the program does

Coding Style See course web site for C programming standards and indentation guidelines All homework submissions must conform to these standards And thus bad style will negatively impact your grade Note: comments and indentation are not required during exams