I've stumbled upon a piece of code that look as follows:
void check()
{
int integer = 7;
//integer2 is not declared anywhere
int check = integer, integer2;
//after running
//check = 7
//integer = 7
//integer2 = 0
}
what's the purpose of the comma here?
When you declare a variable, you should also initialize it. Two types of variable initialization exist: explicit and implicit. Variables are explicitly initialized if they are assigned a value in the declaration statement. Implicit initialization occurs when variables are assigned a value during processing.
Each variable is separated by a comma ( , ), and the last variable ends with a semicolon (;) which terminates the var statement.
Basic Syntaxint a, b, c; // declare 3 variables. int z = 35; // declare and initialize variable z with value 35.
a) General syntax for declaring a variable Eg:- char Final_Grade; // Final_Grade is a variable of type char, and no value is assigned to it. data_type variable_name = val; Eg:- int age = 22; // age is a variable of type int and holds the value 22. Here, data_type specifies the type of variable like int, char, etc.
Comma on variable declarations simply allows you to declare a second variable of the same type. It is equivalent to:
int check = integer;
int integer2;
As for:
//integer2 is not declared anywhere
Yes it is; right here! This is the declaration of integer2
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With