Beginning C

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Like most programming languages, C is able to use and process named variables and their contents. Since C is a low-level programming language, it is necessary for a C program to claim the memory needed to store variables before using the memory. This is done by defining variables, the way in which a C program shows the number of variables it needs and what they are going to be named.

Within the C programming language, when we manage and work with variables, it is important for us to know the type of our variables and the size of these types. This is because C is a sufficiently low-level programming language that these aspects of its working can be hardware specific.

All variables in C are typed.

Your First Program[edit]

Your first program, like in every other language, is going to display the text, "Hello World!" There is a problem, though: C does not have any input or output statements. The solution: we must call a function to do the work. This is called printf and is implemented in the standard C library.

In C, functions are defined in headers, which have an extension of *.h. (You will learn that the same is true in C++.) These header files are imported into your code with the preprocessing directive #include. This statement essentially cuts and pastes the functions into your code. (Yes, this is legal. Don't worry about copyrights in this case.)

Here is how to use an #include directive to get the printf function, which is in the <stdio.h> header:

 #include <stdio.h>

Good! You have the printf function, as well as many others that do input, output and the like. Now, it is time to code!

C programs start with a function called main. In programming terms, this is called the entry point of the program. Since the C program is fairly simple, this will be the only function we will write.

To start the function, type the following code:

 int main(void) {

The int specifies that the function will return an integer. This will be the program's exit code. Main is the name of the entry point function, and the parentheses surround the parameters of the program. You do not want parameters in this program, but future programs will possibly use this to get command line arguments.

The open bracket means that the function's code is starting here.

Inside the function, type the following code:

  printf("Hello, World!");
  return 0;

The first line is a function call. printf is the name of the function, and like in main, the parentheses surround the arguments. "Hello, World!" is a string, which is a bunch of text surrounded by quotes.

All statements, whether they are declarations, function calls or another type, end in semicolons. This is like a period in an English sentence. return 0; ends the function and gives a return value. The value of 0 means that everything went okay. Finally, the } ends the function.

Your code should look like this:

 #include <stdio.h>
 int main(void) {
  printf("Hello, World!");
  return 0;

Compile this, and enjoy!!

Simple C types[edit]

In C, there are a small key set of types. The rest are modifiers, arrays and other such low-level data structures. The keywords for these types are int, char and float.

When we wish to store an integer, we use an integer type. The C name for this type is called int. Ints usually require 4 bytes per variable.

When we wish to store a single character or byte, we use a character type. The C name for this type is called char. Chars always use one byte each.

When we wish to store a floating-point (non-integer) value, we use either a single precision or a double precision floating point type. For single precision, C terms this type float. For double, double. Variables of type float usually use 4 bytes of memory and those of type double, 8.

There is no fundamental predefined string type in C. Strings in C are more complicated (they are arrays of chars), requiring manual handling, and we will deal with them later.

Other type keywords[edit]

long is a type specifier for numeric variables (those of type int or type double) which (possibly) allocates more memory for the variable, allowing it to contain and process larger numeric values than usual. Some C implementations allow this modifier to be used twice ('long long') to define even larger variables. However, this is not in standard C, unless you use the 1999 standard (which currently (2003) has very meager support by implementations.)

short is a type specifier that may only be applied to derivatives of int. This might allocate less memory than a normal int.

signed is a type specifier which explicitly allows a variable to have either positive or negative values at the expense of a smaller possible range of values. By default all variables in a C program are defined as 'signed' variables (except for char, which is either signed or unsigned by default.)

unsigned is a type modifier which allows a variable to only have nonnegative values (zero or greater). However, it allows a range of values twice as large as that of the equivalent signed variable.

When long, short, signed or unsigned are used without specifying a base type, the type is int. In some cases you can get away with using absolutely no keywords, but this is considered bad style, and is invalid in the 1999 standard ("C99").

register is a hint to the compiler to attempt to optimise the storage of the given variable by storing it in a register of the computer's CPU when the program is run. Most modern optimising compilers do this anyway, so use of this keyword is often unnecessary.

volatile is a special type modifier which informs the compiler that the value of the variable may be changed by external entities other than the program itself. This is necessary for certain programs compiled with optimisations - if a variable were not defined volatile then the compiler may assume that certain operations involing the variable were safe to optimise away when in fact they aren't.

const specifies that the variable may not be modified.

Arrays could be considered an extension of an existing variable's type: arrays cannot exist as a type on their own but act just to extend an existing variable. An array applied to a variable implies not a single variable but a series of variables of a single type indexed by integers starting from zero. The characters used to indicate an array are '[x]' where 'x' is the number of variables in the series. For example, a single integer variable would be defined by int, but an array (series) of seven integer variables could be defined by int[7]. Assuming this variable had the name a, the individual integer variables could be accessed as a[0], a[1], a[2] and so on until a[6]. The integer used to index the series of variables is also known as a subscript.

Declaring variables[edit]

All type declarations using the above simple types follow a standard format

type_name variable_name

where type_name represents one of the C type names, and variable name a generic name for the variable.

For example, if we want to declare a variable called number and of type int, we write

int number;.

In C, all declarations are terminated by semicolons. If we wish to declare a number of variables of the same type, we can write

int variable1, variable2, variable3;

which declares all variable1, variable2, and variable3 to be of type integer.

Control structures[edit]

C is an imperative language, that is it requires a sequence of steps to be followed in order to solve a problem.

We will need certain C constructs in order to achieve a certain result; we will need to make decisions and to perform steps over and over again in a certain way, iteration.


Decisions are important to programming. When we want to consider different actions for different values, we need to make a decision. For example, say if we are writing a program to calculate the square root of a number. C does not contain capabilities to calculate the square root of negative numbers. So we wish to make a decision to return an error message if we get a negative number, and to calculate the square root otherwise.

C provides two main capabilities of making decisions:

  • if..else
  • switch

if and else[edit]

The if..else construct allows us to make a binary decision, that is, a decision that can go two ways (binary) -- one way or the other.

We write this construct:

if (condition)

If there is only one statement, the braces may be removed.


The switch construct allows us to make a decision based upon an expression and multiple possible values for it.

The construct is written:

switch (expression)
  case expression_1:
  /* more case statements if desired */

When the switch statement is encountered, the value of the expression given as an argument is calculated. The calculated value is then compared with each of the subsequent expressions given as arguments to the 'case' statements in order. If the value of the switch expression matches that of a case expression, the statements subsequent from the corresponding case statement are executed until the break statement.

If the value of the switch expression is not equal to any of those of the case statements, the statements between the default statement and its corresponding break statement are executed.


C has a number of control structures to perform iteration:

  • while loops
  • for loops
  • do..while loops