What's the difference between int a; int b = 42; a = 10; b = 3; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the difference between int a; int b = 42; a = 10; b = 3;

declaring a variable type and without declaring a variable type

30th Mar 2017, 2:14 PM
gopal
gopal - avatar
4 Answers
+ 4
We must declare variable types in c++ because it is a type safe programming language. And computers aren't smart enough to understand what we are meaning. Example: char c = 'c'; string s = "s"; Computers won't understand the difference. That's why you must specify the types.
30th Mar 2017, 2:39 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 2
You must declare variable type then you can assign it a value. Once You declare a variable then you can assign it values as many times as you can but you can't assign a value to a variable whose variable type is not declared.
30th Mar 2017, 2:19 PM
Shafiq
Shafiq - avatar
+ 1
It depends on which programming languages you use. In C# int a; // it means 'a' = 0 by default; int b = 42; a = 10; // it means the value of 'a' becomes 10, it replaces initial value of 0. b = 3; // it means the value of 'b' becomes 3 than 42 now. ---------------------------------------------------------------------------------------------- it is like in Stack which store Variables, when you declare int a with no value but it sets the initial value to 0, it means --------- | int | --------- | a = 0 | --------- in somewhere (I don't know where is the memory address though :D), Integer type variable named a is created with default value 0. int b = 42; // it is set up in Stack as ---------- | int | ---------- | b = 42 | ---------- so when you replace a = 10, it becomes as --------- | int | --------- | a = 10 | --------- it replaces the value in Stack with 10, not 0 anymore. b = 3; ---------- | int | ---------- | b = 42 | ---------- becomes --------- | int | --------- | b = 3 | --------- now.
30th Mar 2017, 9:18 PM
Yhal Htet Aung
Yhal Htet Aung - avatar
0
Kk thnks
30th Mar 2017, 2:21 PM
gopal
gopal - avatar