Fundamental C - Program Structure
Fundamental C - Program Structure
Written by Mike James   
Monday, 17 October 2016
Article Index
Fundamental C - Program Structure
The Flow of Control
Loops
Nesting

This is an introduction to the very basics of programming in C. It covers the least you have to know to begin creating a program - variables, conditionals and loops. If you are already a C programmer then move along because there is nothing to see here. If you are a beginner then read on. 

Fundamental C For The IoT & Embedded Programming

 

Part I  - The Core Language

  1. Getting Started With C Using NetBeans
  2. Program Structure
  3. Low Down Data
  4. Functions
  5. Arrays
  6. Pointer Basics
  7. Strings
  8. Structs

Part II - Intermediate C

  1. Flow of Control 
  2. Data types
  3. Functions - parameters and results 
  4. Casting
  5. Operators
  6. Bit Operators & Manipulation
  7. Pointers to Pointers
  8. Macros

Part III - Beyond C

  1. Fixed point arithmetic
  2. File I/O Streams and File Descriptors
  3. Sockets
  4. Memory mapped files
  5. sysFS
  6. systemd
  7. Threading

 

 

Cbookcover

 

Which dialect of C?

The first question facing the new C programmer is which dialect of C you are going to use. You have a choice of C89, C99 or C11 plus  some slightly different versions of C that are close to these standards but with additional features. Of the main standards C89, also known as C90, is the most widely supported and often the front runner for this reason. You can spend a lot of time worrying about which version of C to use but if you are writing code for embedded systems and the IoT you have a lot more to worry about in variations in the libraries you will use. The differences between C89, C90 and C11 are mostly minor.  

In the rest of this book we will use C99 simply because some of its extra features make life easier. However, the differences are minor and come down to the ability to use single line comments and declare variables in for loops.  

Variables, assignment and expressions

Before we can move on and look at instructions - the verbs of the program - we have to have something for them to work with  - the nouns of the program.

The fundamental entity in C is the variable.

A variable is roughly speaking a chunk of storage that you have given a name to. What you can store there depends on what you specify when you create the variable.

For example, in C, and most modern languages you can store an integer (whole number) value in an int variable (int is just short for integer).

So for example

int a;

creates a variable called a that you can store an integer in.

How do you store a value in a variable?

You use the assignment operator "=". For example:

a=10;

isn't a statement that a is equal to ten, it is an instruction to store the value 10 in the variable a.

Understanding what is going on becomes much easier if you always read the equals as "assign to" as in "assign 10 to a" or "store in" as in "store 10 in a". (The point is never read it as "a equals 10" because this could be a statement or a question.)

A variable can only store one value at a time so if you store another value in it then the anything already stored is lost.

For example:

a=20;

stores 20 in a and any record of the 10 previously stored is lost.

Most programmers who have been working with variables quickly forget that there is any other way it could be done and might even wonder why I'm bothering to tell you all this. 

If you are happy with these basic ideas then we can add two more observations. You can both create and store a value in a variable at the same time. That is instead of writing:

int a;
a=1;

you can write

int a=1;

as a shorthand.

The second idea is slightly more complicated. You can use a variable on both sides of an assignment. For example,

a=b;

retrieves the value stored in b and stores it in a. After this instruction a and b store the same value. (Again it is important not to read this as "a equals b" but "store the contents of b in a". Remember that it is a command not a description.)

You can also write things like:

a=2*4+b;

Here the right hand side, an arithmetic expression, will be worked out and stored in a. Notice you can use variables on the right hand side and their values will be retrieved and used in the calculation. 

You can generally write arithmetic expressions as you would normally but notice that the multiplication sign is * (an asterisk) and the division sign is /. Also feel free to use parenthesis to make sure things are worked out in the correct order. More on arithmetic and other types of expression in a later chapter. C has a very large number of operators that go well beyond the well known operations of arithmetic.

An expression can be thought of as a mini-program. It is a recipe for how to combine values stored in variables and constants to produce a new value. 

An arithmetic expression that causes a lot of problems when you first see it is:

a=a+1;

Which if you read it as "a equals a plus 1" sounds like nonsense. What is does is to increase the value stored in a by one - i.e. its increments the value in a. If you read it as "retrieve the value in a, add one to it and store the result back in a" - then all should be clear.

Adding one to a variable is so common that C has a special command for it:

a++;

is the same as a=a+1;

There are lots of different types of data that can be stored in a variable but for the moment integer variables will serve well as an example.

Now we have something to write instructions about lets see what sort of things we can do. 

Program Structure

The first thing we need to look at it what makes a minimum program in C. If you start a new project in NetBeans, the system automatically generates a file containing:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
 return (EXIT_SUCCESS);
}

This is the most basic of C programs. It is simple but it contains features that will  only become clear after we have looked at quite a lot more C. 

The #includes are instructions to include the files specified, i.e. stdio.h and stdlib.h. These files are read into your program file as if you had written what they contain into your file. This is the standard way of getting library files into your program. There is more to say about this, but for the moment all that matters is that you understand that #including a file is a way to access existing code contained in the standard C libraries or third party libraries. 

Using libraries is important in C because the language itself is very simple and has few facilities. It is a minimalist language so many of features that you need to make C a useful language are provided by its libraries. For example, C has no statements or commands that relate to I/O - no print or input commands. These are simply not part of the C language and in this sense they are not "standard" - however the I/O functions that you find in the standard library might as well be part of the language because they are what every C programmer uses.

C makes use of a function called main to determine where to start executing your program. You can specify the name of the function where you want your program to start using an argument passed to the compiler, but most C programs start at main.

The only complication with main are its parameters. The first, int argc is easy, it is the number of parameters passed to main from the command line. The second is slightly more difficult to explain this early, but it is essentially an array containing the command line parameters specified by the user. We can return to this when we have explained how arrays work in C.

The main program always returns an integer to indicate success or failure. A zero is success and any other value is considered to be an error code.  This is what return EXIT_SUCCESS does for you - it returns a zero to indicate that everything has works. Notice that we use the constant EXIT_SUCCESS. Again this is not part of the language but it is defined in stdlib. You could just as well write

return 0;



Last Updated ( Wednesday, 26 October 2016 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.