0
#import <Foundation/Foundation.h>

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

    @autoreleasepool {

        NSLog(@"HELLo %i");
    }
    return 0;
}

I tried to print a integer value in objective-C XCode compiler but i forgot to specify the variable. When i executed it, i got some garbage value like 4144 for integer, 98489866930523080936567411769317361312251531363217687183360.000000 for float and values like that for other data types too. I'm just really interested in knowing the reason behind this garbage output ??????

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
Jaffer Sheriff
  • 1,444
  • 13
  • 33

1 Answers1

0

This is because the values to print are passed in registers or on the stack/frame to the function that will print the results.

The printf() function (or in your case NSLog), and functions like it, take 1 or more parameters. The maximum number of parameters is not specified by the function header.

This function first gets a pointer to the string to print, and parses it. Every time it encounters %i, %d, %f etc., it starts pulling the values to print off of the stack, one by one.

In your case, the value it is pulling is uninitialized since it isn't specified, and in this case its value translated to 4144 when interpreted as an integer.

There are some good answers here: How do vararg functions find out the number of arguments in machine code?

Community
  • 1
  • 1
c.fogelklou
  • 1,781
  • 1
  • 13
  • 26