Search:

Submit Article

# Declaring a constant

Posted By: Muhammed Athil     Category: C Programming     Views: 8653

### For beginners to show how to declare a constant and assign value to it

Constant v/s Variables
Variables and constant are memory locations, used for holding some data...  The main difference b/w a constant and a variable is the value of constant cannot be change during the execution of a program. But the value of a variable can be change during the execution of a program. It is compulsory  that you (programmer) need to give value to a constant at the time of declaration. But in the case of variable it is optional

Declaring a variable
synatx
<datatype> <varuable name> (= <value>);
Note: the value enclosed in brackets() is optional. the means we need not want to assign a value to the variable at the declaration time.
Eg:-
int a;
int b;
int abc=10;
char name='a';
int abc[10]={10,20,30,40,50,60,70,80,90,100}; // assigning value to array variable at the time of declaration.
Declaring a string variable
Eg:-
char name[20];
char school[]={'A','B','C'}; //assigning value at the time of declaration
Note: If we are giving the value to a array at the time of declaration, we need not want to define it's size shown as above.
Through a program

#include<stdio.h> //program to find the factorial of a number
#include<conio.h>
void main()
{
int n,f=1;
printf("Enter a number\n");
scanf("%d",&n);
while(n>0)
{
f=f*n;
n=n-1;
}
printf("Factorial = %d",f);
getch();
}
Out put:
Enter a number
5
Factorial = 120

Declaring a constant
Declaring constant by using the keyword 'const'.
syntax
const <variable name>=<value>;
Note: We need to assign the value to a constant at the time of declaration...
Eg:-
const abc='a';
const number=10;
const number[10]={1,2,3,4,5,6,7,8,9,10};
const name[]={'A','T','H','I','L'};

Through a program
#include<stdio.h>
#include<conio.h>
main()
{
int i;
const name[]={'A','T','H','I','L'};
for (i=0;i<5;i++)
{
printf("%c",name[i]);
}
getch();
}

Out put
ATHIL
Plz comment         Thanks
***********

Share:

Didn't find what you were looking for? Find more on Declaring a constant Or get search suggestion and latest updates.

 Muhammed Athil author of Declaring a constant is from Calicut, India. Muhammed Athil says Hello Everyone,I am Muhammed Athil from India View All Articles

 Please enter your CommentComment should be atleast 30 Characters.Please put code inside [Code] your code [/Code]. No Comment Found, Be the First to post comment!