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();
}
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();
}
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();
}