6.s096. Introduction to C and C++

Similar documents
6.S096 Lecture 1 Introduction to C

Chapter 13 - The Preprocessor

Lecture 27 C and Assembly

1 Abstract Data Types Information Hiding

How To Use The C Preprocessor

Sources: On the Web: Slides will be available on:

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

The programming language C. sws1 1

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

The C Programming Language course syllabus associate level

Informatica e Sistemi in Tempo Reale

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Semantic Analysis: Types and Type Checking

How To Write Portable Programs In C

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CP Lab 2: Writing programs for simple arithmetic problems

Fundamentals of Programming

Section 6 Spring 2013

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

arrays C Programming Language - Arrays

How to Write a Simple Makefile

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Introduction to Python

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

C++FA 5.1 PRACTICE MID-TERM EXAM

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Lecture 22: C Programming 4 Embedded Systems

An Incomplete C++ Primer. University of Wyoming MA 5310

Next, the driver runs the C compiler (cc1), which translates main.i into an ASCII assembly language file main.s.

Introduction to Data Structures

Keil C51 Cross Compiler

Parallel Computing. Parallel shared memory computing with OpenMP

Getting Started with 1. Borland C++Builder Compiler

Introduction to MS Visual Studio 6.0

A deeper look at Inline functions

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

CSC230 Getting Starting in C. Tyler Bletsch

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

Introduction to Java

Parallel Computing. Shared memory parallel programming with OpenMP

Comp151. Definitions & Declarations

Computer Programming Tutorial

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

Moving from CS 61A Scheme to CS 61B Java

sys socketcall: Network systems calls on Linux

public static void main(string[] args) { System.out.println("hello, world"); } }

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Link time dead code and data elimination using GNU toolchain. Denys Vlasenko

Simple C Programs. Goals for this Lecture. Help you learn about:

ELEC3730 Embedded Systems Lecture 1: Introduction and C Essentials

Advanced Bash Scripting. Joshua Malone

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

CS 241 Data Organization Coding Standards

Getting Started with the Internet Communications Engine

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

J a v a Quiz (Unit 3, Test 0 Practice)

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

C++ INTERVIEW QUESTIONS

Lecture 5: Java Fundamentals III

Using Visual C++ and Pelles C

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Beyond the Mouse A Short Course on Programming

csce4313 Programming Languages Scanner (pass/fail)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

The Function Pointer

Number Representation

CS Software Engineering for Scientific Computing. Lecture 25:Mixed Language Programming.

Systems Programming & Scripting

COS 217: Introduction to Programming Systems

Example of a Java program

TODAY, FEW PROGRAMMERS USE ASSEMBLY LANGUAGE. Higher-level languages such

IntroClassJava: A Benchmark of 297 Small and Buggy Java Programs

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Intro to Intel Galileo - IoT Apps GERARDO CARMONA

PostgreSQL Functions By Example

Object Oriented Software Design

#pragma omp critical x = x + 1; !$OMP CRITICAL X = X + 1!$OMP END CRITICAL. (Very inefficiant) example using critical instead of reduction:

Software documentation systems

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

In this Chapter you ll learn:

Illustration 1: Diagram of program function and data flow

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Parallel and Distributed Computing Programming Assignment 1

Tutorial on C Language Programming

Removing False Code Dependencies to Speedup Software Build Processes

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

Lecture 2 Notes: Flow of Control

Java Basics: Data Types, Variables, and Loops

Transcription:

6.s096 Introduction to C and C++ 1

Why? 2

1 You seek performance 3

1 You seek performance zero-overhead principle 4

2 You seek to interface directly with hardware 5

3 That s kinda it 6

C a nice way to avoid writing assembly language directly 7

C++ responds to the demands of maintaining large C projects 8

C++11 responds to the demands of maintaining large C++ projects 9

Maintain power and flexibility of what came before 10

Compilation Today: Pipeline 11

Source Code Program 12

main.c prog int a = 1; @*)!%./prog gcc -o prog main.c 13

main.c int a = 1; prog main2.c @*)!%./prog int b = 2; gcc -o prog main.c main2.c 14

$ gcc -o prog main.c $./prog Hello, World! $ 15

$ gcc -o prog main.c $./prog Hello, World! $ 16 DONE

To debug the sausage, one must see how it is made. Someone, probably 17

Main.java int a = 1; Main.class %!(*@ java Main 18

Main.java int a = 1; Main.class %!(*@ java Main main.py a = 1 main.pyc %!(*@ python main.py 19

Main.java int a = 1; Main.class %!(*@ java Main main.py a = 1 main.pyc %!(*@ python main.py main.c main.o prog int a = 1; int a = 1; %!(*@ @*)!%./prog 20

21

22

main.c main.o prog int a = 1; int a = 1; %!(*@ @*)!%./prog Pre-Process Compile Link 23

main.c main.o prog int a = 1; int a = 1; %!(*@ @*)!%./prog Pre-Process main2.o %!(*@ Compile Link 24

Pre-Process Compile Link 25

Pre-Process Compile Link 26

#include #define #ifdef 27

rimshot.txt ba-dum chh joke.txt A man walks into a bar. Ouch! #include "rimshot.txt" cpp -P joke.txt 28

output: A man walks into a bar. Ouch! ba-dum chh cpp -P joke.txt 29

double.py #define fosho def #define kthx return #define wutz print These are called macros fosho double(x): kthx x * 2 wutz double(6) cpp -P double.py 30

output: def double(x): return x * 2 print double(6) cpp -P double.py 31

output: def double(x): return x * 2 print double(6) cpp -P double.py python 12 AWESOME cpp -P double.py 32

beer.txt #define beer(x) x bottles of \ beer on the wall... beer(99) beer(98) beer(97)... cpp -P beer.txt 33

beer.txt #define beer(x) x bottles of \ beer on the wall... beer(99) beer(98) beer(97)... cpp -P beer.txt 34

output: 99 bottles of beer on the wall... 98 bottles of beer on the wall... 97 bottles of beer on the wall...... cpp -P beer.txt 35

answer.txt What s 7 times 6? #ifdef REVEAL 42 #endif cpp -P answer.txt 36

output: What s 7 times 6? cpp -P answer.txt 37

output: What s 7 times 6???? cpp -P answer.txt 38

#define REVEAL cpp -P answer.txt 39

#define REVEAL or: cpp -P -D REVEAL answer.txt cpp -P answer.txt 40

output: What s 7 times 6? 42 cpp -P -D REVEAL answer.txt 41

answer.txt What s 7 times 6? #ifndef REVEAL 42 #endif cpp -P answer.txt 42

(Fancy) String Substitution 43

How is this used in C? 44

hello.c #include <stdio.h> int main() { printf("hello, World!\n"); return 0; } gcc -E hello.c 45

hello.c * angle brackets -> use the system search path #include <stdio.h> int main() { printf("hello, World!\n"); return 0; } gcc -E hello.c 46

hello.c * angle brackets -> use the system search path #include <stdio.h> int main() { printf("hello, World!\n"); return 0; } gcc -E hello.c 47

output: int printf(const char *,...) attribute (( format ( printf, 1, 2))); int main() { } printf("hello, World!\n"); * pretending printf is all that s defined in stdio.h gcc -E hello.c 48

output: int printf(const char *,...); int main() { } printf("hello, World!\n"); * pretending printf is all that s defined in stdio.h gcc -E hello.c 49

#include is not import pickle import java.io.*; 50

fib.c #define MAX_FIB 20 int fib[max_fib]; int main() { fib[0] = 0; fib[1] = 1; for(int i = 2; i < MAX_FIB; i++) fib[i] = fib[i-1] + fib[i-2]; return 0; } gcc -E fib.c 51

output: int fib[20]; int main() { fib[0] = 0; fib[1] = 1; for(int i = 2; i < 20; i++) fib[i] = fib[i-1] + fib[i-2]; } gcc -E fib.c 52

debug.c #include <stdio.h> int main() { #ifdef DEBUG printf("hello, World!\n"); #endif return 0; } gcc -DDEBUG debug.c -o debug 53

debug.c #include <stdio.h> int main() { printf("hello, World!\n"); return 0; } gcc -DDEBUG debug.c -o debug 54

debug.c #include <stdio.h> int main() { } return 0; gcc debug.c -o debug 55

Pre-Process Compile Link 56

main.c main.o prog int a = 1; int a = 1; %!(*@ @*)!%./prog Compile 57

Compile Type-checking Linear processing 58

Type-checking int reptile() { } return "frog"; 59

Type-checking int reptile() { } return "frog"; reptile.c: In function reptile : reptile.c:2:5: warning: return makes integer from pointer without a cast 60

Type-checking def vegetable(day): if day!= "Tuesday": return "tomato" else: return 1000 61

Type-checking def vegetable(day): if day!= "Tuesday": return "tomato" else: return 1000 Python says: no problem 62

int reptile() { } return "frog"; 63

int () { } return char*; 64

int vegetable(char *day) { if (strcmp(day, "Tuesday")!= 0){ return "tomato"; } else { return 1000; } } 65

int (char*) { if (int){ return char*; } else { return int; } } 66

int (char*) { if (int){ return char*; } else { return int; } } 67

int (char*) { if (int){ return char*; } else { return int; } } 68

Everything has a single, fixed type 69

def foo(a, b): return a + b foo(2, 3) foo("2", "3") 70

Variable Declarations int foo; float foo; double foo; char foo; int foo[42]; int *foo; struct Bar foo; 71

Function Declarations double fmin(double, double); return type argument types 72

Function Declarations void exit(int); returns nothing int rand(void); takes no arguments 73

int foo(int a, int b){ } return a + b; 74

reptile.c: In function reptile : reptile.c:2:5: warning: return makes integer from pointer without a cast 75

reptile.c: In function reptile : reptile.c:2:5: warning: return makes integer from pointer without a cast int a = 4; float b = (float)a; 76

all theoretical casts int float double char int[] int* void (*f )(int) struct X int float double char int[] int* void (*f )(int) struct X 77

allowed casts int float double char int[] int* void (*f )(int) struct X int float double char int[] int* void (*f )(int) struct X 78

79

allowed casts int float double char int[] int* void (*f )(int) struct X int float double char int[] int* void (*f )(int) struct X 80

allowed casts int float double char int[] int* void (*f )(int) struct X int float double char int[] int* void (*f )(int) struct X 81

implicit casts int float double char int[] int* void (*f )(int) struct X int float double char int[] int* void (*f )(int) struct X 82

implicit casts int float double char int[] int* void (*f )(int) struct X int float double char int[] int* void (*f )(int) struct X 83

implicit casts int float double char int[] int* void (*f )(int) struct X int float double char int[] int* void (*f )(int) struct X 84

implicit casts int float double char int[] int* void (*f )(int) struct X int float double char int[] int* void (*f )(int) struct X 85

Compile Type-checking Linear processing 86

Linear processing ( just a small note) 87

You can only use what s declared above 88

int main() { printf("%d\n", answer()); return 0; } int answer() { } return 1337; 89

int main() { printf("%d\n", answer()); return 0; } int answer() { } return 1337; 90

int answer() { } return 1337; int main() { printf("%d\n", answer()); return 0; } 91

int answer(); declaration int main() { printf("%d\n", answer()); return 0; } int answer() { definition return 1337; } 92

HMM 93

int answer(); declaration int main() { printf("%d\n", answer()); return 0; } int answer() { definition return 1337; } 94

int answer(); declaration int main() { printf("%d\n", answer()); return 0; } int answer() { definition return 1337; } 95

int answer(); declaration #include "answer.h" int main() { printf("%d\n", answer()); return 0; } int answer() { definition return 1337; } 96

Pre-Process Compile Link 97

main.o int main() int answer() 98

main.o int main() 99

answer.c: In function main : answer.c:4: warning: implicit declaration of function answer Undefined symbols for architecture x86_64: "_answer", referenced from: _main in ccuzmrrm.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status 100

answer.c: In function main : answer.c:4: warning: implicit declaration of function answer Undefined symbols for architecture x86_64: "_answer", referenced from: _main in ccuzmrrm.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status 101

answer.c: In function main : answer.c:4: warning: implicit declaration of function answer Undefined symbols for architecture x86_64: "_answer", referenced from: _main in ccuzmrrm.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status Compiler: I don t know what answer is. I ll assume it returns an int. 102

answer.c: In function main : answer.c:4: warning: implicit declaration of function answer Undefined symbols for architecture x86_64: "_answer", referenced from: _main in ccuzmrrm.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status 103

answer.c: In function main : answer.c:4: warning: implicit declaration of function answer Undefined symbols for architecture x86_64: "_answer", referenced from: _main in ccuzmrrm.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status Linker: I looked in all the object files, but I couldn t find answer. 104

main.o int main() 105

main.o answer.o int main() int answer() 106

main.o answer.o int main() int answer() int main() { printf("%d\n", answer()); return 0; } int answer() { return 1337; } 107

main.o answer.o int main() int answer() 108

prog int main() int answer() gcc -o prog main.c answer.c 109

answer.c: In function main : answer.c:4: warning: implicit declaration of function answer 110

answer.c: In function main : answer.c:4: warning: implicit declaration of function answer Compiler: I don t know what answer is. I ll assume it returns an int. 111

answer.h int answer(); main.c #include "answer.h" int main() { printf("%d\n", answer()); return 0; } 112

answer.h int answer(); answer.c #include "answer.h" int answer() { } return 1337; 113

Summary 114

answer.h int answer(); main.c #include "answer.h" int main() { printf("%d\n", answer()); return 0; } 115

Preprocess: gcc -E main.c int answer(); int main() { printf("%d\n", answer()); return 0; } 116

Compile: gcc -c main.c main.c main.o %!(*@ answer.o %!(*@ 117

Link: gcc -o prog main.o main.o prog %!(*@ 118

Pre-Process Compile Link 119

MIT OpenCourseWare http://ocw.mit.edu 6.S096 Introduction to C and C++ IAP 2013 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.