A Quick Look at C for C++ Programmers

Size: px
Start display at page:

Download "A Quick Look at C for C++ Programmers"

Transcription

1 COMP 40: Machine Structure and Assembly Language Programming (Fall 2017) A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University noah@cs.tufts.edu Web:

2 Let s look at some code

3 Hello world compared #include <iostream> #include <string> using namespace std; string world = "world"; cout << "Hello " << world << endl; #include <stdlib.h> char *world = "world"; printf("hello %s\n", world); return EXIT_SUCCESS; C++ C 3

4 Hello world compared No namespaces in C #include <iostream> #include <string> using namespace std; string world = "world"; cout << "Hello " << world << endl; #include <stdlib.h> char *world = "world"; printf("hello %s\n", world); return EXIT_SUCCESS; C++ C 4

5 Hello world compared C++: stream I/O w/cout C: stdio with stdout, printf, etc. #include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) string world = "world"; cout << "Hello " << world << endl; #include <stdlib.h> char *world = "world"; printf("hello %s\n", world); return EXIT_SUCCESS; C++ C 5

6 Hello world compared Format string allows substitution #include <iostream> #include <string> #include <stdlib.h> using namespace std; string world = "world"; cout << "Hello " << world << endl; char *world = "world"; printf("hello %s\n", world); return EXIT_SUCCESS; C++ C 6

7 Format strings: Hello world compared %s string %d integer (decimal, signed) %u integer (decimal, unsigned) #include <iostream> %ld integer (decimal, long) #include <string> #include <stdlib.h> %x integer (base 16 hex) using %c namespace single std; ASCII character %5d integer (5 chars wide) int %-10s string (10 chars left justified) char *world = "world"; main(int Etc, etc. argc, char *argv[]) printf("hello %s\n", world); string world = "world"; return EXIT_SUCCESS; cout << "Hello " << world << endl; C++ C 7

8 Hello world compared #include <iostream> #include <string> using namespace std; string world = "world"; cout << "Hello " << world << endl; \n = new line char \t = tab char \\ = \ char Etc. #include <stdlib.h> char *world = "world"; printf("hello %s\n", world); return EXIT_SUCCESS; C++ C 8

9 Basic Datatypes

10 C and C++ mostly share basic data types char int short int long int long long int float double a single byte, capable of holding one character in the local character set. an integer, typically reflecting the natural size of integers on the host machine. an integer, possibly smaller than int an integer, possibly longer than int an integer, possibly longer than long single-precision floating point. double-precision floating point. Abbreviations: short is same as short int ; long same as long int Examples: int x; short int s; short s; double gpa; 10

11 Pointers char c; /* a single byte character */ char *cp; /* a pointer to a single byte character */ A pointer variable holds a reference to some other variable. 11

12 What does this code do? int x; int y; int z; int *ip; variable x holds an integer variable y holds an integer variable z holds an integer variable ip holds pointer to an integer x = 2; y = 3; ip = &z; *ip = x + y; printf( First answer is %d\n, z); If you re not sure, try this code yourself. Try changing it! 12

13 What does this code do? int x; int y; int z; int *ip; variable x holds an integer variable y holds an integer variable z holds an integer variable ip holds pointer to an integer x = 2; y = 3; ip = &z; *ip = x + y; printf( First answer is %d\n, z); *ip = *ip + z; printf( Seconed answer is %d\n, z); If you re not sure, try this code yourself. Try changing it! 13

14 More about C Strings

15 C characters #include <stdlib.h> (void)argc; (void)argv; Printing each one twice in different formats! unsigned char var1 = 'N'; unsigned char var2 = 'O'; unsigned char var3 = 'A'; unsigned char var4 = 'H'; /* %c prints as character %u prints unsigned integer */ printf("the number for %c is %u\n", var1, var1); printf("the number for %c is %u\n", var2, var2); printf("the number for %c is %u\n", var3, var3); printf("the number for %c is %u\n", var4, var4); exit(exit_success); 15

16 C characters #include <stdlib.h> (void)argc; (void)argv; unsigned char var1 = 'N'; unsigned char var2 = 'O'; unsigned char var3 = 'A'; unsigned char var4 = 'H'; This program prints: The number for N is 78 The number for O is 79 The number for A is 65 The number for H is 72 /* %c prints as character %u prints unsigned integer */ printf("the number for %c is %u\n", var1, var1); printf("the number for %c is %u\n", var2, var2); printf("the number for %c is %u\n", var3, var3); printf("the number for %c is %u\n", var4, var4); exit(exit_success); 16

17 C characters are integers! #include <stdlib.h> Interesting Characters are also numbers! (void)argc; (void)argv; unsigned char var1 = 'N'; unsigned char var2 = 'O'; unsigned char var3 = 'A'; unsigned char var4 = 'H'; This program prints: The number for N is 78 The number for O is 79 The number for A is 65 The number for H is 72 /* %c prints as character %u prints unsigned integer */ printf("the number for %c is %u\n", var1, var1); printf("the number for %c is %u\n", var2, var2); printf("the number for %c is %u\n", var3, var3); printf("the number for %c is %u\n", var4, var4); exit(exit_success); 17

18 Hello world compared C: arrays of characters #include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) string world = "world"; cout << "Hello " << world << endl; int main(int argc, char *argv[]) char *world = "world"; printf("hello %s\n", world); return 0; C++ C C++: string type 18

19 Our first hello world Huh? How come this is a pointer? C: arrays of characters int main(int argc, char *argv[]) char *world = "world"; printf("hello %s\n", world); return 0; In many cases, a reference to a C array turns into a pointer to the first element using pointers to represent arrays is very common in C. Read about this in K&R! 19

20 Our first hello world #inlcude <stdlib.h> String literal gives array of characters char world[] = "world"; printf("hello %s\n", world); return EXIT_SUCCESS; 20

21 C arrays addressed by pointer to first element These do almost the same thing int main(int argc, char *argv[]) char *world = "world"; char universe[] = universe ; printf("hello %s\n", world); printf("hello %s\n", universe); return 0; The relationship between arrays and pointers is subtle & important! This one you need to research using K&R or Harbison & Steele 21

22 A trickier hello #include <stdlib.h> #include <string.h> char world[] = "world"; printf("hello %s\n", world); world[1] = '\0'; printf("hello %s your string is %d bytes long!\n", world, strlen(world)); return EXIT_SUCCESS; What does this print? 22

23 If you understand this, you re well on your way! #include <stdlib.h> #include <string.h> char world[] = "world"; printf("hello %s\n", world); world[1] = '\0'; printf("hello %s your string is %d bytes long!\n", world, strlen(world)); world[3] = 'm'; printf("hello %s your string is %d bytes long!\n", world, strlen(world)); world[1] = 'o'; world[4] = '\0'; printf("hello %s your string is %d bytes long!\n", world, strlen(world)); return EXIT_SUCCESS; What does this print? 23

24 If you understand this, you re well on your way! #include <stdlib.h> #include <string.h> These examples show that: char *world = "world"; 1) The printf("hello logical length %s\n", of a C world); string is determined by the terminating null world[1] character = \0 '\0'; printf("hello %s your string is %d bytes long!\n", 2) Printing using %s and checking length with strlen() respect this world, strlen(world)); 3) In a correct program, there should be at least enough world[3] = 'm'; space for the string, but you may have allocated more printf("hello %s your string is %d bytes long!\n", 4) In buggy world, programs, strlen(world)); you fail to null terminate or index off the world[1] end of the = 'o'; allocated space world[4] = '\0'; printf("hello %s your string is %d bytes long!\n", world, strlen(world)); return EXIT_SUCCESS; 24

25 Structured Data

26 Some structured data #include <stdlib.h> struct student char *name; int age; ; struct student students[ ] = "mary", 15, "bob", 9, "tina", 12, ; Primitive types mostly the same as C++ unsigned int i; (void)argc; (void)argv; for (i = 0; i < sizeof(students) / sizeof(struct student); i++) printf("student %s is %d years old.\n", students[i].name, students[i].age); ; return EXIT_SUCCESS; 26

27 Some structured data C has structs, not classes #include <stdlib.h> struct student char *name; int age; ; structs have data only no methods! struct student students[ ] = "mary", 15, "bob", 9, "tina", 12, ; unsigned int i; (void)argc; (void)argv; for (i = 0; i < sizeof(students) / sizeof(struct student); i++) printf("student %s is %d years old.\n", students[i].name, students[i].age); ; return EXIT_SUCCESS; 27

28 Some structured data #include <stdlib.h> struct student char *name; int age; ; struct student students[ ] = "mary", 15, "bob", 9, "tina", 12, ; unsigned int i; (void)argc; (void)argv; Unlike C++: keyword struct required when naming a structured type for (i = 0; i < sizeof(students) / sizeof(struct student); i++) printf("student %s is %d years old.\n", students[i].name, students[i].age); ; return EXIT_SUCCESS; 28

29 Some structured data #include <stdlib.h> struct student char *name; int age; ; Initializers more or less the same as C++ struct student students[ ] = "mary", 15, "bob", 9, "tina", 12, ; unsigned int i; (void)argc; (void)argv; for (i = 0; i < sizeof(students) / sizeof(struct student); i++) printf("student %s is %d years old.\n", students[i].name, students[i].age); ; return EXIT_SUCCESS; 29

30 Some structured data #include <stdlib.h> struct student char *name; int age; ; We can leave out array bound if initializer determines the size same as C++ struct student students[ ] = "mary", 15, "bob", 9, "tina", 12, ; unsigned int i; (void)argc; (void)argv; for (i = 0; i < sizeof(students) / sizeof(struct student); i++) printf("student %s is %d years old.\n", students[i].name, students[i].age); ; return EXIT_SUCCESS; 30

31 You saw printf and format strings earlier! #include <stdlib.h> struct student char *name; int age; ; struct student students[ ] = "mary", 15, "bob", 9, "tina", 12, ; You will want to learn about printf and fprintf format specifications unsigned int i; (void)argc; (void)argv; for (i = 0; i < sizeof(students) / sizeof(struct student); i++) printf("student %s is %d years old.\n", students[i].name, students[i].age); ; return EXIT_SUCCESS; 31

32 Good Practice: Single Point of Truth

33 Single point of truth int main(int argc, char *argv[]) struct student char *name; int age; ; What s going on here? struct student students[ ] = "mary", 15, "bob", 9, "tina", 12, ; unsigned int i; (void)argc; (void)argv; for (i = 0; i < sizeof(students) / sizeof(struct student); i++) printf("student %s is %d years old.\n", students[i].name, students[i].age); ; return 0; 33

34 Single point of truth int main(int argc, char *argv[]) struct student char *name; int age; ; struct student students[ ] = "mary", 15, "bob", 9, "tina", 12, albert", 22, ; What if number of students changes? 34 unsigned int i; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) printf("student %s is %d years old.\n", students[i].name, students[i].age); ; return 0;

35 Single point of truth #include <stdlib.h> struct student char *name; int age; ; struct student students[ ] = "mary", 15, "bob", 9, "tina", 12, albert", 22, ; unsigned int i; (void)argc; (void)argv; There is a single point of truth for the number of students Try to have single points of truth for anything in your program that s likely to change, or on which multiple other things depend if we change this structure, everything just works! for (i = 0; i < sizeof(students) / sizeof(struct student); i++) printf("student %s is %d years old.\n", students[i].name, students[i].age); ; 35 return EXIT_SUCCESS;

36 Why we use Hanson in COMP 40

37 What about abstract data types like list, or table? Many modern languages have them built into the standard Python, Ruby, etc., etc. C++ Standard Template Library (STL) C does not standardize implementation of types like these Hanson gives us C based ADT implementations that: Are useful in our programs Teach us what s going on in many of those higher level language implementations Teach us many good techniques for building modular structures in C 38

38 C vs C++ File I/O

39 Access to files and input Feature C C++ Pre-opened streams Open a file Typical use In both languages: stdin, stdout, stderr FILE *fp fopen(filename, r ) fprintf(stderr, error\n ); fprintf(fp, Hi! ); cin, cout, cerr ifstream myfile( filename ); cerr << error << endl; myfile << Hi! ; The operating system pre-opens the three standard streams They can be redirected from the command line: myprog < somefile # stdin reads from somefile myprog > somefile # stdout writes to somefile otherprog myprog # stdin of myprog is output of otherprog 40

40 Memory allocation

41 There is no new in C! C++ new allocates and initializes objects: Car *mycar = new Car(V8, Blue); // Create a new car The above allocates space and initializes all the data for mycar delete mycar; Runs destructors and releases memory Also: int *ip = new int[len]; // allocate array delete[] ip; // delete array Also: std:vector // truly dynamic array 42

42 There is no new in C! C++ new builds objects: Car *mycar = new Car(V8, Blue); // Create a new car The above allocates space and initializes all the data for car delete mycar; Runs destructors and releases memory Also: int *ip = new int[len]; // allocate array delete[] ip; // delete array Also: std:vector // truly dynamic array C malloc/free allocate and free bytes: struct car.members here ; struct car *car_p = malloc(sizeof struct car); allocate unitialized bytes struct car *car_p = malloc(sizeof *carp); Same, but keeps working if structure name changes: single point of truth! You must check the return value to make sure it worked! free(car_p); /* frees the bytes */ 43

43 There is no new in C! C++ new builds objects: Car *mycar = new Car(V8, Blue); // Create a new car The above allocates space and initializes all the data for car delete mycar; Runs destructors and releases memory Also: int *ip = new int[len]; // allocate array delete[] ip; // delete array Also: std:vector // truly dynamic array C malloc/free allocate and free bytes: struct car.members here ; struct car *car_p = malloc(sizeof struct car); allocate unitialized bytes struct car *car_p = malloc(sizeof *carp); Same, but keeps working if structure name changes: single point of truth! You must check the return value to make sure it worked! free(car_p); /* frees the bytes */ Rule of thumb: The first statement after a call to malloc handles NULL 44

44 A Bit of History

45 C++ is an extension to C C was invented many years earlier (c. 1970) C is a quite small language; C++ is much more complicated C is much closer to the hardware, why we re using it! Good programmers know how each C construct compiles No single C expression compiles to huge amounts of code Most of C survives in C++ Primitive data types: int, float, double, etc Control structures (if, while, function call) Source structures (#include and preprocessor) Much more C does not have: Classes, methods, namespaces, dynamic arrays, dynamic strings, IO streams (in the C++ sense), inheritance, built in higher level ADTs (list, vector, deque, table, etc.) 46

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

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017 C Concepts - I/O Lecture 19 COP 3014 Fall 2017 November 29, 2017 C vs. C++: Some important differences C has been around since around 1970 (or before) C++ was based on the C language While C is not actually

More information

C for C++ Programmers

C for C++ Programmers C for C++ Programmers CS230/330 - Operating Systems (Winter 2001). The good news is that C syntax is almost identical to that of C++. However, there are many things you're used to that aren't available

More information

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

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008 CSC 4304 - Systems Programming Fall 2008 Lecture - II Basics of C Programming Summary of Last Class Basics of UNIX: logging in, changing password text editing with vi, emacs and pico file and director

More information

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print CSC 4304 - Systems Programming Fall 2010 Lecture - II Basics of C Programming Summary of Last Class Basics of UNIX: logging in, changing password text editing with vi, emacs and pico file and directory

More information

Contents. A Review of C language. Visual C Visual C++ 6.0

Contents. A Review of C language. Visual C Visual C++ 6.0 A Review of C language C++ Object Oriented Programming Pei-yih Ting NTOU CS Modified from www.cse.cuhk.edu.hk/~csc2520/tuto/csc2520_tuto01.ppt 1 2 3 4 5 6 7 8 9 10 Double click 11 12 Compile a single source

More information

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 { Memory A bit is a binary digit, either 0 or 1. A byte is eight bits, and can thus represent 256 unique values, such as 00000000 and 10010110. Computer scientists often think in terms of hexadecimal, rather

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C Lecture 1 C primer What we will cover A crash course in the basics of C You should read the K&R C book for lots more details Various details will be exemplified later in the course Outline Overview comparison

More information

C++ basics Getting started with, and Data Types.

C++ basics Getting started with, and Data Types. C++ basics Getting started with, and Data Types pm_jat@daiict.ac.in Recap Last Lecture We talked about Variables - Variables, their binding to type, storage etc., Categorization based on storage binding

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Lecture 05 - I/O using the standard library, stl containers, stl algorithms Dan Pomerantz School of Computer Science 5 February 2013 Basic I/O in C++ Recall that in C, we

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 2 - Language Basics Milena Scaccia School of Computer Science McGill University January 11, 2011 Course Web Tools Announcements, Lecture Notes, Assignments

More information

Lab # 02. Basic Elements of C++ _ Part1

Lab # 02. Basic Elements of C++ _ Part1 Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special

More information

BSM540 Basics of C Language

BSM540 Basics of C Language BSM540 Basics of C Language Chapter 4: Character strings & formatted I/O Prof. Manar Mohaisen Department of EEC Engineering Review of the Precedent Lecture To explain the input/output functions printf()

More information

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

More information

211: Computer Architecture Summer 2016

211: Computer Architecture Summer 2016 211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Data Representation I/O: - (example) cprintf.c Memory: - memory address - stack / heap / constant space - basic data layout Pointer:

More information

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CSC209H Lecture 3. Dan Zingaro. January 21, 2015 CSC209H Lecture 3 Dan Zingaro January 21, 2015 Streams (King 22.1) Stream: source of input or destination for output We access a stream through a file pointer (FILE *) Three streams are available without

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

PRINCIPLES OF OPERATING SYSTEMS

PRINCIPLES OF OPERATING SYSTEMS PRINCIPLES OF OPERATING SYSTEMS Tutorial-1&2: C Review CPSC 457, Spring 2015 May 20-21, 2015 Department of Computer Science, University of Calgary Connecting to your VM Open a terminal (in your linux machine)

More information

C programming basics T3-1 -

C programming basics T3-1 - C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of

More information

Lecture 8: Structs & File I/O

Lecture 8: Structs & File I/O ....... \ \ \ / / / / \ \ \ \ / \ / \ \ \ V /,----' / ^ \ \.--..--. / ^ \ `--- ----` / ^ \. ` > < / /_\ \. ` / /_\ \ / /_\ \ `--' \ /. \ `----. / \ \ '--' '--' / \ / \ \ / \ / / \ \ (_ ) \ (_ ) / / \ \

More information

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL Many programs require complex data to be represented That cannot easily

More information

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams Physics 6720 I/O Methods October 30, 2002 We have been using cin and cout to handle input from the keyboard and output to the screen. In these notes we discuss further useful capabilities of these standard

More information

CSE 333 Lecture 9 - intro to C++

CSE 333 Lecture 9 - intro to C++ CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia & Agenda Main topic: Intro to C++ But first: Some hints on HW2 Labs: The

More information

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

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018 C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

More information

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

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable. CMPS 12M Introduction to Data Structures Lab Lab Assignment 3 The purpose of this lab assignment is to introduce the C programming language, including standard input-output functions, command line arguments,

More information

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Princeton University COS 333: Advanced Programming Techniques A Subset of C90 Princeton University COS 333: Advanced Programming Techniques A Subset of C90 Program Structure /* Print "hello, world" to stdout. Return 0. */ { printf("hello, world\n"); -----------------------------------------------------------------------------------

More information

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

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( ) Systems Group Department of Computer Science ETH Zürich Tutorial 1: Introduction to C Computer Architecture and Systems Programming (252-0061-00) Herbstsemester 2012 Goal Quick introduction to C Enough

More information

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program CMPT 102 Introduction to Scientific Computer Programming Input and Output Janice Regan, CMPT 102, Sept. 2006 0 Your first program /* My first C program */ /* make the computer print the string Hello world

More information

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-43 Exam Review February, 01 Presented by the RIT Computer Science Community http://csc.cs.rit.edu C Preprocessor 1. Consider the following program: 1 # include 3 # ifdef WINDOWS 4 # include

More information

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet High-performance computing and programming Intro to C on Unix/Linux IT Uppsala universitet What is C? An old imperative language that remains rooted close to the hardware C is relatively small and easy

More information

ECE264 Fall 2013 Exam 3, November 20, 2013

ECE264 Fall 2013 Exam 3, November 20, 2013 ECE264 Fall 2013 Exam 3, November 20, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.

More information

CHAPTER 3 BASIC INSTRUCTION OF C++

CHAPTER 3 BASIC INSTRUCTION OF C++ CHAPTER 3 BASIC INSTRUCTION OF C++ MOHD HATTA BIN HJ MOHAMED ALI Computer programming (BFC 20802) Subtopics 2 Parts of a C++ Program Classes and Objects The #include Directive Variables and Literals Identifiers

More information

Work relative to other classes

Work relative to other classes Work relative to other classes 1 Hours/week on projects 2 C BOOTCAMP DAY 1 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Overview C: A language

More information

Chapter 2. Procedural Programming

Chapter 2. Procedural Programming Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy CSCI 334: Principles of Programming Languages Lecture 18: C/C++ Homework help session will be tomorrow from 7-9pm in Schow 030A instead of on Thursday. Instructor: Dan Barowy HW6 and HW7 solutions We only

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Functions Similar to (static) methods in Java without the class: int f(int a, int

More information

Abstract Data Types. 2501ICT/7421ICTNathan. René Hexel. Semester 1, School of Information and Communication Technology Griffith University

Abstract Data Types. 2501ICT/7421ICTNathan. René Hexel. Semester 1, School of Information and Communication Technology Griffith University Collections 2501ICT/7421ICTNathan School of Information and Communication Technology Griffith University Semester 1, 2012 Outline Collections 1 Collections 2 Linear Collections Collections Collections

More information

G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 18 G52CPP C++ Programming Lecture 18 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Welcome Back 2 Last lecture Operator Overloading Strings and streams 3 Operator overloading - what to know

More information

CSE 12 Spring 2016 Week One, Lecture Two

CSE 12 Spring 2016 Week One, Lecture Two CSE 12 Spring 2016 Week One, Lecture Two Homework One and Two: hw2: Discuss in section today - Introduction to C - Review of basic programming principles - Building from fgetc and fputc - Input and output

More information

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.

More information

A Tour of the C++ Programming Language

A Tour of the C++ Programming Language A Tour of the C++ Programming Language We already know C Everything that can be done with a computer, can be done in C Why should we learn another language? Newer languages provide a bigger toolbox Some

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

Lecture 7: file I/O, more Unix

Lecture 7: file I/O, more Unix CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 7: file

More information

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments File I/O Content Input Output Devices File access Function of File I/O Redirection Command-line arguments UNIX and C language C is a general-purpose, high-level language that was originally developed by

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO Input and Output Streams and Stream IO 1 Standard Input and Output Need: #include Large list of input and output functions to: Read and write from a stream Open a file and make a stream Close

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

CS24 Week 3 Lecture 1

CS24 Week 3 Lecture 1 CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation

More information

Review Topics. Final Exam Review Slides

Review Topics. Final Exam Review Slides Review Topics Final Exam Review Slides!! Transistors and Gates! Combinational Logic! LC-3 Programming!! Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox,

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia New exercise posted yesterday afternoon, due Monday morning - Read a directory

More information

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

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006 C Compilation Model Comp-206 : Introduction to Software Systems Lecture 9 Alexandre Denault Computer Science McGill University Fall 2006 Midterm Date: Thursday, October 19th, 2006 Time: from 16h00 to 17h30

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

Should you know scanf and printf?

Should you know scanf and printf? C-LANGUAGE INPUT & OUTPUT C-Language Output with printf Input with scanf and gets_s and Defensive Programming Copyright 2016 Dan McElroy Should you know scanf and printf? scanf is only useful in the C-language,

More information

A brief introduction to C++

A brief introduction to C++ A brief introduction to C++ Rupert Nash r.nash@epcc.ed.ac.uk 13 June 2018 1 References Bjarne Stroustrup, Programming: Principles and Practice Using C++ (2nd Ed.). Assumes very little but it s long Bjarne

More information

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

Program Organization and Comments

Program Organization and Comments C / C++ PROGRAMMING Program Organization and Comments Copyright 2013 Dan McElroy Programming Organization The layout of a program should be fairly straight forward and simple. Although it may just look

More information

Streams. Rupesh Nasre.

Streams. Rupesh Nasre. Streams Rupesh Nasre. OOAIA January 2018 I/O Input stream istream cin Defaults to keyboard / stdin Output stream ostream cout std::string name; std::cout > name; std::cout

More information

Functions & Memory Maps Review C Programming Language

Functions & Memory Maps Review C Programming Language Functions & Memory Maps Review C Programming Language Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department Constants c 2 What Is A Constant? Constant a Value that cannot be altered by

More information

Introduction to C++ (Extensions to C)

Introduction to C++ (Extensions to C) Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.

More information

C PROGRAMMING. Characters and Strings File Processing Exercise

C PROGRAMMING. Characters and Strings File Processing Exercise C PROGRAMMING Characters and Strings File Processing Exercise CHARACTERS AND STRINGS A single character defined using the char variable type Character constant is an int value enclosed by single quotes

More information

Chapter 2: Introduction to C++

Chapter 2: Introduction to C++ Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 05 - I/O using the standard library & Introduction to Classes Milena Scaccia School of Computer Science McGill University February 1, 2011 Final note on

More information

Physics 234: Computational Physics

Physics 234: Computational Physics Physics 234: Computational Physics In-class Midterm Exam Friday, February 12, 2010 Student s Name: Fill-in-the-blank and multiple choice questions (20 points) Mark your answers on the exam sheet in blue

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

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

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley

More information

HW1 due Monday by 9:30am Assignment online, submission details to come

HW1 due Monday by 9:30am Assignment online, submission details to come inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers and Arrays Administrivia Buggy Start Lab schedule, lab machines, HW0 due tomorrow in lab 2009-06-24 HW1 due Monday by 9:30am

More information

LAB 1: C PRIMER CS444/544 WENJIN HU JAN 16TH, 2009

LAB 1: C PRIMER CS444/544 WENJIN HU JAN 16TH, 2009 LAB 1: C PRIMER CS444/544 WENJIN HU JAN 16TH, 2009 SIMPLE C PROGRAM helloworld.c #include int main() printf("hello world!"); return 0; csguest:~$ gcc helloworld.c -o helloworld Notice: what's

More information

File I/O Christian Schumacher, Info1 D-MAVT 2013

File I/O Christian Schumacher, Info1 D-MAVT 2013 File I/O Christian Schumacher, chschuma@inf.ethz.ch Info1 D-MAVT 2013 Input and Output in C++ Stream objects Formatted output Writing and reading files References General Remarks I/O operations are essential

More information

CSE 12 Spring 2018 Week One, Lecture Two

CSE 12 Spring 2018 Week One, Lecture Two CSE 12 Spring 2018 Week One, Lecture Two Homework One and Two: - Introduction to C - Review of basic programming principles - Building from fgetc and fputc - Input and output strings and numbers - Introduction

More information

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

Figure 1 Ring Structures

Figure 1 Ring Structures CS 460 Lab 10 The Token Ring I Tong Lai Yu ( The materials here are adopted from Practical Unix Programming: A Guide to Concurrency, Communication and Multithreading by Kay Robbins and Steven Robbins.

More information

Where do we go from here?

Where do we go from here? Where do we go from here? C++ classes and objects, with all the moving parts visible operator overloading templates, STL, standards, Java components, collections, generics language and performance comparisons

More information

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

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011 Two s Complement Review CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part I) Instructor: Michael Greenbaum http://inst.eecs.berkeley.edu/~cs61c/su11 Suppose we had

More information

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve

More information

STL. Prof Tejada Week 14

STL. Prof Tejada Week 14 STL Prof Tejada Week 14 Standard Template Library (STL) What is the STL? A generic collection of commonly used data structures and algorithms In 1994, STL was adopted as a standard part of C++ Why used

More information

a data type is Types

a data type is Types Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental

More information

SWEN-250 Personal SE. Introduction to C

SWEN-250 Personal SE. Introduction to C SWEN-250 Personal SE Introduction to C A Bit of History Developed in the early to mid 70s Dennis Ritchie as a systems programming language. Adopted by Ken Thompson to write Unix on a the PDP-11. At the

More information

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated

More information

Pointers and File Handling

Pointers and File Handling 1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY

More information

A Tour of the C++ Programming Language

A Tour of the C++ Programming Language A Tour of the C++ Programming Language We already know C Everything that can be done with a computer, can be done in C Why should we learn another language? Newer languages provide a bigger toolbox Some

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

Your First C++ Program. September 1, 2010

Your First C++ Program. September 1, 2010 Your First C++ Program September 1, 2010 Your First C++ Program //*********************************************************** // File name: hello.cpp // Author: Bob Smith // Date: 09/01/2010 // Purpose:

More information

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

CSE au Midterm Exam Nov. 2, 2018 Sample Solution Question 1. (16 points) Build tools and make. We re building a C++ software back-end prototype for a new food web site. So far, we ve got the following source files with the code for two main programs

More information

377 Student Guide to C++

377 Student Guide to C++ 377 Student Guide to C++ c Mark Corner January 21, 2004 1 Introduction In this course you will be using the C++ language to complete several programming assignments. Up to this point we have only provided

More information

Layers of Abstraction CS 3330: C. Compilation Steps. What s in those files? Higher-level language: C. Assembly: X86-64.

Layers of Abstraction CS 3330: C. Compilation Steps. What s in those files? Higher-level language: C. Assembly: X86-64. Layers of Abstraction CS 3330: C 25 August 2016 x += y add %rbx, %rax 60 03 Higher-level language: C Assembly: X86-64 Machine code: Y86 (we ll talk later) Logic and Registers 1 2 Compilation Steps compile:

More information

Question 1. [15 marks]

Question 1. [15 marks] Note to Students: This file contains sample solutions to the term test together with the marking scheme and comments for each question. Please read the solutions and the marking schemes and comments carefully.

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading Last time Intro to C++ Differences between C and C++ Intro to OOP Today Object

More information

Homework 4. Any questions?

Homework 4. Any questions? CSE333 SECTION 8 Homework 4 Any questions? STL Standard Template Library Has many pre-build container classes STL containers store by value, not by reference Should try to use this as much as possible

More information

Stack memory - "scratch pad" memory that is used by automatic variables.

Stack memory - scratch pad memory that is used by automatic variables. Dynamic Memory Allocation In C and C++ three types of memory are used by programs: Static memory - where global and static variables live Stack memory - "scratch pad" memory that is used by automatic variables.

More information

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m. CS 31 Discussion 1A, Week 8 Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m. Today s focus Pointer Structure Clarifications Pointer A pointer is the memory address of a variable.

More information

COMP 117 Ping Demonstration Programs & Makefile / C++ Hints

COMP 117 Ping Demonstration Programs & Makefile / C++ Hints COMP 150-IDS: Internet Scale Distributed Systems (Spring 2018) COMP 117 Ping Demonstration Programs & Makefile / C++ Hints Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah

More information

Assumptions. History

Assumptions. History Assumptions A Brief Introduction to Java for C++ Programmers: Part 1 ENGI 5895: Software Design Faculty of Engineering & Applied Science Memorial University of Newfoundland You already know C++ You understand

More information

Input And Output of C++

Input And Output of C++ Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout

More information