Variables and Constants in C
C Programming

VARIABLES AND CONSTANTS IN C

 

In C programming language, a constant is an identifier that contains a specific value The value cannot be altered during normal c program execution as the value is constant.
Example
const float PI = 3.1415927; // (Pi is a constant)

for(int i=0;i<10,i++) // i is a variable

Floating-point constants

Floating point constants in C programming are the type of numeric constants that can either take decimal points or exponent form.
Example:
-2.0
0.0000234
-0.22E-5

Integer constants

Integer constants in C language are the type of numeric constants (constants associated with numbers) that do not have any fractional part or exponential part.

Three types of integer constants are available in the C programming language:

Decimal constant (base 10)
E.g. Decimal numbers: 0 1 2 3 4 5 6 7 8 9
Octal constant (base 8)
E.g. Octal numbers: 0 1 2 3 4 5 6 7
Hexadecimal constant (base 16)
E.g. Hexadecimal numbers: 0 1 2 3 4 5 6 7 8 9 A B C D E F.

Character constants

Character constants in C language are the type of constant which use single quotations around characters. For example: ‘l’, o’, ‘v’, ‘e’ etc.

Variables:

An identifier that holds the value that can be altered during normal program execution is called a variable. Variables can be visualized as memory location in the computer’s memory to store data. In other words, Variable names are just the symbolic representation of a memory location.

In C programming following basic variable types are available:

char: This is an integer type. Consume 1 byte of memory location
int: Contains decimal point free integer value. Consume 2 bytes of memory
float: Single precision floating point value. Consume 4 bytes of memory
double: Double precision floating point value. Consume 8 bytes of memory
void: Represents the absence of type.

Example

#include<stdio.h>

#include<conio.h>

void main()

{

int a=2;

float b=6.78;

char c=’x’;

double d=456.678;

clrscr();

printf(“integer=%d”,a);

printf(“\nfloat=%lf”,b);

printf(“\nchar=%c”,c);

printf(“\ndouble=%lf”,d);

getch();

}

Variables & Constant in c programming

Example

#include<stdio.h>

main()

{

int i;

float f;

char c;

clrscr();

printf(“size of i= %d”,sizeof (i));

printf(“\nsize of f= %d”, sizeof (f));

printf(“\nsize of c= %d”,sizeof (c));

getch();

}

size of operator in C

The C programming language also gives support for two important kinds of variables that could be used for the dynamic allocation of memory.

Static variable:

static int shared = 3; //shared is of static type variable
A static variable is defined outside all blocks and has static. Always declared outside of the main block.

global variable:

global variable in C language is a type of variable that has global scope, In other words, a global variable is accessible throughout the program, unless the programmer hides it.

#include <stdio.h>
static int shared = 3; //note declaration of variable just after the library
main()
{
}

Example

#include<stdio.h>

#include<string.h>

void main()

{

int i;

static char name[7]={‘X’,’c’,’n’,’o’,’t’,’e’,’s’};

clrscr();

printf(“\n elements of the array\n”);

for(i=0;i<=6;i++)

printf(“name[%d]=%c\n”,i,name[i]);

getch();

}

static constant in C

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment