Skip to main content

Constant Data type


1.Constant Data Types:
                   Constants refer to fixed values that may not be altered by the program. All the data types we have previously covered can be defined as constant data types if we so wish to do so. The
constant data types must be defined before the main function. The format is as follows:
#define CONSTANTNAME value
for example:
#define SALESTAX 0.05
The constant name is normally written in capitals and does not have a semi-colon at the end. The use of constants is mainly for making your programs easier to be understood and modified
by others and yourself in the future. An example program now follows:

#define SALESTAX 0.05
#include <stdio.h>
main()
 {
   float amount, taxes, total;
   printf("Enter the amount purchased : ");
   scanf("%f",&amount);
   taxes = SALESTAX*amount;
   printf("The sales tax is £%4.2f",taxes);
   printf("\n The total bill is £%5.2f",total);
 }

The float constant SALESTAX is defined with value 0.05. Three float variables are declared amount, taxes and total. Display message to the screen is archieved using printf and user
input handled by scanf. Calculation is then performed and results sent to the screen. If the value of SALESTAX alters in the future it is very easy to change the value where it is defined rather
than go through the whole program changing the indivual values separately, which would be very time consuming in a large program with several references. The program is also improved
when using constants rather than values as it improves the clarity.

Comments

Popular posts from this blog

Drupal Answers Weekly Newsletter - Wednesday, December 31, 2014

Top new questions this week: Can I delete old hook_update_N functions? Suppose you have a custom module, and you have hook_update_N() implementations in your .install file. If you have old update functions, and all updates have run in all sites that the module is ... node-update hook-update-n   asked by AyeshK ...

[New post] 8th Class Result 2014 PEC Hafizabad Board

Muhammad Waqas posted: "PEC Hafizabad Board 8th Class Result 2014 expected date is 28th March, 2014 by PEC. Punjab Examination Commission (PEC) will announce 8th class result for Hafizabad Board soon and all the students of Hafizabad Board who are extremely waiting for the resul" New post on Jobs in Pakistan 8th Class Result 2014 PEC Hafizabad Board by Muhammad Waqas ...

[New post] 1st Year (11th Class) Result 2014 BISE Rawalpindi Board

Xaib Aslam posted: "BISERWP board Inter part 1 result expected on 10th October 2014 according our source. students of Rawalpindi board desperately waiting for 11th class result. 1st they upload the 12th class result and after some time they ready for showing the 1st year fin" New post on Jobs in Pakistan 1st Year (11th Class) Result 2014 BISE Rawalpindi Board by Xaib Aslam ...