Skip to content
EntityQ121572· pop 19· linked from 53 articles

notation for representing a fixed value in source code

Described at

Difference Between Constant and Literals - GeeksforGeeks

Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

geeksforgeeks.org

In this article, we are going to discuss the differences between Constant and Literals in a programming language with examples. Constants are used to define values that remain fixed and cannot be changed during the execution of a program. In C++, they are typically declared using the const keyword. When you declare a constant, you specify its data type and give it a name. Constants are often used to make code more readable, and self-documenting, and to prevent accidental changes to important values. You can define constants of various data types, including integers, floating-point numbers, characters, and more. Constants are evaluated at compile-time, and their values are replaced directly in the code. In the below code, maxCount, pi, and newline are constants, and their values cannot be modified after declaration. Literals are the actual values that are directly written into your code to represent specific data. They are used to provide initial values for variables, as operands in expressions, or as direct values in statements. 1) Integer Literals: These represent whole numbers, and they can be written in decimal, octal, or hexadecimal formats. 2) Floating-Point Literals: These represent real numbers and can be written in decimal or exponential notation. double decimalNum = 3.14159; // Decimal floating-point literal double exponentNum = 6.02e23; // Exponential floating-point literal 3) Character Literals: These represent single characters and are enclosed in single quotes. 4) String Literals: These represent sequences of characters and are enclosed in double quotes. 5) Boolean Literals: C++ has two boolean literals, true and false, which represent the Boolean values. Definition Named values with fixed, unchanging values. Actual values directly used in code. Declaration Declared using the 'const' keyword with a name. Used directly in the code without names or identifiers. Mutability Cannot be changed after initialization. Fixed values, not meant to be changed. Usage Commonly used to define important, unchanging values in the program. Used for initializing variables, as operands in expressions, or in statements. Data Types Can be of various data types (int, double, char, etc.). Different types of literals for different data types (integer, floating-point, character, etc.). Examples const int maxCount = 100; const double pi = 3.14159; const char newline = ' n'; int x = 42; double price = 19.99; char ch = 'A'; const char text = "Hello, World!"; bool flag = true; Constants and literals are both essential in programming. Constants help make code more maintainable by providing meaningful names for fixed values, and literals are used to specify actual data values directly in the code. Depending on your programming needs, you'll use both constants and literals to build robust and readable programs.

Excerpt from a page describing this subject · 8,695 chars · not written by Vinony

Wikidata facts

Subclass of
value
Show 4 more facts
Sources (2)

via Wikidata · CC0