CS240: Programming in C. Lecture 2: Overview

Similar documents
CS240: Programming in C

GNU Make 1. 1 material adapted from GNU Make by Richard Stallman

Chapter 2: An Introduction to Makeles 5 2 An Introduction to Makeles You need a le called a makele to tell make what to do. Most often, the makele tel

CS Programming In C

Work relative to other classes

Intermediate Programming, Spring 2017*

2 Compiling a C program

Basic Compilation Control with Make

Build. System building

C - Basics, Bitwise Operator. Zhaoguo Wang

Topic 6: A Quick Intro To C

Introduction to Supercomputing

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

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

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

211: Computer Architecture Summer 2016

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

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

Program Development Tools

Presented By : Gaurav Juneja

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

UNIT 7A Data Representation: Numbers and Text. Digital Data

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

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Lectures 5-6: Introduction to C

CS Basics 15) Compiling a C prog.

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

Chapter 7. Basic Types

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Chapter 11 Introduction to Programming in C

(T) x. Casts. A cast converts the value held in variable x to type T

PRINCIPLES OF OPERATING SYSTEMS

C Introduction. Comparison w/ Java, Memory Model, and Pointers

LESSON 4. The DATA TYPE char

Lecture 03 Bits, Bytes and Data Types

Chapter 11 Introduction to Programming in C

Week 3 Lecture 2. Types Constants and Variables

EL2310 Scientific Programming

Chapter 11 Introduction to Programming in C

EL2310 Scientific Programming

Variables and literals

SWEN-250 Personal SE. Introduction to C

Computers Programming Course 5. Iulian Năstac

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Chapter 11 Introduction to Programming in C

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

Lectures 5-6: Introduction to C

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

BSM540 Basics of C Language

CSE 351. Introduction & Course Tools

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Lecture 3: C Programm

Programming refresher and intro to C programming

Programming in C. What is C?... What is C?

Chapter 11 Introduction to Programming in C

COMP2121: Microprocessors and Interfacing. Number Systems

Follow us on Twitter for important news and Compiling Programs

Lecture 2: C Programm

Introduction to Supercomputing

Chapter 11 Introduction to Programming in C

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

C introduction: part 1

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

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

Lecture 3. More About C

1 You seek performance 3

Outline. Configuration management. Main Phases MOTIVATION

CS 61C: Great Ideas in Computer Architecture Introduction to C

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

BSM540 Basics of C Language

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

A Fast Review of C Essentials Part I

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

Your first C and C++ programs

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

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

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

PROGRAM COMPILATION MAKEFILES. Problem Solving with Computers-I

EC 413 Computer Organization

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Embedded Systems - FS 2018

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

CSE 333 Midterm Exam 7/27/15 Sample Solution

SISTEMI EMBEDDED. Basic Concepts about Computers. Federico Baronti Last version:

EECS2031 Software Tools

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

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

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

CS 473: Algorithms. Chandra Chekuri 3228 Siebel Center. Fall University of Illinois, Urbana-Champaign

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16

Final Labs and Tutors

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

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

CSC231 C Tutorial Fall 2018 Introduction to C

Transcription:

CS240: Programming in C Lecture 2: Overview 1

Programming Model How does C view the world? Stack Memory code Globals 2

Programming Model Execution mediated via a stack function calls and returns local variables Variables name memory and globals contents of memory contain values or references (pointers) to other pieces of memory stack frame context v1 v2 Memory v3 3

Programming Model Imperative Program execution defined by effects on memory and globals Control-flow expressed primarily through: loops and iteration conditionals heavyweight function calls Variables are containers for values x = 3 -- x is a memory cell that holds 3; it s not the same as 3 referential transparency (or lack thereof) Aggregate structures (records, arrays) define contiguous (potentially variable-sized) regions of memory 4

Types Abstractly, we can think of a type as a set of values and operations on those values in Java, a type is a class that represents a set comprising the fields of the object and its methods In C, types have a much less rigid definition a convenient way of reasoning about memory layout all values (regardless of their type) have a common representation as a sequence of bytes in memory 5

Types Reflects underlying machine model There are base types (int, char,...) There are pointers to base types (*int, *char,...) There are pointers to pointers to base types (**int, **char,...) and so on Arrays and structs provide a way of aggregating and naming regions of memory Functions have types that reflect their input/output behavior: int f (char c[]) return type argument type We can have pointers to functions just as we have pointers to base types. But, functions are not values... 6

Why understanding types matter Types define an abstraction or approximation of a computation... what is a specification? More practically, there are implicit conversions that take place and they may result in truncation, and... Some data types are not interpreted the same on different platforms, they are machine-dependent 7

Type representation How is data represented in memory? 0xbffffaab 10101101 8 bits = byte (2 8 = 256 different values ) Data unit is bytes, for representing positive and negative values, 1 bit is used for representing sign The address of the location of memory where the data is stored is also a number and a type (it is a pointer) 8

Type representation char: 1 byte int: an integer, the size of integers on that machine typically 4 bytes float: single-precision floating point typically 4 bytes 1 sign bit, 8 bit exponent, 24 bit significand double: double-precision floating point typically 8 bytes 1 sign bit, 11 bit exponent, 52 bit significand 9

Basic types Type Size (byes) Value char 1-128 to 127 short(int) 2-32768 to 32767 long(int) 4 or 8 You compute int Machine dependent unsigned char 1 0 to 255 unsigned short 2 0 to 65535 unsigned long 4 You compute float 4 double 8 long double 12 sizeof(x) returns the size in bytes. 10

Integers Its size depends on the machine architecture (often 4 bytes). For portability purposes, many programs define int32 as a 4 bytes int and work with this type instead of int. Additional types: short int or short (2 bytes) long int or long (4 bytes) 11

Signed vs. unsigned Other types: unsigned char unsigned int The range of int values that can be represented if size of int is 4 bytes? signed: -2^31 to 2^31-1 (two s complement) unsigned: 0 to 2^32 12

Characters representation ASCII code (American Standard Code for Information Interchange): defines 128 character codes (from 0 to 127), In addition to the 128 standard ASCII codes there are other 128 that are known as extended ASCII, and that are platformdependent. Examples: The code for A is 65 The code for a is 97 The code for 0 is 48 13

Static libraries Library: file containing several object files used as a single entity in the linking phase of a program. the library is indexed, so it is easy to find symbols (functions, variables) in them. Static libraries: collections of object files that are linked into the program during the linking phase of compilation. Examples: Unix: XXX.a Windows: XXX.lib 14

Static vs. shared libraries Shared libraries: Only one copy of the library is stored in memory at any given time (use less memory to run our programs, the executable files are much smaller). Slightly slower start of the program. Static libraries: Each process has its own copy of the static libraries is using, loaded in memory. Executable files linked with static libraries are bigger. 15

Shared libraries Shared libraries (dynamic libraries) are linked into the program in two stages. During compilation time, the linker verifies that all the symbols (functions, variables) required by the program, are either linked into the program, or in one of its shared libraries. The object files from the dynamic library are not inserted into the executable file. When the program is started, a program in the system (called a dynamic loader or linker) checks out which shared libraries were linked with the program, loads them to memory, and attaches them to the copy of the program in memory. 16

libc C Standard library is an interface standard which describes a set of functions and their prototype used to implement common operations Libc is the implementation of the C Standard library on UNIX systems libc is linked in by default as a shared library 17

Let s speak C Hello World #include<stdio.h> int main() { /* every program must have a main */ printf("hello world!\n"); } return 0; gcc c hello.c /* compile*/ gcc o hello hello.o /* link */ OR gcc hello.c /* compile and link */ 18

Main function Every C program has to have a main It has to be declared int main for portability Returning 0 means the program exited OK The return value is interpreted by the operating system Main takes arguments: We will see later how to pass parameters to a program using main 19

More C #include <stdio.h> int main() { int c; } c = getchar(); while(c!= EOF) { putchar(c); c = getchar(); } return 0; 20

Making our coding life easier Source and header files compiled in object files and then linked in an executable. Requires linking with other external libraries. For complex projects, need for an organized and efficient way to do this. The make utility automatically determines which pieces of a large program need to be recompiled and issues commands to recompile them. can be used with any programming language (not only C) whose compiler can be run with a shell command. not limited to programs: documentation, distribution. 21

Running make Write a make file, the default name is Makefile`` that describes the relationships among files in the program and provides commands for updating each file. Then run from the shell the command: make make f Makefile_name The `make' program uses the Makefile data base and the last-modification times of the files to decide which of the files need to be updated. For each of those files, it issues the commands recorded in the data base. 22

An example of a Makefile C files: main.c, command.c, display.c, utils.c H files: defs.h, command.h This is a tab edit : main.o command.o display.o utils.o gcc -o edit main.o command.o display.o \ utils.o main.o : main.c defs.h gcc -c main.c command.o : command.c defs.h command.h gcc -c command.c display.o : display.c defs.h gcc -c display.c utils.o : utils.c defs.h gcc -c utils.c clean : 23

Variables and implicit rules It is not necessary to spell out the commands for compiling the individual C source files, `make' can figure them out: it has an "implicit rule" for updating a `.o' file from a correspondingly named `.c' file using a `gcc -c' command. To simplify writing make files, one can define variables: OBJS = main.o command.o display.o utils.o edit : $(OBJS) cc -o edit $(OBJS) 24

all: hello hello : helloworld.o gcc -o hello helloworld.o helloworld.o : helloworld.c gcc -c helloworld.c clean: rm hello helloworld.o 25

Some portability issues Representation issues Endianess Integer representation Size Alignment Standard libraries Sometimes the functions that have the same functionality have different names/parameters on different platforms; Use the right one Include the right header files Link with the right libraries 26

Byte order Different systems store multibyte values (for example int) in different ways. HP, Motorola 68000, and SUN systems store multibyte values in Big Endian order: stores the high-order byte at the starting address Intel 80x86 systems store them in Little Endian order: stores the low-order byte at the starting address. Data is interpreted differently on different hosts. 27

Printf format c Character d or i Signed decimal integer f Decimal floating point s String of characters u Unsigned decimal integer x Unsigned hexadecimal integer p Pointer address NOTE: read printf man pages for additional formats 28

What will this program output? #include <stdio.h> int main() { char c = a ; printf( %c %d %x \n", c, c, c); } return 0; 29

What will this program output? #include <stdio.h> int main() { char c = a ; printf( %c %d %x \n", c, c, c); a 97 61 } return 0; 29

What can go wrong? include <stdio.h> int main () { short s = 9; long l = 32768; printf("%d\n", s); s = l; printf("%d\n", s); } return 0; 30

What can go wrong? include <stdio.h> int main () { short s = 9; long l = 32768; printf("%d\n", s); s = l; printf("%d\n", s); 9-32768 } return 0; 30