21
C And C Based IO

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

Embed Size (px)

Citation preview

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– …

Current Relation

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

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

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

C++ Bootstrapped on C

• C++ providesown versions of commonc libraries

Libraries

• C++ and C libraries are subtly different

C IO

• No cin/cout objects– Use functions:

C++ Library C Library

Printf

• Printf prints formatted strings:

• As many extra parameters as format codes

Printf

• Format Codes:

Examples

• Basic formatting

Printf Formatting

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

Printf Formatting

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

– %+numbercode • Print + for positive

– % numbercode : • Space for +

Printf Formatting

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

– Default behavior : %.6

assume e = 2.718281828:

Printf Formatting

• Examples

Scanf

• Scanf reads information into variables:

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

Scanf

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

Scanf

• Type mismatch = weird errors:

Scanf

• Can scan from formatted strings:

• Space in format string = unlimited whitespace

Skip unlimited white space before reading

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;