Top Banner
C And C Based IO
21

C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Dec 27, 2015

Download

Documents

Jasmin Ross
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

C And C Based IO

Page 2: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

C

• C = portable assembly language• Developed 1972 w/Unix– Systems focus

Page 3: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

C++

• C++ introduced new features to C– Objects• Collection of data with associated behaviors

– Templates– References– …

Page 4: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Current Relation

• C and C++ separate languages, separate governing bodies

• C++ often adopts changes in C, not always

Page 5: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Why Two Languages

• Tools C++ provides give us– Ways to program in different styles– Ways to build tighter abstractions– Ways to make things more/less efficient

• C gives us– Simplicity– De facto standard for linking code

Page 6: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

C++ Bootstrapped on C

• C++ providesown versions of commonc libraries

Page 7: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Libraries

• C++ and C libraries are subtly different

Page 8: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

C IO

• No cin/cout objects– Use functions:

C++ Library C Library

Page 9: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Printf

• Printf prints formatted strings:

• As many extra parameters as format codes

Page 10: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Printf

• Format Codes:

Page 11: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Examples

• Basic formatting

Page 12: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Printf Formatting

• Format Codes:– %numbercode : min width• -number : left justify

Page 13: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Printf Formatting

• Format Codes:– %0numbercode • Pad with 0s

– %+numbercode • Print + for positive

– % numbercode : • Space for +

Page 14: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Printf Formatting

• For floating point – %.2f • any number of chars, 2 decimal places

– Default behavior : %.6

assume e = 2.718281828:

Page 15: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Printf Formatting

• Examples

Page 16: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Scanf

• Scanf reads information into variables:

Page 17: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Addresses

• Two meanings for &

int& x– C++ only : variable is a reference to another int

variable

&x (x already exists)– C/C++ : address of x

Page 18: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Scanf

• Scanf reads information into variables:– Wants address of anything it is reading in

Page 19: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Scanf

• Type mismatch = weird errors:

Page 20: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Scanf

• Can scan from formatted strings:

• Space in format string = unlimited whitespace

Skip unlimited white space before reading

Page 21: C And C Based IO. C C = portable assembly language Developed 1972 w/Unix – Systems focus.

Why Do I Care

• If you are writing C• C functions may make life easier:

C printf("%10.3f", &x);

C++ cout << fixed << setprecision(3) << setw(10) << x;