Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -



Chapter:Our first C program

From Juneday education
Jump to: navigation, search

Meta information about this chapter

Expand using link to the right to see the full content.

Introduction

Here we expose the students to their first real-world C program. We show source code, directory layout and the edit-compile-run cycle.

Purpose

Getting the student familiar with a C program (with multiple files and function invocation).

Give the students a frame of reference for the coming lectures on syntax and control flow etc.

N.b.! The students are not expected to understand every part of this small program or be able to write it themselves, rather to help them to better understand where the syntax and constructs in coming lectures will fit into a program.

Goal

The students should have been introduced to a C program with:

  • multiple files
  • include statement
  • main function
  • function invocation

Instructions to the teacher

Common problems

Chapter videos

All videos in this chapter:

  • Our first C program (eng) (no swedish)


See below for individual links to the videos.

Source code

Video

Video discussing you through the source code below: Our first C program (eng) (no Swedish)

hello.h

1 void print_hello();

Line by line explanation

  1. This is a declaration of a so called function, print_hello(). Basically what we mean by stating this is that there exists a function called print_hello and that it returns void (for now you can think of it as returning nothing).

hello.c

1 #include <stdio.h>
2 #include "hello.h"
3 
4 void print_hello() {
5   printf("Hello Cleveland\n");
6 }

Line by line explanation

  1. statement processed by the preprocessor. In short we're adding a description (prototype) of the printf function. Otherwise the compiler would have to guess what the function returns and what parameters it accepts.
  2. Here we're including the function declaration from the file hello.h.
  3. a blank line. Indenting code and using blank lines make the code easier to read.
  4. void is the so called return type, using void for return means we don't give something back at all. Next is the name of the function. And after that the list of parameters, which in this case is empty. And finally we have the { which starts the block, which means that the code following this and until the closing } is part of the function.
  5. a call to the standard function printf with the arguments "Hello Cleveland!\n". Every time this function is invoked "Hello Cleveland!\n" is outputted to the screen.
  6. And finally the closing } which means that the definition of the function is done.

main.c

1 #include "hello.h"
2 
3 int main() {
4   print_hello();
5 
6   return 0;
7 }

Line by line explanation

  1. statement processed by the preprocessor. In short we're adding a description (prototype) of the print_hello function. Otherwise the compiler would have to guess what the function returns and what parameters it accepts.
  2. a blank line. Indenting code and using blank lines make the code easier to read.
  3. int is the so called return type, using int for return means we want to give something (actuallt an integer) back to the caller. Next is the name of the function. And after that the list of parameters, which in this case is empty. And finally we have the { which starts the block, which means that the code following this and until the closing } is part of the function.
  4. a call to the function print_hello with no arguments (think, no extra information). invking this function is outputting "Hello Cleveland\n" (the letters "Hellow Cleveland" followed by a newline) to the screen.
  5. a blank line.
  6. A statement where we return the integer 0 from the main function, which results in that the program returns 0.
  7. And finally the closing } which means that the definition of the function is done.


Compile and run the whole program

Start by creating a directory for this chapter (you may call it what you want but "our-first-c-program" could be a good name). Enter that directory and download the files (see the links above).

Compile:

$ gcc hello.c main.c -o hello

Execute the program:

./hello

Exercises

1. The file main.c above contains the starting point of the program. What is the starting point of a program in C.

2. Let's look at the main function in that file a bit. What characters do you think symbolizes the start of the main function's content (or body) and the end of that function.

Solutions

Expand using link to the right to see the full content.


1. The main function.

2. The { marks the start and } marks the end.

Questions and Answers

Expand using link to the right to see the full content.

Chapter Links

Our C FAQ

External links

C Programming/A taste of C (wikibooks)

Books this chapter is a part of

Programming with C book

Book TOC | previous chapter | next chapter