CS 241: Systems Programming Lecture 9. More C Spring 2020 Prof. - - PowerPoint PPT Presentation

cs 241 systems programming lecture 9 more c
SMART_READER_LITE
LIVE PREVIEW

CS 241: Systems Programming Lecture 9. More C Spring 2020 Prof. - - PowerPoint PPT Presentation

CS 241: Systems Programming Lecture 9. More C Spring 2020 Prof. Stephen Checkoway 1 Operators The same as Java Arithmetic: + , - , * , /, % Logical: && , || , ! Bitwise: & , | , ^ , ~ , << , >> Pre/post


slide-1
SLIDE 1

CS 241: Systems Programming Lecture 9. More C

Spring 2020

  • Prof. Stephen Checkoway

1

slide-2
SLIDE 2

Operators

The same as Java

  • Arithmetic: +, -, *, /, %
  • Logical: &&, ||, !
  • Bitwise: &, |, ^, ~, <<, >>
  • Pre/post increment, decrement: ++, --
  • Relational: ==, !=, <, <=, >, >=
  • Assignment: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

There are some others we'll talk about later

  • sizeof
  • .
  • ->

2

slide-3
SLIDE 3

C has pre- and post-increment (++) and -decrement (--) operators. What does this code print? (%d means print an integer) int main(void) {
 int x = 3;
 int y = 5;
 printf("%d %d\n", x--, ++y);
 return 0;
 }

  • A. 2 5
  • B. 2 6
  • C. 3 5
  • D. 3 6
  • E. Undefined

3

slide-4
SLIDE 4

C has pre- and post-increment (++) and -decrement (--) operators. What does this code print? (%d means print an integer) int main(void) {
 int x = 3;
 printf("%d\n", x-- + --x);
 return 0;
 }

  • A. 3
  • B. 4
  • C. 5
  • D. 6
  • E. Undefined

4

slide-5
SLIDE 5

Huge difference from Java

C is full of undefined behavior, implementation-defined behavior, and unspecified behavior Undefined behavior gives the compiler license to do whatever it wants, including nothing Implementation-defined behavior means the compiler gets to choose (and document) its behavior Unspecified behavior means the compiler gets to pick from among several choices

5

slide-6
SLIDE 6

What does the code print?

  • A. foo


bar
 1 2

  • B. bar


foo
 1 2

  • C. 1 2


foo
 bar

  • D. Undefined

behavior, could print anything

  • E. Unspecified

behavior, either A or B.

6

#include <stdio.h> int foo(void) { printf("foo\n"); return 1; } int bar(void) { printf("bar\n"); return 2; } int main(void) { printf("%d %d\n", foo(), bar()); return 0; }

slide-7
SLIDE 7

Control flow

if statements; for, while, do-while loops almost identical to Java zero is false, nonzero is true

7

slide-8
SLIDE 8

Examples

8

int signum(int x) { if (x < 0) return -1; if (x > 0) return 1; return 0; } int sum_of_squares(int n) { int result = 0; for (int i = 1; i < n; ++i) result += i * i; return result; }

slide-9
SLIDE 9

Examples

9

bool get_reponse(void) { int response; do { printf("Enter y or n\n"); response = getchar(); } while (response != EOF && response != 'y' && response != 'n'); return response == 'y'; }

slide-10
SLIDE 10

Compiling code

$ ⟨compiler⟩ ⟨options⟩ ⟨.c files⟩ ⟨libraries⟩ $ clang -Wall -o program -std=c11 *.c -lm If you omit -o output, the default is a.out If you omit -std=c11, clang and gcc have different defaults!

10

slide-11
SLIDE 11

Compiler options (gcc/clang)

11

slide-12
SLIDE 12

Compiler options (gcc/clang)

  • E

preprocessor only

11

slide-13
SLIDE 13

Compiler options (gcc/clang)

  • E

preprocessor only

  • S

compile only (no assembly or linking)

11

slide-14
SLIDE 14

Compiler options (gcc/clang)

  • E

preprocessor only

  • S

compile only (no assembly or linking)

  • c

compile/assemble (produce .o file)

11

slide-15
SLIDE 15

Compiler options (gcc/clang)

  • E

preprocessor only

  • S

compile only (no assembly or linking)

  • c

compile/assemble (produce .o file)

  • o foo specify output file as foo

11

slide-16
SLIDE 16

Compiler options (gcc/clang)

  • E

preprocessor only

  • S

compile only (no assembly or linking)

  • c

compile/assemble (produce .o file)

  • o foo specify output file as foo
  • lxxx use library named libxxx.so or libxxx.a

11

slide-17
SLIDE 17

Compiler options (gcc/clang)

  • E

preprocessor only

  • S

compile only (no assembly or linking)

  • c

compile/assemble (produce .o file)

  • o foo specify output file as foo
  • lxxx use library named libxxx.so or libxxx.a
  • g

emit debugging symbols (enables debugging)

11

slide-18
SLIDE 18

Compiler options (gcc/clang)

  • E

preprocessor only

  • S

compile only (no assembly or linking)

  • c

compile/assemble (produce .o file)

  • o foo specify output file as foo
  • lxxx use library named libxxx.so or libxxx.a
  • g

emit debugging symbols (enables debugging)

  • std=c11 use C11 standard

11

slide-19
SLIDE 19

Compiler options (gcc/clang)

  • E

preprocessor only

  • S

compile only (no assembly or linking)

  • c

compile/assemble (produce .o file)

  • o foo specify output file as foo
  • lxxx use library named libxxx.so or libxxx.a
  • g

emit debugging symbols (enables debugging)

  • std=c11 use C11 standard
  • pedantic be pedantic
  • Wall turn on "all" warnings
  • Wextra

turn on extra warnings

  • Werror

make warnings into errors

11

slide-20
SLIDE 20

Formatting your code

It's more important to be consistent than anything else when it comes to format Use tools! $ clang-format foo.c # Writes formatted code to stdout $ clang-format -i foo.c # Writes formatted code back to foo.c

12

slide-21
SLIDE 21

In-class exercise

https://checkoway.net/teaching/cs241/2020-spring/exercises/Lecture-09.html Grab a laptop and a partner and try to get as much of that done as you can!

13