error: expected identifier or '(' before '{' token| [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 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)
}

Related

why are my returns values adding up when i have multiple if statements? 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 10 months ago.
Improve this question
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int compare (int a, int b, int c, int d)
{
if (a > b){return 1;}
else if (a < b) {return -1;}
else if (a == b){ if (c > d){return 1;}
else if (c < d) {return -1;}
else if (c == d) {return 0;}}
}
int main(void) {
int r = compare(11, 2, 2, 1);
printf("r: %d\n");}
when I print the function result I get a result of 2 or 4 I don't know what I'm doing incorrectly. this should be simply 1, -1 or 0
printf("r: %d\n"); fails to pass any value for printf to print. It should be printf("r: %d\n", r);.
Enable warnings in your compiler and elevate warnings to errors. With Clang, start with -Wmost -Werror. With GCC, start with -Wall -Werror. With MSVC, start with /W3 /WX.

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.

MsVc++ weird error: not declared variable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The compiler is rising this error:
1>....\server\sv_init.c(528): error C2143: syntax error : missing ';' before 'type'
1>....\server\sv_init.c(529): error C2065: 'v' : undeclared identifier
...(all instruction lines containing v)
here is a portion of the code :
while(shl>=7) {
shl-=7;
int v = (sh>>shl)&127;// <-- Error is here
if (v==0 || v=='"' || v=='%' || v=='#') {
tmp[ol++] = '#';
// Com_Printf("OUT:%02X\n",tmp[ol-1]);
if (ol==sizeof(tmp)-1) {
tmp[ol]=0;
if (csnr==PURE_COMPRESS_NUMCS) {
Com_Printf(err_chunk);
return 1;
}
SV_SetConfigstring( MAX_CONFIGSTRINGS-PURE_COMPRESS_NUMCS+csnr, tmp);
csnr++;
ol=0;
}
tmp[ol++] = v+1;
} else {
tmp[ol++] = v;
}
I tried to remove the line before the error line the code build fine, any help or suggestion would be thankfully welcomed.
Your code is being compiled in C mode (since the file has a .c extension). In C mode you can't declare variables after statements. You have three options:
Change the code to declare variables at the start of the enclosing scope like this:
while(shl>=7) {
int v; // declaration
shl-=7;
v = (sh>>shl)&127;
This may be a lot of work if there is a lot of code like this.
Compile in C++ mode by changing the file extension to .cpp or specifying the /TP option. This may work because C++ is mostly a superset of C, but there a few C constructs that C++ doesn't allow.
Use a different compiler that does support this feature in C, such as GCC.

syntax error near unexpected token '(' in C 7 [duplicate]

This question already has an answer here:
Syntax error near unexpected token '(' when using terminal [closed]
(1 answer)
Closed 7 years ago.
I am having problem with the following code
#include <stdio.h>
int main()
{
int tall = 175;
int weight = 68;
printf("I am %d slim.\n", tall);
printf("I am %d kilogram.\n", weight);
return 0;
}
i have created these code following a course but i have an issue (The Compiler said that )
./file.c: line 3: syntax error near unexpected token `('
./file.c: line 3: `int main()'
for more information I am using GCC in linux destro ..
and i am using terminal To run ...
there's No IDE
It seems that you are not compiling your program, but that you are trying to execute it without compiling. The errors that you see is /bin/bash that tries to interpret this as shell code.

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