DEV Community

kalvala ruthvik
kalvala ruthvik

Posted on

Variables , Constants And Data Types in C

Variables

A variable is an identifier which is used to store some value. Variables can change during the execution of a program and update the value stored inside it.
A single variable can be used at multiple locations in a program. A variable name must be meaningful. It should represent the purpose of the variable.

Example:
Height, age, are the meaningful variables that represent the purpose it is being used for. Height variable can be used to store a height value. Age variable can be used to store the age of a person.

Rules :

  • A variable name should consist of only characters, digits and an underscore.

  • A variable name should not begin with a number.

  • A variable name should not consist of whitespace.

  • A variable name should not consist of a keyword.

  • C is a case sensitive language that means a variable named 'age' and 'AGE' are different.

Example : int my_variable;
my_variable = 48;

Constants

Constants are the fixed values that never change during the execution of a program.
Following are the various types of constants:

Integer constants :

An integer constant is nothing but a value consisting of digits or numbers. These values never change during the execution of a program. Integer constants can be octal, decimal and hexadecimal.

Example : 111, 1234

Character constants :

A character constant contains only a single character enclosed within a single quote (''). We can also represent character constant by providing ASCII value of it.

Example :'A', '9'

String constants :

A string constant contains a sequence of characters enclosed within double quotes ("").

Example: "Hello", "Programming"

Data types

C provides various data types to make it easy for a programmer to select a suitable data type as per the requirements of an application. Following are the three data types:

  • Primitive data types
  • Derived data types
  • User-defined data types

There are five primary fundamental data types,

1.int for integer data
2.char for character data
3.float for floating point numbers
4.double for double precision floating point numbers
5.void

Integer data type :

Integer is nothing but a whole number. The range for an integer data type varies from machine to machine. The standard range for an integer data type is -32768 to 32767.

Example : int age;

Float data type :

The 'float' keyword is used to represent the floating point data type. It can hold a floating point value which means a number is having a fraction and a decimal part. A floating point value is a real number that contains a decimal point.

Example :float division;
double Bank Balance;

Character Data type :

Character data types are used to store a single character value enclosed in single quotes.

Example : Char letter;

Void data type:

A void data type doesn't contain or return any value. It is mostly used for defining functions in 'C'.

Example :void display Data()

Top comments (0)