Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Similar documents
Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

Fundamental Data Types

Data Representation and Binary Arithmetic. Lecture 2

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Fundamentals of Programming (C)

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Midterm CSE 131 Winter 2012

Programming. Data Structure

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Chapter 2 Bits, Data Types, and Operations

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Simple Data Types in C. Alan L. Cox

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Chapter 3. Information Representation

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Chapter 2 Bits, Data Types, and Operations

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Lecture (09) x86 programming 8

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

CSCI 2132 Software Development. Lecture 8: Introduction to C

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

Appendix A Developing a C Program on the UNIX system

Midterm CSE 131 Winter 2014

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

Fundamentals of Programming

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

THE FUNDAMENTAL DATA TYPES

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

CMPE-013/L. Introduction to C Programming

Chapter 8. Characters and Strings

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Number Representations

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

The Binary Number System

Midterm CSE 131 Fall 2014

Final CSE 131 Winter 2010

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Final CSE 131 Fall 2014

Do not start the test until instructed to do so!

ASSIGNMENT 5 TIPS AND TRICKS

Chapter 2 Bits, Data Types, and Operations

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Basic Types and Formatted I/O

6.096 Introduction to C++ January (IAP) 2009

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Do not start the test until instructed to do so!

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Do not start the test until instructed to do so!

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Midterm CSE 131 Winter 2013

Oberon Data Types. Matteo Corti. December 5, 2001

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

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

Variables and data types

CSE 30 Fall 2012 Final Exam

Chapter 2 Number System

Midterm CSE 131 Winter 2015

CSE 30 Spring 2006 Final Exam

Final CSE 131 Winter 2012

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

EE 109 Unit 2. Binary Representation Systems

CSE 30 Spring 2007 Final Exam

Number System (Different Ways To Say How Many) Fall 2016

CSE 30 Fall 2007 Final Exam

Characters Lesson Outline

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

Final CSE 131 Fall 2015

CPSC 301: Computing in the Life Sciences Lecture Notes 16: Data Representation

Fundamental of Programming (C)

3.1. Unit 3. Binary Representation

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Fundamentals of Programming

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

Chapter 3. Fundamental Data Types

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

Number Systems Base r

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

CSE 30 Fall 2013 Final Exam

C Examples. Goals of this Lecture. Overview of this Lecture

Week 3 Lecture 2. Types Constants and Variables

Transcription:

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals Alvin Valera School of Engineering and Computer Science Victoria University of Wellington

Admin stuff People Course Coordinator Lecturer Alvin Valera Alvin.valera@ecs.vuw.ac.nz AM 401 Tutors Alex Mitchell Brady Hanna Celine Young Daniel Ko Inti Mateus Resende Albuquerque Jakob Pfender Sean Stevenson Yueying Stella Chang Class Representatives Hannah Pachoud Christian Lee Stefan Andelic NWEN 241: Systems Programming 2

Admin stuff Helpdesk sessions Mon Tue Wed Thu Fri [09:00-11:00] Helpdesk CO 246 [14:00-16:00] Helpdesk CO 246 [14:00-16:00] Helpdesk CO 246 [14:00-16:00] Helpdesk CO 246 Tutors will be on duty from Weeks 2-11 during Helpdesk sessions Helpdesk sessions are optional; no signup is necessary NWEN 241: Systems Programming 3

Admin stuff Tutorial setup & preparation First tutorial will be a walk-through on how to write and debug C programs using Plain text editor and command line interface Integrated development environment If you want to follow the walk-through session using your laptop*: (1) Install Oracle VM VirtualBox (2) Download NWEN 241 VM to your laptop hard disk *Detailed instructions are available in the course web page: https://ecs.victoria.ac.nz/courses/nwen241_2018t1/tutorialsetup NWEN 241: Systems Programming 4

Recap A C program consists of one or more functions, and one of those functions must be main Program execution begins with the main function /* A simple program */ #include <stdio.h> int main(void) { printf("hello world\n"); } return 0; NWEN 241: Systems Programming 5

This lecture Program structure (continued) Compilation process Data types NWEN 241: Systems Programming 6

Program structure Indicates start of compound statement /* A simple program */ #include <stdio.h> String start and end int main(void) { printf("hello world\n"); Indicates end of compound statement } return 0; Indicates end of statement NWEN 241: Systems Programming 7

Whitespaces A note on whitespaces in C source code: Compiler ignores whitespaces (space, tab, newline), except in string literals and for separating tokens int main(void) { printf("hello world\n"); } return 0; int main(void){printf("hello world\n");return 0;} NWEN 241: Systems Programming 8

Header files #include <stdio.h> A header file usually contains function prototypes, constant definitions, type definitions, etc. Which header file to include? Include header files that contain the function prototype, constant definition, type definition, etc., used in your program NWEN 241: Systems Programming 9

Header files C provides a standard library* which consists of the following headers: <assert.h> <ctype.h> <errno.h> <float.h> <limits.h> <locale.h> *C99 and C11 added more header files. <math.h> <setjmp.h> <signal.h> <stdarg.h> <stddef.h> <stdio.h> <stdlib.h> <string.h> <time.h> To know more about the C standard library, visit https://www.tutorialspoint.com/c_standard_library/index.htm NWEN 241: Systems Programming 10

Identifiers /* Program to calculate the area of a circle */ #include <stdio.h> #define PI 3.14 float sq(float); int main(void) { float radius, area; } /* Ask user to input */ printf("radius = "); scanf("%f", &radius); area = PI * sq(radius); printf("area = %f\n", area); return 0; To uniquely identify constants, variables, functions and labels, you need to name them with an identifier float sq(float r) { return (r * r); } NWEN 241: Systems Programming 11

Identifiers in C An identifier is a sequence of letters and digits The first character must be a letter The underscore character _ counts as a letter Upper and lower case letters are diferent Identifiers may have any length Usually, only the first 31 characters are significant For macro names, only the first 63 characters are significant C reserved keywords cannot be used as identifiers! Use meaningful names when naming, i.e., a name that describes the purpose of the entity NWEN 241: Systems Programming 12

Examples counter Valid: consists of letters _Temp_variable_2 Valid: consists of letters and digits 1myVariable steps{2} continue Invalid: first character is not a letter Invalid: uses non-letter and nondigit characters Invalid: reserved word NWEN 241: Systems Programming 13

Writing a program & compilation Using a text editor and command line interface % vi hello.c /* A simple program */ #include <stdio.h> int main(void) This opens hello.c (if it exists) or creates a new one { printf("hello world\n"); return 0; } NWEN 241: Systems Programming 14

Writing a program & compilation Afer saving your changes to the file, you can then compile it % gcc hello.c -o hello This invokes gcc to compile hello.c and generate executable output file called hello %./hello Hello world This executes hello NWEN 241: Systems Programming 15

Compilation process hello.c Source program (text) Preprocessor (cpp) hello.i Compiler (cc1) Modified source program (text) Preprocessing phase. hello.s Assembly program (text) Assembler (as) printf.o hello.o Relocatable object programs (binary) Linker (ld) The preprocessor (cpp) modifies the original C program according to directives that begin with the '#' character, e.g., #include <stdio.h> command in line 3 of hello.c tells the preprocessor to read the contents of the system header file stdio.h and insert it directly into the program text. The result is another C program, typically with the.i sufix. hello Executable object program (binary) NWEN 241: Systems Programming 16

Compilation process printf.o hello.c Source program (text) Preprocessor (cpp) hello.i Compiler (cc1) Modified source program (text) hello.s Assembly program (text) Assembler (as) hello.o Relocatable object programs (binary) Linker (ld) hello Executable object program (binary) Compilation phase. The compiler (cc1) translates the text file hello.i into the text file hello.s, which contains an assemblylanguage program. NWEN 241: Systems Programming 17

Compilation process hello.c Source program (text) Preprocessor (cpp) hello.i Compiler (cc1) Modified source program (text) hello.s Assembly program (text) Assembler (as) printf.o hello.o Relocatable object programs (binary) Linker (ld) hello Executable object program (binary) Assembly phase. The assembler (as) translates hello.s into machine-language instructions, packages them in a form known as a relocatable object program, and stores the result in the object file hello.o. This file is a binary file containing 17 bytes to encode the instructions for function main. If you try to open hello.o with a text editor, it would appear to be gibberish. NWEN 241: Systems Programming 18

Compilation process hello.c Source program (text) Preprocessor (cpp) hello.i Compiler (cc1) Modified source program (text) hello.s Assembly program (text) Assembler (as) printf.o hello.o Relocatable object programs (binary) Linker (ld) Linking phase. printf function, which is part of the standard C library provided by every C compiler. printf function resides in a separate pre-compiled object file called printf.o, which must be merged with our hello.o program. The linker (ld) performs this merging, creating an executable object file (or simply executable) that is ready to be loaded into memory and executed by the system. hello Executable object program (binary) NWEN 241: Systems Programming 19

Data types What do you see? NWEN 241: Systems Programming 20

Data types 01000001 What do you see? NWEN 241: Systems Programming 21

Data types Programming is about describing data and algorithms How data is represented in memory? Four basic data types: int (integer quantity) char (single character) float (floating-point number) double (double-precision floating-point number) Note: There are also qualifiers associated with some of the types: short / long, and signed / unsigned. NWEN 241: Systems Programming 22

Data types Two groups of types Integral types: int and char Can be used to hold integer values Floating types: float and double Can be used to hold real values NWEN 241: Systems Programming 23

char char is meant to hold 1 ASCII character see https://www.asciitable.com/ 0 NUL 1 SOH 2 STX 3 ETX 4 EOT 5 ENQ 6 ACK 7 BEL 8 BS 9 HT 10 NL 11 VT 12 NP 13 CR 14 SO 15 SI 16 DLE 17 DC1 18 DC2 19 DC3 20 DC4 21 NAK 22 SYN 23 ETB 24 CAN 25 EM 26 SUB 27 ESC 28 FS 29 GS 30 RS 31 US 32 SP 33! 34 " 35 # 36 $ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44, 45-46. 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 125 } 126 ~ 127 DEL NWEN 241: Systems Programming 24

Data types 01000001 What do you see? Interpreted as an integer: 65 Interpreted as an ASCII character: A NWEN 241: Systems Programming 25

Floating types Most computers use the IEEE 754 standard for representing floating types IEEE 754 defines two representations Single precision (32-bit) Positive range: 1.17549435 10-38 3.40282347 10 +38 Double precision (64-bit) Positive range: 2.2250738585072014 10-308 1.7976931348623157 10 +308 NWEN 241: Systems Programming 26

Floating types single: 8 bits double: 11 bits single: 23 bits double: 52 bits S E M x = ( 1) S (1+M ) 2 ( E Bias) S: sign bit (0 non-negative, 1 negative) E: exponent in excess representation Bias = 127 in single Bias = 1023 in double M: Mantissa or fraction part NWEN 241: Systems Programming 27

Data type size Sizes of diferent types Use sizeof() to find out The sizes may vary from machine to machine The following rules are always guaranteed: sizeof(char) = 1 sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) sizeof(signed) = sizeof(unsigned) = sizeof(int) sizeof(float) <= sizeof(double) <= sizeof(long double) Does Java have varied sizes between systems? NWEN 241: Systems Programming 28

Declaring variables A variable must be declared before it can be used A declaration specifies a type, and contains a list of one or more variables of that type int lower, upper, step; char c, line[1000]; Variables can be distributed among declarations in any fashion int lower; int upper, step; char c; char line[1000]; NWEN 241: Systems Programming 29

Declaring variables A variable may be initialized in its declaration If variable name is followed by an equals sign and an expression, the latter serves as an initializer int i = 0; char c = 'A'; float f = 1.25; Possible initializers Constant Expression (depends on whether variable is automatic or not) To be discussed in detail in the future NWEN 241: Systems Programming 30

Type casting Type casting is a way to convert a variable from one data type to another data type C performs automatic type casting int i = 2; double d = 2.5; i = (int)d; /* explicit type casting */ i = d; /* d is converted to an int * and then assigned to i. */ NWEN 241: Systems Programming 31

Constants Constants integer constants floating-point constants character constants string constants enumeration constants Naming constants Use the const qualifier const float PI = 3.14; Use the preprocessor #define PI 3.14 NWEN 241: Systems Programming 32

To be continued More on data types in the next lecture NWEN 241: Systems Programming 33