Working of C ++
Working of C ++
" Program
1. // Your First C++ Program
In C++, any line starting with // is a comment. Comments are intended for the person
reading the code to better understand the functionality of the program. It is completely
ignored by the C++ compiler.
2. #include <iostream>
The #include is a preprocessor directive used to include files in our program. The above
code is including the contents of the iostream file.
This allows us to use cout in our program to print output on the screen.
For now, just remember that we need to use #include <iostream> to use cout that
allows us to print output on the screen.
3. int main() {...}
A valid C++ program must have the main() function. The curly braces indicate the start
and the end of the function.
std::cout prints the content inside the quotation marks. It must be followed by <<
followed by the format string. In our example, "Hello World!" is the format string.
The return 0; statement is the "Exit status" of the program. In simple terms, the
program ends with this statement.
#include <iostream>
// program begins
int main()
return 0;
Output
Hello World
C Variables
Variables in C++ is a name given to a memory location. It is the basic unit of storage in a
program.
The value stored in a variable can be changed during program execution.
A variable is only a name given to a memory location, all the operations done on the variable
effects that memory location.
In C++, all the variables must be declared before use.
// variable
#include <iostream>
int main()
// this is declaration of
variable a
int a;
// this is initialisation of a
a = 10;
// this is definition =
declaration + initialisation
int b = 20;
// of variable 'a123'
float c;
return 0;
Output
a
Time Complexity: O(1)
Space Complexity: O(1)
Types of Variables
There are three types of variables based on the scope of variables in C++
Local Variables
Instance Variables
Static Variables
Contants in C
The constants in C are the read-only variables whose values cannot be modified once they are
declared in the C program. The type of constant can be an integer constant, a floating pointer
constant, a string constant, or a character constant. In C language, the const keyword is used to
define the constants.
In this article, we will discuss about the constants in C programming, ways to define constants in
C, types of constants in C, their properties and the difference between literals and constants.
What is a constant in C?
As the name suggests, a constant in C is a variable that cannot be modified once it is declared in
the program. We can not make any change in the value of the constant variables after they are
defined.
How to Define Constant in C?
We define a constant in C language using the const keyword. Also known as a const type
qualifier, the const keyword is placed at the start of the variable declaration to declare that
variable as a constant.
Syntax to Define Constant
const data_type var_name = value;
Example of Constants in C
// C program to illustrate constant
variable definition
#include <stdio.h>
int main()
int_const);
char_const);
float_const);
return 0;
Output
Printing value of Integer Constant: 25
Printing value of Character Constant: A
Printing value of Float Constant: 15.660000
One thing to note here is that we have to initialize the constant variables at declaration.
Otherwise, the variable will store some garbage value and we won’t be able to change it. The
following image describes examples of incorrect and correct variable definitions.
Types of Constants in C
The type of the constant is the same as the data type of the variables. Following is the list of the
types of constants
Integer Constant
Character Constant
Floating Point Constant
Double Precision Floating Point Constant
Array Constant
Structure Constant
We just have to add the const keyword at the start of the variable declaration.
Properties of Constant in C
The important properties of constant variables in C defined using the const keyword are as
follows:
1. Initialization with Declaration
We can only initialize the constant variable in C at the time of its declaration. Otherwise, it will
store the garbage value.
2. Immutability
The constant variables in c are immutable after its definition, i.e., they can be initialized only
once in the whole program. After that, we cannot modify the value stored inside that variable.
// C Program to demonstrate the
behaviour of constant
// variable
#include <stdio.h>
int main()
var = 20;
return 0;
Output
In function 'main':
10:9: error: assignment of read-only variable 'var'
10 | var = 20;
| ^
Constants are defined by using the const keyword They themselves are the values that are assigned to
in C. They store literal values in themselves. the variables or constants.
#include <stdio.h>
#define pi 3.14
int main()
return 0;
Output
The value of pi: 3.14
Note: This method for defining constant is not preferred as it may introduce bugs and make the
code difficult to maintain.
FAQs on C Constants
Q1. Define C Constants.
Answer:
Constants in C are the immutable variables whose values cannot be modified once they are
declared in the C program.
Q2. What is the use of the const keyword?’
Answer:
The const keyword is the qualifier that is used to declare the constant variable in C language.
Q3. Can we initialize the constant variable after the declaration?
Answer:
No, we cannot initialize the constant variable once it is declared.
Q4. What is the right way to declare the constant in C?
Answer:
The right way to declare a constant in C is to always initialize the constant variable when we
declare.
Q5. What is the difference between constant defined using const qualifier
and #define?
Answer:
The following table list the differences between the constants defined using const qualifier and
#define in C:
Constants using const Constants using #define
They are handled by the compiler. They are handled by the preprocessor.
#include <stdio.h>
int main()
// changing value
*ptr = 500;
return 0;
Output
Initial Value of Constant: 10
Final Value of Constant: 500
Constant Qualifier in C
The qualifier const can be applied to the declaration of any variable to specify that its value will
not be changed (which depends upon where const variables are stored, we may change the value
of the const variable by using a pointer). The result is implementation-defined if an attempt is
made to change a const.
Using the const qualifier in C is a good practice when we want to ensure that some values should
remain constant and should not be accidentally modified.
In C programming, the const qualifier can be used in different contexts to provide various
behaviors. Here are some different use cases of the const qualifier in C:
1. Constant Variables
const int var = 100;
In this case, const is used to declare a variable var as a constant with an initial value of 100. The
value of this variable cannot be modified once it is initialized. See the following example:
// C program to demonstrate that constant
variables can not
// be modified
#include <stdio.h>
int main()
// 'var'
var = 200;
return 0;
Output
./Solution.cpp: In function 'int main()':
./Solution.cpp:11:9: error: assignment of read-only variable 'var'
var = 200;
^
2. Pointer to Constant
const int* ptr;
OR
int const *ptr;
We can change the pointer to point to any other integer variable, but cannot change the value of
the object (entity) pointed using pointer ptr. The pointer is stored in the read-write area (stack in
the present case). The object pointed may be in the read-only or read-write area. Let us see the
following examples.
Example 1:
// C program to demonstrate that the
pointer to point to
#include <stdio.h>
int main(void)
int i = 10;
int j = 20;
*ptr = 100;
ptr = &j; /* valid */
return 0;
Output
./Solution.c: In function 'main':
./Solution.c:12:10: error: assignment of read-only location '*ptr'
*ptr = 100;
^
Example 2: Program where variable i itself is constant.
// C program to demonstrate that the
pointer to point to
#include <stdio.h>
int main(void)
int j = 20;
/* error */
*ptr = 100;
/* valid. We call it up
qualification. In
ptr = &j;
return 0;
Output
./Solution.c: In function 'main':
./Solution.c:18:10: error: assignment of read-only location '*ptr'
*ptr = 100;
^
Down qualification is not allowed in C++ and may cause warnings in C. Down qualification
refers to the situation where a qualified type is assigned to a non-qualified type.
Example 3: Program to show down qualification.
// C program to demonstrate the down
qualification
#include <stdio.h>
int main(void)
{
int i = 10;
ptr = &j;
*/
return 0;
Output
main.c: In function ‘main’:
main.c:16:9: warning: assignment discards ‘const’ qualifier from pointer
target type [-Wdiscarded-qualifiers]
16 | ptr = &j;
| ^
*ptr: 10
*ptr: 20
3. Constant Pointer to Variable
int* const ptr;
The above declaration is a constant pointer to an integer variable, which means we can change
the value of the object pointed by the pointer, but cannot change the pointer to point to another
variable.
Example
// C program to demonstrate that the value
of object pointed
// to another variable
#include <stdio.h>
int main(void)
int i = 10;
int j = 20;
return 0;
Output
./Solution.c: In function 'main':
./Solution.c:15:9: error: assignment of read-only variable 'ptr'
ptr = &j; /* error */
^
4. Constant Pointer to Constant
const int* const ptr;
The above declaration is a constant pointer to a constant variable which means we cannot change
the value pointed by the pointer as well as we cannot point the pointer to other variables. Let us
see with an example.
// C program to demonstrate that value
pointed by the
#include <stdio.h>
int main(void)
int i = 10;
int j = 20;
return 0;
Output
./Solution.c: In function 'main':
./Solution.c:12:9: error: assignment of read-only variable 'ptr'
ptr = &j; /* error */
^
./Solution.c:13:10: error: assignment of read-only location '*ptr'
*ptr = 100; /* error */
^
Advantages of const Qualifiers in C
The const qualifier in C has the following advantages:
Improved Code Readability: By marking a variable as const, you indicate to other programmers
that its value should not be changed, making your code easier to understand and maintain.
Enhanced Type Safety: By using const, you can ensure that values are not accidentally modified,
reducing the chance of bugs and errors in your code.
Improved Optimization: Compilers can optimize const variables more effectively, as they know
that their values will not change during program execution. This can result in faster and more
efficient code.
Better Memory Usage: By declaring variables as const, you can often avoid having to make a
copy of their values, which can reduce memory usage and improve performance.
Improved Compatibility: By declaring variables as const, you can make your code more
compatible with other libraries and APIs that use const variables.
Improved Reliability: By using const, you can make your code more reliable, as you can ensure
that values are not modified unexpectedly, reducing the risk of bugs and errors in your code.
Summary
Pointer Value Change Pointing Value Change
Type Declaration
(*ptr = 100) (ptr = &a)