answersLogoWhite

0


Best Answer

A constant variable cannot have its value changed at program run time.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is it necessary to initialize the const variable in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a constant as used in C programming?

A variable of which the value cannot change. Example of its usage: const int foo = 5;


What are data type qualifiers?

In programming languages, a type qualifier indicates the special properties of a variable. Type qualifiers can be used for code optimization and for finding defects. In C/C++ one of the popular type qualifiers is the const qualifire. It is used to fix the value of a variable after the variable is initialized.


Perform addition multiplication subtraction of 2-D array using Operator Overloading in C plus plus?

#include<iostream> #include<vector> #include<random> template<const size_t R, const size_t C> class Matrix { public: using row_type = int[C]; private: // attributes int m_data[R][C]; public: // construction/assignment Matrix (); Matrix (const Matrix& source); Matrix (Matrix&& source); Matrix& operator= (const Matrix<R,C>& source); Matrix& operator= (Matrix<R,C>&& source); ~Matrix () {} public: // accessors row_type& row (const size_t index) { return m_data[index]; } const row_type& row (const size_t index) const { return m_data[index]; } row_type& operator[] (const size_t index) { return m_data[index]; } const row_type& operator[] (const size_t index) const { return m_data[index]; } size_t size() const { return R * C; } size_t rows() const { return R; } size_t cols() const { return C; } public: // operations Matrix<R,C>& operator+= (const Matrix<R,C>&); Matrix<R,C>& operator-= (const Matrix<R,C>&); }; template<const size_t R, const size_t C> Matrix<R,C>::Matrix() { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = 0; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator+= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] += rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator-= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] -= rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C> operator+ (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sum (lhs); return sum += rhs; } template<const size_t R, const size_t C> Matrix<R,C> operator- (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sub (lhs); return sub -= rhs; } template<const size_t R, const size_t C, const size_t R1, const size_t C1> Matrix<R,C1> operator* (const Matrix<R,C>& lhs, const Matrix<R1,C1>& rhs) { static_assert (C==R1, "Matrix dimension mismatch!"); Matrix<R,C1> mul; for (size_t x=0; x!=R; ++x) { for (size_t y=0; y!=C1; ++y) { int prod = 0; for (size_t z=0; z!=C; ++z) { prod += lhs[x][z] * rhs[z][y]; } mul[x][y] = prod; } } return mul; } template<const size_t R, const size_t C> std::ostream& operator<< (std::ostream& os, const Matrix<R,C>& m) { for (size_t row=0; row<R; ++row) { for (size_t col=0; col<C; ++col) { std::cout << m[row][col] << '\t'; } std::cout << std::endl; } return os; } int main() { std::default_random_engine generator; std::uniform_int_distribution<int> distribution (1,9); const size_t rows = 2; const size_t cols = 3; Matrix<rows, cols> a, b; for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) { a[row][col] = distribution (generator); b[row][col] = distribution (generator); } } std::cout << "Matrix a:\n\n" << a << '\n' << std::endl; std::cout << "Matrix b:\n\n" << b << '\n' << std::endl; std::cout << "Matrix a + b:\n\n" << a + b << '\n' << std::endl; std::cout << "Matrix a - b:\n\n" << a - b << '\n' << std::endl; Matrix<cols, rows> c; for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) { c[col][row] = distribution (generator); } } std::cout << "Matrix c:\n\n" << c << '\n' << std::endl; std::cout << "Matrix a * c:\n\n" << a * c << '\n' << std::endl; }


What is the purpose if declaring object with keyword const in c plus plus?

The const keyword transforms a variable into a constant. This means the constant cannot be altered via that constant's identifier. The const keyword can also be applied to a class instance method, such that the method will not alter a class instance's immutable members. Note that the const keyword is merely a programming aid that provides assurances a constant will not be altered inadvertently. However, it is still possible to alter the constant by using a non-constant pointer to the constant. However you have to make a conscious effort to override constant behaviour.


What are the different types of integer constants in c language?

Well, uh, const unsigned int and const signed int..

Related questions

How do you declare and initialize constant arrays?

example in C: const int a [] = {10, 20, 40, 80};


What is a constant variables in c language?

It are a variable you add modifer 'const' to. It means these variable mustn't be modified.


What is a constant as used in C programming?

A variable of which the value cannot change. Example of its usage: const int foo = 5;


What is consonants in c language?

I think you mean constants. A constant is a variable that can not have its value changed at run time eg. const int a = 100;


What are data type qualifiers?

In programming languages, a type qualifier indicates the special properties of a variable. Type qualifiers can be used for code optimization and for finding defects. In C/C++ one of the popular type qualifiers is the const qualifire. It is used to fix the value of a variable after the variable is initialized.


Write a program in C plus plus to accept the order of two matrices check the possibility of multiplication and perform the multiplication?

#include<iostream> #include<vector> #include<random> template<const size_t R, const size_t C> class Matrix { public: using row_type = int[C]; private: // attributes int m_data[R][C]; public: // construction/assignment Matrix (); Matrix (const Matrix& source); Matrix (Matrix&& source); Matrix& operator= (const Matrix<R,C>& source); Matrix& operator= (Matrix<R,C>&& source); ~Matrix () {} public: // accessors row_type& row (const size_t index) { return m_data[index]; } const row_type& row (const size_t index) const { return m_data[index]; } row_type& operator[] (const size_t index) { return m_data[index]; } const row_type& operator[] (const size_t index) const { return m_data[index]; } size_t size() const { return R * C; } size_t rows() const { return R; } size_t cols() const { return C; } void randomise(std::uniform_int_distribution<int>& distribution, std::default_random_engine& generator); public: // operations Matrix<R,C>& operator+= (const Matrix<R,C>&); Matrix<R,C>& operator-= (const Matrix<R,C>&); }; template<const size_t R, const size_t C> void Matrix<R,C>::randomise(std::uniform_int_distribution<int>& distribution, std::default_random_engine& generator) { for (size_t row=0; row!=R; ++row) { for (size_t col=0; col!=C; ++col) { m_data[row][col] = distribution (generator); } } } template<const size_t R, const size_t C> Matrix<R,C>::Matrix() { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = 0; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator+= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] += rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator-= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] -= rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C> operator+ (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sum (lhs); return sum += rhs; } template<const size_t R, const size_t C> Matrix<R,C> operator- (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sub (lhs); return sub -= rhs; } template<const size_t R, const size_t C, const size_t R1, const size_t C1> Matrix<R,C1> operator* (const Matrix<R,C>& lhs, const Matrix<R1,C1>& rhs) { static_assert (C==R1, "Matrix dimension mismatch!"); Matrix<R,C1> mul; for (size_t x=0; x!=R; ++x) { for (size_t y=0; y!=C1; ++y) { int prod = 0; for (size_t z=0; z!=C; ++z) { prod += lhs[x][z] * rhs[z][y]; } mul[x][y] = prod; } } return mul; } template<const size_t R, const size_t C> std::ostream& operator<< (std::ostream& os, const Matrix<R,C>& m) { for (size_t row=0; row<R; ++row) { for (size_t col=0; col<C; ++col) { std::cout << m[row][col] << '\t'; } std::cout << std::endl; } return os; } int main() { std::default_random_engine generator; std::uniform_int_distribution<int> distribution (1,9); const size_t rows = 2; const size_t cols = 3; Matrix<rows, cols> a, b; a.randomise (distribution, generator); b.randomise (distribution, generator); std::cout << "Matrix a:\n\n" << a << '\n' << std::endl; std::cout << "Matrix b:\n\n" << b << '\n' << std::endl; std::cout << "Matrix a + b:\n\n" << a + b << '\n' << std::endl; std::cout << "Matrix a - b:\n\n" << a - b << '\n' << std::endl; Matrix<cols, rows> c; c.randomise (distribution, generator); std::cout << "Matrix c:\n\n" << c << '\n' << std::endl; std::cout << "Matrix a * c:\n\n" << a * c << '\n' << std::endl; }


Write a c plus plus program that calculate the matrix of three by three addition?

#include<iostream> #include<vector> #include<time.h> template<const size_t R, const size_t C> class Matrix { public: using row_type = int[C]; private: // attributes int m_data[R][C]; public: // construction/assignment Matrix (); Matrix (const Matrix& source); Matrix (Matrix&& source); Matrix& operator= (const Matrix<R,C>& source); Matrix& operator= (Matrix<R,C>&& source); ~Matrix () {} public: // accessors row_type& row (const size_t index) { return m_data[index]; } const row_type& row (const size_t index) const { return m_data[index]; } row_type& operator[] (const size_t index) { return m_data[index]; } const row_type& operator[] (const size_t index) const { return m_data[index]; } size_t size() const { return R * C; } size_t rows() const { return R; } size_t cols() const { return C; } public: // operations Matrix<R,C>& operator+= (const Matrix<R,C>&); }; template<const size_t R, const size_t C> Matrix<R,C>::Matrix() { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = 0; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator+= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] += rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C> operator+ (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sum (lhs); return sum += rhs; } template<const size_t R, const size_t C> std::ostream& operator<< (std::ostream& os, const Matrix<R,C>& m) { for (size_t row=0; row<R; ++row) { for (size_t col=0; col<C; ++col) { std::cout << m[row][col] << '\t'; } std::cout << std::endl; } return os; } int main() { srand ((unsigned)time(nullptr)); const size_t rows = 3; const size_t cols = 3; Matrix<rows, cols> a, b, c; for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) { a[row][col] = rand() % 10; b[row][col] = rand() % 10; } } std::cout << "Matrix a:\n\n" << a << '\n' << std::endl; std::cout << "Matrix b:\n\n" << b << '\n' << std::endl; std::cout << "Matrix a + b:\n\n" << a + b << '\n' << std::endl; }


Perform addition multiplication subtraction of 2-D array using Operator Overloading in C plus plus?

#include<iostream> #include<vector> #include<random> template<const size_t R, const size_t C> class Matrix { public: using row_type = int[C]; private: // attributes int m_data[R][C]; public: // construction/assignment Matrix (); Matrix (const Matrix& source); Matrix (Matrix&& source); Matrix& operator= (const Matrix<R,C>& source); Matrix& operator= (Matrix<R,C>&& source); ~Matrix () {} public: // accessors row_type& row (const size_t index) { return m_data[index]; } const row_type& row (const size_t index) const { return m_data[index]; } row_type& operator[] (const size_t index) { return m_data[index]; } const row_type& operator[] (const size_t index) const { return m_data[index]; } size_t size() const { return R * C; } size_t rows() const { return R; } size_t cols() const { return C; } public: // operations Matrix<R,C>& operator+= (const Matrix<R,C>&); Matrix<R,C>& operator-= (const Matrix<R,C>&); }; template<const size_t R, const size_t C> Matrix<R,C>::Matrix() { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = 0; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; } template<const size_t R, const size_t C> Matrix<R,C>::Matrix(Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (const Matrix<R,C>& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = source.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator= (Matrix<R,C>&& source) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] = std::move (source.m_data[row][col]); return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator+= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] += rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C>& Matrix<R,C>::operator-= (const Matrix<R,C>& rhs) { for (size_t row=0; row<R; ++row) for (size_t col=0; col<C; ++col) m_data[row][col] -= rhs.m_data[row][col]; return *this; } template<const size_t R, const size_t C> Matrix<R,C> operator+ (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sum (lhs); return sum += rhs; } template<const size_t R, const size_t C> Matrix<R,C> operator- (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs) { Matrix<R,C> sub (lhs); return sub -= rhs; } template<const size_t R, const size_t C, const size_t R1, const size_t C1> Matrix<R,C1> operator* (const Matrix<R,C>& lhs, const Matrix<R1,C1>& rhs) { static_assert (C==R1, "Matrix dimension mismatch!"); Matrix<R,C1> mul; for (size_t x=0; x!=R; ++x) { for (size_t y=0; y!=C1; ++y) { int prod = 0; for (size_t z=0; z!=C; ++z) { prod += lhs[x][z] * rhs[z][y]; } mul[x][y] = prod; } } return mul; } template<const size_t R, const size_t C> std::ostream& operator<< (std::ostream& os, const Matrix<R,C>& m) { for (size_t row=0; row<R; ++row) { for (size_t col=0; col<C; ++col) { std::cout << m[row][col] << '\t'; } std::cout << std::endl; } return os; } int main() { std::default_random_engine generator; std::uniform_int_distribution<int> distribution (1,9); const size_t rows = 2; const size_t cols = 3; Matrix<rows, cols> a, b; for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) { a[row][col] = distribution (generator); b[row][col] = distribution (generator); } } std::cout << "Matrix a:\n\n" << a << '\n' << std::endl; std::cout << "Matrix b:\n\n" << b << '\n' << std::endl; std::cout << "Matrix a + b:\n\n" << a + b << '\n' << std::endl; std::cout << "Matrix a - b:\n\n" << a - b << '\n' << std::endl; Matrix<cols, rows> c; for (size_t row=0; row<rows; ++row) { for (size_t col=0; col<cols; ++col) { c[col][row] = distribution (generator); } } std::cout << "Matrix c:\n\n" << c << '\n' << std::endl; std::cout << "Matrix a * c:\n\n" << a * c << '\n' << std::endl; }


What is the purpose if declaring object with keyword const in c plus plus?

The const keyword transforms a variable into a constant. This means the constant cannot be altered via that constant's identifier. The const keyword can also be applied to a class instance method, such that the method will not alter a class instance's immutable members. Note that the const keyword is merely a programming aid that provides assurances a constant will not be altered inadvertently. However, it is still possible to alter the constant by using a non-constant pointer to the constant. However you have to make a conscious effort to override constant behaviour.


What are the different types of integer constants in c language?

Well, uh, const unsigned int and const signed int..


What is difference between constant in c plus plus and final in java?

The final keyword can be used to modify a class, method, or variable.When used on a variable, it means that variable cannot be changed once set.When used on a method, it means that no subclasses may override that method.


Why static variables are initialized to zero?

When a variable is declared, your computer assigns a section of memory to that variable. If the variable isn't initialized, there's no way of knowing what data is already stored in that section of memory, which can cause errors in your programs. In managed languages, such as C#, this is done automatically at declaration. Although it's still good practice to initialize variables yourself.