Download presentation
Presentation is loading. Please wait.
1
Symbolic Constants in C
Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Symbolic Constants in C
2
Specifying Symbolic Constants in C
Two ways Textual substitution Declaration of const data object CS-2303, A-Term 2012 Symbolic Constants in C
3
Constant – Textual Substitution
See page 14 & 89 in K&R #define NAME replacement-text E.g., #define PI #define LOWER 0 #define UPPER 300 #define STEP 20 A preprocessor directive! It is traditional in C for textual substitution names to be all UPPER CASE CS-2303, A-Term 2012 Symbolic Constants in C
4
Constant – Textual Substitution
See page 14 & 89 in K&R #define NAME replacement-text E.g., #define PI #define LOWER 0 #define UPPER 300 #define STEP 20 When a textual substitution constant is used in a program, the compiler simply substitutes the replacement text on the fly CS-2303, A-Term 2012 Symbolic Constants in C
5
Symbolic Constants in C
Constant Declaration const double pi = ; const double c = e+8; /* speed of light in meters/sec */ Defines a value of the declared type with the declared name I.e., creates storage to hold this value Must be initialized May never be left side of an assignment CS-2303, A-Term 2012 Symbolic Constants in C
6
Symbolic Constants in C
Constants in C and C++ Vast majority of C-programs define constants by textual substitution and “#define” Reason:– It was the only kind of constant definition available in K&R and in early versions of the C-compiler Has been expanded to a “macro” facility over the years Most C++-programs define constants using const Makes for cleaner, clearer programs CS-2303, A-Term 2012 Symbolic Constants in C
7
Symbolic Constants in C
Questions? Next Topic CS-2303, A-Term 2012 Symbolic Constants in C
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.