User Tools

Site Tools


Data Types and Variables

There are several simple types of data commonly used in C and C++ programs:

Name Data Typical Size (bytes)1)
int Integer 4
char Character 1
float Floating-point number 4
double Floating-point number with twice the precision of a float 8
void No type (useful for defining functions with no return value). N/A

A variable is a location in memory used to store data. Variables are assigned names and data types by the programmer at the beginning of the program or function. Examples:

int iter=0;         // An integer variable initially assigned a value of zero
double energy;      // A double-precision floating-point number
int z_values[50];   // An array of integers
double geom[10][3]; // A two-dimensional array (a matrix) of doubles

Note: You should never assume that the value of a variable has been “initialized” (set to zero) by the compiler. More often, the memory location specified by a variable contains (seemingly) random bits until you assign a value to it.

The size of data types can vary among computer systems, so you should never assume particular values. The sizes given in this table are only guidelines. Use the sizeof() function to determine the size of a type or variable inside your programs.
crawdad/programming/data_type.txt · Last modified: 2012/11/19 04:33 by crawdad