1 / 36

Strings and IO

Strings and IO. Strings. Integers and Characters Character Strings Input and Output. Strings. Types – So Far. To date all we have seen are numeric data types int , short, long, long long , ... float, double, ... char

sienna
Download Presentation

Strings and IO

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings and IO

  2. Strings • Integers and Characters • Character Strings • Input and Output

  3. Strings

  4. Types – So Far • To date all we have seen are numeric data types • int, short, long, long long, ... • float, double, ... • char • The char type stores a numeric code which is translated to a character when appropriate • And C treats them as numbers

  5. Characters are Integers • It’s easy to print the ASCII code for a character • charch = ‘x’; • printf(“code for %c = %d”, ch, ch); • The first placeholder print the letter that the code represents • The second placeholder prints the code • C will also allow arithmetic to be performed on char variables • The underlying numeric codes are operated

  6. Arithmetic and Char • Let’s say that we want to print all of the letters from A to Z • We could write 26 printfstatements • printf('A'); • printf('B'); • ... • Or we could do this charch = 'A'; while(ch < 'A' + 26){ printf("%c\n", ch); ch++; }

  7. Character Strings • We have used character strings in printf calls • e.g. printf(“Hello World”); • It would also be useful to store character strings in variables • And to get user input in the form of strings • In C a character string is a sequence of characters • Stored in an array

  8. Name and Age • Let’s write a program to find out the name and age of the user • And then print them

  9. Name and Age Program int main() { char name[20]; int age; printf("What is your name? "); scanf("%s", name); printf("What is your age? "); scanf("%d", &age); printf("Your name is %s, and your age is %d\n", name , age); return 0; } things to note square brackets no & What is your name? Jenny What is your age? 11 Your name is Jenny, and your age is 11

  10. Character Arrays • The line char name[20]; declares an array of 20 characters • A sequence in main memory with enough space for twenty characters • An array is an ordered sequence of data elements of one type • The brackets identify name as an array rather than a single character • And 20 indicates the size of the array

  11. Arrays and Memory A sequence of 2o adjacent bytes in main memory Why 20 bytes? Because each character is stored in one byte What is the \0 in the 6th. byte? A null character to indicate the end of the string Because memory locations can’t be empty, so it is important to distinguish between characters we want to store and garbage values Why is this necessary?

  12. Strings and scanf • We used scanf like this: scanf(“%s”, name); • The format specification %s is for a string • The variable name is not preceded by an & • Remember that when using scanf it is necessary to provide the address of a variable • Using the address of operator (i.e. &) • Array variables, like name, in fact contain addresses already, an array variable is a pointer • Which is why the & is not necessary

  13. Characters and Strings • A string consists of an array containing the words in the string and the null character • Consequently, a string containing a single character is not the same as a char variable • The type of a character array is not the same as the type of a character • That is char name[20] declares an array not a char the character 'a' a the string "a" a \0

  14. The Null Character • Usually we (as programmers) are not responsible for inserting the null character • This task is performed by a function that is responsible for creating a string • Such as scanf, for string input • Note also that functions such as printf are able to detect the null character • To print the appropriate part of a character array

  15. String Functions • There are a number of functions that are useful when working with strings • It can be important to know how long a character array is and how long a string is • The maximum length • And the length of the string currently stored in the array • The sizeof and strlen functions can be used to determine this information

  16. Finding Sizes – sizeof • The sizeof function can be used to find the size in bytes of any variable or type int x = 212; double d = 2.713; charch = 'a'; charstr[20]; printf("sizeof(x) = %d\n", sizeof(x)); printf("sizeof(d) = %d\n", sizeof(d)); printf("sizeof(ch) = %d\n", sizeof(ch)); printf("sizeof(str) = %d\n", sizeof(str)); printf("sizeof(short) = %d\n", sizeof(short));

  17. Finding the Size of a String • The strlen function can be used to find the size of a string stored in character array • The number of characters before the null character char name[20]; printf("What is your name? "); scanf("%s", name); printf("char[] size = %d\n", sizeof(name)); printf("size of %s = %d\n", name, strlen(name));

  18. #define(d) Strings • What gets printed? The difference is the null character #define BATMAN "Bruce Wayne" int main() { • printf("size of %s = %d\n", BATMAN, strlen(BATMAN)); • printf("BATMAN size = %d\n\n", sizeof(BATMAN)); return 0; }

  19. #define and Strings • The #define pre-processor directive can be used to create string constants • As well as any other kind of constant • #define SFU "Simon Fraser University" • When using #define it is not necessary to specify the type of data being defined • This is because the directive is essentially replacing the constants with the data • Before the compiler runs

  20. #define Syntax • Define constants using #define like this • #define <NAME> <value> • #define PI 3.14159 • #define TITLE “Lord of the Rings” • #define DAYS 365 • Constants are in upper-case by convention • To distinguish them from variables

  21. Size Constants • If you want to find the size of various types they are recorded in the limits.h file • e.g. INT_MAX, LONG_MAX, LLONG_MAX • This can be useful if you want to know the byte size of a type on a particular OS • There are similar constants for floats in float.h

  22. Other Constants • A more modern approach to creating constants is to use the const modifier • const double PI = 3.14159; • Unlike #define this is not a pre-processor directive so must • Declare the type, • Use assignment, and • End in a semi-colon • Defining constants with const is more flexible when dealing with projects with multiple files

  23. Input and Output

  24. Using printf • As we’ve seen the printf function can take the following arguments (i.e. input) • A string – required • Placeholders (conversion specifiers) in the string • Introduced by the % symbol • Variable or literal arguments that should match the placeholders • If the variables don’t match the placeholders then the output will be incorrect

  25. Placeholders • Placeholders indicate the type to be printed • e.g. %d for decimal integer • These can be further modified for • Type size • e.g. %lld specifies a long longint • Field width, right justified by default • e.g. %4d specifies a minimum field width of four • Precision – used for %e, %E, and %f to indicate the number of digits to the right of the decimal place • e.g. %4.2f prints a float five characters wide with two digits after the decimal place

  26. Flags for printf • Output can be further modified by the use of flags • - left justifies the output • e.g. %-20sprints a left justified string of width 20 • + indicates signed values with +, or – • e.g. "**%+8.2f"prints a float like this: ** +123.45 • <space> indicates signed values should start either with a space, or the – sign, to allow columns of signed values to line up • e.g. "**% 10.3f"prints a float like this: ** 123.456 • 0 indicates that numbers should be padded with zeros • e.g. "**%8d"prints an int like this: **00000123 • I’ve printed **s to show the spacing

  27. More About Placeholders • We used different placeholders to print a character and a number • Consider this common error double dbl = 123.1; printf("dbl = %d", dbl); • The placeholder indicates how the data stored in the argument should be printed • Strictly speaking it also indicates how many bytes of the argument should be accessed

  28. Mismatched Conversions • If a variable doesn’t match the placeholder the output may be incorrect • The placeholder indicates two things • The number of bytes of an argument to look at • What those bytes represent • Consider the %d placeholder • It is an instruction to look at 4 bytes of the appropriate argument • And interpret them as a 2’s complement int

  29. Conversion Example Here is an example of a mismatched conversion 3rd %d Incidentally printing the fourth line was fun ... 2nd %d Note that the first placeholder prints the right most 4 bytes of n1 and the second prints the left most 4 bytes 1st %d part of the stack printf, and all other function calls use an area of memory called the stack printf("printf(\"n1 = %%d, n2 = %%d, n3 = %%d\",n1, n2, n3);\n\n");

  30. Printing Long Strings • A long C statement may take up more than one line • Since the compiler ignores white space • A string may not take up more than one line • The compiler gives an error if a string takes up more than one line • But printf will concatenate two strings written on two lines printf("blah blah blah"); printf("blah blah " "blah"); error! OK

  31. Input With scanf • Similar to printf, scanf uses a control string with a list of arguments • The control string contains shows the destination data types for the input stream • %s, %f, %s and so on • Characters that do not match the destination types will cause problems • The arguments contains the addresses of the variables

  32. Whitespace • The scanf function uses whitespace to determine that the end of input is reached • New lines, tabs or spaces • Other input functions allow whitespace to be included in input (for strings or characters) • The scanf function treats input as a stream of characters • Processing or ignoring each character • Ignored characters are left in the stream

  33. Example Here is a code fragment to request a name and age int age; char name[20]; printf("Enter your age, and last name: "); scanf("%d%s", &age, name); printf("Your name is %s, and you are aged %d\n", name, age); I could have written this in two lines: scanf("%d", &age); scanf("%s", name); When a non-numeric character is reached (e) input stops for age Then e3 was read as the name, and input stops once whitespace is encountered What should happen: What might happen:

  34. Characters In Format String • It is permissible to put ordinary characters in the format string • These characters must be matched exactly by the input string • e.g. scanf(“%d,%d”, &x, &y); • scanf expects the user to type a number, a comma, and another number • I would recommend not doing this!

  35. scanf Return Value • The scanf function returns a value, allowing for functions to detect unexpected results • The value is the number of items that have been successfully read intx; intchars_read; printf("Please enter a number: "); chars_read = scanf("%d", &x); printf("Items successfully read = %d\n\n", chars_read);

  36. More Later • We will cover more details on many of these subjects later • Arrays • Strings • Pointers • More string functions • Other input and output functions

More Related