LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   C program (https://www.linuxquestions.org/questions/linux-newbie-8/c-program-591775/)

sjcorp 10-14-2007 04:47 PM

C program
 
I have copied this program into pico and compiled it using cgg. Then I get an error that states, "Badly placed ()'s." Everything I'm reading tells me it should be done like this, so, I need assistance from experts. What am I doing wrong?

#include <stdio.h>

int main()
{
int this_is_a_number;

printf( "Please enter a number: " );
scanf( "%d", &this_is_a_number );
printf( "You entered %d", this_is_a_number );
getchar();
}

matthewg42 10-14-2007 05:40 PM

Please post code inside [code] tags. This helps improve readability by using a fixed-width font and preserving whitespace.

What command are you using to compile and link the program. There is no cgg. Perhaps you mean gcc? Please post the exact command you used, and the output of that command.

As far as I can see, your program should build OK. You should get into the habit of putting the argc and argv parameters to main, and explicitly return an integer value (0 for success, 1-127 for other statuses).

sjcorp 10-14-2007 07:50 PM

Thank you. I did mean gcc. I copied the program into pico, and then ran gcc ctest.c. That reported nothing. Then, I typed at the command prompt "./ctest.c". That's when it errored with "Badly placed ()'s." I copied the program from a tutorial on line.

AlucardZero 10-14-2007 08:12 PM

Code:

alucard@thrawn:~$ cat c.c
#include <stdio.h>

int main()
{
  int this_is_a_number;

  printf( "Please enter a number: " );
  scanf( "%d", &this_is_a_number );
  printf( "You entered %d\n", this_is_a_number );
  getchar();
}
alucard@thrawn:~$ gcc -o c c.c
alucard@thrawn:~$ ./c
Please enter a number: 1
You entered 1
alucard@thrawn:~$

Works for me.

I see what you're doing. You don't execute ctest.c, you execute the program produced by gcc. In my example the "-o c" named it "c". In yours, you got the default name, which iirc is "a.out". Try ./a.out or passing "-o outputname" to gcc.

sjcorp 10-14-2007 08:18 PM

Thank you very much. I tried ./a.out, and that did it. I'm really glad you are there to help.


All times are GMT -5. The time now is 11:46 AM.