C Variables and Data Types | C Programming Tutorials

Data types in C. This article lists and explains all the data types available in C programming language. It also explains what are variables and how to use them in a C Program.

There are five basic data types in c, void, int, char, float, double. These are called standard data types. While C provides a facility to user to create their own data types called as user defined data types. We will discus how and when to use them in detail.

C Variables

Definition: A variable is an identifier which is used to refer some location in memory. A variable simply means a name given to memory location in which some value is stored. We use this variable (name) to refer corresponding memory location when we want to perform some operation on the value contained in that memory location.

Before we use a variable in our program to store something we have to declare it first.

Syntax

type variable1,variable2,variable3,...,variableN;

Example

If we have to store roll no of a particular student we need a variable with integer data type. See example below.

#include<stdio.h>
int main()
{

int roll_no; // Declared variable roll_no of integer data type

printf("Enter roll no:- "); 
scanf("%d",&roll_no); // Accepting and saving roll no to declared variable

printf("%d",roll_no); // Displaying the value in the variable

return 0;
}

How to initialize variables in C?

Initialization in C means assigning a value to a variable during declaration.

Example

int x=10; 

Here is declared as a integer variable using int data type. x is assigned a value 10. It means x is storing value 10 for now.

Data Type In C:

  1. Simple or Standard
    1. char
    2. int
    3. float
    4. double
    5. void
  2. User Defined
    1. enum

Details about each data type are given below:-

Data Type Meaning Size(In Bytes) Range
void empty data type 0 valueless
char A single character 1 -128 to +127
int an integer number 2 (16 bit machine) -32768 to 32767
4 (32 bit machine) -2147483648 to 2147483647
float A single precision point floating number(6 precision digits) 4 3.4e-38 to 3.4e+38
double A double precision point floating number(14 precision digits) 8 1.7e-308 to 1.7e+308

1. void

void is usually used with functions that return normally. But it doe not provide a result value to its caller. We will discuss about this data type in more specific manner in later sections.

2. char

char is character data type. It is used to store a single character in variable. It requires only 1 byte space irrespective of the system or machine.

Syntax

char variable_name;

3. int

int data type in c is used to store integer values i.e. digits and numbers. Syntax of variable declaration using int is given below.

Syntax

int variable_name;

4. float

float data type is used when we have to deal with floating point numbers. It has a precision of 6 digits.

Example

12.124

Syntax

float variable_name;

5. double

This data type is used when we want the result of an expression to be more accurate or store a number with precision digits more than 6. this data type can store a number with 14 precision digits.

Syntax

double variable_name;

Now the last data type is enum which is user defined. It means user have to define a set of finite values. These values are called as enumeration constants. A user defined data type along with set of identifiers or values can be created by following declaration

enum data_type_name{const1,const2,.....};

Example

enum color{red,blue,green};

Here we have created new data type called as color. Now we can use it like any other data type. But it can take only red or blue or green as its value.

As you are beginner in C you should look at examples of some simple C programs.

Here are some. Happy learning.

Prev - Comments in C Next - If Statement