C - Data Type Variables Declaration

Introduction

The basic three steps for using variables in the C language:

  • Declare the variable, giving it a variable type and a name.
  • Assign a value to the variable.
  • Use the variable.

These steps must be completed in that order.

The declaration is a statement ending with a semicolon:

type name; 

type is the variable type: char, int, float, double, and other specific types.

name is the variable's name.

A variable's name must not be the name of a C language keyword or any other variable name that was previously declared.

The name is case sensitive.

You can add numbers, dashes, or underscores to the variable name, but always start the name with a letter.

The equal sign is used to assign a value to a variable. The format is very specific:

variable = value; 

variable is the variable's name.

value is either an immediate value, a constant, an equation, another variable, or a value returned from a function.

After the statement is executed, the variable holds the value that's specified.

In the following code four variable types are declared, assigned values, and used in printf() statements.

Demo

#include <stdio.h> 

int main() // ww w .j av  a  2  s  . c o m
{ 
     char c; 
     int i; 
     float f; 

     double d; 

     c = 'a'; 
     i = 1; 
     f = 19.0; 
     d = 23456.789; 

     printf("%c\n",c); 
     printf("%d\n",i); 
     printf("%f\n",f); 
     printf("%f\n",d); 
     return(0); 
}

Result

Related Topic