C Programming: Introduction to Data Types

Page content

Data Types

Every day we meet names and numbers—at home, at work, at school or at play. A person’s name is a type of data; so is a number. We can thus speak of the two data types called ‘name’ and ‘number.’ In the statement:

Mimi bought 3 dresses for $199.95

we can find:

  • an example of a name: Mimi;
  • two examples of numbers: 3 and 199.95

Usually, we find it convenient to divide numbers into two kinds:

(1) whole numbers, or integers;

(2) numbers with a decimal point, so-called real or floating-point numbers.

In the example, 3 is an integer and 199.95 is a real number.

Exercise: Identify the data types—names, integers and real numbers—in the following:

(a) Michael’s scoring average was 25.25 with a highest score of 45.

(b) Abigail, who lives at 41 Third Ave, worked 36 hours at $11.50 per hour.

(c) In his 8 subjects, Richard’s average mark was 68.5.

Generally speaking, programs are written to manipulate data of various types. We use the term numeric to refer to numbers (integer or floating-point). We use the term string to refer to non-numeric data such as a name, address, job description, title of a song or vehicle number (which is not really a number as far as the computer is concerned—it usually contains letters, e.g. PBN8652).

Programming languages in general, and C in particular, precisely define the various types of data which can be manipulated by programs written in those languages. Integer, real (or floating-point), character (data consisting of a single character such as ‘K’ or ‘%’) and string data types are the most common.

Each data type defines constants of that type. For example:

  • some integer constants are 3, -52, 0 and 9813;
  • some real (or floating-point) constants are 3.142, -5.0, 345.21 and 1.16;
  • some character constants are ’t’, ‘+’, ‘8’ and ‘R’;
  • some string constants are “Hi there”, “The Art of Computer Programming” and “C World”.

Note that, in C, a character constant is delimited by single quotes and a string constant is delimited by double quotes.

When we use a variable in a program, we have to say what type of data (the kind of constants) we intend to store in that variable—we say we must declare the variable. It is usually an error if we declare a variable to be of one type and then attempt to store a different type of value in it. For example, it would be an error to attempt to store a string constant in an integer variable.

Upcoming articles will show you how declare and use data types to create programs which solve problems.

References

Reference text: C Programming - A Beginner’s Course

Related programming references

This post is part of the series: C Programming for Beginners

A straightforward introduction to Programming in C for people with no previous programing experience.

  1. C Programming For Beginners - Part 1
  2. C Programming For Beginners - Part 2
  3. C Programming for Beginners – Part 3
  4. C Programming for Beginners – Part 4
  5. C Programming For Beginners - Part 5
  6. C Programming For Beginners - Part 6
  7. C Programming For Beginners - Data Types
  8. C Programming For Beginners - Part 8
  9. C Programming For Beginners - Part 9
  10. C Programming For Beginners - Part 10
  11. C Programming For Beginners - Part 11
  12. C Programming For Beginners - Part 12
  13. C Programming For Beginners - Part 13
  14. C Programming For Beginners - Part 14
  15. C Programming For Beginners - Integer Data Types
  16. C Programming for Beginners - Part 16
  17. C Programming For Beginners - Integer Expressions, Operators and Precedence
  18. C Programming For Beginners - Part 18
  19. C Programming For Beginners - Printing Double and Float
  20. C Programming For Beginners - Mixing double, float and int