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.
Related
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.
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));
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)
}
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.
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
Really odd error I am getting at the moment,
In my #include section, I have #include <stdio.h> which according to the man page for fopen is required
"SYNOPSIS
#include <stdio.h>"
I am also using fdopen which is in the stdio.h in the same code as well and it is working
fp = fdopen(fdes, "r+");
The error points to the following code
fout = fopen(fname, "wb");
With the full error being
getFileCli.c:94:12: error: called object ‘fopen’ is not a function or function pointer
fout = fopen(fname, "wb");
^
getFileCli.c:22:8: note: declared here
FILE *fopen;
now if I use the code from the fopen header of FILE *fopen(const char *path, const char *mode); in the main section it works perfectly fine.
I ran a locate on my system
locate stdio.h
/usr/include/stdio.h
/usr/include/c++/4.9/tr1/stdio.h
/usr/include/x86_64-linux-gnu/bits/stdio.h
/usr/lib/x86_64-linux-gnu/perl/5.20.2/CORE/nostdio.h
So I have it, any reason why the include is not working?
As you compiler tries to tell you, you declared fopen to be a variable of type FILE*:
getFileCli.c:22:8: note: declared here
FILE *fopen;
And since your variable is neither a function nor a function-pointer, you can't call it:
getFileCli.c:94:12: error: called object ‘fopen’ is not a function or function pointer
fout = fopen(fname, "wb");
^
To fix it, remove the declaration FILE *fopen; in line 22 or rename the variable.
I think what you want to write FILE *fout; instead of FILE *fopen;