Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to declare a variable without assigning value in C++?
int a; int a=15; Which one will be Correct?
If i assign value to a variable 3 times or more which one will count at the end in C++??
int a=15;
int a=10;
int a=5;
Which value will be execute for a at the end?
int a; // declared but not assigned
a = 1; // assigning a value
a = 2; // assigning a different value
a = 3; // assigning another value
std::cout << a << "\n"; // will print 3 since only the last assignment matters
int x; declares a variable x without assigning a value.
If you assign to a variable three times then which ever assignment executed last will be the variables final value. Very important idea, C++ statements execute in a specific order, and which order they execute in is essential to understanding what a program does.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I wanted to assign a value to a constant but as it is constant, we cant assign value to it through cin. So I came up with this idea. Is it okay if we use it like this?
{
int data = 0;
cout << "enter number of students"<<endl;
cin>>data;
const int number = data;
cout << number<<endl;
}
This is fine. const values do not need to be compile time constants. They just can't be changed after they are defined.
Similarly you could do:
int getNumberFromStdin() {
int data;
cin >> data;
return data;
}
int main() {
const int number = getNumberFromStdin();
}
Yes, because it is not an assignment; it is a declaration.
Despite looking quite similar, the two constructs are different: assignment changes something that is already declared, while the declaration sets up something new, and optionally gives it a value. The value does not need to be hard-coded, though: it can be a result of some processing, as is the case in your example.
Naturally, assignment is not allowed for constants, because they already have a value.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am new to C++ and have come across the const keyword. I looked it up online and read that The main use is to prevent the changing the value throughout the program. I have these two snippets below
const int size = 56;
Compared to using
int size = 56;
Why should I use one over the other? What is the idea behind using it.
Yes, you should make all variables const if you are never going to change its value after initialization. This prevents bugs where you accidentally change a value you're not supposed to. You seem to be aware of this already.
In addition, there is something that you might not be aware of, which is that making an int variable const also makes it a constant-expression, so long as the initializer itself is also a constant-expression, e.g. the int literal 56. This allows you to use it in contexts where you need a constant-expression, e.g. as the dimension of a static array:
const int size = 56;
int a[size]; // ok
int size = 56;
int a[size]; // error
In c++ CONST is used when you want to declare something which will not change its value throughout the program. But if you accidentally try to rewrite its value, the compiler will through an error. Most of the time it is recommended to declare a variable of int type so you can redefine its value when it's required. CONST is used when you want to declare a static array i.e.
const int a = 100;
int sampleAray[a];
And int is used when you, the value of the variable will be modified at some point i.e.
int a = 12;
int arr[4] = {11,22,33,554};
for (int i=0; i<4; i++){
if(arr[i]%2 == 0){
a+=arr[i];
}
}
When you have a const variable holding something that should not change and accidentally write code that modifies the variable anyway, you get a nice compiler error and can correct your mistake easily. If the variable is non-const, the compiler cannot help you and you now have a bug that may be hard to find.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
int function(int a, int b){
a = a*b;
return(a);
}
This is a really stupid questions but, if i pass in 4 and 2.
Why doesnt the a need to be declared?
int function(int a, int b){ // two variables, a and b, are declared and set equal to whatever you passed in when you called the function.
a = a*b; // now you are using the two already-declared variables
return(a); // return the value of 'a' which was declared in the first line and then modified
}
Note that 'a' and 'b' in the above example are now destroyed. They only live inside of the function and are re-created every time you call function() with new values passed in. You can't call the 'a' and 'b' you were using in this function anywhere outside of this function, because they doesn't exist outside of this function. This means that you can declare another 'int a' and 'int b' outside of this function.
If I call this function in main:
int oldA = 5; // declare an int called oldA and set it to 5
int oldB = 10; // declare an int called oldB and set it to 10
cout << function(oldA, oldB); // makes a copy of oldA and oldB and then uses them inside function()
// note that oldA and oldB are still 5 and 10. A copy of them was made and then
// used inside of function.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
int f_point(int kek,int *lol) {
*lol *= *lol;
return kek;
}
int main {
int x;
std::cin >> x;
int *adress_of_x = &x;
int a,b = f_point(x,&x); //how does it work?
std::cout << a << LINE_JUMP;
std::cout << b << LINE_JUMP;
}
For example, if I give 2 to program then I will get 0 and 2. Why?
b = f_point(x,&x) in this statement value of first parameter is 2.
Your function is not changing the value of first parameter and returns the same value.
Your are passing first parameter by value so it has no relation with the updated value of x. Variable a is uninitialized, so it is taking a garbage value.
int a,b = f_point(x,&x); //how does it work?
The variable declaration leaves a uninitialized and initializes b from the result of f_point(x,&x);.
Since it's an uninitialized variable, accessing the value of a in the
std::cout << a << LINE_JUMP;
statement leads to undefined behavior of your program. Having an output of 0 is just one of any possibilities (including your fridge explodes unexpectedly or little demons flying out of your nostrils).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
what makes type cast affected this?
where is the value stored?
The program has undefined behavior: with the const_cast<int*>(b) you remove the const qualifier from an object which actually is const and the assignment to that object may have arbitrary effect.
The observed effects indicate that the implementation replaced uses of a with its immutable value while it dereferences b to determine the value. It could have arbitrary other effect, too, though. For example, a segmentation fault when trying to write a write protected location could be a possible outcome. Well, anything can happen.