Basic C Programming (Part 1)
By Arthur Ed LeBouthillier
This article was published in the December 1999 issue of The Robot Builder.
The C programming language is one of the most important programming languages you can learn. It is available for virtually every kind of computer around and is one of the more powerful languages. This series of articles will introduce some basic
ideas of C programming.
C is a typed language
From your grade-school math, you’ll remember hearing the idea of different kinds of sets of numbers. You probably barely remember hearing about natural numbers, counting numbers, whole numbers, real numbers and imaginary numbers. The
C language also uses this idea of different types of numbers. Let’s look at the first kind: integers. If you remember the idea of whole numbers, they were numbers which didn’t have a fractional portion after them. Therefore, 0, 1, 2, -1, and 100 are examples of whole numbers, whereas 0.5, 1.25, 2.9, -1.6 and 100.75 are not examples of whole numbers. C has its own terminology for these kinds of numbers; they are known as integers. In C, an integer is a whole number without a decimal point like -1, 0, 1, 5 and -3. C represents what is called a typed language because you must always be aware of the type of value you are operating on. Going further, C makes a distinction between type int (short for integer) and type float which are numbers which do have fractional values (i.e. -6.2, 33.1, 600.5). You must learn to distinguish these kinds of numbers in
order to use C properly. There are other types in C such as long integers and many others and you can even define your own types, but it is enough to understand that C requires you to be conscious of the type of number you are using.
C is a Functional Language
C has another idea that it borrows from math, that of functions. Writing a C program consists of defining functions which perform the operations you wish to perform. You’re probably familiar with the idea of a function from grade school math. You’ll
probably remember the general function formula: y = f(x). In this formula, y represents a variable which will be assigned some value, which is the product of applying the function f to the variable x. We don’t know the value of f(x), though, because it is not
defined. In math terms, we could define: f(x) = 5x+3. This definition of f(x) means that whenever we evaluate a function such as y = f(5), we evaluate the formula for f(x) by replacing the general variable, x, with the value, 5 and then compute the result.
When you write a program in C, you also define functions, although C has its own peculiar way of writing down functions. If we were to define the function f in C, it would look like this:
float f(float x)
return 5 * x + 3;
Remembering the idea from the last section of typing, we now see how C uses it. In this function definition for f, we see the words float f(float x); this is how we tell C that we are going to define a function f which returns a type float and which takes an argument, x, also of type float. Having defined this function, we could then call it from within a program by typing:
y = f(5.0);
The result of this would be that the float number 5.0 would be assigned to the variable x in the function f, the answer would be calculated and then the result would be assigned to the variable y. One other thing to notice about this line of code is the presence of the semi-colon at the end of the line. Statements in C are separated by a semicolon and most often appear at the end of a line in order to tell the system where the end of the statement is.
That’s all for now, we’ll look at some more ideas about C programming next month.
This next article in this series is: "Basic C Programming (Part 2)"