2: variables and data typeseceweb.ucsd.edu/courses/ece15/fa_2015/lectures/lecture2.pdf · all...

55
ECE15: Introduction to Computer Using the C Language 2: Variables and Data Types A. Orlitsky and A. Vardy, based in part on slides by A. Orlitsky and A. Vardy, based in part on slides by S. Arzi, G. Ruckenstein, E. Avior, S. Asmir, and M. Elad

Upload: others

Post on 29-Sep-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

ECE15: Introduction to Computer

Using the C Language

2: Variables and Data Types

A. Orlitsky and A. Vardy, based in part on slides by

A. Orlitsky and A. Vardy, based in part on slides by S. Arzi, G. Ruckenstein, E. Avior, S. Asmir, and M. Elad

Page 2: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 2

printf()scanf()

3+4

& much more

Page 3: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Lecture 5 ECE15: Introduction to Computer Programming Using the C Language

❖ Writing

❖ Reading

❖ Variables and data types

‣ Text ‣ Integers ‣ Reals ‣ Characters ‣ Strings

❖ Rudimentary file input/output

3

Outline

Page 4: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Writing (Printing)❖ C printing function printf() “print formatted”

❖ Name shared by several programming languages

❖ Prints

‣Text (“Hello world”)

‣ Integers

‣Reals

‣Characters

‣Strings

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 4

Page 5: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Printing Text

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 5

printf(“Hello world!\n”); Hello world! >

printf(“Hello world!”); Hello world!>

printf(“Hello\n world!\n”); Hello world! >

printf(“Hello\nworld!\n”); Hello world! >

Page 6: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 6

Page 7: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Integer Constants

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Used to print, write in program, type

‣ 7

‣ -13

‣ 1234 (not 1,234)

7

Page 8: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Printing Integers

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Format using %d (decimal) or %i (integer)

8

printf("%d", -13);

printf("my %i cents", 2);

printf("%d+%d=%d", 1, 2, 1+2);

-13

my 2 cents

1+2=3

printf("%d", 2*3+4); 10

printf("-13");

More math later

Same as

Format statement Regular text gets printed normally. %d, %i, etc. don’t get printed. They determine how arguments after “...” get printed.

printf("%d+%d=%d", 1, 2, 1+3); 1+2=4

Page 9: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Minimum Print Size

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ %3d - use at least 3 locations

❖ Useful for printing table

9

printf("#%d#%3d#%-3d#",1, 2, 3);

#1# 2#3456#

printf("#%5.3d#%-5.3d#", 1, 2); # 001#002 #

printf("#%d#%3d#%3d#",1, 2, 3456);

#1# 2#3 #

left adjust

Precede w. 0’s

Page 10: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Formatting errors

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ %d and %i indicate how subsequent arguments get printed ❖ # of %d %i should match # of subsequent arguments ❖ What if these numbers don’t match?

10

printf("%d", 1, 2); Compilation warning

printf("%d %d", 1); Compilation warning 1 73858

1

mismatch.c

Page 11: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Variables

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 11

❖ So far, everything fixed and rigid, nothing changes

‣program always prints same thing (“Hello, world”, -13)

❖ Need something more flexible, dynamic

‣Print different values, read user input

❖ Variables

‣Hold information ‣Take user input ‣Can change with time

Page 12: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Data Types

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 12

❖ All constants and variables have a type ‣ Integer, real number, character, string

❖ Type determines ‣# memory bytes allocated ‣ Interpretation / meaning of the bits stored in these bytes ‣Operations allowed

❖ Variables must be declared ‣ type, name, optional initial value

‣ Instructs compiler to ๏ Allocate right # memory cells ๏ Interpret content of these cells according to type ๏ Associate variable name with these cells

int num,sum; double weight = 0.0; char digit = '4';

NowShortly

Page 13: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

int❖ Basic integer type (more types later) ❖ Declaration

❖ Uninitialized (contains previously stored value) ❖ Initialization

‣ At declaration

‣ Assignment

❖ Printing: same as constants

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 13

int year; int temperature, humidity;

printf("%d", year);

int a=0;

int a; a=0;

int a=0, b=1;

int a, b; a=b=0;

int a, b; a=0; b=1;

Hint = does not mean equal. a = 5 means store 5 in the variable called a.

Hint Initialize or assign value to a variable before using its contents.

Page 14: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Quiz

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ What’s printed?

14

#include <stdio.h>

int main() { int a=0, b, c, d, e, f; b=1; c=d=2; printf("a=%d\n", a);

printf("b=%d\n", b);

printf("c=%d\n", c);

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

printf("e=%d\n", e);

printf("f=%d\n", f);

return 0; }

a=0

b=1

c=2

d=2

e=589

f=-2381039

Or whatever stored before

quiz.c

Likewise

Page 15: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Reading Integers

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ scanf() with %d (or %i)

15

Always (for now..) use scanf with & - stores value in that location

#include <stdio.h>

int main() { int myvar;

printf("Type an integer: ");

scanf("%d", &myvar);

printf("It is: %d\n", myvar);

return 0;

}

scanf1.c

Think of &myvar as the address of myvar More later

Page 16: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Non-Integer Input

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

15 You typed 15

rest left for future scanf’s

%d skips initial white space (space, tab, newline)

1.5 You typed 1

whatever stored before

#include <stdio.h> int main() { int value; printf("Type an integer: "); scanf("%d", &value); printf(“You typed %d\n", value); return 0; }

1b

b < .5

1+234

You typed -183793

scanf1.c

15 You typed 15

Then reads till first non-integer character

Preceding white space ignored

Page 17: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Reading More Values

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 17

#include <stdio.h>

int main() { int value1, value2;

printf("Now gimme two: ");

scanf("%d%d", &value1, &value2);

printf("Their sum: %d\n", value1+value2);

return 0; }

scanf(" %d %d", &value1, &value2);

❖ Spaces preceding %d don’t matter (as skips initial spaces) same

scanf2.c

❖ Spaces following last %d may matter (later)

Page 18: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Maximum Field Size

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

Integer between % and d specifies maximum # digits read

scanf("%2d", &a);

scanf("%1d%d", &a, &b);

a=11

123 a=12

123 a=1 b=23

scanf("%1d", &a); scanf("%d", &b);

123 a=1 b=23

same

Page 19: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Field Separators

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 19

❖ Non % text is expected and must appear

❖ ALLOWED modifications ‣ %d skips initial space, so space after separator ok

‣ Format space means >=0 spaces, so remove/add space ok

❖ DISALLOWED ‣ Add space before separator

‣ Change or remove (non-space) separator symbols

scanf("%d,%d", &a, &b); 1,2 a=1 b=2

1xy2 a=1 b=2

1xy 2

a=1 b=2

scanf("%dxy%d", &a, &b);

1 xy2

a=1 b=#&<)

1, 2 a=1 b=2

1 ,2

1 2 a=1 b=*)%-

scanf("%d x y %d", &a, &b); 1 x y 2 a=1 b=2

a=1 b=2

1.2 a=1 b=!?^)

1 xy2 a=1 b=<?)!

scanf("%d,%d", &a, &b);

scanf("%dxy%d", &a, &b);

scanf("%d,%d", &a, &b);

scanf("%dxy%d", &a, &b);

scanf("%d,%d", &a, &b);

scanf("%dxy%d", &a, &b); 1x2 a=1 b=<?)!

Remember mainly this

part!

separator.c

Keep rest in mind, but

less likely to be in exams.

Page 20: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

How Much Time Left

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Homework is due tonight at 23:59 ❖ Ask user for current time h:m, output time left

20

#include <stdio.h> int main() { int hour, minute; printf("Enter current time (hh:mm): "); scanf("%d:%d", &hour, &minute); printf("%d hours and %d minutes left\n", 23-hour, 59-minute); return 0; }

6 hours and 44 minutes left 17:15

Enter current time (hh:mm):

19 hours and 54 minutes left 4:5

time.c

Page 21: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

How Many Values scanf Read?

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 21

❖ Returns # of successfully read variables

#include <stdio.h>

int main() { int numRead, firstVal, secondVal; printf("Two integers please: ");

numRead = scanf("%d%d", &firstVal, &secondVal);

printf("Read %d ints, first was %d, second %d\n ", numRead, firstVal, secondVal); return 0; }

scanf_num.c

Try entering 5a

Page 22: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Using # Values scanf Read

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Calculate average of up to 4 integers

❖ Later - better ways of doing that

22

#include <stdio.h> int main() { int a1=0, a2=0, a3=0, a4=0; int number;

printf("Enter up to 4 integers followed by *: "); number = scanf("%d%d%d%d", &a1,&a2,&a3,&a4); printf("Sum=%d, Number=%d\n",a1+a2+a3+a4,number); printf("APPROXIMATE average is %d\n", (a1+a2+a3+a4)/number); return 0; }

Approximate because integer!!

average.c

Page 23: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Can we Print Everything?

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 23

printf(“\””);

printf(“\\n”);

printf(“%%d”);

\n

%d

Means How to print? Like so

” end format ” \”

\n newline \ \\

%d decimal % %%

printf(“\\\\”); \\

Page 24: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Rest of the material about integers is important, and you should know it

❖ Yet a bit harder to memorize, hence less likely to appear in in-class midterm and exam

24

Page 25: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Representation of Integers

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Computers represent everything in binary: 0,1

❖ Nonnegative integers: standard binary representation

‣ Reserve leftmost bit for sign

‣ For 3 bits: 0 → 000, 1 → 001, 2 → 010, 3 → 011

‣ Always start with 0

‣ With n bits, represent 0 to 2n-1-1

❖ Negative integers: 2’s complement, -x represented as 2n-x

‣ For 3 bits: -1 → 111, -2 → 110, -3 → 101, -4 → 100

‣ Start with 1

‣ With n bits, represent -1 to -2n-1

25

Though not on exam, you should know this!

Page 26: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Size of int

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language26

❖ Standards don’t determine size of int

❖ Most compilers allocate 4 bytes (32 bits)

❖ Range: -231,…,-1,0,1,…,231-1❖ 231 = 2,147,483,648 ~ 2 Billion

❖ Often not enough ❖ How do we know # bytes?

> a.out x=2147483647 x+1=-2147483648 >

#include <stdio.h> int main() { int x=2147483647;

printf("x=%d\n", x); printf("x+1=%d\n", x+1);

return 0; } largest_int.c

byte = 8 bits

210 1,024

~thousand

220 1,048,567 ~million

230 1,073,741,824 ~billion

Page 27: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

sizeof

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 27

❖ returns # bytes used to store object ❖ object:

‣ Type name: int ‣ Variable name: x ‣ Expression: 3*x+5

❖ Shortly use for other types

sizeof(object)

printf("Number of bytes:\n");

printf("int: %lu\n", sizeof(int)); int dummy=57; printf("dummy: %lu\n", sizeof(dummy)); printf("3*dummy: %lu\n", sizeof(3*dummy));

Number of bytes: int: 4 dummy: 4 3*dummy: 4

sizeof1.c

Page 28: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

❖ short, long (often = int), long long (large integers)

‣ Sizes guaranteed to satisfy

Other Integer Types

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 28

sizeof(short)≤sizeof(int)≤ sizeof(long)≤ sizeof(long long)

short: 2, int: 4, long int: 8, long long int: 8

dummy: 8, 3*dummy: 8

printf("short: %lu, int: %lu, \ long int: %lu, long long int: %lu\n",

sizeof(short), sizeof(int),

sizeof(long int), sizeof(long long int));

long long int dummy=57;

printf("dummy: %lu, 3*dummy: %lu\n", sizeof(dummy), sizeof(3*dummy));sizeOfTypes1.c

Line continues

Page 29: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

❖ Constants

‣ long - 35L, long long - 21537LL ❖ printf and scanf

‣ short - %hd, long - %ld, long long - %lld

#include <stdio.h> int main() { long int a = 5, b = 5L;

int c = 2147483648, d = 2147483648L;

long int e = 2147483648L;

long long int f=2147483648LL;

printf("a=%ld b=%ld e=%lld f=%lld\n", a,b,h,h); return 0; }

sizeOfTypes2.c

Reading & Writing Integer Types

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 29

Needed only for really long integers

✘ ✘ü ü

ü

Largest int: 2147483647

ü

Page 30: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

❖ unsigned represents only positive integers, hence doubles the range. For example, if int occupies 4 bytes

❖ Similarly: unsigned char, unsigned short, unsigned long

❖ Same for scanf

Unsigned

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 30

int: -231,…,-2,-1,0,1,2,…,231-1unsigned int: 0,1,2,…,232-1

#include <stdio.h> int main() { unsigned int us1 = 5u, us2 = 5U, us3 = 5; printf("us1=%d us2=%u us3=%U\n, us1,us2,us3); return 0; }

unsigned.cneeded only when large

Page 31: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Octal and Hexadecimal Constants❖ Constants ‣ Octal - precede with 0 (zero)

‣ Hexadecimal - precede with 0x

❖ Printing

‣ Octal (%o)

‣ Hexadecimal (%x,%X)

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 31

printf("%o", 13);

printf("%x", 13);

15

d

printf("%d", 012); 10

printf("%d", 0x12); 18

Page 32: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 32

Page 33: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Constants

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Floating point

❖ Scientific notation

❖ Integers (converted)

33

+13.0 -13.0.7 .7

1.23e1

1.23

.123E+2 =.123*102 = 12.3

13 Caution when printing integer as float

Page 34: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Printing

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Print: %g, %lf, %f, or %e

34

printf("%g", 24.00);

printf("%g", .174e2);

printf("%g", 7);

printf("%f", 2.4);

printf("%.2f", 2.4);

printf("%e", -24.);

24

17.4

-3.23732e-232

2.400000

-2.400000e+01

2.40

printf("#%-6.2f#", 2.4); #2.40 #

by default 6 digits after point

2 digits after point

print total of at least 6 digits including point

scientific notation .174e2=.174*102

- left justify

ûgcc -Wall

alerts to problem

Page 35: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

float and double

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 35

double fraction; float width, height;

printf("%f", width);

float a=0.5;

double a; a=.13;

double a=5, b=3e-2;

double a, b; a=b=-17.23;

❖ Basic types, double provides more precision, more common

❖ Constants are double

❖ Declaration

❖ Uninitialized (contains previously stored value) ❖ Initialization

‣ At declaration

‣ Assignment

❖ Printing: same as constants (for both double and float)

Page 36: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Reading

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ scanf() with %f (float), %lf (double)

36

#include <stdio.h>

int main() { double x;

printf("Real #: "); scanf("%lf", &x); printf("Typed: %g\n", x);

return 0; }

int i; double x; float y; printf("Enter int, double, and float: "); scanf("%d%lf%f", &i, &x, &y);

printf("Int: %d dbl: %f flt: %f\n", i, x, y);

❖ Reading & writing mixed types

Again!

mix.c

Page 37: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Representation

0|10|11001001000011111

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 37

sign exponent mantissa

3.1415926 11.001001000011111

Represented by binary floating-point expansion

Integers (42.0) and byadic fractions (42/1024) represented exactly

Other reals are approximated

You should know, but not in exams.

Page 38: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Other Real-Valued Data Types

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 38

sizeof(float) ≤ sizeof(double) ≤ sizeof(long double)

sizeofFloatingPoint.c

❖ In addition to double and float can use long double

❖ Standard guarantees

❖ Most compilers allocate

‣ double:8 bytes, ±5.0×10-324 to ±1.7×10308, 15 precision digits ‣ float: 4 bytes, ±1.5×10-45 to ±3.4×1038, 7 precision digits

Page 39: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

Characters

39

Page 40: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

❖ Constants ‣ Enclosed in ‘ ‘: 'a', 'Z', '3', '+', '#', '$' ‣ Some letters cannot be written directly, use escape: \

❖ Declaration

❖ Uninitialized (contains previously stored value) ❖ Initialization

‣Wrong:

char

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 40

char letter_name; char letter_name, initial;

char a=’d’;char a; a=’#’;

char a, b; a=b=’\b’;

char a=d; Thinks it’s a variableç

'\n'-- newline '\t'-- tab '\b'-- backspace '\a' -- bell (alert)

moves back, doesn’t erase

Forward

Page 41: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Printing and Reading

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Printing

❖ Reading

41

printf("%c", ’d’); printf("d");same as

char vowel=’H’; printf("%ca%c", vowel, ’!’); Ha!

printf("%c%c%c", ’a’,’\b’,’b’);

printf("%c%c%c%c",’\a’,’\a’,’\a’,’\a’);

b

printf("%c%c%c%c", ’a’,’\b’,’b’,’\b’); b

scanf("%c", &a1); again

scanf("%c%c", &a1, &a2); printf("%ce%c\n", a2, a1);

nt tenchar.c

char a1, a2;Assume:

Page 42: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

❖ Character stored in one byte (8 bits)

❖ Represented as integer from 0 to 127 using ASCII (American Standard Code for Information Interchange)

Representation

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 42

Character Represented as Decimal

A 01000001 64+1=65

B 01000010 64+2=66

a 01100001 64+32+1=97

0 00110000 32+16=48

Page 43: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

ASCII Table Free ASCII Chart

Char Desc Dec Hex Key Char Dec Hex HTML Char Dec Hex Char Dec Hex Char HTML Code

!"# NULL 0 00 ^@ $%&'( 32 20 ) 64 40 * 96 60 $%&'( &nbsp;

$+, Start of Heading 1 01 ^A - 33 21 . 65 41 & 97 61 / &amp;

$01 Start of Text 2 02 ^B 2 34 22 &quot; 3 66 42 4 98 62 5 &lt;

601 End of Text 3 03 ^C 7 35 23 8 67 43 ' 99 63 9 &gt;

6+0 End of Transmission 4 04 ^D : 36 24 ; 68 44 < 100 64 = &copy;

6!> Enquiry 5 05 ^E ? 37 25 6 69 45 ( 101 65 @ &reg;

.8A Acknoledge 6 06 ^F / 38 26 &amp; B 70 46 C 102 66 D &sup1;

36# Bell 7 07 ^G E 39 27 F 71 47 G 103 67 H &sup2;

3$ Backspace 8 08 ^H I 40 28 , 72 48 J 104 68 K &sup3;

0.3 Horizontal Tab 9 09 ^I L 41 29 M 73 49 M 105 69 E &apos;

#B Line Feed 10 0A ^J N 42 2A O 74 4A P 106 6A 2 &quot;

Q0 Home 11 0B ^K R 43 2B A 75 4B S 107 6B T &frac14;BB Form Feed 12 0C ^L U 44 2C # 76 4C V 108 6C W &frac12;8X Carriage Return 13 0D ^M Y 45 2D Z 77 4D [ 109 6D \ &frac34;$+ Shift Out 14 0E ^N ] 46 2E ! 78 4E ^ 110 6E ! &pi;

$M Shift In 15 0F ^O _ 47 2F + 79 4F ` 111 6F a &trade;

;#6 Data Link Escape 16 10 ^P b 48 30 c 80 50 % 112 70 " &infin;

;8d Device Control 1 17 11 ^Q d 49 31 > 81 51 e 113 71 # &ne;

;8f Device Control 2 18 12 ^R f 50 32 X 82 52 g 114 72 $ &le;

;8h Device Control 3 19 13 ^S h 51 33 $ 83 53 i 115 73 % &ge;

;8j Device Control 4 20 14 ^T j 52 34 0 84 54 k 116 74 & &asymp;

!.A Negative Acknowledge 21 15 ^U l 53 35 " 85 55 m 117 75 ! &equiv;

$n! Syncronous Idle 22 16 ^V o 54 36 Q 86 56 p 118 76 ' &sum;

603 End of trans. Block 23 17 ^W q 55 37 r 87 57 s 119 77 t &bull;

8.! Cancel 24 18 ^X u 56 38 1 88 58 v 120 78 w &hellip;

6Z End of Medium 25 19 ^Y x 57 39 n 89 59 y 121 79 ( &Delta;

$"3 Substitute 26 1A ^Z z 58 3A { 90 5A | 122 7A " &larr;

6$8 Escape 27 1B ^[ } 59 3B ~ 91 5B � 123 7B # &uarr;

B$ Cursor Right (File Seperator) 28 1C ^\ 5 60 3C &lt; Ä 92 5C Å 124 7C $ &rarr;

F$ Cursor Left (Group Seperator) 29 1D ^] Ç 61 3D É 93 5D Ñ 125 7D % &darr;

X$ Cursor Up (Record Seperator) 30 1E ^^ 9 62 3E &gt; Ö 94 5E Ü 126 7E & &harr;

"$ Cursor Down (Unit Seperator) 31 1F ^_ á 63 3F à 95 5F ;6# 127 7F â &fnof;

HTML Codes use the #& with the decimal value followed by a semi colon. Example: &#64; for the @ symbol.

HTML Post Operation use the % sign and the hex value. Example: Space would be %20

To obtain codes 0-31, console Control Key is pressed while simultaneously pressing the Letter Key.

www.kellermansoftware.com Free quick reference sheets and .net components

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 43

Free ASCII Chart

Char Desc Dec Hex Key Char Dec Hex HTML Char Dec Hex Char Dec Hex Char HTML Code

!"# NULL 0 00 ^@ $%&'( 32 20 ) 64 40 * 96 60 $%&'( &nbsp;

$+, Start of Heading 1 01 ^A - 33 21 . 65 41 & 97 61 / &amp;

$01 Start of Text 2 02 ^B 2 34 22 &quot; 3 66 42 4 98 62 5 &lt;

601 End of Text 3 03 ^C 7 35 23 8 67 43 ' 99 63 9 &gt;

6+0 End of Transmission 4 04 ^D : 36 24 ; 68 44 < 100 64 = &copy;

6!> Enquiry 5 05 ^E ? 37 25 6 69 45 ( 101 65 @ &reg;

.8A Acknoledge 6 06 ^F / 38 26 &amp; B 70 46 C 102 66 D &sup1;

36# Bell 7 07 ^G E 39 27 F 71 47 G 103 67 H &sup2;

3$ Backspace 8 08 ^H I 40 28 , 72 48 J 104 68 K &sup3;

0.3 Horizontal Tab 9 09 ^I L 41 29 M 73 49 M 105 69 E &apos;

#B Line Feed 10 0A ^J N 42 2A O 74 4A P 106 6A 2 &quot;

Q0 Home 11 0B ^K R 43 2B A 75 4B S 107 6B T &frac14;BB Form Feed 12 0C ^L U 44 2C # 76 4C V 108 6C W &frac12;8X Carriage Return 13 0D ^M Y 45 2D Z 77 4D [ 109 6D \ &frac34;$+ Shift Out 14 0E ^N ] 46 2E ! 78 4E ^ 110 6E ! &pi;

$M Shift In 15 0F ^O _ 47 2F + 79 4F ` 111 6F a &trade;

;#6 Data Link Escape 16 10 ^P b 48 30 c 80 50 % 112 70 " &infin;

;8d Device Control 1 17 11 ^Q d 49 31 > 81 51 e 113 71 # &ne;

;8f Device Control 2 18 12 ^R f 50 32 X 82 52 g 114 72 $ &le;

;8h Device Control 3 19 13 ^S h 51 33 $ 83 53 i 115 73 % &ge;

;8j Device Control 4 20 14 ^T j 52 34 0 84 54 k 116 74 & &asymp;

!.A Negative Acknowledge 21 15 ^U l 53 35 " 85 55 m 117 75 ! &equiv;

$n! Syncronous Idle 22 16 ^V o 54 36 Q 86 56 p 118 76 ' &sum;

603 End of trans. Block 23 17 ^W q 55 37 r 87 57 s 119 77 t &bull;

8.! Cancel 24 18 ^X u 56 38 1 88 58 v 120 78 w &hellip;

6Z End of Medium 25 19 ^Y x 57 39 n 89 59 y 121 79 ( &Delta;

$"3 Substitute 26 1A ^Z z 58 3A { 90 5A | 122 7A " &larr;

6$8 Escape 27 1B ^[ } 59 3B ~ 91 5B � 123 7B # &uarr;

B$ Cursor Right (File Seperator) 28 1C ^\ 5 60 3C &lt; Ä 92 5C Å 124 7C $ &rarr;

F$ Cursor Left (Group Seperator) 29 1D ^] Ç 61 3D É 93 5D Ñ 125 7D % &darr;

X$ Cursor Up (Record Seperator) 30 1E ^^ 9 62 3E &gt; Ö 94 5E Ü 126 7E & &harr;

"$ Cursor Down (Unit Seperator) 31 1F ^_ á 63 3F à 95 5F ;6# 127 7F â &fnof;

HTML Codes use the #& with the decimal value followed by a semi colon. Example: &#64; for the @ symbol.

HTML Post Operation use the % sign and the hex value. Example: Space would be %20

To obtain codes 0-31, console Control Key is pressed while simultaneously pressing the Letter Key.

www.kellermansoftware.com Free quick reference sheets and .net components

Free ASCII Chart

Char Desc Dec Hex Key Char Dec Hex HTML Char Dec Hex Char Dec Hex Char HTML Code

!"# NULL 0 00 ^@ $%&'( 32 20 ) 64 40 * 96 60 $%&'( &nbsp;

$+, Start of Heading 1 01 ^A - 33 21 . 65 41 & 97 61 / &amp;

$01 Start of Text 2 02 ^B 2 34 22 &quot; 3 66 42 4 98 62 5 &lt;

601 End of Text 3 03 ^C 7 35 23 8 67 43 ' 99 63 9 &gt;

6+0 End of Transmission 4 04 ^D : 36 24 ; 68 44 < 100 64 = &copy;

6!> Enquiry 5 05 ^E ? 37 25 6 69 45 ( 101 65 @ &reg;

.8A Acknoledge 6 06 ^F / 38 26 &amp; B 70 46 C 102 66 D &sup1;

36# Bell 7 07 ^G E 39 27 F 71 47 G 103 67 H &sup2;

3$ Backspace 8 08 ^H I 40 28 , 72 48 J 104 68 K &sup3;

0.3 Horizontal Tab 9 09 ^I L 41 29 M 73 49 M 105 69 E &apos;

#B Line Feed 10 0A ^J N 42 2A O 74 4A P 106 6A 2 &quot;

Q0 Home 11 0B ^K R 43 2B A 75 4B S 107 6B T &frac14;BB Form Feed 12 0C ^L U 44 2C # 76 4C V 108 6C W &frac12;8X Carriage Return 13 0D ^M Y 45 2D Z 77 4D [ 109 6D \ &frac34;$+ Shift Out 14 0E ^N ] 46 2E ! 78 4E ^ 110 6E ! &pi;

$M Shift In 15 0F ^O _ 47 2F + 79 4F ` 111 6F a &trade;

;#6 Data Link Escape 16 10 ^P b 48 30 c 80 50 % 112 70 " &infin;

;8d Device Control 1 17 11 ^Q d 49 31 > 81 51 e 113 71 # &ne;

;8f Device Control 2 18 12 ^R f 50 32 X 82 52 g 114 72 $ &le;

;8h Device Control 3 19 13 ^S h 51 33 $ 83 53 i 115 73 % &ge;

;8j Device Control 4 20 14 ^T j 52 34 0 84 54 k 116 74 & &asymp;

!.A Negative Acknowledge 21 15 ^U l 53 35 " 85 55 m 117 75 ! &equiv;

$n! Syncronous Idle 22 16 ^V o 54 36 Q 86 56 p 118 76 ' &sum;

603 End of trans. Block 23 17 ^W q 55 37 r 87 57 s 119 77 t &bull;

8.! Cancel 24 18 ^X u 56 38 1 88 58 v 120 78 w &hellip;

6Z End of Medium 25 19 ^Y x 57 39 n 89 59 y 121 79 ( &Delta;

$"3 Substitute 26 1A ^Z z 58 3A { 90 5A | 122 7A " &larr;

6$8 Escape 27 1B ^[ } 59 3B ~ 91 5B � 123 7B # &uarr;

B$ Cursor Right (File Seperator) 28 1C ^\ 5 60 3C &lt; Ä 92 5C Å 124 7C $ &rarr;

F$ Cursor Left (Group Seperator) 29 1D ^] Ç 61 3D É 93 5D Ñ 125 7D % &darr;

X$ Cursor Up (Record Seperator) 30 1E ^^ 9 62 3E &gt; Ö 94 5E Ü 126 7E & &harr;

"$ Cursor Down (Unit Seperator) 31 1F ^_ á 63 3F à 95 5F ;6# 127 7F â &fnof;

HTML Codes use the #& with the decimal value followed by a semi colon. Example: &#64; for the @ symbol.

HTML Post Operation use the % sign and the hex value. Example: Space would be %20

To obtain codes 0-31, console Control Key is pressed while simultaneously pressing the Letter Key.

www.kellermansoftware.com Free quick reference sheets and .net components

Free ASCII Chart

Char Desc Dec Hex Key Char Dec Hex HTML Char Dec Hex Char Dec Hex Char HTML Code

!"# NULL 0 00 ^@ $%&'( 32 20 ) 64 40 * 96 60 $%&'( &nbsp;

$+, Start of Heading 1 01 ^A - 33 21 . 65 41 & 97 61 / &amp;

$01 Start of Text 2 02 ^B 2 34 22 &quot; 3 66 42 4 98 62 5 &lt;

601 End of Text 3 03 ^C 7 35 23 8 67 43 ' 99 63 9 &gt;

6+0 End of Transmission 4 04 ^D : 36 24 ; 68 44 < 100 64 = &copy;

6!> Enquiry 5 05 ^E ? 37 25 6 69 45 ( 101 65 @ &reg;

.8A Acknoledge 6 06 ^F / 38 26 &amp; B 70 46 C 102 66 D &sup1;

36# Bell 7 07 ^G E 39 27 F 71 47 G 103 67 H &sup2;

3$ Backspace 8 08 ^H I 40 28 , 72 48 J 104 68 K &sup3;

0.3 Horizontal Tab 9 09 ^I L 41 29 M 73 49 M 105 69 E &apos;

#B Line Feed 10 0A ^J N 42 2A O 74 4A P 106 6A 2 &quot;

Q0 Home 11 0B ^K R 43 2B A 75 4B S 107 6B T &frac14;BB Form Feed 12 0C ^L U 44 2C # 76 4C V 108 6C W &frac12;8X Carriage Return 13 0D ^M Y 45 2D Z 77 4D [ 109 6D \ &frac34;$+ Shift Out 14 0E ^N ] 46 2E ! 78 4E ^ 110 6E ! &pi;

$M Shift In 15 0F ^O _ 47 2F + 79 4F ` 111 6F a &trade;

;#6 Data Link Escape 16 10 ^P b 48 30 c 80 50 % 112 70 " &infin;

;8d Device Control 1 17 11 ^Q d 49 31 > 81 51 e 113 71 # &ne;

;8f Device Control 2 18 12 ^R f 50 32 X 82 52 g 114 72 $ &le;

;8h Device Control 3 19 13 ^S h 51 33 $ 83 53 i 115 73 % &ge;

;8j Device Control 4 20 14 ^T j 52 34 0 84 54 k 116 74 & &asymp;

!.A Negative Acknowledge 21 15 ^U l 53 35 " 85 55 m 117 75 ! &equiv;

$n! Syncronous Idle 22 16 ^V o 54 36 Q 86 56 p 118 76 ' &sum;

603 End of trans. Block 23 17 ^W q 55 37 r 87 57 s 119 77 t &bull;

8.! Cancel 24 18 ^X u 56 38 1 88 58 v 120 78 w &hellip;

6Z End of Medium 25 19 ^Y x 57 39 n 89 59 y 121 79 ( &Delta;

$"3 Substitute 26 1A ^Z z 58 3A { 90 5A | 122 7A " &larr;

6$8 Escape 27 1B ^[ } 59 3B ~ 91 5B � 123 7B # &uarr;

B$ Cursor Right (File Seperator) 28 1C ^\ 5 60 3C &lt; Ä 92 5C Å 124 7C $ &rarr;

F$ Cursor Left (Group Seperator) 29 1D ^] Ç 61 3D É 93 5D Ñ 125 7D % &darr;

X$ Cursor Up (Record Seperator) 30 1E ^^ 9 62 3E &gt; Ö 94 5E Ü 126 7E & &harr;

"$ Cursor Down (Unit Seperator) 31 1F ^_ á 63 3F à 95 5F ;6# 127 7F â &fnof;

HTML Codes use the #& with the decimal value followed by a semi colon. Example: &#64; for the @ symbol.

HTML Post Operation use the % sign and the hex value. Example: Space would be %20

To obtain codes 0-31, console Control Key is pressed while simultaneously pressing the Letter Key.

www.kellermansoftware.com Free quick reference sheets and .net components

Page 44: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Implications

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Can (not recommended) use numbers instead of characters

❖ Can add and subtract

44

char let = 98;char let = ’b’;

0 ... 7 ... 48 49 ... 65 66 ... 97 98 ... 127\0 ... \a ... 0 1 ... A B ... a b ... DEL

same as

printf("%d", let);

scanf("%c", &let); scanf("%d", &let);b 98same as

b98 printf("%c", let);

printf("%c", ’b’+2); d printf("%d", ’b’+2); 100

printf("%d", ’I’-’B’); printf("%c", ’I’-’B’);7

bell.c

Page 45: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Quiz

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ What does the following program do?

45

#include <stdio.h>

int main() {

char c; printf("Lower case letter: ");

scanf("%c", &c);

printf("I druther type: %c\n", c + ’A’ - ’a’);

return 0;

} what.clower case to

UPPER

0 ... 48 49 ... 65 66 ... 97 98 ... 127\0 ... 0 1 ... A B ... a b ... DEL

+ 66 - 97

-32

Page 46: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Skipping White Space

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ %d reads integers, skips white space ❖ %c reads characters, doesn’t skip anything ❖ What if scanf %d followed by %c

‣ Newline after int is not be read by %d, read as char ❖ To skip white spaces before char, use " %c"

46

printf("Number: "); scanf("%d", &num); printf("Letter: "); scanf("%c", &let); printf("%d %c\n", num, let);

> a.out Number: 57 Letter: 57

>

> a.out Number: 57 Letter: c 57 c >

scanf("%d %c", &num, &let);

skipWhite.c

scanf(" %c", &let);

or

Page 47: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Skipping Specific Input

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ To skip specific input use * ❖ %*d skips an integer

❖ To skip specific values (at least one):

47

#include <stdio.h> int main() { int i, j; printf("Three ints: "); scanf("%d%*d%d", &i, &j); printf("i=%d j=%d\n",i,j); return 0; }

#include <stdio.h> int main() { int i; char c; printf("Int<white>char: "); scanf("%d%*[b \t\n]%c",&i,&c); printf("i=%d c=%c\n",i,c); return 0; }

Read three integers, print first and last

scanf("%*d");

skip_b_white.c

scanf("%*[ \t\n]");

skip_int.c

Read integer, skip white spaces and b’s, and read character

Page 48: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

getchar() and putchar()

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Simple way of reading and writing single characters

❖ Useful for pausing programs

48

#include <stdio.h>

int main() { char a, b; a=getchar(); b=getchar(); putchar(b); putchar(a); return 0; }

getputchar.c

Especially demos...

Page 49: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 49

Strings

Page 50: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Strings

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 50

❖ Sequence of characters enclosed in quotes

❖ 'a' and "a" stored differently (more when study arrays)

❖ Printing

❖ Reading: when study arrays (need to allocate space)

"hello world.\n""Hi" " ?!# @" " " ""

printf("hello");

"a"

printf("%s", "hello");

printf("%d+%d%s%d\n", a, b, " equals ", a+b);

printf("\a"); printf("\b "); space, so overwrites

same as hello

1+2 equals 3

int a=1, b=2;

Page 51: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 51

File Input and Output

Page 52: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Basic File Input / Output

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Read input from a file or write output to a file? ❖ Useful: long in/out, saving, consistency ❖ Simplest method (others later): ‣ a.out < file1 ‣ a.out > file2

❖ All input from file1 all output to file2 (none from keyboard / to screen)

52

#include <stdio.h>

int main() { int x, y;

scanf("%d %d", &x, &y); printf("1st: %d\n2nd: %d\n", x,y);

return 0; }

> m InFile > 7 11 a.out < InFile 1st: 7 2nd: 11 > m OutFile OutFile: No such file.. a.out > OutFile 20 11 > m OutFile 1st: 20 2nd: 11 > a.out <InFile >OutFile > m OutFile 1st: 7 2nd: 11 >

inOut.c

input read from file1

output printed to file2

Page 53: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Lecture 5 ECE15: Introduction to Computer Programming Using the C Language

❖ Writing

❖ Reading

❖ Variables and data types

‣ Text ‣ Integers ‣ Reals ‣ Characters ‣ Strings

❖ Rudimentary file input/output

53

Outline

Page 54: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Who gets?

❖ Most

❖ Some

❖ None

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 54

Page 55: 2: Variables and Data Typeseceweb.ucsd.edu/courses/ECE15/FA_2015/lectures/lecture2.pdf · All constants and variables have a type ‣Integer, real number, character, string Type determines

Where are we?

Lecture 2 ECE15: Introduction to Computer Programming Using the C Language

❖ Two weeks old ❖ Already can read & write! ❖ What’s next?

55