Sunday, January 28, 2018

Variables in C/C++

In the last blog about C language, you were shown how to output Hello World !! on your screen. For this blog, I'm going to talk about the variables in C.

Variable Definition

A variable is a container to store data inside. Variable are divided into two categories: basic data type, and derived data type. So how it works? A variable is a name you give to put inside the RAM; the place it takes inside the volatile memory depends on the data types that I will talk about. For each data type, a range of values can be stored within the data type. For example char (a data type) has a value range from -128 to 127.

How to write the name of the variable

The name of the variable can be composed of letter, digits, and the underscore "_". The variable must have the following order: it can't start with a number, it should not contain a space.
The variable must begin either with either a character or an underscore. Uppercase and lowercase letters are distinct because C is case-sensitive. Let's take examples on variables in C: a, A, first_variable, a1b1, etc. But it can't be 1a or a b (a space b).

Variable types

As I said each data type has a range of values. Let's see what are the data types and the values for each type.


Difference between data types

char is used for a single character.
int are whole numbers that can have both positive and negative values without decimal i.e. 0, -5, 10.
unsigned int is only used for positive numbers.
float is used for numbers with a fractional part (decimal numbers).
double is a double precision floating point numbers.
long double is that data type that contains 19 decimals place.
N.B: void is used with function and represents the absence of data type.

It's very important to note that I have only mentioned basic data types. Derived data types contains pointers, functions, array, classes, union, and structure.

C++ program example



No comments:

Post a Comment