User Tools

Site Tools


tech:tutorial:c_basic

Basic C Tutorial

Resources

Here are the locations of existing free tutorial content that I have reviewed, and seen fit to refer to:

C Programming

Exercise 1

The first portion of this exercise is to complete the TutorialsPoint C-Environment Setup tutorial phase (but skip the “Try It Option Online” part).
Once that is complete, proceed with:

  • Make sure you have the “Hello world” program from TutorialsPoint in a .c file
  • Compile your program (you can use this step if you like)
  • Read the man page for the GNU make command
  • Write a Makefile to build your program
  • Ensure your Makefile also supports a clean target, which removes products of compilation
  • Test out all aspects of your Makefile to ensure it operates as expected

Remember to have your work reviewed by, as well as to seek help/advice from, your instructor.

Here is a sample Makefile as a kick-start:

PGM = hello

all : $(PGM)
        @echo Done

clean :
        @rm -f $(PGM) *.o

$(PGM) : hello.c
        gcc -g -o $(PGM) hello.c

Exercise 2

Read these sections in the TutorialsPoint tutorial:

… then, read the manpage for printf.

Once complete with these readings, write a program that stores integer values in four different integer variables, and prints each out using the printf function, making sure the name of the variable is shown before the value.
Once you have completed that, read this TutorialsPoint section, and then alter your program to do a number of typical arithmetic operations upon your variables, printing out both a label showing the calculation being done and the result for each.

Exercise 3

Read these sections in the TutorialsPoint tutorial:

… then, alter your program to start with one number (as one of your variables), and compute the result of n2 - 1 for n up to a higher number (as another of your variables). Make sure that the calculation being performed and the result are printed for each iteration.

When that program is complete, read this TutorialsPoint section, and then move your calculations into a function that takes the beginning and ending for your loop as parameters.

Exercise 4

Read this section in the TutorialsPoint tutorial:

… then, refactor your program to use an array of integers for n, and perform the calculation for each of the n in the array.

Once that is complete and working, read this section at TutorialsPoint, and then add a new function that creates a string, fills it with some text, and then iterates over the string to print each character out on a separate line.

Exercise 5

Read this section in the TutorialsPoint tutorial, in this order:

… then, create a new function that declares a local variable of a structure type, which you will also have to define at the top of your source file. The structure should have an integer and a character array of a length suitable for your name. In your function, fill the name field of the structure in with your name, set the integer to a value, then print out both.

Once that is working, refactor this function to take a pointer to your structure type as a parameter, and print out the integer and name from the structure passed in. Initialize a variable of your structure type to pass into the function from where you call it.

Exercise 6

One important aspect of maintaining the simplicity, extensibility, and managability of your code is “modularity”, and there are also various aspects of modularity to pay attention to.

For this exercise, we're going to focus on the arrangement of your code into files, and the respective build mechanics. After reading this TutorialsPoint section on C headers, proceed with:

  • Refactor all code written so far into functions
  • Merge all “main” functions into one central “main” function, which will be capable of deciding which functions to call for running the various tests
  • Make sure that there are multiple .c files, with your functions located in whichever files makes the most sense to you
  • Create a header (.h) file with function prototypes for all the functions that will need to be called from the “main” function
  • Refactor your Makefile with a two-pass process - include rules for building each .c file into a .o file, and then linking all .o files into the executable.
  • Make sure your Makefile is driven by dependencies, so that a .o file is only created when its respective .c file changes, and the executable is likewise only created when one or more of the .o files changes

Exercise 7

Read this section of the TutorialsPoint tutorial: Command Line Arguments, then:

  • Change your main function to the correct prototype for accepting command-line arguments
  • Read in command-line arguments to determine which of your scenarios to run
  • Read in command-line arguments for the various values that you pass to your functions
  • Have your program print usage instructions if there are no command-line arguments

Exercise 8

Read this section of the TutorialsPoint tutorial: C Input & Output, and then refactor your program to get some of its information from input instead of command-line arguments:

  • Add a function to your program as a central “event loop”, which is called once from the main function, and returns an overall “exit value” (an int that is 0 for normal exit, or other numbers reflecting errors)
  • In your “event loop” function, create a loop that will read a line from input for each iteration, and end when a certain string is encountered
  • For each string read, determine what needs to happen (according to your own logic), and call your own functions accordingly - you will need to design logic for current state and parsing the strings read
  • Overall, your program should prompt the user for some input, read the user's response, and then turn that response into some actions that involve calling your functions, possibly with values also read from input

Exercise 9

Read this section of the TutorialsPoint tutorial: C File IO, and then refactor your program to determine some of its behavior by values read from a configuration file:

  • Create a data structure (perhaps a struct, maybe an array - your choice of design) to contain all of the variables that you have previously read from input or set from the command line
  • Write a function that will populate that data structure with values read from a configuration file
  • Use the function to initialize your data from config file as “defaults”, then proceed with reading from the command-line, allowing anything on the command-line to “override” the defaults from config file
  • Change your code to utilize the data, in whatever ways make the most sense (either directly or passing to functions, etc)
  • Create a config file that provides the required values, in the format that your program reads
  • Run the program multiple times, changing the configuration file each time, and verifying that the program reads the values and correctly assigns and uses the variables configured
  • Run the program multiple times, verifying that any values provided on the command-line do actually over-ride the values from the configuration file

Exercise 10

Read this section of the TutorialsPoint tutorial: C Memory Management, and then refactor your program to save a history of user interaction, as a fifo queue of segments that can be used to dump a history report:

  • Create a data structure to store what information you want to show historically about the user interaction
  • Make sure that your program honors a value, via config and/or command-line, for how far back in history to keep track of
  • Write code that allocates the memory for your queue based on the configured history duration
  • Write logic that adds a new history step to the end of the queue when your application's context implies it
  • Write a function to output a report of the current history queue, and another to clear it
  • Make sure your event loop detects some user input to trigger the history report and the history clear, and possibly a combination of both
  • As before, also ensure your event loop also detects some certain user input for terminating the loop
  • Make sure you clean up your memory when appropriate


Links: TutorialsTech Info

tech/tutorial/c_basic.txt · Last modified: 2016/11/15 09:14 by rk4n3