Tutorial 2 (Sep 21): getchar(), putchar(), scanf(), printf(), strcpy(), strlen()
Topics
- characters, string literals, strings (character arrays)
- getchar(), putchar()
- scanf(), printf()
- strcpy(), strlen()
In-Class Exercises
input/output
strcopy
Characters, string literals, strings (character arrays)
- int c = 'A'; // c = 65, since the ASCII value of 'A' is 65
- a character in C is stored as an integer
- this integer represents the ASCII value of the character
- you can freely convert between a character and an int, since it is stored as an int
- char string_name[100] // create a character array of size 100
- C has no primary construct for strings
- a string is represented/stored as an array of characters
- the char array is manipulable ie. you can change the contents freely
- however, the size of the array is fixed. To change the size of the memory block containing your array, you can use realloc()
- char *mystring = "hello, world!"; // create a string literal containing my string
- a string literal is a pointer to a read-only memory block, containing the string
- *mystring is the pointer to where mystring is stored (the start of the string literal)
- *(mystring+1) will return the second character of the string literal (in this case, the letter "e")
getchar(), putchar()
- getchar()
- gets one single character from the user
- NOTE: when the user types a character then presses ENTER (eg. <a><ENTER>), the letter 'a' is grabbed by getchar(), while the <ENTER> stays in the buffer. If you read from the buffer using getchar() or scanf() after, it will first read the leftover <ENTER>, before reading any other input from the user.
- putchar(int char)
- prints one character to the output
- you can use putchar('\n') to print a newline character
scanf(), printf()
- scanf(const char *format, ...)
- gets user input, according to the format specified (eg. %s for a word, %d for text, %[a-ZA-Z] for all letters, %[ a-zA-Z] for all letters plus spaces, %[^\t\n] for anything other than tab/newline characters)
- eg. scanf("%s", string_to_save_to); // get the first word (delimited by a space), and save it to string_to_save_to
- eg. scanf("Numbers: %d %d", first_num, second_num); // if the user types Numbers: 10, 20, then first_num = 10 and second_num = 20)
- printf(const char *format, ...)
- prints strings to output, along with any variables provided as arguments
- eg. printf("%s", string_to_print); // print the first word saved in string_to_print
- eg. printf("Your two numbers are %d and %d.", first_num, second_num) // if first_num = 10 and second_num = 20, then the output is "Your two numbers are 10 and 20."
strcpy()
- strcpy(char *dest, const char *src)
- copies the string value of the src to dest
- C does not have a primary construct for strings, so you need to include <string.h> to use strcpy() and other string manipulation methods (eg. strlen() )
- trying to copy a string like in Java will result in only copying the pointer
- to copy the value of the string (which the pointer is pointed at), we need to use strcpy()