syntax error near unexpected token `(' in cygwin terminal [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am running the follwong c program related with fork() system call using the cygwin terminal in the windows OS! and i get the following error! how can i rectify this?
./fork.cpp: line 3: syntax error near unexpected token `('
'/fork.cpp: line 3: `int main(int argcc,char *argv[])
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
# include<conio.h>
int main(int argcc,char *argv[])
{
printf("I m %d\n",(int) getpid());
pid_t pid = fork();
printf("Fork returned: %d\n", (int) pid);
printf("I am %d\n, (int) getpid());
getch();
}

You should state the line at which it gives you the error but I am assuming it is this statement : printf("I am %d\n, (int) getpid()); since you have not put the closing quotes in the printf statement ". The unexpected token `(' is the ( just before the word int since the compiler is looking for the closing quotes and can not find them.

Related

What is wrong with using %var after format specifier in a scanf call? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
main ()
{
int a,b,toplam;
float ort;
printif("iki sayi girin :");
scanf("%d,%d",%a,%b);
toplam=a+b;
ort=toplam/2;
printif("Ortalama= %f olarak hesaplandı.",ort);
}
error: expected expression before '%' token scanf("%d,%d",%a,%b);
What was my mistake?
Replace scanf("%d,%d",%a,%b); with scanf("%d,%d",&a,&b);
Check this answer for more information.
You need to replace the '%' before a and b with '&'.
Also you need to use printf not printif.
int a,b,toplam;
float ort;
printf("iki sayi girin :");
scanf("%d,%d",&a,&b);
toplam=a+b;
ort=toplam/2;
printf("Ortalama= %f olarak hesaplandı.",ort);
return 0;
Read Modern C and see this C reference
Your scanf statement should be (using the & prefix "address-of" operator):
scanf("%d,%d",&a,&b);
and your code is incomplete, since scanf can fail. You should read its documentation.
Consider coding a test against such failure using:
if (scanf("%d,%d", &a, &b) < 2) {
perror("scanf failure");
exit(EXIT_FAILURE);
}
Read also the documentation of your C compiler, e.g. GCC, and of your debugger, e.g. GDB. You could compile your code with gcc -Wall -Wextra -g to get all warnings and debug information, and later use gdb to debug your executable and understand its runtime behavior.
Your code is missing a #include <stdio.h> before your main.
The printif is wrong. You want to use printf (and read its documentation).
Consider studying for inspiration the source code of some existing free software, such as GNU make or GNU bash.

What is the mistake in read() and write() in C? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
This is a small C program to test the client and server programs so that the client sends an integer to the client. The server multiplies the number by 10 and returns the integer*10 back to the client. When writing the integers to the FIFO.
This is my code so far:
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
main (void)
{
int fda;
int fdb;
int number;
int outputnumber;
if((fda=open("FIFO_to_server", O_WRONLY))<0)
printf("cant open fifo to write");
if((fdb=open("FIFO_to_client", O_RDONLY))<0)
printf("cant open fifo to read");
printf("Client: Please enter an integer: ");
scanf("%d", &number);
write(fda, number, sizeof(number));
printf("\nClient: Got the number sent, now waiting for response ");
read(fdb, outputnumber, sizeof(outputnumber));
printf("\nClient: received from server %s", outputnumber);
close(fda);
close(fdb);
printf ("\nall done!\n");
}
after compiling, I had some error:
-bash-3.2$ gcc clientHw.c -o client
clientHw.c: In function `main':
clientHw.c:36: warning: passing arg 2 of `write' makes pointer from integer
without a cast
clientHw.c:38: warning: passing arg 2 of `read' makes pointer from integer
without a cast
You must pass the address of the variables like this:
write(fda, &number, sizeof(number));
...
read(fdb, &outputnumber, sizeof(outputnumber));

error: expected identifier or '(' before '{' token| [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
sorry for bothering you with such basic question but I am trying to learn C (and later C++) programming but I can not get compiler to compile so while struggling to get to understand how to the IDEs work (I tried both Code Blocks and CodeLite to get this code to work) then I keep getting the same errors .
IDE keep showing this problem at first opening brace which I do not understand as the braces looks placed correctly to me (?).
I have also tried moving the main function to above the addtwo function but it doesn't seem to make any difference .
Program :
/* program to add two numbers and return result */
#include <stdio.h>
/* This function adds two numbers */
int addtwo( int x , int y );
{
int result;
int result = x+y ;
return (result)
}
int main()
{
int sum ;
sum=addtwo(25,49);
printf("25 + 49 = %d \n",sum);
getchar();
return sum;
}
Compiler output :
||=== Build: Debug in CB Test 01 (compiler: GNU GCC Compiler) ===|
C:\Users\User\Documents\CodeBlocks files\CB Test 01\main.c|8|error: expected identifier or '(' before '{' token|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
No semicolon after (result) in addtwo function. Also the parentheses () are not necessary.
Extra semicolon in,
int addtwo( int x , int y );
/* ^ here */
{
int result;
int result = x+y ;
return (result)
}

Terminal not running C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
so i'm trying to run a simple code for trial to see how i run a C code in terminal but getting this error:
main.c: line 3: syntax error near unexpected token `('
main.c: line 3: `int main(int argc, const char * argv[]) {'
any ideas why this is happening? runs fine on xcode?
The Code that i ran:
#include <stdio.h>
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
for (int i = 0; i <= 10;i++)
{
printf("IT WORKS!\n");
}
return 0;
}
command to run file:
bash main.c
This error is returned from the shell that tries to parse your c file as a shell script, since you try to run your C code directly.
sh-3.2$ cat > mycode.c
#include <stdio.h>
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
for (int i = 0; i <= 10;i++)
{
printf("IT WORKS!\n");
}
return 0;
}
sh-3.2$ chmod +x mycode.c
sh-3.2$ ./mycode.c
./mycode.c: line 3: syntax error near unexpected token `('
./mycode.c: line 3: `int main(int argc, const char * argv[]) {'
You can't run C code directly, first you need to compile it into an executable, and then execute the compiled executable.
for example:
$ gcc -o myexe main.c
$ ./myexe

C Newbie Syntax Error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to write code that opens an external file, and prints out the lines in that file, but keep getting
Line 7: warning: incompatible implicit declaration of built-in function 'exit'
Line 15: error: expected expression before '%' token
Line 15: error: stray '\' in program
When trying to compile this code:
#include <stdio.h>
#include <assert.h>
main(int argc, char *argv[]){
if (argc != 2){
fprintf(stderr, "usage: strgen <file>\n");
exit(1);
}
char *infile = argv[1];
FILE *fp = fopen(infile, "r");
assert(fp != NULL);
char buffer[50];
while( fgets( buffer, 50, fp) != NULL){
printf("%d\n",buffer);
printf(%s\n, buffer);
}
fclose(fp);
return(0);
}
Two problems:
You need to #include <stdlib.h> to get the declaration of exit()
You need quotes around the %s\n in the second printf() statement
you need to include header file
#include <stdlib.h>
It would help if you formatted your code correctly. There's a stray printf statement in there without any quotes around the %s\n, hence the error.

Resources