Section 4c

Fundamental Data Types


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Fundamental or Primitive Data Types

(in the C Programming Language)

The first kind of data that will be discussed here is called primitive or fundamental data. Primitive data is the kind of data that is part of the programming language, and is designed to hold data in program memory. For example, the C programming language has four basic data types that can be held by its variables: char, int, float, and double, as described in the table below. These data types can be found in almost all programming languages.

The one data type that is also commonly found in other programming languages is the Boolean data type which is not found in standard C. However, as it is frequently used, a library named stdbool.h allows this data type to be included in programming actions. For purposes of this reference, it will be considered a primitive data type.

Table of Data Types
Data Name C Identifier Type of Data (or Data Type)
character char computers cannot actually store characters, although they can store numbers that represent characters; the char data type is actually an integer variable (described below), however, it holds integer quantities that represent characters using a standard convention called ASCII1; for example the integer value 71 represents the letter 'G', and the value 123 represents the open curly brace ( '{' )
integer int positive or negative non-fractional or "counting" numbers; e.g., 1, 15, -45, 25726
floating point double or float2 fractional numbers, having a decimal point; these numbers are held in memory in a form of scientific notation (i.e., 1.245 x 103)

Boolean bool this data type only holds the logical quantities true and false; it is used by the program to provide logical conditions or state (i.e., status); note that this is not officially in the standard C language but will be included in programs from the stdbool.h library

 

Table Notes:

1It should be noted that the actual ASCII convention was discontinued years ago, and replaced by Unicode, which conveniently kept the ASCII standard within it. However, since you can never tell when conventions like this might change, you must never hard code (i.e., write into your program) any ASCII values. Always use the desired character itself so the compiler can set the conventional value for you. This will keep your program from becoming an antique over night.

2note that while the float type is a legal C data type, it is not used much any more since its minimum specified storage capacity in the C programming language is not as large as the double type; the real-world truth is that with most operating systems, the amount of storage capacity is the same but virtually all programmers have chosen to follow the convention of using the double type.

 

Modifications. All programs can be written and executed with these different types of data, or data types. There are modifiers to the data types that can change how much the data type holds, but they do not change the kind of data held. For example, a long int (or just long) may allow the computer to hold larger values than the unmodified data type int. There can also be a short or a byte, which may hold smaller values. C specifies the minimum limits of each of the data types so if you are ever looking for the right type to use for a project, check the references.

Variable Declaration. As mentioned, the data types shown above will allow all programs to work properly. They are the data types that are specified to be used in the C programming language, and they allow folks to write programs that will manipulate and/or process most number or character based information. The initial manipulation process requires that if a variable (of a given data type) is to be used in a program, it must be declared.

Examples of the declaration process are shown below:

int myAge; // informs the compiler that a variable
// with the name “myAge” will be used
// to store integer data

double mySalary; // informs the compiler that a
// variable with the name “mySalary”
// will be used to store floating
// point data, which is currency
// in this case

char midInitial; // informs the compiler that a
// variable with the name “midInitial”
// will be used to store a character

bool programRun; // informs the compiler that a
// variable with the name “programRun”
// will be used to store a Boolean
// value (i.e. true or false)

Using variables makes programs much more powerful since you will be able to store, access, process, and display things once you have a place to store them. What's more, you can process many different things because the variables won't just hold one thing; they can hold anything the program needs at a given time. Hence the name - variables.

Quality Software Engineering. The last thing to discuss in this section is a standard or conventional way to declare variables. Good program structure means that various program components can be found, analyzed, and modified easily as needed. The C programming language allows variables to be declared anywhere in the program. This is not a good idea, and can lead to big problems in code readability, structure, and execution. Declaring variables at various places in the program will work until the programs become more significant and larger; then they can make the programs hard to work with while folks spend time looking for where a variable got declared.

Your standard should always be to declare variables at the beginning of whatever function you are creating. At the beginning of most courses, you are only working with the main function, so life is simple. Always declare your variables at the beginning of the main function. However, in a pretty short time, you will be developing and using your own functions. The idea is the same. All variables should be declared at the beginning of all functions. Besides being better quality programming and software engineering, it also keeps things simple for the programmer. Again, this issue is important enough that it will be raised again later in this chapter.

Watch this video and learn a little bit more about data types.