Basics of the C Programming Language: Variables and Values, Statements and Structure, and Comments

By Dan Gookin

Like communicating with any human, you have to know the language in order to communicate with a computer. In computer programming, many people use C as the programming language.

Variables and values

A program works by manipulating information stored in variables. A variable is a container into which you can stuff values, characters, or other forms of information. The program can also work on specific, unchanging values called immediate values:

result = 5 + sqrt(value);

In this example, result and value are variables; their content is unknown by looking at the code, and the content can change as the program runs. The number 5 is an immediate value.

C sports different types of variables, each designed to hold specific values.

Statements and structure

As with human languages, programming languages feature syntax — it’s the method by which the pieces fit together. Unlike English, where syntax can be determined by rolling dice, the method by which C puts together keywords, functions, operators, variables, and values is quite strict.

The core of the C language is the statement, which is similar to a sentence in English. A statement is an action, a direction that the program gives to the hardware. All C language statements end with a semicolon, the programming equivalent of a period:

beep();

Here, the single function beep() is a statement. It can be that simple. In fact, a single semicolon on a line can be a statement:

;

The preceding statement does nothing.

Statements in C are executed one after the other, beginning at the top of the source code and working down to the bottom. Ways exist to change that order as the program runs.

The paragraph-level syntax for the C language involves the use of curly brackets, or braces. They enclose several statements as a group:

{
  if( money < 0 ) getjob();
  party();
  sleep(24);
}

These three statements are held within curly brackets, indicating that they belong together. They’re either part of a function or part of a loop or something similar. Regardless, they all go together and are executed one after the other.

You’ll notice that the statements held within the curly brackets are indented one tab stop. That’s a tradition in C, but it’s not required. The term white space is used to refer to tabs, empty lines, and other blank parts of your source code.

Generally, the C compiler ignores white space, looking instead for semicolons and curly brackets. For example, you can edit the source code from project ex0201 to read:

#include <stdio.h>
int main(){puts("Greetings, human.");return 0;}

That’s two lines of source code where before you saw several. The #include directive must be on a line by itself, but the C code can be all scrunched up with no white space. The code still runs.

Thankfully, most programmers use white space to make their code more readable.

  • The most common mistake made by beginning C language programmers is forgetting to place the semicolon after a statement. It may also be the most common mistake made by experienced programmers!

  • The compiler is the tool that finds missing semicolons. That’s because when you forget the semicolon, the compiler assumes that two statements are really one statement. The effect is that the compiler becomes confused and, therefore, in a fit of panic, flags those lines of source code as an error.

Comments

Some items in your C language source code are parts of neither the language nor the structure. Those are comments, which can be information about the program, notes to yourself, or filthy limericks.

Traditional C comments begin with the /* characters and end with the */ characters. All text between these two markers is ignored by the compiler, shunned by the linker, and avoided in the final program.

Overly Commented Source Code shows an update to the code from project ex0201 where comments have been liberally added.

OVERLY COMMENTED SOURCE CODE

/* Author: Dan Gookin */
/* This program displays text on the screen */
#include <stdio.h>  /* Required for puts() */
int main()
{
  puts("Greetings, human."); /* Displays text */
  return 0;
}

You can see comments. A comment can appear on a line by itself or at the end of a line.

The first two lines can be combined for a multiline comment, as shown in Listing 3-2.

MULTILINE COMMENTS

/* Author: Dan Gookin
  This program displays text on the screen */
#include <stdio.h>  /* Required for puts() */
int main()
{
  puts("Greetings, human."); /* Displays text */
  return 0;
}

All text between the /* and the */ is ignored. The Code::Blocks editor displays commented text in a unique color, which further confirms how the compiler sees and ignores the comment text. Go ahead and edit the ex0201 source code to see how comments work.

A second comment style uses the double-slash (//) characters. This type of comment affects text on one line, from the // characters to the end of the line, as shown in Double-Slash Comments.

DOUBLE-SLASH COMMENTS

#include <stdio.h>
int main()
{
  puts("Greetings, human."); // Displays text
  return 0;
}

Don’t worry about putting comments in your text at this point, unless you’re at a university somewhere and the professor is being anal about it. Comments are for you, the programmer, to help you understand your code and remember what your intentions are. They come in handy down the road, when you’re looking at your code and not fully understanding what you were doing. That happens frequently.