Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Compiler Error

I don't remember if I have messed with C since I put Mavericks on the computer, but I have started getting the following error when I compile, cc or gcc:

temp.c:6:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]

main(void)

^~~~

1 warning generated.


This warning showed up today. I do remember some type of warning when I used cc rather than gcc in the past, however, when using gcc, I things went smoothly, no warnings.


The code is from KR Temperature program:

#include <stdio.h>



/* print Fahrenheit-Celsius table

for fahr = 0, 20, ..., 300 */



main(void)

{

int fahr, celsius;

int lower, upper, step;


lower = 0; /* lower limit of temperature table */

upper = 300; /* upper limit */

step = 20; /* step size */


fahr = lower;

printf("Fahr\tCel\n");


while (fahr <= upper) { /* While loop */

celsius = 5 * (fahr-32) / 9;

printf("%d\t%d\n", fahr, celsius);

fahr = fahr + step;

}

}

/* NOTE: {} and ; are separator character tolkens */

/* <= is a compound operator tolken */

MacBook Pro, OS X Mavericks (10.9.2)

Posted on Apr 17, 2014 9:33 AM

Reply
Question marked as Best reply

Posted on Apr 17, 2014 11:43 AM

The declaration for main should be


int main(void)

15 replies

Apr 17, 2014 12:09 PM in response to danuke

Frank Caggiano has given you the answer.


More details...


If that "KR" reference is to K&R, that's a reference to K&R C, which is C code that predates the ANSI and ISO C standards. That's very old C code. K&R C code will usually generate warnings and errors, as compilers have become much better at detecting errors and much more strict in the twenty+ years since K&R C was the current standard.


In C, warnings are coding errors that may well lead the program astray; best to address and fix those as found. (With clang, the -Weverything switch turns on all of the warnings. That's found more than a few latent bugs in C code, and particularly in K&R C code...)

Apr 17, 2014 1:56 PM in response to MrHoffman

Thank you for the reply.

The K&R C book that I am using is based on the proposed ANSI Standard and I understand that it is well out of date, however, I was not getting this error until today. As I said I don't know if Mavericks changed Unix/compillers etc or not.

I'm trying to figure out what has changed in the past week or so. It's really odd that it started happening out of the blue.

Apr 17, 2014 2:03 PM in response to Frank Caggiano

Howdy Frank,

Thank you for your reply. I am aware of the main function declaration that you provied, I am just trying to see what is now going on as per my reply to MrHoffman.

To be honest, I don't know why, but I just like the K&R book for some reason. I know that I will have to grab hold of something newer one of these days. But then again, I still like Text adventure games the best. ;^)

Apr 17, 2014 2:10 PM in response to danuke

main has returned a value ab=nd has been required to be type for quite some time, I'm surprised you are just seeing the warning now.


In any case the correct way to write that funtion wourl be


int main(void)

{

…code


return 0;

}


Main is ust a function like any other and should return a value. In its case the value returned indicates the success of failure of the program.


K&R is fine, still refer to it myself from time to time. But as Mr. Hoffman wrote a lot has changed since it was written and even if you are learnign from it or using it for reference you need to update the parts that are out-of-date.


regards


Message was edited by: Frank Caggiano - of course of you are writing on a Mac you really should make use of Xcode, you are missing a lot if you do not take the time to learn it. Apple has created a very good IDE.

Apr 17, 2014 4:42 PM in response to danuke

danuke wrote:


To be honest, I don't know why, but I just like the K&R book for some reason. I know that I will have to grab hold of something newer one of these days. But then again, I still like Text adventure games the best. ;^)


I'd suggest acquiring the second edition of the Kernigan and Richie book, if you're fond of the original K&R book. That's updated for the ANSI/ISO C C89/C90 standards.


The more recent C99 and C11 standards are not covered in that book. But at least the code from the K&R second edition will generally still compile with current-generation compilers, unlike the pre-ANSI code from the first edition.


Here's some code that compiles cleanly, and runs correctly.


#include <stdio.h>


/* print Fahrenheit-Celsius table

for fahr = 0, 20, ..., 300 */


int main(void)

{

int fahr, celsius;

int lower, upper, step;

lower = 0; /* lower limit of temperature table */

upper = 300; /* upper limit */

step = 20; /* step size */

fahr = lower;

printf("Fahr\tCel\n");

while (fahr <= upper) { /* While loop */

celsius = 5 * (fahr-32) / 9;

printf("%d\t%d\n", fahr, celsius);

fahr = fahr + step;

}

exit( EXIT_SUCCESS );

}


I used an exit() in the above example, but you can also return off the end of main().


If you code K&R, you'll be chasing various of these changes around.


Yes, I'd (also) suggest use Xcode, and would create a command-line project here, and then build and test and run your code within that IDE. Xcode is a very good IDE for writing C.


FWIW, there are more developers over in the Apple developer forums, as well.

Apr 17, 2014 7:45 PM in response to MrHoffman

Thank you again.

I have been using BBEdit to write the code, accessing BBEdit from the command line. Just a comfort level thing I suppose. I have messed around with Xcode a little. I am indeed using the Second Edition, 1988 of K&amp;R. The original edition is hiding on a shelf somewhere. I also have some of those learn C on the Mac somewhere, alas, they all are based on older versions of the IDE.

I really appreciate the time that you have spent with me.

Apr 18, 2014 7:27 AM in response to danuke

If you want to be blown away, install and launch Xcode and create a new command-line tool project.


Xcode C syntax highlighting and live diagnostics are all quite impressive, and very useful. As a long-time command-line compiler user (and one that still uses the command line heavily), Xcode is very powerful, and a very effective tool or C development.


The switch from gcc to clang meant that the compiler diagnostics, code analysis, code correction suggestions and the rest could all be directly tied directly into Xcode, too. As you enter the source code in the editor, the compiler will be run and will spot errors as you enter them, suggest corrections, and the source code completion will suggest symbols directly from your source code.


I'm moving more and more of my command line development over into Xcode, as it's easier and faster — even if I later come back to the command line and create a script for a non-Xcode build or other such tasks.


This before even getting to the available frameworks, which are massively more advanced than what is available in standard C. (The command line template in Xcode will set you up to use the frameworks from a command line program, so you can either learn more about those as you go, or — at least for now — just delete out the template code that you don't need for your work. Or create or find and install an ANSI/ISO template for Xcode if you wanted to get fancy, but I've not bothered with that.)

Apr 19, 2014 7:17 AM in response to MrHoffman

Howdy MrHoffman,

I had been trying to learn C from way back in the Metrowerks Codewarrior days, but I was trying to start up and operate three nuclear power plants, 7/18s can kinda kill play time. I have some books about learning C that are current for Xcode 3 (on my G5), and was looking at hitting those. However as per your advise, I decided to take a look at Xcode 5.1.1 on my wife's Macbook Pro (Mavericks).

When I first got that up, and created a project, the opening page scared the heck out of me. I guess that it is the target page? Build Settings, Build Phases, Build rules. eeeekkkkk!

However, replaced the project main.c page with the temperature page and have to admit how right you are.

It was showing the main function error and when I played with the code, the errors showed up right then and there, no build / run required. I suppose that I will need to see if Mark or Hillegass have written Learn C books that are up to date with this latest version of Xcode.

Compiler Error

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.