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
The char type stores a numeric code which is translated to a character when appropriate And C treats them as numbers
Characters are Integers
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
Arithmetic and Char 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 thischar ch = 'A';
while(ch < 'A' + 26){
printf("%c\n", ch);
ch++;
}
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
Name and Age
Let’s write a program to find out the name and age of the user And then print them
Name and Age Programint 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 notesquare brackets
no &
What is your name? JennyWhat is your age? 11Your name is Jenny, and your age is 11
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
Arrays and Memory
... J e n n y \0
A sequence of 2o adjacent bytes in main memory
Why 20 bytes?
Because each character is stored in one byteWhat is the \0 in the 6th.
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
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
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
athe character 'a' athe string "a" \
0
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
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
Finding Sizes – sizeof
The sizeof function can be used to find the size in bytes of any variable or typeint 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));
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
characterchar 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));
#define(d) Strings
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;}
The difference is the null character
#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
#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
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
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
Input and Output
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
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 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
Flags for printf 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
<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
More About Placeholders We used different placeholders to print a
character and a number Consider this common errordouble 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
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
Conversion ExampleHere 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 bytesprintf, and all other function calls use an area of memory called the stack
...
n3(4byte
s)n2(4
bytes)
n1(8
bytes)...1st %d2nd %d3rd %d
part of the
stack
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 linesprintf("blah blah blah");
error! printf("blah blah ""blah");
OK
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
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
ExampleHere 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);
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
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!
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 readint x;int chars_read;printf("Please enter a number: ");chars_read = scanf("%d", &x);printf("Items successfully read = %d\n\n", chars_read);
More Later
We will cover more details on many of these subjects later Arrays Strings Pointers More string functions Other input and output functions
Top Related