Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Chapter 3. Data and Expressions

Really useful programs take in data, perform actions on it and output it somewhere. In C, you use named pieces of memory called variables to store data. C programs can change the data stored in a variable at any time, hence the name. Every variable has an identifier which you can use to refer to it's data when you want to use or change it's value. An expression is anything that can be evaluated i.e. 1 + 1 is an expression of the value 2. In this expression, the plus sign is a binary operator; it operates on two values to create a single value.

The rules for naming a variable are the same as for naming a function, you can use letters, numbers, and the underscore character and the first character must not be a number. Also like functions, variables must be declared before they can be used. The identifier you give to a variable should say what the the variable will be used for, this makes you code much easier to read. You can define your own variables or you can use one of the types already defined for you. Before we get bogged down in terminology let's look at a quick code example to show how simple it all is. In this example we will use two variables of the pre-defined type int.

Example 3-1. bicycles.c

#include <stdio.h>

int
main()
{
  int number_of_bicycles;
  int number_of_wheels;

  number_of_bicycles = 6;
  number_of_wheels = number_of_bicycles * 2;

  printf("I have %d bicycles\n", number_of_bicycles);
  printf("So I have %d wheels\n", number_of_wheels);

  return 0;
}
   

3.1. Bicycle Dissection

There are a few new things to look at here, we'll break the program into chunks to explain them.

  int number_of_bicycles;
  int number_of_wheels;
   

These two lines each declare a variable. int is one of the built-in data types of the C language. Variables of type int can store positive or negative whole numbers.

  number_of_bicycles = 6;
   

This line stores the value 6 in the variable number_of_bicycles. The equals sign is known as "the assignment operator", it assigns the value on the right hand side of it to the variable on the left hand side.

  number_of_wheels = number_of_bicycles * 2;
   

Again, this line uses the assignment operator but it also uses the multiplication operator. The asterisk is another binary operator, it multiplies two values to create a single value. In this case it creates the value 12 which is then stored in number_of_wheels.

  printf("I have %d bicycles\n", number_of_bicycles);
  printf("So I have %d wheels\n", number_of_wheels);
   

Here we see printf() again but it's being used unlike we have seen before. Here it is taking two arguments which are separated by a comma. The first argument to printf() is known as the format string. When a %d is encountered in the format string printf() knows to expect an extra argument. The %d is replaced by the value of this extra argument. One addition argument is expected for each %d encountered.

With this new knowledge it should be no surprise that when we compile and run this piece of code we get the following:

I have 6 bicycles
So I have 12 wheels
   
As always, don't worry if you are unsure about certain parts. We'll do plenty more examples.

 
 
  Published under the terms of the GNU General Public License Design by Interspire