0

I'm learning the basics of objective-C by Reading 'Objective C For Dummies'. I'm using XCode 4.4, and I'm trying to get some simple code to work. This question has been posed online before. However - the code doesn't seem to compile with the new version of XCode.

At issue seems to be the line NSLog (@"Here is some amazing text! %i",c); This throws an 'Expected Expression' Error. Per the previous form posting, I have disabled automatic reference checking in preferences and this still fails.

#include <stdio.h>

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

    //declare variables
    int a;
    int b;
    int c;

    //set the variables
    a = 2;
    b = 3;

    //Perform the computations
    c = a % b;

    //Output the results
    NSLog (@"Here is some amazing text! %c",c);

    return 0;
}
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
Brad Hines
  • 107
  • 2
  • 15

2 Answers2

3

Add #import <Foundation/Foundation.h> at the top, and change the NSLog to this:

NSLog (@"Here is some amazing text! %d",c);

Because %c doesn't mean "a variable called c", but rather a char. %d means an int, which is what c is.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • 4
    "NEVER, ever, EVER disable ARC" unless you're working through a book from 2008. Then disable ARC by all means. Also note that you can use bold instead of caps in your answers. – Dan Rosenstark Jul 27 '12 at 22:53
  • @Yar: True, but you shouldn't be using a book from 2008 unless you're maintaining legacy code from 2008. I want to encourage good form. – Linuxios Jul 27 '12 at 22:54
  • @Yar: Thanks. I never like to see a score of -1 on my posts if I've fixed them. – Linuxios Jul 27 '12 at 22:59
  • @Yar - The cioyright date on this text is: 'Pub. Date: October 05, 2009' ... I'd love to get something newer if that's more constructive. Can you suggest a title? – Brad Hines Jul 28 '12 at 16:44
  • @BradHines what background are you coming from? – Dan Rosenstark Jul 28 '12 at 21:20
  • @Yar 3 Years of ColdFusion and ASP.NET forms experience. MVC is a new model for me. I'm expected to learn 'droid and iPhone development for a company I am working for. I know Java reasonably well - I had three classes on it in college and did some projects about 9 years ago in it. – Brad Hines Jul 29 '12 at 05:18
  • I like the book objective-c for java developers, just to get a start, but it's pre-ARC, I just realized, so no go there. Take a look on Stackoverflow to see what's recommended. – Dan Rosenstark Jul 29 '12 at 06:21
  • I started with the 'Big Nerd Ranch guide to Objective C programming'. It's solid and recommend the title to anyone who reads this thread. – Brad Hines Jul 30 '12 at 04:39
1

You forgot to include the Foundation header:

#import <Foundation/Foundation.h>

Sidenote: The format specifier should be %d.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • Euh, nope. 1. If it doesn't find a function, GCC assumes it to be int func(...). 2. %i for integers is just fine. –  Jul 27 '12 at 22:56
  • 1
    @H2CO3 Wake up, it's 2012 (and Xcode 4.4), there's no gcc, just clang. Also, there's no implicit function declaration since C99. And %c is surely wrong when sending an int. – Nikolai Ruhe Jul 27 '12 at 23:03