Computer Science
C Constant
In computer programming, a C constant is a value that cannot be altered during the execution of a program. It is a fixed value that remains constant throughout the program's execution. Constants are used to represent values that are known and unchanging, such as mathematical constants or fixed values used in a program.
Written by Perlego with AI-assistance
Related key terms
1 of 5
7 Key excerpts on "C Constant"
- eBook - ePub
- Dan Gookin(Author)
- 2020(Publication Date)
- For Dummies(Publisher)
Table 6-2 .name is the constant’s name, just like a variable with the same naming restrictions, rules, and whatnot.The value must be assigned to the constant as it’s declared in your code, all on one line. That’s because, once the constant is declared, its value cannot be altered elsewhere in the code. For example:const char apple = 'a';Constant apple is used just like a char variable, though its value — the lowercase letter a — never changes:printf("Start with letter %c.",apple); The preceding statement outputs this text: Start with letter a.If the code attempts to change a constant, the compiler generates an error message and the program isn’t created. This error is the protection afforded by the compiler to the const keyword: Constants cannot be changed.-
Constants must be assigned values on the same line where they’re declared.
- You can define a constant of any data type.
- Unlike with a variable, you cannot reassign a constant’s value. Otherwise, it’s used like any other variable in your code.
-
Like a variable declaration, a constant declaration is valid only within the function where it’s created. When a constant is required in multiple functions, use the #define preprocessor directive, as covered in Chapter 10 .
Putting constants to use
Anytime your code uses a single value over and over (something significant, like the number of rows in a table or the maximum number of items you can stick in a shopping cart), declare the value as a constant.Exercise 6-15: Rewrite the source code for Listing 6-8 . Use a constant v to represent the value 3 used in the various printf() statements.Remember that constants are declared just like variables, though the statement begins with the const - eBook - PDF
- Joyce Farrell(Author)
- 2017(Publication Date)
- Cengage Learning EMEA(Publisher)
Languages such as C 11 , C#, Visual Basic, and Java distinguish between integer (whole number) numeric variables and floating-point (fractional) numeric variables that contain a decimal point. (Floating-point numbers also are called real numbers .) Thus, in some languages, the values 4 and 4.3 would be stored in different types of numeric variables. Additionally, many languages allow you to distinguish between very small and very large values that occupy different numbers of bytes in memory. You will learn more about these specialized data types when you study a programming language, but this book uses only the two broadest types: numeric and string. Understanding Unnamed, Literal Constants When you use a specific value in a computer program, it is one of two types of constants: • A numeriC Constant (or literal numeriC Constant ) is a number that does not change— for example, 43. When you store a numeric value in computer memory, additional characters such as dollar signs and commas typically are not input or stored. Those characters might be added to output for readability, but they are not part of the number. Declaring and Using Variables and Constants Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 40 C H A P T E R 2 Elements of High-Quality Programs • A string constant (or literal string constant ) appears within quotation marks in computer programs. String values are also called alphanumeric values because they can contain alphabetic characters as well as numbers and other characters. For example, “Amanda”, “51”, and “$3,215.99 U.S.” all are strings because they are enclosed in quotation marks. Although strings can contain numbers, numeric values cannot contain alphabetic characters. The numeriC Constant 43 and the string constant “Amanda” are examples of unnamed constants —they do not have identifiers like variables do. - eBook - PDF
- Subrata Saha, Subhodip Mukherjee(Authors)
- 2017(Publication Date)
- Cambridge University Press(Publisher)
S UMMARY A constant is an entity that does not change its value throughout the program. Variables are storage where we can store values of specific types. C has 32 keywords which serve as the building block of a program. data type indicates the type of the data. The five basic data types are: char, int, float, double and void. signed, unsigned, short and long are type modifiers. signed and short are default type modifiers. The keyword const is used to declare a variable as constant. Every C statement terminated with the symbol ‘;’ (semicolon). Without declaring variables cannot be used. An integer variable can store only any positive or negative value between −32768 and 32767. R EVIEW E XERCISES 1. What do you mean by constant? 2. Explain different type of constants with example. 3. What do you mean by identifier? 4. How many keywords are in C? 5. What is data type? What are the basic data types in C? 6. What is the size of an integer variable? 7. Is it possible to store 50000 in an integer type variable? Explain. 8. What is the utility of declaring a character type variable as unsigned? 9. What is the difference between float and double? Basic Computation and Programming with C 66 10. Which of the following declarations are correct? a. int x, int y, int z; b. char student name; c. char student’s_name; d. float Rs., paisa; 11. Is the following statements are valid? a. int marks1 = marks2 = marks3 = 0; b. char roll = 5; c. float total = 100; d. int alpha = ‘a’; 12. How can we declare a variable as constant? What is its utility? L EARN BY Q UIZ – Q UESTIONS 1. Which one of the following variable names is invalid? (1) 2A (2) A2 (3) A2a 2. What is the size of an integer variable? (1) It depends on the word size of compiler in use (2) 2 bytes (3) 4 bytes 3. Which of the following is a valid identifier? (1) 123-45 (2) Return (3) Kolkata_city 4. Which of the following is used to terminate every C statement? (1) . - eBook - PDF
- Diane Zak(Author)
- 2015(Publication Date)
- Cengage Learning EMEA(Publisher)
C H A P T E R 3 Variables and Constants After studying Chapter 3, you should be able to: Distinguish among a variable, a named constant, and a literal constant Explain how data is stored in memory Select an appropriate name, data type, and initial value for a memory location Declare a memory location in C++ Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. C H A P T E R 3 Variables and Constants 52 Beginning Step 4 in the Problem-Solving Process Chapter 2 covered the first three steps in the problem-solving process: analyze the problem, plan the algorithm, and then desk-check the algorithm. When the programmer is satisfied that the algorithm is correct, he or she moves on to the fourth step, which is to code the algorithm into a program. Coding the algorithm refers to the process of translating the algorithm into a language that the computer can understand; in this book, you will use the C++ programming language. Programmers use the information in the IPO chart, which they created in the analysis and planning steps, as a guide when coding the algorithm. The programmer begins by assigning a descriptive name to each unique input, processing, and output item. The programmer also assigns to each item a data type and (optionally) an initial value. The name, data type, and initial value are used to store the input, processing, and output items in the computer’s internal memory while the program is running. Internal Memory The internal memory of a computer is composed of memory locations, with each memory loca-tion having a unique numeric address. - eBook - PDF
- Joyce Farrell(Author)
- 2017(Publication Date)
- Cengage Learning EMEA(Publisher)
Most programming languages support several additional data types, including multiple types for numeric values that are very large or very small and for those that do and do not have fractional decimal digits. Languages such as C11, C#, Visual Basic, and Java distinguish between integer (whole number) numeric variables and floating-point (fractional) numeric variables that contain a decimal point. (Floating-point numbers also are called real numbers.) Thus, in some languages, the values 4 and 4.3 would be stored in different types of numeric variables. Additionally, many languages allow you to distinguish between very small and very large values that occupy different numbers of bytes in memory. You will learn more about these specialized data types when you study a programming language, but this book uses only the two broadest types: numeric and string. Understanding Unnamed, Literal Constants When you use a specific value in a computer program, it is one of two types of constants: • A numeriC Constant (or literal numeriC Constant) is a number that does not change— for example, 43. When you store a numeric value in computer memory, additional characters such as dollar signs and commas typically are not input or stored. Those characters might be added to output for readability, but they are not part of the number. Declaring and Using Variables and Constants Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. 40 C H A P T E R 2 Elements of High-Quality Programs • A string constant (or literal string constant) appears within quotation marks in computer programs. String values are also called alphanumeric values because they can contain alphabetic characters as well as numbers and other characters. For example, “Amanda”, “51”, and “$3,215.99 U.S.” all are strings because they are enclosed in quotation marks. Although strings can contain numbers, numeric values cannot contain alphabetic characters. - Martin Oliver Steinhauser(Author)
- 2012(Publication Date)
- De Gruyter(Publisher)
2.2.1 Variables in C The choice of names in C, be it for a function, for variables, or for constants, is subject to a special rule: Function or variable names in C must start with a character and may further only contain letters, characters or underscores. With respect to the length of names in C, the following rule applies: The length of a name in C can be arbitrarily long, but only the first 32 characters are used to discriminate between different names. 102 Chapter 2 Scientific Computing in C All names in C are case-sensitive . In addition, variable names in C are not allowed to clash with C keywords that have a special role, such as int , double , if , return , void , etc. A list of reserved C-keywords can be found in Appendix E. The following names are all examples of valid and different variable names in C: SpeedOfLight speedOfLight variable Variable sound_velocity soundVelocity Integer constants in C are denoted in the regular fashion, by strings of Arabic numbers, e.g.: 0 12 34567 89012345 Floating point constants can be written in either regular or scientific notation, e.g.: 0.01 70.567 3e+5 .5678e-15 What exactly is a “variable”? A variable is nothing other than an address in computer memory. When you assign a value to a certain variable, you actually store a value at a certain address in the address space of your computer’s memory and access it later, when needed. For easy access to the contents of memory one needs a unique identifier, i.e. the name of the variable. The compiler later translates this name into an address in memory. Each variable uses some space in memory, depending on its datatype and the type of system ( 16 -bit, 32 -bit, 64 -bit) 13 you are using. The size a certain datatype – for example an integer – uses in memory is not explicitly specified in any C standard 14 , so this may vary on different systems. The typical sizes of standard datatypes on most systems are listed in Table 1.5 on Page 30.- eBook - PDF
- Xingni Zhou, Qiguang Miao, Lei Feng(Authors)
- 2020(Publication Date)
- De Gruyter(Publisher)
3 Basic data types Main contents – Basic data types, the essence of types, storage mechanism of integers, and floating-point numbers – Definition of variables, referencing method, and their way of storage in memory – Operators and their usage, the concept of expressions, categorization of results of operations – Summary of data elements Learning objectives – Understand and master concept of data types, data storage, data referencing, and data operation – Know how to use common operators and expressions – Understand and master the usage of constants and variables 3.1 Constants and variables At the checkout in supermarkets, we are given a receipt by a cashier, on which in-formation of our purchases is written as shown in Figure 3.1. The column total is computed by multiplying per-unit price with quantity, where the per-unit price is a constant and quantity is a variable. Some values are fixed, whereas others keep changing in many problems. For example, we have speed, time, and distance in moving object problems. In circles, we have a radius, perimeter, and Pi. In the shopping example above, we have the number of purchased goods, per-unit price, and total. Data in programs can be categorized into two types based on how they are used: constants and variables. The value of a constant cannot be modified during the execu-tion of programs, whereas the value of a variable can be changed during execution. 3.1.1 Constants There are two kinds of constants: literals and symboliC Constants. One can use lit-eral constants directly in programs as needed without having to define in advance. However, if a constant is used multiple times in a program, we can use a symboliC Constant instead to allow easy modification. In this case, we only need to modify once if we need to change the value of the constant. SymboliC Constants should be defined before being used.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.






