• No results found

6.s096. Introduction to C and C++

N/A
N/A
Protected

Academic year: 2021

Share "6.s096. Introduction to C and C++"

Copied!
120
0
0

Loading.... (view fulltext now)

Full text

(1)

6.s096 Introduction to C and C++

(2)

Why?

(3)

1

You seek performance

(4)

1

You seek performance

“zero-overhead principle”

(5)

2

You seek to interface

directly with hardware

(6)

3

That’s kinda it

(7)

C

a nice way to avoid writing

assembly language directly

(8)

C++

responds to the demands of

maintaining large C projects

(9)

C++11

responds to the demands of

maintaining large C++ projects

(10)

Maintain power and flexibility

of what came before

(11)

Today:

Compilation

Pipeline

(12)

Source Code

Program

(13)

main.c prog

int a = 1; @*)!% ./prog

gcc -o prog main.c

(14)

main.c

int a = 1; prog

@*)!% ./prog main2.c

int b = 2;

gcc -o prog main.c main2.c

(15)

$ gcc -o prog main.c

$ ./prog

Hello, World!

$

(16)

DONE

$ gcc -o prog main.c

$ ./prog

Hello, World!

$

16

(17)

“To debug the sausage, one must see how it is made.”

—Someone, probably

(18)

Main.java Main.class

%!(*@

int a = 1; java Main

(19)

Main.java Main.class

%!(*@

main.pyc

%!(*@

a = 1

main.py

int a = 1; java Main

python main.p y

(20)

Main.java Main.class

int a = 1; %!(*@

main.o

@*)!%

prog

%!(*@

main.pyc

%!(*@

int a = 1;

main.c

a = 1

main.py

int a = 1; java Main

python main.p y

./prog

(21)
(22)
(23)

main.c

int a = 1; %!(*@ @*)!%

int a = 1; ./prog

Pre-Process

Compile

Link

main.o prog

(24)

main.c

int a = 1;

int a = 1;

Pre-Process

%!(*@

main2.o

%!(*@

@*)!% ./prog

Compile

Link

prog main.o

(25)

Pre-Process

Compile

Link

(26)

Pre-Process

Compile

Link

(27)

#include

#define

#ifdef

(28)

rimshot.txt

ba-dum chh

joke.txt

A man walks into a bar. Ouch!

#include "rimshot.txt"

cpp -P joke.txt

(29)

output:

A man walks into a bar. Ouch!

ba-dum chh

cpp -P joke.txt

(30)

double.py

#define fosho def

#define kthx return

#define wutz print fosho double(x):

kthx x * 2 wutz double(6)

These are called

“macros”

cpp -P double.py

(31)

output:

def double(x):

return x * 2 print double(6)

cpp -P double.py

(32)

AWESOME

output:

def double(x):

return x * 2 print double(6) cpp -P double.py | python

12

cpp -P double.py

(33)

beer.txt

#define beer(x) x bottles of \ beer on the wall...

beer(99) beer(98) beer(97) ...

cpp -P beer.txt

(34)

beer.txt

#define beer(x) x bottles of \ beer on the wall...

beer(99) beer(98) beer(97) ...

cpp -P beer.txt

(35)

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

(36)

answer.txt

What’s 7 times 6?

#ifdef REVEAL 42

#endif

cpp -P answer.txt

(37)

output:

What’s 7 times 6?

cpp -P answer.txt

(38)

? ? ?

output:

What’s 7 times 6?

cpp -P answer.txt

(39)

#define REVEAL

cpp -P answer.txt

(40)

#define REVEAL or:

cpp -P -D REVEAL answer.txt

cpp -P answer.txt

(41)

output:

What’s 7 times 6?

42

cpp -P -D REVEAL answer.txt

(42)

answer.txt

What’s 7 times 6?

#ifndef REVEAL 42

#endif

cpp -P answer.txt

(43)

(Fancy)

String Substitution

(44)

How is this used in C?

(45)

hello.c

#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)

hello.c * angle brackets -> use the system search path

#include <stdio.h>

int main() {

printf("Hello, World!\n");

return 0;

}

gcc -E hello.c

(48)

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

(49)

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

(50)

#include is not

import pickle

import java.io.*;

(51)

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

(52)

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

(53)

debug.c

#include <stdio.h>

int main() {

#ifdef DEBUG

printf("Hello, World!\n");

#endif

return 0;

}

gcc -DDEBUG debug.c -o debug

(54)

debug.c

#include <stdio.h>

int main() {

printf("Hello, World!\n");

return 0;

}

gcc -DDEBUG debug.c -o debug

(55)

debug.c

#include <stdio.h>

int main() {

return 0;

}

gcc debug.c -o debug

(56)

Pre-Process

Compile

Link

(57)

int a = 1; %!(*@

main.o

@*)!%

prog

int a = 1;

main.c

./prog

Compile

(58)

Compile

Type-checking

Linear processing

(59)

Type-checking

int reptile() {

return "frog";

}

(60)

Type-checking

int reptile() {

return "frog";

}

reptile.c: In function ‘reptile’:

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

(61)

Type-checking

def vegetable(day):

if day != "Tuesday":

return "tomato"

else:

return 1000

(62)

Type-checking

def vegetable(day):

if day != "Tuesday":

return "tomato"

else:

return 1000

Python says: no problem

(63)

int reptile() {

return "frog";

}

(64)

int () {

return char*;

}

(65)

int vegetable(char *day) {

if (strcmp(day, "Tuesday") != 0){

return "tomato";

} else {

return 1000;

} }

(66)

int (char*) { if (int){

return char*;

} else {

return int;

} }

(67)

int (char*) { if (int){

return char*;

} else {

return int;

} }

(68)

int (char*) { if (int){

return char*;

} else {

return int;

} }

(69)

Everything has

a single, fixed type

(70)

def foo(a, b):

return a + b foo(2, 3)

foo("2", "3")

(71)

Variable Declarations

int foo;

float foo;

double foo;

char foo;

int foo[42];

int *foo;

struct Bar foo;

(72)

Function Declarations

double fmin(double, double);

return type argument types

(73)

Function Declarations

void exit(int);

returns nothing

int rand(void);

takes no arguments

(74)

int foo(int a, int b){

return a + b;

}

(75)

reptile.c: In function ‘reptile’:

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

(76)

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;

(77)

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

(78)

allowed casts

int

float

double char

int[]

int*

void (*f )(int) struct X

int

float

double char

int[]

int*

void (*f )(int)

struct X

(79)
(80)

allowed casts

int

float double char

int[]

int*

void (*f )(int)

int

float

double char

int[]

int*

void (*f )(int)

struct X struct X

(81)

allowed 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 int

float float

double double

char char

int[] int[]

int* int*

void (*f )(int) void (*f )(int)

struct X struct X

(83)

implicit casts

int

float

double char

int[]

int*

void (*f )(int)

int

float

double char

int[]

int*

void (*f )(int)

struct X struct X

(84)

implicit casts

int int

float float

double double

char char

int[]

int*

int[]

int*

void (*f )(int) void (*f )(int)

struct X struct X

(85)

implicit casts

int int

float float

double double

char char

int[] int[]

int* int*

void (*f )(int) struct X

void (*f )(int)

struct X

(86)

Compile

Type-checking

Linear processing

(87)

Linear processing

( just a small note)

(88)

You can only use

what’s declared above

(89)

int main() {

printf("%d\n", answer());

return 0;

}

int answer() {

return 1337;

}

(90)

int main() {

printf("%d\n", answer());

return 0;

}

int answer() {

return 1337;

}

(91)

int answer() {

return 1337;

}

int main() {

printf("%d\n", answer());

return 0;

}

(92)

int answer();

declaration

int main() {

printf("%d\n", answer());

return 0;

}

int answer() {

definition

return 1337;

}

(93)

HMM

93

(94)

int answer();

declaration

int main() {

printf("%d\n", answer());

return 0;

}

int answer() {

definition

return 1337;

}

(95)

int answer();

int main() {

printf("%d\n", answer());

return 0;

}

declaration

int answer() {

return 1337;

}

definition

(96)

int answer();

declaration

#include "answer.h"

int main() {

printf("%d\n", answer());

return 0;

}

int answer() {

return 1337;

}

definition

(97)

Pre-Process

Compile

Link

(98)

main.o

int main()

int answer()

(99)

main.o

int main()

(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

(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

Compiler: “I don’t know what

answer

is.

I’ll assume it returns an

int

.”

(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

(104)

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

.”

(105)

main.o

int main()

(106)

main.o answer.o int main()

int answer()

(107)

main.o answer.o int main()

int answer()

int main() {

printf("%d\n", answer());

return 0;

int answer() {

return 1337;

}

(108)

main.o answer.o int main()

int answer()

(109)

prog

gcc -o prog main.c answer.c

int main()

int answer()

(110)

answer.c: In function ‘main’:

answer.c:4: warning: implicit declaration of function ‘answer’

(111)

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

.”

(112)

answer.h

int answer();

#include "answer.h"

int main() { main.c

printf("%d\n", answer());

return 0;

}

(113)

answer.h

int answer();

answer.c

#include "answer.h"

int answer() {

return 1337;

}

(114)

Summary

(115)

answer.h

int answer();

main.c

#include "answer.h"

int main() {

printf("%d\n", answer());

return 0;

}

(116)

Preprocess:

gcc -E main.c

int answer();

int main() {

printf("%d\n", answer());

return 0;

}

(117)

Compile:

gcc -c main.c main.c

main.o

%!(*@

answer.o

%!(*@

(118)

Link:

gcc -o prog main.o main.o

prog

%!(*@

(119)

Pre-Process

Compile

Link

(120)

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.

References

Related documents

This committee, which is commonly used in Philosophy, and sometimes the students peers are allowed to view andor join in, a student is required to write a thesis research paper

The model of keepwell deed and equity interest purchase undertaking (short for EIPU) is one form of financial innovation which attracts overseas financing by using the flaw of

Assessment category 2: Bond strength &gt; 0,5 MPa Pukkila Vedeneristysmassa with and without Pukkila Kosteussulku 1 (the instructions of the approval holder must be

In This method they encrypt the original image with two shares mechanism encryption algorithm then embed the encrypted image with patient information by using lossless

Bab III merupakan bagian yang membahas analisis bentuk lagu “Groovin’ High” yang asli karya Dizzy Gillespie, dan analisis bentuk dan permainan musik Keith Jarrett, serta membahas

Contract Manufacturing Organisations (CMO) have been established longer than CROs in China, and with an increased commitment to international standards, Chinese CMOs are

Since sign language communication is multimodal it in- volves not only hand gestures (i.e., manual signing) but also non-manual signals (NMS) conveyed through facial ex- pressions,

The Carrier CO 2 NSERVATION Meter calculates avoided greenhouse gas (GHG) emissions as a result of the installation of high- efficiency Carrier air conditioning, heating