29 April 2012

Data Types in C

All programming languages use different ways to store different types of data. 'C' is a strongly typed language. this means the programmer must know what type a particular piece of data is, and create a variable suitable to hold it.

The types of data we will be using for now are integers, floats, characters, and strings. There are other types, which will be covered later.


  • Integers - An integer variable can store only whole numbers. These numbers can be positive or negative.
  • Floats - A float variable can store positive or negative numbers which contain decimal points.
  • Characters - A character variable can store only one single character at a time. this can be any character in the ASCII character set.
  • Strings - A string variable can store a string of characters.

Variable Declaration

Before a variable can be used in a program, it must be declared; that is, the program must be told its name and type. For example:
int total;
float average;
char letter;
char words[20];      //note that the string is declared as a required number of characters.
Variables can be initilized as part of the declaration, For example:
int total = 0;
float average = 123.75;
char letter = 'A';
char words[20] = "These are words";
NOTE: the contents of variables can be changed during the running of the program code.
For example, using the variables defined above, you can write:
total = 10;
average = 321.88;
letter = 'Q';
This process is called assignment (a value is assigned to the variable).
The "=" sign is called the assignment operator.

However, you cannot write:
words = "New Words";
The 'C' language does not allow this! There is a method to change the contents of a string variable.

No comments:

Post a Comment