Xcode basics

I am trying to learn Xcode with C as a start to learning to program. So far I have not gotten Xcode to successfully build and run a program using C on Mac OS.

Any recommendations on basic troubleshooting for Xcode setup and orientation. Every program I try to run says "Build failed".

Answered by endecotp in 812775022

What language would you recommend a beginning programmer learn?

That depends on a lot of things.

I would like to create programs that capture product info and prices from retail websites and then create infographics of product data. Also want to design / build website for my design business and have compatible app for iOS.

To be honest I’m not the best person to advise on that sort of work, because it’s a long way from what I do day-to-day. But the same is probably true of most people on this particular forum. The answer is certainly not C, though. It might be Python or something Javascript-like.

Do you see this as just the first programming problem of a decades-long career? If you do, it’s probably more useful to understand the fundamentals of computer science generally, rather than specific languages.

Good luck.

Is there a particular reason you've picked C as your first language to learn? You will likely find Swift a much easier language to learn, and it's built right into Xcode.

If you're set on C, it's not particularly easy for us to determine why you're getting "Build failed" without seeing your code.

I'm wanting to develop good understanding of programming logic and processes, so I am starting with C as per some recommendations I have seen online.

Here is a program I am trying to build and run that is getting "build failed"

#include <stdio.h> #include <math.h>

int main() { float a,b;

printf("Enter value for A: ");
scanf("%f" ,&a);
printf("Enter value for B: ");
scanf("%f" ,&b);

printf("%f + %f = %f\n",a,b,a+b);
printf("%f - %f = %f\n",a,b,a-b);
printf("%f * %f = %f\n",a,b,a*b);
printf("%f / %f = %f\n",a,b,a/b);
printf("%f to the power of %f = %f\n",a,b,pow(a,b));
printf("The square root of %f is %f\n",a,sqrt(a));
printf("The square root of %f is %f\n",a,sqrt(b));

return(0);

}

You could post the build log here, that would tell us some more.

But there's (almost) nothing wrong with your code itself. I copied, pasted it, compiled and ran it. You probably configured your project wrongly (don't worry, there are LOT of options!)

In Xcode, choose New Project. Choose the macOS Command-line Tool template. Give the project a name, choose C as the language.

Once you save it, you'll get an Xcode project containing a command-line tool target which will print "hello, world" to stdout when run.

You can select all the code you posted here, paste it in to main.c (replacing everything in the template code). Xcode will flag some errors due to formatting which are easy to correct - for example each #include line needs to be on a separate line.

You'll end up with one warning about int main() being deprecated. You can fix that by changing

int main () {

to

int main (void) {

or

int  main(int argc, char ** argv) {

after that, your program should build and run.

If it doesn't, read the detailed build log, and if you don't understand it, post it here.

I am starting with C as per some recommendations I have seen online.

Don’t believe everything you read on the internet…

"build failed"

Somewhere there will be some much more detailed error messages; without more details it won’t be easy for anyone to offer useful advice.

Your code works fine here:

/tmp $ clang test.c
/tmp $ ./a.out
Enter value for A: 3
Enter value for B: 6
3.000000 + 6.000000 = 9.000000
3.000000 - 6.000000 = -3.000000
3.000000 * 6.000000 = 18.000000
3.000000 / 6.000000 = 0.500000
3.000000 to the power of 6.000000 = 729.000000
The square root of 3.000000 is 1.732051
The square root of 3.000000 is 2.449490

But you notice that I'm not using Xcode. Because it's just a basic C program, I'm building on the command line.

Xcode is designed for building iOS apps. It's really not suited for beginners at all. There is no way to tell how you've configured your Xcode project. You would need to pick some obscure options in Xcode in order to get it to compile this code.

I recommend a programming course at a local college or university instead. Stick with the command line for now.

Accepted Answer

What language would you recommend a beginning programmer learn?

That depends on a lot of things.

I would like to create programs that capture product info and prices from retail websites and then create infographics of product data. Also want to design / build website for my design business and have compatible app for iOS.

To be honest I’m not the best person to advise on that sort of work, because it’s a long way from what I do day-to-day. But the same is probably true of most people on this particular forum. The answer is certainly not C, though. It might be Python or something Javascript-like.

Do you see this as just the first programming problem of a decades-long career? If you do, it’s probably more useful to understand the fundamentals of computer science generally, rather than specific languages.

Good luck.

Xcode basics
 
 
Q