C and C++ I. Spring 2014 Carola Wenk

Similar documents
Understanding main() function Input/Output Streams

Scientific Computing

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

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

Chapter 1 INTRODUCTION

Key Differences Between Python and Java

CSI33 Data Structures

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

Computer Programming : C++

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

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CSI33 Data Structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

4. Structure of a C++ program

More Programming Languages. Spring 2014 Carola Wenk

Introduction to Programming Using Java (98-388)

Java Bytecode (binary file)

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

( &% class MyClass { }

CPU Architecture system clock

CS 231 Data Structures and Algorithms, Fall 2016

Algorithms and Programming I. Lecture#12 Spring 2015

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

C++ Support Classes (Data and Variables)

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

COMP322 - Introduction to C++

C - Basics, Bitwise Operator. Zhaoguo Wang

An overview of Java, Data types and variables

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

assembler Machine Code Object Files linker Executable File

C++ for Python Programmers

EC 413 Computer Organization

Computer Science II Lecture 1 Introduction and Background

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Programming refresher and intro to C programming

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

Chapter 2: Introduction to C++

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

CSc Introduction to Computing

UEE1302 (1102) F10: Introduction to Computers and Programming

Pace University. Fundamental Concepts of CS121 1

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

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

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

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Assumptions. History

Baby Steps: Some Simple Programs in C, C++, and Java

CSCE 206: Structured Programming in C++

CS 11 java track: lecture 1

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

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

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

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

CE221 Programming in C++ Part 1 Introduction

CS11 Intro C++ Spring 2018 Lecture 1

1 Unit 8 'for' Loops

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

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

The Compilation Process

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

AMCAT Automata Coding Sample Questions And Answers

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

EL2310 Scientific Programming

REVIEW. The C++ Programming Language. CS 151 Review #2

Introduction to C++ Systems Programming

Short Notes of CS201

Lectures 5-6: Introduction to C

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

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

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

CS201 - Introduction to Programming Glossary By

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Computing and Statistical Data Analysis Lecture 3

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

A brief introduction to C programming for Java programmers

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

the gamedesigninitiative at cornell university Lecture 6 C++: Basics

C++: Overview and Features

CPS122 Lecture: From Python to Java

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

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

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

a data type is Types

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

C++ Programming: From Problem Analysis to Program Design, Third Edition

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Variables. Data Types.

Fast Introduction to Object Oriented Programming and C++

Introduction to C An overview of the programming language C, syntax, data types and input/output

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Fundamentals of Programming CS-110. Lecture 2

Transcription:

C and C++ I Spring 2014 Carola Wenk

Different Languages Python sum = 0 i = 1 while (i <= n): sum += i i += 2 Java/C++ Python Interpreter Java/C++ Compiler int sum = 0; for (int i = 1; i <= n; i +=2) { sum += i; C Compiler C int sum = 0; int i; for (i = 1; i <= n; i +=2) { sum += i lw $t0, 1 lw $t1,0 lw $t2, n loop: beq $t0,$t2,done add $t0, $t1, $t1 add $t0, 2 jmp loop done: Scheme Interpreter Scheme (define (sum n) (if (= n 0) 0 (+ n (sum n-1)))) Languages are translated to machine code by either by a compiler or interpreter.

Categories and Uses Imperative Python: Interpreted, Easy to prototype ideas Java : Interpreted/Compiled, Platform-independent C/C++: Compiled, General purpose PHP: Interpreted/Compiled, Web scripting Functional LISP/Scheme: Interpreted, no differentiation between data/instructions Languages are translated to machine code by either by a compiler or interpreter.

C and C++ The C language was originally developed in the 1970s to assist in the implementation of the UNIX operating system. It was designed to be one step above machine language. C++ is a superset of C introduced in the early 1980s to add objected-oriented features to C. Hello World in C: Hello World in C++: #include <stdio.h> int main() { printf("hello World!!"); return 0; #include <iostream> using namespace std; int main() { cout << "!!!Hello World!!!" << endl; return 0;

Variables and Types Although Python doesn t care about types, they exist: numbers, strings, and lists. C and C++ have the same primitive types as Java: int/short/long, float/double, boolean, char. A variable name is simply a placeholder for a memory address. Memory 0 1 2 3 4 5 6 7... 8 Bits (= 1 Byte) boolean/char short int, float long, double

Conditionals Python if <condition>: elif <condition>: else: Java/C/C++ if (<condition>) { else if (<condition>){ else { For conditional statements, the only real difference in syntax between Python and Java/C/C++ has to do with scope declaration. Java/C/C++ use braces to delimit blocks of statements, instead of indentation. Also, in Java/C/C++ the condition has to be enclosed in parentheses.

Looping Python for i in <list>: while (<condition>): Java/C/C++ for (<init>; <condition>; <increment>) { while (<condition>) { <block of statments> Again, looping constructs are fairly similar, except for how scope is defined. Java/C/C++ also have a do-while construct that can be convenient at times. do { while (<condition>);

C++ #include <iostream> using namespace std; int main() { cout << Hello World! << endl; return 0; One key difference between Java and C/C++ is that not everything is (necessarily) object. So C/C++ program flow is Python-like.

Python Program Structure def f(a, b): print this is function f return a+b; x = 1; y = 2; evens = 0; odds = [] print f(1, 2) print f('z', f('a', 'b'))for i in range(1,10): if (i % 2 == 0): evens += i else: odds.append(i) print evens; print sum(odds) function declaration sequence of statements A Python script is a sequence of function declarations followed by a sequence of statements. A function is just a way to reuse useful blocks of statements.

C++ Program Structure #include <iostream> using namespace std; void foo(int x) { cout << x is << x << endl; int main() { cout << Hello World! << endl; return 0; function declaration sequence of statements Syntax in C/C++ is very similar to Java, for historical reasons. However, not everything is an object, and programs are initiated from a main function.

C Program Structure #include <stdio.h> void foo(int x) { printf( x is %d\n, x); function declaration int main() { printf( Hello World!\n ); return 0; sequence of statements C is older than C++, and is somewhat more low-level, with different input/output syntax, and no facility to define classes.

Python Interpreted languages operate in an environment that provides some language features under the hood. Interpreter Parse statements Resolve ambiguities Report errors Manage memory Statements Program Machine Instructions Operating System To CPU

Java Runtime System import --; class HelloWorld { public void f(int x1, char x2,...) {... Java Compiler public long g(boolean y1, float y2,...) {... private int h(double z1, int z2,...) {... public static void main() { System.out.println( hello world! ) System.out.println( goodbye world! ) Java Byte Code Pretty Good Safety Net To CPU Java Virtual Machine Operating System

C/C++ Compiled languages operate in a self-contained environment, and generally do not have a safety net. Compiler Parse statements Resolve ambiguities Report static errors Manage memory Machine Code Statements Program Operating System To CPU

C #include <stdio.h> int main() { printf("hello World!!"); return 0; C Compiler Machine Language Absolutely No Safety Net To CPU Operating System

C++ #include <iostream> using namespace std; int main() { cout << "!!!Hello World!!!" << endl; return 0; C++ Compiler Machine Language Absolutely No Safety Net To CPU Operating System