Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Integers and Characters  Character Strings  Input and Output.

Similar presentations


Presentation on theme: " Integers and Characters  Character Strings  Input and Output."— Presentation transcript:

1

2  Integers and Characters  Character Strings  Input and Output

3

4  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  It’s easy to print the ASCII code for a character  char ch = ‘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  Let’s say that we want to print all of the letters from A to Z  We could write 26 printf statements ▪ printf('A'); ▪ printf('B'); ▪...  Or we could do this char ch = 'A'; while(ch < 'A' + 26){ printf("%c\n", ch); ch++; }

7  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  Let’s write a program to find out the name and age of the user  And then print them

9 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 What is your name? Jenny What is your age? 11 Your name is Jenny, and your age is 11

10  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 ...Jenny\0 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 6 th. byte? A null character to indicate the end of the string Why is this necessary? Because memory locations can’t be empty, so it is important to distinguish between characters we want to store and garbage values

12  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  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 a a the character 'a' a a the string "a" \0

14  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  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  The sizeof function can be used to find the size in bytes of any variable or type int x = 212; double d = 2.713; char ch = 'a'; char str[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  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)); 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  What gets printed? #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; } #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; } The difference is the null character

19  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 constants using #define like this  #define  #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  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  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

24  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 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 long int  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  Output can be further modified by the use of flags  - left justifies the output ▪ e.g. %-20s prints a left justified string of width 20  + indicates signed values with +, or – ▪ e.g. "**%+8.2f " prints a float like this: ** +123.45  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  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  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 Here is an example of a mismatched conversion Incidentally printing the fourth line was fun... printf("printf(\"n1 = %d, n2 = %d, n3 = %d\",n1, n2, n3);\n\n"); Note that the first placeholder prints the right most 4 bytes of n1 and the second prints the left most 4 bytes printf, and all other function calls use an area of memory called the stack... n3 (4bytes) n2 (4 bytes) n1 (8 bytes)... 1 st %d 2 nd %d 3 rd %d part of the stack

30  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! printf("blah blah " "blah"); printf("blah blah " "blah"); OK

31  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  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 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); 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); I could have written this in two lines: scanf("%d", &age); scanf("%s", name); What should happen: What might happen: 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 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

34  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  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 int x; int chars_read; printf("Please enter a number: "); chars_read = scanf("%d", &x); printf("Items successfully read = %d\n\n", chars_read); int x; int chars_read; printf("Please enter a number: "); chars_read = scanf("%d", &x); printf("Items successfully read = %d\n\n", chars_read);

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


Download ppt " Integers and Characters  Character Strings  Input and Output."

Similar presentations


Ads by Google