Lab 0: First C Programs

Before attempting this, perhaps you should read a little about pointers, files and return values here

The Task

Your task here is to write, compile and run your first C programs. Feel free to use the text and notes, but you should avoid the temptation to just copy stuff; you know it when you can make it up yourself without help - except perhaps for looking up something very specific.

Programming Environment

Learning a modern IDE like Eclipse is a great idea, but for this course we would like you to use a plain (but syntax aware) editor to write your programs. You will have a second window open, where you will use the command line to compile and run your programs.

Be sure to compile your programs using the -Wall flag to show all warnings. And, of course, where necessary, change your program so that it does not produce any warnings.
When you compile a program called someProgram.c, let the name of the executable be someProgram, i.e. the same name, but without the .c at the end.

The Programs

  1. Write an ansi C program called hello.c that prints out Hello World! on a line.
  2. Write an ansi C program called output.c. It should contain the following declarations:
    char c = 'a';
    short s = 55;
    int n = 5;
    long int nn = 22l; /* that's ell, not one. Can also use = 22L */
    long long int nnn = 33ll /* can also use = 33LL */
    float x = 7.5f;
    double y = 99.1234567;
    long double z = 99.5L;
    char someString[] = "This is some string.";
    		
    BUT DO NOT copy and paste. Type it in.

    It should use printf() to produce exactly the following output:
    c as an integer: 97
    c as a character: a
    s is 55
    n is 5
    nn is 22
    nnn is 33
    x is 7.500
    y is   99.12
    z is 99.500000
    someString is This is some string.
    "someString" is "This is some string."
    		
    The last item output, like all the others, should end with a newline character.
    Recall that the printf() function is invoked as printf(format string, additional arguments). In the printf() statements, use the variable names for the additional arguments, not the value of the variable. For example, use printf(" ... ", n), not printf(" ... ", 55).

  3. Write an ansi C program called input.c. It should contain the following declarations:
    int a, b, c;
    int scanfReturnValue; 
    		
    Write code to read in values for a, b and c from standard input and then print out the values read. First print out the value returned by scanf in each case. The statement to read in the values and assign them to a, b and c would look like this:
    scanfReturnValue = scanf("%d %d %d", &a, &b, &c);
    printf("scanf returned %d\n", scanfReturnValue);
    printf("a = %d, b = %d, c = %d\n", a, b, c);
    		
    Look at the scanf() statement. The "%d" says you're expecting to read in an int (in decimal/base 10) and the "&" before the a, b and c indicates the address where a, b, c are stored and scanf reads the input values entered and stores them at these addresses. (Remember that C, like java, is pass by value.) Running your program might produce results like this:
    $ 
    $ input
        45
            33
      22
    scanf returned 3
    a = 45, b = 33, c = 22
    $ 
    $ 
    		
    The first 3 lines are echoed program input; the last 2 are program output.

    Try putting some numbers into a file and then redirecting standard input to come from the file, as in:
    $ input < fileName
    		
    What happens if there are ...
    more than 3 numbers in the file?
    fewer than 3?
    none?
    2 integers and then a double like 5 7 15.75?
    some text like 5 7 hello?
    or 5 7 99hello?

  4. In the last exercise, you saw how to use scanf() to read in an integer. Of course, you'll want to use it to read in all kinds of values. In this exercise, you'll redo the first exercise, but instead of the variable values being given explicitly with numeric or string literals, they'll be assigned using scanf(). Before, you had
    char c = 'a';
    short s = 55;
    int n = 5;
    float x = 7.5f;
    double y = 99.1234567;
    long double z = 99.5L;
    char someString[] = "This is some string.";
    
    Now, instead, you'll start with
    char c;
    short s;
    int n;
    float x;
    double y;
    long double z;
    char someString[];
    

End of the exercise