- Richard Feynman -
Chapter:Programming in C
Contents
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:
- Programming in C (eng)
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
- Programming in C (eng)
Exercises
- 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 }
- Compile your code using gcc
- What is the name of the compiler program?
- What is the argument given to the compiler?
- Execute your program
- What is the name of your program?
- Compile the code and store the output (the program) in a file called
hello
- Execute the newly compiled program
hello
- Where does the execution start, in a C program?
- Change the name of the main function to main2 and recompile. Can you compile and run the program?
- Change the printout statement so it prints out "Hello world!" instead.
Solutions
Expand using link to the right to see the full content.
- Hopefully, you entered the text as shown in the question (without line numbers) and stored the text in a file called
hello.c
. - You compile the code like this:
-
$ gcc hello.c
- The name of the compiler is gcc
- The argument is the file we want the compiler to compile, namely hello.c
-
- We execute the file like this:
$ ./a.out
- The name of the program is a.out
-
$ gcc hello.c -o hello
-
$ ./hello
- The execution starts in the main function.
- You can compile but link will fail. So we cannot run the program - since it can not be created.
- Hope it worked.
Questions and Answers
Expand using link to the right to see the full content.
Chapter Links
Our C FAQ