Can't use NSInteger and NSUInteger to check with printf()

Hello. 1st timer and super newbie with programming here.

I’m trying to print the NSInteger and NSUInteger as shown in the book but the build fails for me to check the result with print().
Am I missing something here?

#include <stdio.h>

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

NSInteger x = -5;
NSUInteger y = 6;
printf("Here they are: %ld, %lu", (long)x, (unsigned long)y);
return 0;

}

Build for above will fail with
"! Use of undeclared identifier ‘NSInteger’"
"! Use of undeclared identifier ‘NSUInteger’"
"! Use of undeclared identifier ‘X’"

I tried adding “#include <unistd.h>” to the main.c but that didn’t help.

If someone could help me out I would greatly appreciate it.
Thank you very much in advance.

Also I don’t know if it’s related or not but “UInt32 and SInt16” in the book doesn’t work as well.

#include <stdio.h>

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

UInt32 x = 5; // An unsigned 32-bit integer
SInt16 y = -5; // A signed 16-bit integer
printf("Here they are: %lu, %d", (unsigned long)x, y);
return 0;

}

Unfortunately above will fail with,
"! Use of undeclared identifier ‘UInt32’"
"! Use of undeclared identifier ‘SInt14’"
"! Use of undeclared identifier ‘x’"

[quote]I’m trying to print the NSInteger and NSUInteger as shown in the book but the build fails for me to check the result with print().
Am I missing something here?

Also I don’t know if it’s related or not but “UInt32 and SInt16” in the book doesn’t work as well.
[/quote]
An import statement is missing.

You need to import Foundation.h:

#import "Foundation/Foundation.h"

int main (int argc, const char * argv[])
{
   NSInteger x = -5;
   NSUInteger y = 6;
   printf ("Here they are: %ld, %lu", (long)x, (unsigned long)y);
   return 0;
}

To access NSInteger and NSUInteger, you need Foundation.h.

Did not the book ask you to import that file?

THANK YOU SO MUCH!!!
I can resume my studying thx to u!

I need to double check but unfortunately the book did not mention it.
(At least from P1 to this chapter)

Thank u so so much!!!

:smiley: :smiley: :smiley:

U really made my day :stuck_out_tongue:

ibex10

Unfortunately that didn’t work either.

it shows a lot of red exclamation marks on the left sidebar saying “Parse Issue”.
Do you think it’s safe for me to ignore NSInteger and UInt32 for now and keep studying?

#include <stdio.h>
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
NSInteger x = -5;
NSUInteger y = 6;
printf (“Here they are: %ld, %lu”, (long)x, (unsigned long)y);
return 0;
}

Thank you.

[quote]… it shows a lot of red exclamation marks on the left sidebar saying “Parse Issue”.
[/quote]

[quote][code]
#include <stdio.h>
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
NSInteger x = -5;
NSUInteger y = 6;
printf (“Here they are: %ld, %lu”, (long)x, (unsigned long)y);
return 0;
}
[/code][/quote]
The above code looks good.

You have a different problem, which is hard to tell without seeing the actual code and the error Xcode is giving.

[quote]Do you think it’s safe for me to ignore NSInteger and UInt32 for now and keep studying?
[/quote]
Better to find out why you are getting that parse error and learn from it.

Yikes! I am an experienced user (programming Macs since 1986), and I use the book as the official textbook for a course I teach … but even I cannot yet figure out what is going on with this one.

I am getting errors every which way I turn. Core Foundation Tool? Nope. Foundation? Nope. C tool but include <objc/objc.h>? Nope.

Tried several other variations, and still no luck. I am sure I will crack the problem soon, but the fact that I’ve been stumped this long does not bode well for a beginner. :frowning:

@Ibex10

Thank you very much for reply!
At least I have the code correct which is good enough for me right now.
I’m super beginner and can’t troubleshoot anything at this moment.
Hopefully I can at some point in my life.

@RichardLoosemore

Phew, thank you so much! It’s so nice to hear I’m not the only one.

By any chance if you do ever come to a solution one day, will you please inform and teach us with your findings?

NSInteger and all those goodies sounds very handy to use.

Again, thank you very much!

The full answer to this puzzle is that NSInteger is located in libraries associated with a Cocoa Project, not a C command line tool.

So, create a new project in Xcode with the options OS X, Application, Cocoa Application, (then uncheck “Create Document-Based Application” and uncheck “Use Core Data”).

When the project opens, navigate to the file AppDelegate.m, and within that you will see the method (which is what we call “function” in objected-oriented speak) applicationDidFinishLaunching … and inside that you will see the comment:

Whew!! That is now the equivalent place to where you were working inside main() in the command-line tool projects used in Chapter 7. The function applicationDidFinishLaunching is what gets called first after the Cocoa application finishes doing all its opening routine stuff (just as the function main() is the first real function called inside an ordinary C project.

If you then insert the lines:

[code] NSInteger x = 3;

printf("The value of the NSInteger variable x is %ld.\n", (long)x);[/code]

you will find everything works fine.

Enjoy!

One other small point: when the application runs it puts up a nice (but totally empty) window … that being the place where you would normally be sending some of your output when you write a real Cocoa application. Just ignore that and look behind at the Xcode window and you should see the printf output in the usual place, the console area at the bottom.

To RichardLoosemore-sensei,

Phew! I’m so happy everything is working so smooth.
I’m amazed that you’re explanation is right on the spot. You must be an amazing/great teacher.

Thanks to you I now learned what all those special integers can do and it makes total sense to me now.

May I ask one personal question?

I’m currently learning how to program from scratch with books and videos by myself but if I ever come to a wall like this one and want to learn from a teacher, where can I find a teacher like you?
Are college and community college the place to attend?
I currently live in NYC working in IT field(infrastructure). I haven’t really looked around yet and I’m sure there are other small schools with the classes but it would be VERY nice to have a teacher to ask questions and learn it the most efficient way as possible from the teacher’s knowledge and experience.

Anyway, thank you for helping me and all of us who might come to this question/wall in the future! :smiley: :smiley: :smiley:

tldr: Don’t worry about it and skip this for now.

A bit late to the party, but I think this will not compile as a C program because NSInteger and NSUInteger are not types supported in C.

Also in the book, it says that it is used often in Objective C.

[quote]NSInteger and ISUInteger are used extensively in Apple’s libraries, so when you start working in Objective-C, you will use them a lot[/quote].

I really think that the authors should’ve done a better job that this piece of code will not compile in C compiler, and make it clear that this will only work in Objective C setting.

[quote]I’m trying to print the NSInteger and NSUInteger as shown in the book but the build fails for me to check the result with print().
Am I missing something here? [/quote]

The book doesn’t tell you to test the code out. The book specifically says that that code is used in Objective-C and not C ("…so when you start working in Objective-C you will use them a lot.").

So they won’t work at all.

Thank you

@ magellan: I also had the same problem and found the following solution. It works with the OS X Command Line Tool applications.
Just add the following code before main() function, then both NSInteger and NSUInteger will be available to use.

#include <objc/NSObjCRuntime.h>

Hope this helps! Happy coding :slight_smile:

Thank you very much. My hats off to everyone on this thread.

@bobbyang: thanks for solution. It works :smiley: