DOC PREVIEW
UW CSE 303 - Lecture Notes

This preview shows page 1 out of 4 pages.

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

Unformatted text preview:

11/6/20091David Notkin  Autumn 2009  CSE303 Lecture 17#preprocessorDebugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.--Brian W. KernighanType char• char : A primitive type representing single characters– literal char values have apostrophes: 'a' or '4' or '\n' or '\''• you can compare char values with relational operators– 'a' < 'b' and 'X' == 'X' and 'Q' != 'q'• What does this example do?for (char c = 'a'; c <= 'z'; c++) {printf(“%c”,c);}char and int• chars are stored as integers internally (ASCII encoding)'A' 65 'B' 66 ' ' 32 '\0„ 0'a' 97 'b' 98 '*' 42 '\n' 10char letter = 'S';printf("%d", letter); // 83• mixing char and int causes automatic conversion to int– 'a' + 2 is 99, 'A' + 'A' is 130– to convert an int into the equivalent char, type-cast it --(char) ('a' + 2) is 'c'Strings• in C, strings are just arrays of characters (or pointers to char)• the following code works in C – why?char greet[7] = {'H', 'i', ' ', 'y', 'o', 'u'};printf(greet); // output: Hi you• the following versions also work and are equivalent:char greet[7] = "Hi you";char greet[] = "Hi you";• Why does the array have 7 elements?Null-terminated strings• in C, strings are null-terminated (end with a 0 byte, aka '\0')• string literals are put into the "code" memory segment– technically "hello" is a value of type const char*char greet[7] = {'H', 'i', ' ', 'y', 'o', 'u'};char* seeya = "Goodbye";greetseeyaindex 0 1 2 3 4 5 6char'H' 'i' ' ' 'y' 'o' 'u' '\0'index 0 1 2 3 4 5 6 7char'G' 'o' 'o' 'd' 'b' 'y' 'e' '\0'(stack)(heap)String input/outputchar greet[7] = {'H', 'i', ' ', 'y', 'o', 'u'};printf("Oh %8s!", greet); // output: Oh hi you!char buffer[80] = {'\0'}; // inputscanf("%s", buffer);• scanf reads one word at a time into an array (note the lack of &)– if user types more than 80 chars, will go past end of buffer (!)• other console input functions:– gets(char*) reads an entire line of input into the given array– getchar() reads and returns one character of input11/6/20092Looping over chars• don't need charAt as in Java; just use [] to access charactersint i;int s_count = 0;char str[] = "Mississippi";for (i = 0; i < 11; i++) {printf("%c\n", str[i]);if (str[i] == 's') {s_count++;}}printf("%d occurrences of letter s\n", s_count);String literals• when you create a string literal with "text", really it is just a const char* (unchangeable pointer) to a string in the code area// pointer to const string literalchar* str1 = "str1"; // okstr1[0] = 'X'; // not ok// stack-allocated string bufferchar str2[] = "str2"; // okstr2[0] = 'X'; // ok// but pointer can be reassignedstr1 = "new"; // okstr2 = "new"; // not okmainstr1str2availableheapglobal datacodes t r 1 \0s t r 2 \0n e w \0Pointer arithmetic• +/- n from a pointer shifts the address by n times the size of the type being pointed to– Ex: Adding 1 to a char* shifts it ahead by 1 byte– Ex: Adding 1 to an int* shifts it ahead by 4 byteschar[] s1 = "HAL";char* s2 = s1 + 1; // points to 'A'int a1[3] = {10, 20, 30, 40, 50};int* a2 = a1 + 2; // points to 30a2++; // points to 40for (s2 = s1; *s2; s2++) {*s2++; // what does this do?} // really weird!How about this one?char* s1 = "HAL";char* s2;for (s2 = s1; *s2; s2++) {printf("%c\n",(char)((*s2)+1));};CSE303 Au09 10Strings as user inputchar buffer[80] = {0};scanf("%s", buffer);• reads one word (not line) from console, stores into buffer• problem : might go over the end of the buffer– fix: specify a maximum length in format string placeholder– scanf("%79s", buffer); // why 79?• if you want a whole line, use gets instead• if you want just one character, use getchar (reads \n explicitly)String library functions• #include <string.h>function descriptionint strlen(s)returns length of string s until \0strcpy(dst, src)copies string characters from src into dstchar* strdup(s)allocates and returns a copy of sstrcat(s1, s2)concatenates s2 onto the end of s1 (puts \0)int strcmp(s1, s2)returns < 0 if s1 comes before s2 in ABC order; returns > 0 if s1 comes after s2 in ABC order;returns 0 if s1 and s2 are the sameint strchr(s, c)returns index of first occurrence of c in sint strstr(s1, s2)returns index of first occurrence of s2 in s1char* strtok(s, delim)breaks apart s into tokens by delimiter delimstrncpy, strncat, strncmplength-limited versions of above functions11/6/20093Comparing strings• relational operators (==, !=, <, >, <=, >=) do not work on stringschar* str1 = "hello";char* str2 = "hello";if (str1 == str2) { // no• instead, use strcmp library function (0 result means equal)char* str1 = "hello";char* str2 = "hello";if (!strcmp(str1, str2)) {// then the strings are equal...}More library functions• #include <ctype.h> (functions for chars)– isalpha('A') returns a nonzero result (true)function descriptionint atoi(s)converts string (ASCII) to integerdouble atof(s)converts string to floating-pointsprintf(s, format, params)writes formatted text into ssscanf(s, format, params)reads formatted tokens from sfunction descriptionint isalnum(c),isalpha, isblank, isdigit, islower, isprint, ispunct, isspace, isupper, isxdigit, tolower, touppertests info about a single characterCopying a string• copying a string into a stack buffer:char* str1 = "Please copy me";char str2[80]; // must be >= strlen(str1) + 1strcpy(str2, str1);• copying a string into a heap buffer:char* str1 = "Please copy me";char* str2 = strdup(str1);• do it yourself (hideous, yet beautiful):char* str1 = "Please copy me";char str2[80];while (*s2++ = *s1++); // why does this work?Midterm A• Suppose you have a shell script named abc and you execute$ ./abc > /dev/null Since standard output is redirected to /dev/null there is no output sent to the console. Does this always, never, or sometimes have the same effect as simply not executing the script? Briefly explain.CSE303 Au09 16Midterm BConsider the following commands and output in the shell:$ grep grep grepgrep: grep: No such file or directory$ grepUsage: grep [OPTION]... PATTERN [FILE]...Try `grep --help' for more information.If you instead enter $ grep grepwhat happens? Be precise.CSE303 Au09 17Midterm C•Consider the following commandgrep -E "(/\*([^*]|(\*+[^*/]))*\*+/)|(//.*)" *.c•It is intended to search C


View Full Document

UW CSE 303 - Lecture Notes

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?