Practice Problems

Comments

  1. Give an example of an in-line comment
  2. Give an example of a multi-line comment
  3. What are comments used for?
  4. What happens in C if you put a multi-line comment inside another multi-line comment?

Math

  1. What is 13 / 7 in C?
  2. What is 13 % 7 in C?
  3. What is 13 % 7.7 in C?
  4. What is 'A' % 10 in C?

printf and scanf

Notice when you get compile errors (when gcc gives you an error)
vs. errors that crash the program (run-time errors)
vs. logical errors (compile and run fine but problem with your code)

printf

  1. What happens when you print a float as a %d?
    		float f = 3.14;
    		printf( "%d", f );
    	
  2. What happens when you print an int as a %f?
    		int a = 3;
    		printf( "%f", a );
    	
  3. What happens when you print &variable?
    		int a = 3;
    		printf( "%d", &a );
    	
  4. What happens when you print a char as a %d?
    		char c = 'A';
    		printf( "%d", c );
    	

    To really get it, try also printing B, C, D... then a, b, c, ... What does it map to?
  5. What happens when you forget to list the variables?
    		int a = 15;
    		char c = 'Z';
    		printf( "%d %c"  );
    	
  6. Write a program that prompts the user to enter an integer and then print the integer first as a character, then as a decimal, and finally as a float.

scanf

  1. Write a program to test each of the following, such that it reads in the data from the keyboard and stores the values in to variables a, b, c and d
  2. What does your scanf need to look like if the user will input the following (a, b, c are integers, d is a char):
    			213  145  14Z
    		
  3. What does your scanf need to look like if the user will input the following (a, b, c are integers, d is a char):
    			213  145  14 Z
    		
  4. What does your scanf need to look like if the user will input the following (a is an integer, b is a float, d is a char):
    			213  1.45  Z
    		
  5. What's the difference between the following:
    		scanf( "%d%d%c", &a, &b, &c );
    	
    and
    		scanf( "%d %d %c", &a, &b, &c );
    	
  6. How would you get the user to enter a date with slashes, e.g. 11/25/2006 and store the month in a, day in b and year in c?
  7. What happens if you have
    		scanf( "%d %d %c", &a, &b, &c );
    	
    BUT you only enter 2 values then press enter when the program runs?
  8. What happens if you have
    		scanf( "%d %d %c", &a, &b, &c );
    	
    BUT you enter 4 values then press enter when the program runs?
  9. What happens if you have
    		scanf( "%d %d", &a, &b, &c );
    	
  10. Write a program that prompts the user to enter a quantity and a cost. The values are to be read in to an integer named qty and a float named unitPrice. Use only one statement to read in the values. Then skip a line and print each value with an appropriate description.
  11. Write a program that reads in two values and prints the average.