UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

Compiling C programs and printf function

Previous activities

1.  Questions about gcc

Work Plan

  1. Start the virtual machine and open a command terminal.

  2. Answer individually to the following questions and check the answers with your lab partner:

    • What is the -o option used for?

    • Give an example of a warning that is only shown with the -Wall option.

    • How can you know the time it takes to execute a command?

    • If a program is invoked from the command interpreter with the command program a b c d, What is the value of the first parameter of the main function?

How long did this activity take you? mins.

2.  The preprocessor and the lines starting with #

Resources

  • The virtual machine.

  • Files main.c and main.h that you have to download in your current directory.

Work Plan

  • Make sure you understand what is the effect of the #include, #define and #ifdef ... #endif directives.

  • Answer the following questions and check the answers with your lab partner before the lab session.

    1. What is the difference between the directives #include <file.h> y #include "file.h"?

    2. If program main.c includes the file main.h, is the following command correct?

      $ gcc -o main main.c
    3. What is the difference between the directive #define SYMBOL and #define SYMBOL 10?

    4. Suppose you need to have two versions of a program that you wrote. The first version is used by you to detect errors and the program prints on the screen all types of check messages. The second version is for the customer and those messages cannot appear. How would you achieve this with the #ifdef directive?

  • Prepare your virtual machine to work with a command terminal and the compiler.

  • Edit the files main.c and main.h. Additionally to the #ifdef directive, the program uses the #ifndef directive that is simply the negation of the previous one. Create two executable versions, one that prints msg1 and not msg2, and another that prints both messages.

How long did this activity take you? mins.

3.  The function printf

Resources

Work Plan

  1. Compile and execute the file printf.c.

  2. Rewrite the format strings used in the printf function to produce the output with the following format (do it without using more than two consecutive white spaces):

    The letter is              m
    The number is             60345698
    The hexadecimal number is 0X3FA
    The real number is        3.10e+33
    The string is             ABCDE
How long did this activity take you? mins.