C - Badly Placed ()'s? - c

So I've been trying to get this code to compile using a gcc compiler using c (I found lots of references to c++ but none to c so I asked this) I kept on getting the error Badly placed ()'s every time I go to run the program. So I simplified it to a very simple Hello World test program and I still get the same error.
What could be causing this error?
#include <stdio.h>
int main(int argc, int* argv[])
{
printf("Hello World\n");
return 0;
}

It seems that you are not trying to execute the compiled binary, but that you have a system that runs a tcsh and you are feeding the C source code directly into that shell:
> tcsh /tmp/badly.c
Badly placed ()'s.
A C program must first be compiled to a binary (here: /tmp/badly), and then you have to execute that binary:
> gcc /tmp/badly.c -Wall -o /tmp/badly
/tmp/badly.c:3:5: warning: second argument of 'main' should be 'char **' [-Wmain]
> /tmp/badly
Hello World
As ouah already noticed in his answer, with the -Wall argument to gcc you also get the informative message that the parameters of your main function are wrong.

Related

[msys2][gcc] Wrong output of wprintf() compiling with MSys2's gcc 10.2

I need to get this stripped-down C program working on Windows 7 using MSys2's gcc toolchain:
#include <stdio.h>
void wmain(int argc, wchar_t *argv[])
{
for (int i = 1; i < argc; i++)
wprintf(L"%s\n", argv[i]);
}
The code compiles with
gcc -Wall -municode -O2 -march=x86-64 -m64 test.c
but gives me the following output
>> ./a.exe kk лл
k (!)
:?:?
I have the following questions:
What am I doing wrong?
How would I downgrade the compiler to
version, say, 9.x, or 10.1? (I'm under the impression that the very
same program compiled about one year ago used to work correctly)
Edit [1]: Meanwhile I managed to set up a new MSys2 environment using gcc 9.3. The "error" persists, so it's not the compiler.
Edit [2]: "Some programmer dude" (cmp. below) described the "immediate" solution (THX!).
Even for the wide-character wprintf the format %s is for narrow character strings.
You need to use %ls to print wide-character strings:
wprintf(L"%ls\n", argv[i]);
However this might still not be enough, as the actual encoding of the input (including arguments) might not be what's expected. You need to take into account the encoding used by the terminal the program is running in.

Syntax error reported in basic C code - what is wrong?

The code is given below.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
//FILE *fps;
char secret[512] =" ";
FILE *fps = fopen("/etc/comp2700/share/secret", "r");
if(fps == NULL)
{
printf("Secret file not found\n");
return 1;
}
fgets(secret, 512, fps);
printf("Secret: %s\n", secret);
fclose(fps);
return 0;
}
When I am trying to run this program it is repeatedly throwing the following error:
./attack1.c: line 4: syntax error near unexpected token `('
./attack1.c: line 4: `int main ( int argc, char *argv[] )'
You need to compile your source file with gcc as follows
gcc -o attack attack1.c
then run it with
./attack
You should read up on the difference between compiled versus interpreted languages.
There is a short video here explaining the difference.
You cannot run your C program from the command line as ./attack1.c. Normally the shell would refuse to execute the C source file because it should not have execute permission, but for some reason, on your system, it must have the x bits and is read by the default shell as a script.
Of course this fails because attack1.c contains C code, not a command file. Note that the #include lines are interpreted as comments by the shell and the error only occurs at line 4.
To run a C program, you must first compile it to produce an executable:
gcc -Wall -o attack1 attack1.c
And then run the executable if there were no compilation errors:
./attack1
You can combine these commands as
gcc -Wall -o attack1 attack1.c && ./attack1
First, you need to compile the attack.c code using the following command:
gcc attack.c
This will create one executable file a.out which you can run using the following command:
./a.out
Hope this helps you.

What is causing Segmentation Fault in this code?

I am currently studying C implementations of Linux terminal commands in class but I can't seem to get the following example to run on my machine. I'm using the latest distro of Ubuntu. It will compile but with a warning. "assignment makes pointer from integer without a cast" it refers to the line with a=ctime(&n->ut_time); I found this code online. It is suppose to replicate the terminal command "Who" to display system users. I'm simply trying to study how it works but I cant seem to get it to run. Any help or explanations would be appreciated.
#include<stdio.h>
#include<sys/utsname.h>
#include<utmp.h>
int main(void)
{
struct utmp *n;
char *a;
int i;
setutent();
n=getutent();
while(n!=NULL)
{
if(n->ut_type==7)
{
printf("%-9s",n->ut_user);
printf("%-12s",n->ut_line);
a=ctime(&n->ut_time);
printf(" ");
for(i=4;i<16;i++)
{
printf("%c",a[i]);
}
printf(" (");
printf("%s",n->ut_host);
printf(")\n");
}
n=getutent();
}
}
Transferring comment to answer
The compiler is telling you you've not got #include <time.h> so ctime() is assumed to return an int and not a char *. All hell breaks loose (segmentation faults, etc) because you are not paying attention to the compiler warnings.
Remember, while you're learning C, the compiler knows a lot more about C than you do. Its warnings should be heeded. (When you know C reasonably well, you still pay attention to the compiler warnings - and make the code compile cleanly without warnings. I use gcc -Wall -Wextra -Werror and some extra options — usually -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wold-style-declaration; sometimes -Wshadow, -pedantic; and occasionally a few others.)

Why do I get a syntax error when I try to cross compile libperl for mips64?

I'm trying to cross compile net-snmp for mips64, and in order to do that I need the libperl library. I tried to configure libperl for mips64 using the following command:
./Configure -Dcc=/home/toby/x-tools/mips64-n64-linux-gnu/bin/mips64-n64-linux-gnu-gcc -Dprefix=/home/toby/perl
But I got the following error:
Checking your choice of C compiler and flags for coherency...
I've tried to compile and run the following simple program:
#include <stdio.h>
int main() { printf("Ok\n"); return(0); }
I used the command:
/home/toby/x-tools/mips64-n64-linux-gnu/bin/mips64-n64-linux-gnu-gcc -o try -O -I/usr/local/include try.c
./try
and I got the following output:
./try: 1: Syntax error: "(" unexpected
The program compiled OK, but exited with status 2.
(The supplied flags or libraries might be incorrect.)
You have a problem. Shall I abort Configure [y]
How can I fix this?
I'd turn:
#include <stdio.h>
int main() { printf("Ok\n"); return(0); }
Into:
#include <stdio.h>
int main() {
printf("Ok\n");
return(0);
}
And then run the compile command by hand to see which line really contains the syntax error.
That looks like an error from your shell and not the compiler. Particularly because gcc doesn't return "status 2" for a syntax error, but bash does. The problem happens because you have cross compiled a program called ./try for mips64. How do you expect ./Configure to execute it on your host pc? – indiv

C Build error when getting the value of sin()

I have recently started learning C as a side project. I am working under OpenSuse with the latest NetBeans using the GCC as toolset for compiling.
One of the very first programs that I made was this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
*
*/
int main(int argc, char** argv) {
double rad = 1;
double result = 0;
result = sin(rad);
return (EXIT_SUCCESS);
}
This is a simple, no-brainer example that should have worked without a problem. However, I get a Build Error: Exit code 2(error in line 18, undefined reference to sin) when trying to compile.
Interestingly enough, if I remove the assignment of the value of sin(rad) to result OR replace rad with a hard coded value, the program compiles just fine.
What am I doing wrong here?
In C, you need to link to the math library:
Add this to the command line options:
-lm
Be sure that your are linking with the math library.
$ gcc myprog.c -lm

Resources