DOC PREVIEW
U of I CS 241 - Console I/O

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 241Section Week #1About Sections• Each week:– We’ll spend additional time on topics that the instructors feel should be reviewed.– We’ll prepare you for the upcoming homework or MP submissions.– We’ll provide extra review/guidance for upcoming exams.Topics This Section• Reading and Writing to the Console• Memory• Precedence• Casting• StringsConsole I/O• In lecture, you saw the printf() command.– printf(“%s: %d”, str, i);– printf(“%c%c%c”, c1, c2, c3);–…• In C I/O, you will provide a format string with a parameter list of values to populate the string with.Console I/O• In lecture, you saw the printf() command.– printf(“%s: %d”, str, i);– printf(“%c%c%c”, c1, c2, c3);–…• The embedded format tags tell C how to format the variables you provide.Console I/O• The printf() man page describes all the different types of specifies you can use.• Common specifies:– %c A single character – %d An integer value– %f A floating point value– %s A string– %p A pointerConsole I/O• Example #1:char *s = “the cat and the hat”;printf(“%s”, s);printf(“%c”, s);Console I/O• Example #1:char *s = “the cat and the hat”;printf(“%s”, s); the cat and the hatprintf(“%c”, s); tConsole I/O• Example #1:char *s = “the cat and the hat”;printf(“%s”, s); the cat and the hatprintf(“%c”, s); tWhy?Console I/O• Example #2:int i = 42;printf(“%d”, i);printf(“%c”, i);Console I/O• Example #2:int i = 42;printf(“%d”, i); 42printf(“%c”, i); *Console I/O• Example #2:int i = 42;printf(“%d”, i); 42printf(“%c”, i); *Why?Console I/O• Reading from the console uses nearly the same I/O format as writing to the console.– Key difference: All variables must be references.• printf(“%d”, i); Writes i to console.• scanf(“%d”, &i); Reads an int from consoleinto i.Arrayschar a[8]; /* array of bytes */char b[4][2]; /* 2-dimensional array */int c[2]; /* array of 32-bit words */..................MemoryMemory is just a big array of bytesPointers are indicies into memoryThe type of a pointer determines whether the memory it indexes is viewed as a char, an int, etc.void indicates the no-value type.void *p = ...;(char *) p(int *) p..................Referencing and dereferencingThe & operator creates a pointer to a variable (takes the address of the memory location holding the variable), while the *operator reads the data which a pointer references:int x;int *xptr = &x;/* xptr = 0xAF981DF8 */int y = *xptr;/* y = x */Process memory layoutprogram code(“text segment”)initialized static datauninitialized data (“bss”)dynamic memorygapstacksystem data: argv, envkernel memorylow memoryNULL: 0x00x0804A0080x080496B8&argv: 0xAFA47DD4argv: 0xAFA47E44Pointer arithmeticchar a[8]; /* array of bytes */char *p = a; /* p, a: 0xAF99EFDC */char *q = a+3; /* q: 0xAF99EFDF */..................Pointer arithmetic (2)char a[8]; /* array of bytes */char *q = a+3; /* q: 0xAF99EFDF */char *r = &a[3]; /* r: 0xAF99EFDF */..................Pointer arithmetic (3)int b[2]; /* array of 4-byte words */int *q = b+1; /* q: 0xAF99EFE0 */char *r = &b[1]; /* r: 0xAF99EFE0 */..................Memory• Three main categories of memory that we’ll concern ourselves with in CS 241:– Static Memory:• Memory that is declared with the ‘static’keyword.• Memory is only allocated once.• Memory is always of fixed size.• Memory is never freed.Memory• Three main categories of memory that we’ll concern ourselves with in CS 241:– Heap Allocated Memory:• Memory that is allocated with memory-allocating functions.– malloc(), calloc(), etc• Allocated only when the memory-allocating function is called.• Freed only when free() is called.Memory• Three main categories of memory that we’ll concern ourselves with in CS 241:– Stack Allocated Memory:• Memory that is allocated within the scope of a function.• Stores local variables and function parameters • Allocated when the function begins execution.• Freed when the function finishes execution.• The stack memory associated with a given function is referred to as a “stack frame”.Memory• Code Execution:• Memory:void foo(int myInt){int *x = (int *)malloc(sizeof(int));free(x);}Memory• Code Execution:• Memory:void foo(int myInt){int *x = (int *)malloc(sizeof(int));free(x);}myIntMemory• Code Execution:• Memory:void foo(int myInt){int *x = (int *)malloc(sizeof(int));free(x);}myInt*x4 bytesMemory• Code Execution:• Memory:void foo(int myInt){int *x = (int *)malloc(sizeof(int));free(x);}myInt*xMemory• Code Execution:• Memory:void foo(int myInt){int *x = (int *)malloc(sizeof(int));free(x);}Memory• Code Execution:… start with dog();• Memory:char *cat(char *x, int i){static int r = 4;x[3] = 'h';char *result = (char *)malloc(20);sprintf(result, "%s x %d", x, i);return result; }void dog(){char s[] = "my cat";int z = 12;cat(s, z);}Memory• Code Execution:• Memory:char *cat(char *x, int i){static int r = 4;x[3] = 'h';char *result = (char *)malloc(20);sprintf(result, "%s x %d", x, i);return result; }void dog(){char s[] = "my cat";int z = 12;cat(s, z);}Memory• Code Execution:• Memory:char *cat(char *x, int i){static int r = 4;x[3] = 'h';char *result = (char *)malloc(20);sprintf(result, "%s x %d", x, i);return result; }void dog(){char s[] = "my cat";int z = 12;cat(s, z);}*s“my cat”Memory• Code Execution:• Memory:char *cat(char *x, int i){static int r = 4;x[3] = 'h';char *result = (char *)malloc(20);sprintf(result, "%s x %d", x, i);return result; }void dog(){char s[] = "my cat";int z = 12;cat(s, z);}*s“my cat”z = 12Memory• Code Execution:• Memory:char *cat(char *x, int i){static int r = 4;x[3] = 'h';char *result = (char *)malloc(20);sprintf(result, "%s x %d", x, i);return result; }void dog(){char s[] = "my cat";int z = 12;cat(s, z);}*s“my cat”z = 12*xi = 12cat()Stack Framedog()Stack FrameMemory• Code Execution:• Memory:char *cat(char *x, int i){static int r = 4;x[3] = 'h';char *result = (char *)malloc(20);sprintf(result, "%s x %d", x, i);return result; }void dog(){char s[] = "my cat";int z = 12;cat(s, z);}*s“my cat”z = 12*xi = 12r = 4cat()Stack Framedog()Stack FrameStatic MemoryMemory• Code Execution:• Memory:char *cat(char *x, int i){static int r = 4;x[3] = 'h';char *result = (char *)malloc(20);sprintf(result, "%s x %d", x, i);return result; }void dog(){char s[] = "my cat";int z =


View Full Document

U of I CS 241 - Console I/O

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Console I/O
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Console I/O and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Console I/O 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?