Jump to content

Help with Basic C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
penguin

penguin

    Newbie

  • Members
  • PipPip
  • 15 posts
Im reading the K&R C Programming book. I wrote the hello world example and it would not compile. Here is my code

#include <stdio.h>


main int()


{

      printf("hello, world\n");

}

Edit: I did the next program in the book, here is the code

#include <stdio.h>


/* print Fahrenheit-Celsius table

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

main()

{

      int fahr, celsius;

      int lower, upper, step;

      

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

      upper = 300;      /* upper limit */

      step = 20;        /* step size */

      

      fahr = lower;

      while (fahr <= upper) {

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

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

            fahr = fahr + step;

            }

}

Here are the errors I am getting for both of them; undefined reference to cpu features init. id returned 1 exit status, and then a build error

#2
Irfanerol

Irfanerol

    Newbie

  • Members
  • PipPip
  • 16 posts
hi,

The first program's issue is so simple. You will learn this easily :)
starting line of the main function there exists a returning type, which is 'int'. it's not correct. while you're writing the return type of a function, you should put the return type before function name. Also according to your compiler, maybe you should put 'return 0' before closing the curly bracket of the main function.

For the second program, probably it's a return type problem because i've run it successfully without editing anything. Maybe you should put the return type and returning value of the main function manually, it should work by this way. Or change your compiler :)


#include <stdio.h>


int main()


{

      printf("hello, world\n");

      return 0;

}



#include <stdio.h>


/* print Fahrenheit-Celsius table

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

int main()

{

      int fahr, celsius;

      int lower, upper, step;

      

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

      upper = 300;      /* upper limit */

      step = 20;        /* step size */

      

      fahr = lower;

      while (fahr <= upper) {

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

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

            fahr = fahr + step;

            }

      return 0;

}




#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Generally, there will be a specific line number for each error message. Listing the exact message will help a lot.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
main int()
should be
int main(void)
or
main()
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#5
Irfanerol

Irfanerol

    Newbie

  • Members
  • PipPip
  • 16 posts
while I'm trying to help you, i've copied your codes, and pasted it on my original project source file :) and yes, I haven't got any kind of copy of the source. After I've compiled and closed the compiler. Luckly, I've accomplished the project somehow :) Everything for the Codecall :) I like here very much :)