C Exercises

Here you will find exercises followed by a list of things they require an understanding of. It's a good idea to write comments for your solutions just to get into the habit of commenting things.

Set #1 - Nov 14, 2022

1a: Write a program that uses scanf to get the value of a circle's radius from the user. With this value, print out what the circumference of the circle is, using a macro for pi.

[show solution] [download source file]

1b: Write a program that prints out a text art right triangle in the terminal with nested for loops. The width and height of the triangle should be 10 units.

[show solution] [download source file]

1c: Write a function that returns true if a number passed to it is prime and false if it isn't. Then write a program that uses this function to print out all of the numbers from 1-100 that are prime.

[show solution] [download source file]

1d: Write a program with a function macro PRINTD that prints a variable's name and value. Print out the values of 3 variables with it.

[show solution] [download source file]

1e: Write a function that repeatedly prints a number a certain amount of times. The function header should be: void print_num_rep(int num, int rep);

[show solution] [download source file]

1f: Write a program that will print "HI" if the PRINTHI macro is defined during compilation and print "HELLO" otherwise.

[show solution] [download source file]

1g: Write a function that will repeatedly print "Call" on the same line the same number of times that it has been called in the program so far.

[show solution] [download source file]

Set #2 - Nov 25, 2022

2a: Write a header file named shapes.h that contains multiple structs we'll be using in other exercises for this set. It should contain the type definitions for point and rectangle structs. Be sure to include a header guard!

[show solution] [download shapes.h]

2b: Write a function that takes a pointer to an int as a parameter and changes the value that the pointer points to to 5. Test its functionality by printing out some values.

[show solution] [download source file]

2c - Write a function that takes a void pointer as a parameter and prints out its memory address. Test its functionality by printing out some values. Remember that %p is the conversion specification for pointers.

[show solution] [download source file]

2d - Write a function that takes a pointer to a rectangle struct as a parameter and prints out all of its member variables (variables inside the struct). To get the type definition for rectangles, make sure you #include the header file shapes.h which we made in exercise 2a. Also, check 2a's solution to make sure shapes.h is correct at this point.

[show solution] [download source file]

2e: Write a function that takes a pointer to a rectangle struct as a parameter and returns its area.

[show solution] [download source file]

2f: Let's test out math.h from the C standard library. To use it, you must #include <math.h> in your source file and append "-lm" to your compile command to link it to the math library. Use the sqrt() function from math.h to compute the square root of a number, and then print it out.

For more info on math.h and other standard library headers, check out cppreference.com.

[show solution] [download source file]

2g: Write a function that returns the distance between two points. It should take two pointers to point structs as parameters and return a double. Test it by printing out some values. Remember to #include shapes.h from exercise 2a to get the type definition for point structs.

[show solution] [download source file]

2h: Write a program that prints out a char variable in the terminal using a pointer to that char. Then, it should move through memory using the pointer and print all of the characters it finds until it reaches a segfault. Hint: adding +1 to a char pointer will increase its memory address by 1.

[show solution] [download source file]

2i: This exercise should help you practice using multiple c source files for a single program.

Combine the definitions of functions from exercises 2d, 2e, and 2g into a source file named shape_functions.c. Then, combine their function prototypes into a header file named shape_functions.h.

Finally, create a source file with a main function that utilizes all of these functions, named 2i.c. Compile your program by writing all of the source file names it uses in your compile command.

[show 2i.c] [show shape_functions.h] [show shape_functions.c] [download 2i.c] [download shape_functions.h] [download shape_functions.c]