Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active October 17, 2017 20:40
Show Gist options
  • Save rszewczyk/34ba3156bd2512799bf86b01b3486d35 to your computer and use it in GitHub Desktop.
Save rszewczyk/34ba3156bd2512799bf86b01b3486d35 to your computer and use it in GitHub Desktop.
UMBC CMSC Fall 2017 - 10/5/2017

Assignment - Class #10 10/5/2017

In this assigment you will learn the ins and outs of printing certain types of output to the screen and getting input from the user.

As part of this assigment you will create a program in a file called inout.c. When you complete the assigment you will submit inout.c by uploading it to Slack. If you do not finish today, you have until the end of class (6:45pm) on Tuesday, 10/10/2017 to turn it in. Note this doesn't necessarily mean you will have time to work on it in class on 10/10. If don't finish today, you should try and finish it before Tuesday.

Step 1

Copy and paste the code below into a new file and save it. Give it the name inout.c. Upload this file to the UMBC Linux server, compile it and run it.

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 10.0;

  printf("The area of the circle is %lf\n\n", areaOfCircle(radius1));

  return 0;
}

Line 12 uses the function printf from the library stdio.h which you included on line 1. The first argument to the printf function is a string of characters enclosed in double quotes. On line 12, that's "The area of the circle is %lf\n\n". The string can include one or more placeholders for other values. Placeholders start with the character %. On line 12 the placeholder is %lf. The printf function also takes additional arguments equal in number to the number of placeholders. Placeholders are specific to a data type - %lf is the placeholder for a double. On line 12 there is one placeholder of type double, so there is one additional argument of type double. printf will replace the placeholder with the value of the corresponding argument and print the result on your screen.

Step 2

Modify line 12 so that says: printf("The area of the circle with radius %lf is %lf\n\n", 10.0, areaOfCircle(radius1));

The resulting code should look like:

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 10.0;

  printf("The area of the circle with radius %lf is %lf\n\n", 10.0, areaOfCircle(radius1));

  return 0;
}

Save the file, upload it, compile and run it.

In Step 2 we've added a second placeholder and corresponding argument. Note that the order of the placeholders matches the order of the arguments that replace them (i.e. the first argument corresponds to the first placeholder, the second argument corresponds to the second placeholder).

Observe that the arguments can be anything representing a value of the correct type. The first additional argument on line 12 is the literal value 10.0, while the next argument is the result of a function that returns a value. In both cases the resulting values have the same data type as the placeholder that they replace.

You can also use a variable that contains a value as an additional argument. Replace 10.0 on line 12 with the variable name radius1. Save, upload, compile and run your program.

Step 3

Modify line 12 again so that it says: printf("The area of circle %d, with radius %lf, is %lf\n\n", 1, radius1, areaOfCircle(radius1));. Your code should now look like:

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 10.0;

  printf("The area of circle %d, with radius %lf, is %lf\n\n", 1, radius1, areaOfCircle(radius1));

  return 0;
}

Save the file, upload it, compile and run it.

We've now introduced a different placeholder: %d, which is for the data type int. Note again how the number of additional arguments match the number of placeholders in the string, and that each placeholder has the correct data type for its corresponding argument.

Step 4

Modify line 12 again so that it says: printf("The area of circle %3d, with radius %lf, is %lf\n\n", 1, radius1, areaOfCircle(radius1));. Your program should now look like:

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 10.0;

  printf("The area of circle %3d, with radius %lf, is %lf\n\n", 1, radius1, areaOfCircle(radius1));

  return 0;
}

Save the file, upload it, compile and run it. Notice how the integer 1 is now pushed two places to the right. By adding a 3 between the % and the d we've told the placeholder to print its value in a field that is 3 characters wide. Since the number is less than 3 characters wide it prints two empty spaces to the left.

Now change the first additional argument on line 12 from 1 to 10000. Line 12 should now look like: printf("The area of circle %3d, with radius %lf, is %lf\n\n", 10000, radius1, areaOfCircle(radius1));. Save the file, upload it, compile and run it. Observe that even though the placeholder specifies 3, it doesn't truncate the printed value. This is because the field width is a minimum value. If the number is too large to fit the field width, printf will still print the full number.

The field width configuration can be applied to the %lf placeholder as well. Add a field width of 5 to both of them on line 12, so that they look like %5lf. Save the file, upload it, compile and run it. Observe the results.

Step 5

Modify line 12 again so that it says: printf("The area of circle %3d, with radius %10.3lf, is %2.1lf\n\n", 10000, radius1, areaOfCircle(radius1));.

Your code should now look like:

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 10.0;

  printf("The area of circle %3d, with radius %10.3lf, is %2.1lf\n\n", 10000, radius1, areaOfCircle(radius1));

  return 0;
}

Save the file, upload it, compile and run it. Observe the results.

When you add a . and a number to the placeholder %lf you are telling printf to truncate the decimal portion to a certain number of places. The field width configuration still applies. The decimal portion will be truncated first and then the resulting number will be printed in the configured width. Note that you don't need to provide field width when you truncate a placeholder value. Change %10.3lf to %.3lf. Save the file, upload it, compile and run it. Observe the results.

Step 6

Add a new line line 13 that says: printf("%c is letter number %d of the alphabet\n\n", 'e', 5);.

Your code should now look like:

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 10.0;

  printf("The area of circle %3d, with radius %.3lf, is %2.1lf\n\n", 10000, radius1, areaOfCircle(radius1));

  printf("%c is letter number %d of the alphabet\n\n", 'e', 5);

  return 0;
}

Save the file, upload it, compile and run it. Observe the results.

We've introduced a new type of placeholder, %c for printing values of type char. Just like the other two placeholders, it can print anything that represents a value of the appropriate type. Try assigning 'e' to a variable of type char called myChar just above line 14. Replace 'e' in the additional arguments to printf with myChar. Save the file, upload it, compile and run it. Observe the results.

The field width configuration also works for %c placeholders. Try changing the %c to %4c. Save the file, upload it, compile and run it. Observe the results.

Step 7

You're now going to add four more lines to your program after line 15 as seen below:

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 10.0;

  printf("The area of circle %3d, with radius %.3lf, is %2.1lf\n\n", 10000, radius1, areaOfCircle(radius1));

  char myChar = 'e';
  printf("%4c is letter number %d of the alphabet\n\n", myChar, 5);

  double doubleFromUser = 0;
  printf("Enter a number: ");
  scanf("%lf", &doubleFromUser);
  printf("\nYou Entered %lf\n\n", doubleFromUser);

  return 0;
}

Save the file, upload it, compile and run it. When prompted to enter a number, type a number and press return.

The scanf function is also included as part of stdio. It is used to take input from the keyboard and assign it a variable. Note that it also uses placeholders - in this case %lf for type double. The placeholders for scanf are the same as for printf. The additional arguments in scanf are variable names where you want the input to be stored. Note how they are preceded with the & symbol - this is required for the variable types we have learned so far, don't forget it.

Once you press enter, scanf will read the characters you typed on the keyboard and translate that into the appropriate data type. Note that it won't give you any kind of error if you try to type the wrong kind of data (e.g. non numeric data when you're trying to store a type double). Go ahead and run your program again. When prompted type some characters and press return. Observe the results.

Step 8

Modify your program so that it looks like:

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 10.0;

  printf("The area of circle %3d, with radius %.3lf, is %2.1lf\n\n", 10000, radius1, areaOfCircle(radius1));

  char myChar = 'e';
  printf("%4c is letter number %d of the alphabet\n\n", myChar, 5);

  double doubleFromUser = 0;
  int intFromUser = 0;
  char charFromUser = 'a';
  printf("Enter a decimal number, an integer and a character: ");
  scanf("%lf%d %c", &doubleFromUser, &intFromUser, &charFromUser);
  printf("\nYou Entered %lf, %d and %c\n\n", doubleFromUser, intFromUser, charFromUser);

  return 0;
}

We've declared and initialized two new variables on lines 18 and 19. Notice how we've now included multiple placeholders and additional arguments when we call scanf. This works similar to printf. The input entered for each placeholder is assigned to a corresponding argument. Again, the type of the argument must match the type of the placeholder. Save the file, upload it, compile and run it. Now when you are prompted type a number and return; then another number and return; finally type a single character and return. Observe the results. All three values are assigned to the corresponding variables.

Notice there is a space in front of the %c placeholder. Try removing the space, save the file, upload it, compile and run it. You should observe that the program appears to skip the character input. Any idea why? If you have a theory, post it in the general channel of Slack. Go ahead an add the space back - save, compile, upload and run your program to make sure it works now.

You don't have to gather multiple inputs all at once. Note that this is kind of confusing from the user perspective. It's probably better in this case to prompt the user for a variable and assign them with scanf one at a time. Can you modify your program to do that? That is, the output should look similar to:

The area of circle 10000, with radius 10.000, is 314.0

   e is letter number 5 of the alphabet

Enter a decimal number and press return: 10.0
Enter an integer and press return: 4
Enter a character and press return: g

You Entered 10.000000, 4 and g

Step 9

Your code should now look like this:

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 10.0;

  printf("The area of circle %3d, with radius %.3lf, is %2.1lf\n\n", 10000, radius1, areaOfCircle(radius1));

  char myChar = 'e';
  printf("%4c is letter number %d of the alphabet\n\n", myChar, 5);

  double doubleFromUser = 0;
  int intFromUser = 0;
  char charFromUser = 'a';

  printf("Enter a decimal number and press return: ");
  scanf("%lf", &doubleFromUser);

  printf("Enter an integer and press return: ");
  scanf("%d", &intFromUser);

  printf("Enter a character and press return: ");
  scanf(" %c", &charFromUser);

  printf("\nYou Entered %lf, %d and %c\n\n", doubleFromUser, intFromUser, charFromUser);

  return 0;
}

If it doesn't, make it so. Save the file, upload it, compile and run it.

Step 10

Now, using what you've learned today, try the following:

  1. Modify line 30 so that only two places are printed after the decimal point for the first number
  2. Modify line 30 so that the second number is printed in a field at least 6 characters wide
  3. Add code after line 30 to prompt the user for the radius of a circle then print the resulting area

The final output should look like:

The area of circle 10000, with radius 10.000, is 314.0

   e is letter number 5 of the alphabet

Enter a decimal number and press return: 10.345
Enter an integer and press return: 4
Enter a character and press return: p

You Entered 10.35,      4 and p

Enter the radius of the circle: 23
The area of the circle with radius 23.000000 is 1661.060000

When you are finished upload inout.c to Slack in a direct message to me.

Data Types

These are the data types we've been using in class so far. We will introduce more data types as needed.

type keyword description example
character char a single character char myChar = 'a';
integer int a signed integer int myPositiveInt = 1; or int myNegativeInt = -2;
double double a signed double precision floating point number double myPositiveDouble = 0.245; or double myNegativeDouble = -1.35;

Input

Get char from user

char myChar = 'a';
scanf(" %c", &myChar);

Get int from user

int myInt = 0;
scanf("%d", &myInt);

Get double from user

double myDouble = 0.0;
scanf("%lf", &myDouble);

Output

Print/format int

int myInt = 1;
// print myInt followed by a new line
printf("%d\n", myInt);

// print myInt in a field at least 6 places wide (extra places will be filled with spaces).
printf("%6d, myInt);

Print/format char

char myChar = 'a'
// print myChar followed by a new line
printf("%c\n", myChar);

Troubleshooting Common problems:

#1.

I'm using scanf or printf. My program appears to hang and do nothing. When I compiled, I'm getting a warning that says something like warning: incompatible implicit declaration of built-in function ‘scanf’.

Did you forget #include <stdio.h>?

#2.

When using scanf to get a int, char or float from the user, I get a compiler warning that says something like warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’. If I run the program, it behaves strangely. After inputing a value it might crash or it may not have the right value.

Did you forget the & in scanf("%d", &myInt)?

#3.

When using scanf to get a char value my program compiles, but when I run it, it appears to skip the prompt for input. There is a blank value stored in the variable I tried to use with scanf. I remembered to use the & before the variable name.

When using the format string "%c" and scanf, you often need to tell scanf to ignore any existing whitespace in the input buffer. To do this, use a space in front of the c in " %c". You only need to do this with " %c".

At the end of this class you will be able to:

  • Print a line of text that includes one or more values of type integer
  • Format integer output to be printed in a particular field width
  • Print a line of text that includes one or more values of type double
  • Format decimal output to be printed in a particualr field width and with a particular precision
  • Get numeric and character input from the user and store it in a variable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment