Chapter:Programming in C

From Juneday education
Jump to: navigation, search

Meta information about this chapter

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

Introduction

This is an introduction to programming in the C programming language. It is aimed at giving the student some nomenclature and feeling of the structure of a C program. It is not a theory focused chapter, but rather a first encounter with C programs to give the students a first feel of what C programs look like and what basic components make up such a program.

Purpose

The purpose of this chapter is to provide the students with basic knowledge of the layout of a C program and its source code files so that the student can compile and run a sample (in this chapter provided) program.

Another purpose is to explain to students that a C program has a starting point for the execution of the program in the main method.

Goal

The goal of this chapter is that students understand that a C program has one file with a main function where all the execution starts.

The students shall be able to read and understand a simple sample program, compile it and run it.

Instructions to the teacher

Common problems

Java is a rather simple C language to understand. All we need is a main function which is stored in a file.

Chapter videos

All videos in this chapter:

See below for individual links to the videos.

The layout of a C program

A C program, in its smallest and simplest form, has one source code file. The source code file is a text file with instructions written in the C programming language.

This small, single file C program contains at least one inclusion, one function and one function call.

Individual videos

  1. Programming in C (eng)

Exercises

  1. Write the following small C code yourself using an editor (e.g Atom). Store the code in a file called(hello.c)
    1 #include <stdio.h>
    2 int main() {
    3   printf("Hello Cleveland\n");
    4 }
    
  2. Compile your code using gcc
    1. What is the name of the compiler program?
    2. What is the argument given to the compiler?
  3. Execute your program
    1. What is the name of your program?
    2. Compile the code and store the output (the program) in a file called hello
    3. Execute the newly compiled program hello
  4. Where does the execution start, in a C program?
  5. Change the name of the main function to main2 and recompile. Can you compile and run the program?
  6. Change the printout statement so it prints out "Hello world!" instead.

Solutions

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

  1. Hopefully, you entered the text as shown in the question (without line numbers) and stored the text in a file called hello.c.
  2. You compile the code like this:
    $ gcc hello.c
    
    1. The name of the compiler is gcc
    2. The argument is the file we want the compiler to compile, namely hello.c
  3. We execute the file like this:
    $ ./a.out
    
    1. The name of the program is a.out
    2. $ gcc hello.c -o hello
      
    3. $ ./hello
      
  4. The execution starts in the main function.
  5. You can compile but link will fail. So we cannot run the program - since it can not be created.
  6. Hope it worked.

Questions and Answers

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

Chapter Links

Our C FAQ

External resources

Books this chapter is a part of

Programming with C book

Book TOC | previous chapter | next chapter