CSCI 1300 - Exercise 6
C-Style Strings, printf, and scanf

What You'll Get from This Exercise

There are a number of C++ items that aren't available in C.   We'll show you how to get around these problems should you find yourself needing to program in C.  Specifically, this exercise will look at C-style strings, printf, and scanf.

C-Style Strings

Strings in C are one-dimensional arrays of characters.  Each element of the char array contains one letter in the string, and the last element of the array contains the null character (the NULL constant in C/C++).  C contains a number of functions for string copying, concatenation, comparison, etc.  See http://en.wikipedia.org/wiki/C_string and http://www.cplusplus.com/doc/tutorial/ntcs/ for more information about C-style strings.

The following shows two equivalent ways to declare and initialize a string:
   char first_name[] = "Fred";
char first_name_alt[5] = {'F', 'r', 'e', 'd', NULL};
The size of both the first_name and first_name_alt arrays is 5 elements (4 characters for the word plus the null character).

Task: Write a program that calls a void function that asks the user for his first name and then prints the letters in the first name in reverse order.  Use C-style strings in the implementation of your function.

printf

When programming in C, we use the printf function to print formatted strings to the console.  The printf function accepts a format string and 0 or more additional arguments that specify the values to apply to the format string.   The format string is composed of text and one or more format specifiers; each format specifier is denoted using the "%" character.  Your program must contain the "#include <stdio.h>" directive to use printf.   See http://www.cppreference.com/wiki/c/io/printf for more information about printf. 

As an example of printf usage, the following code snippet:
   char name[20] = "Bob";
int age = 21;
printf("Hello %s, you are %d years old\n", name, age);
produces the following output:
   Hello Bob, you are 21 years old
Note that the '\n' character is the newline character, which is equivalent to "endl" when using cout in C++.

Task: Add a void function to your program that prints a nicely formatted table of numbersi (perhaps passing those numbers to the function as arrays). Implement your function using printf.  Call your function from the main function in your program.

scanf

When programming in C, we use the scanf function to read formatted input from the console.  The scanf function is similar to printf, in that it accepts a format string and one or more additional arguments.  These additional arguments specify the variables that will be used to store the values specified in the format string.  Like printf, the format string is composed of text and one or more format specifiers; each format specifier is denoted using the "%" character.  Your program must contain the "#include <stdio.h>" directive to use scanf.   See http://www.cppreference.com/wiki/c/io/scanf for more information about scanf.

As an example of scanf usage, the following code reads an int, float, and double from the user:
   int i;
float f;
double d;

printf("Enter an integer: ");
scanf("%d", &i);

printf("Enter a float: ");
scanf("%f", &f);

printf("Enter a double: ");
scanf("%lf", &d);

printf("You entered %d, %f, and %f\n", i, f, d);
Note that the variable arguments to scanf must be passed as reference arguments, as indicated by the use of the ampersand (&) operator with each variable.

Task: Add a void function to your program that asks the user to enter 10 int values.  Print the largest of these 10 values.  Use scanf and printf in the implementation of your function, and call your function from the main function in your program.