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



Chapter:Programming in C

From Juneday education
Revision as of 17:52, 3 August 2016 by Henrik Sandklef (Talk | contribs) (new C stuff)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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, part I [ (eng)] [(swe)] (link to presentation)

Exercises

  1. Write the Small C file (hello.c) yourself using an editor (e.g Atom).
    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?
  4. Where does the execution start, in a C program?
  5. 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).
  2. Since the class is called Small, we must name the file Small.java . This is a text file containing code in the Java programming language. Since the language is java we use the suffix java .
  3. We compile the file like this:
    $ javac Small.java
    
    1. The name of the compiler is javac
    2. The argument is the file we want the compiler to compile, namely Small.java
  4. We execute the file like this:
    $ java Small
    
    1. The name of the program is java
    2. The argument is the class we want java to execute, namely Small .
      Note that we do not give Small.class as argument. We give Small (the class name) as argument. Given that class name java knows how to find the file containing that class. Of course this means you have to be in the same directory because that's where java is looking for the Small.class file.
  5. This will not work since, with the syntax used before, java can not find the class (see above). There are ways to make this work, but we will look into that another day.
  6. Execution always start in the "main method" (which is currently empty)
  7. The program doesn't do anything, because the body (contents between { and } ) of the main method is empty. Where the execution starts, there are no instructions!
  8. The source code file Small.java should now look something like this:
    1 public class Small {
    2 
    3   public static void main(String[]args) {
    4     System.out.println("I am a println statement");
    5   }
    6 
    7 }
    
    The amount of white space (spaces) is not relevant, but it sure makes the program simpler to read, doesn't it?
    1. We compile the file like this (as usual):
      $ javac Small.java
      
    2. We run the program like this (as usual):
      $ java Small
      
    3. The program, when run, will actually do something now! It will print I am a println statement in your terminal.
  9. The program will now look something like this:
    1 public class Small {
    2 
    3   public static void main(String[]args) {
    4     System.out.println("I am a println statement");
    5     System.out.println("This is an additional println statement!");
    6   }
    7 
    8 }
    
    1. Again, you compile the program like this:
      $ javac Small.java
      
    2. Again, you run the program like this:
      $ java Small
      
    3. The program now outputs the following:
      I am a println statement
      This is an additional println statement!
    4. See above (regarding the order!)
    5. The order of the output is the same as the order of the corresponding println statements. From this, we learn that statements are executed sequentially (in the same order as they are written in the source code, one by one).

Questions and Answers

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

Q: “You say that even the smallest Java program has at least one ‘class’ declared. Is it common to have more than one class?”
A: Oh, yes! It is in fact more common than having only one! In these first introductory courses, we usually start with the simplest form and structure however. But since it is so uncommon in the real world to write everything in one single file with one single class, we will very quickly move to the situation where you use many classes. Note that since Java comes with a huge library of predefined classes for you to use (the so called standard Java API), it is not uncommon that even if you were to write a stand-alone program in just one file with just one class defined by you, you would anyway use one or many classes from the API! In fact, you hae already used (or seen in the examples) how to use classes from the API.

Q: “Wait, what? There were classes from the standard Java library in your examples?”
A: Indeed! For instance, you remember the so called main method? It had the name String inside the parentheses. String is a predefined class from the Java API. It is a class representing strings of text. If you look closely to the greet() method of the Greeter class, it also uses a class called System. That’s also from the standard Java API. System is a class for accessing and using the computer system where Java is running, so that’s where we have stuff for writing out text to the terminal (standard output stream, which is usually connected to a terminal) for instance. Soon we will look at more predefined classes from the API (in the chapter on using objects for instance).

Q: “But in your example, you said that the Small class was using the name Greeter which was a class in a package/directory called ‘lib’. In order to use stuff like the Greeter class from that package/directory we had to write “import lib.Greeter”. How come we don’t need to import the path to the Java standard API classes like String and System?
A: Well spotted! There is no fooling you it seems! In fact, all classes in the Java API are grouped together in various packages and in order to use the classes in some package, the path to that package needs to be imported to your program. However, the creators of Java have cheated a little when it comes to one particular package called java.lang (where commonly used classes like String and System exist). That is the one package that is always available to your programs with or without an import statement! You may, of course explicitly import “java.lang.String” for instance, but nobody does that. But try for yourself and see that the code still works the same with such a statement. Now try and remove the “import lib.Greeter;” statement from the Small class and re-compile. Greeter is definitely not part of the java.lang package of the standard Java API (probably many people wish it were! No just kidding!). It is a local class that we wrote and put in the lib directory/package. So the compiler will complain about the Greeter name, and probably complain that it can’t find a class named Greeter anywhere. Put back the import statement again and re-compile and see that it works again. This way, you will understand that classes in packages other than java.lang can only be used if the path to the correct directory/package is imported. You may think of the import statement as an instruction to the compiler where to find classes and other names that occur in your code but is not part of the java.lang package. There is actually an alternative, however, to using import. We will see later in the course that you may also use classes by their full names including the package name. So if we wanted to avoid importing lib.Greeter, for instance, we could change every occurrence of Greeter in the code to lib.Greeter (packagename.classname is the pattern here). But that is quite long to write (there are packages with very long names) so the import statement is very handy.

Don’t worry if this sounds confusing at this point. We will use packages and imports a lot throughout the course!

Q: “The Small class did not have a package declaration at the top of the file. So it’s OK to have classes that are not in a package?”
A: It is not forbidden, but it would be wrong to say that a class could not not be in a package. That would actually be the same as saying that the file is not in a directory! Remember, package is the Java way of talking about paths and directories. So, the Small class does actually also belong to a package, but a package without a name. The reason we can compile and run this class in the Small.java file is that we instructed you to navigate to the same directory as the Small.java file. So when we run the Small class, Java will actually know that this is a class in the nameless package. Classes in the nameless package must always exist in the same directory as where you ran the java command (unless you have used a special construct called class path, but that’s a topic for later chapters!).

Chapter Links

External resources

Books this chapter is a part of

Programming with Java book

Book TOC | previous chapter | next chapter