Variables in C Programming: A Comprehensive Guide

Rumman Ansari   Software Engineer   2024-07-04 04:58:21   18175  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. Variables are used to store information to be referenced and manipulated in a computer program. value of the variable can change, depending on conditions or on information passed to the program. variable="vary + able" that means its value can be changed.

variable in java

It is helpful to think of variables as containers that hold information.

Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

You must declare all variables before they can be used. Following is the basic form of a variable declaration

variable in c

Variable Declaration and Initialization in two step;

data_type variable_name ;

variable_name = Constant_value ;

Example

 
int age ;
age = 21 ;

Here,

data_type is int

variable_name is age and

Constant_value 21

Variable Declaration and Initialization in one step;

data_type variable_name = Constant_value ;

Example

 
int age = 21 ;

Here,

data_type is int

variable_name is age and

Constant_value 21

A variable name can be chosen by the programmer in a meaningful way so as to reflect what it represents in the program. some Examples of variable name;

  • average
  • height
  • classStrength
  • roll
  • semester
  • averageLength
  • etc.

Variable may consist of alphabets, digits, underscore(_), and dollar sign;

Rules for naming variables:

  • All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($). The convention is to always use a letter of the alphabet. The dollar sign and the underscore are discouraged.
  • Digit at start is not allowed.
  • A variable’s name can be any legal identifier.
  • After the first initial letter, variable names may also contain letters and the digits 0 to 9. No spaces or special characters are allowed.
  • The name can be of any length, but don't get carried away. Remember that you will have to type this name.
  • Uppercase characters are distinct from lowercase characters. Using ALL uppercase letters are primarily used to identify constant variables. Remember that variable names are case-sensitive.
  • You cannot use a C keyword (reserved word) for a variable name.
  • White space is not permitted.

Integer Number Variables

The first type of variable we need to know about is of class type int - short for integer. An int variable can store a value in the range -32768 to +32767. You can think of it as a largish positive or negative whole number: no fractional part is allowed. To declare an int you use the instruction:

int variable name;

For example:

int roll;

declares that you want to create an int variable called roll.

To assign a value to our integer variable we would use the following C statement:

roll=20;

The C programming language uses the "=" character for assignment. A statement of the form roll=20; should be interpreted as take the numerical value 20 and store it in a memory location associated with the integer variable a. The "=" character should not be seen as an equality otherwise writing statements of the form:

roll=roll+20;

will get mathematicians blowing fuses! This statement should be interpreted as take the current value stored in a memory location associated with the integer variable a; add the numerical value 20 to it and then replace this value in the memory location associated with a.

Decimal Number Variables

As described above, an integer variable has no fractional part. Integer variables tend to be used for counting, whereas real numbers are used in arithmetic. C uses one of two keywords to declare a variable that is to be associated with a decimal number: float and double. They are each offer a different level of precision as outlined below.

float

A float, or floating point, number has about seven digits of precision and a range of about 1.E-36 to 1.E+36. A float takes four bytes to store.

double

A double, or double precision, number has about 13 digits of precision and a range of about 1.E-303 to 1.E+303. A double takes eight bytes to store.

For example:

float total;

double sum;

To assign a numerical value to our floating point and double precision variables we would use the following C statement:

total=0.0;

sum=12.50;

Character Variables

C only has a concept of numbers and characters. It very often comes as a surprise to some programmers who learned a beginner's language such as BASIC that C has no understanding of strings but a string is only an array of characters and C does have a concept of arrays which we shall be meeting later in this course.

To declare a variable of type character we use the keyword char. - A single character stored in one byte.

For example:

char ch;

To assign, or store, a character value in a char data type is easy - a character variable is just a symbol enclosed by single quotes. For example, if c is a char variable you can store the letter A in it using the following C statement:

ch='A'

Notice that you can only store a single character in a char variable. Later we will be discussing using character strings, which has a very real potential for confusion because a string constant is written between double quotes. But for the moment remember that a char variable is 'A' and not "A".

Another Types of Variables

Type Description
char

char ch="A";

Typically a single octet(one byte). This is an integer type.

int

int roll=21;

The most natural size of an integer for the machine.

float

float fl=21.1;

A single-precision floating point value.

double

double d=21.22;

A double-precision floating point value.

void Represents the absence of type.
MCQ Available

There are 16 MCQs available for this topic.

16 MCQTake Quiz