Curnelious Curnelious - 4 months ago 39
C Question

Testing a pure C code on Xcode?

I am trying to use the latest Xcode to simulate some C code, that has nothing to do with iPhone, only test some data manipulation on C functions, and print results to see it working.

I have started a new playground in Xcode , for iOS , but it gives error when you try to write some C code (

char me[]= "this is me" ;
)

How can i do it anyway ?
thanks.

Answer

The playground is for swift, not C, hence the errors.

You can develop C programs using Xcode, but you want to target OSX, probably using the command line project template.

Start by writing main():

#include <stdio.h>

int main(int argc, const char **argv)
{
    printf("Hello world!\n");
    return 0;
}

EDIT: In order to call a function outside of main() you must declare it before it is referenced:

// Forward declaration of function
static void printit(const char *message);

int main(int argc, const char **argv)
{
    printit("Hello World\n");
    return 0;
}

static void printit(const char *message)
{
    fwrite(message, 1, strlen(message), stdout);
}
Comments