Skip to content

Data Types and Variables

Andrew James edited this page Sep 25, 2016 · 6 revisions

There are several data types that are commonly used in C and C++ programs

Name Data Typical Size (bytes)
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 function with no return N/A

A variable is a location in memory used to store data. Variables are assigned names and data types by the programer either at the first time they are used, or before hand. Here are some examples.

int iter = 0;       // An integer with an inital value of zero 
double energy;      // A double-precision float with no initial values
int z_vals[50];     // an array of 50 integers
double geom[10][3]; // a 2-d array of doubles, there are 10 sets of 3 values each

Note: You should never assume that an uninitialized variable has been zero'ed out. You should never assume the size of a data type, you should use the sizeof() function to get the size of a data type in your program.

Clone this wiki locally