Abd-Elrhman Eid Abd-Elrhman Eid - 1 year ago 200
Objective-C Question

"Variable may be uninitialized when used here" Xcode compiler warning

So I wrote this little program that gives this warning despite the fact that I have the variable initialized, not a duplicate question.

int main(int argc, const char * argv[]) {
@autoreleasepool {

int num1, num2, product, largestProduct = 0;

while (num2 < 1000) {

while (num1 < 1000) {

product = num1 * num2;

if (isPalindrome(product)) {

largestProduct = product>largestProduct?product:largestProduct;


num1 = 0; //If I delete that line the warning disappears.

NSLog(@"%i", largestProduct);

return 0;

The weird thing is if I delete that commented line the warning disappears, and if I initialize
individually it also disappears. Am I doing something wrong or is that a bug in Xcode?

Answer Source

The line

int num1, num2, product, largestProduct = 0;

only initializes largestProduct to 0. None of the other variables on that line are explicitly initialized.

It's arguably poor practice to declare multiple variables on one line.

I would suggest:

int num1 = 0;
int num2 = 0;
int product = 0;
int largestProduct = 0;

This is easier to read and debug.

But if you really want one line, do:

int num1 = 0, num2 = 0, product = 0, largestProduct = 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download