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.
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 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 needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm completely new to programming. I'm actually learning from a Harvard class on iTunes U. When trying to code along side the instructor I've ran into a problem. I can't run my .c program in terminal. I've tried the sudo commands, and I've searched with Google and I can't seem to find an answer, probably because I'm so new to programming. It's probably something I've overlooked or I just don't understand yet.
#include <stdio.h>
int
main(void)
{
printf("temperature in f: ");
float f = GetFloat();
float c=f / 9.0 * (f-32);
Printf("%. if F = %. if c\n", f, c)
I'm using Sublime text editor on a MacBook with Mac OS X Yosemite (10.10.x).
Your compiler should have warn you about some errors:
// string.c
#include <stdio.h>
int main(void) // There must be a return statement
{
printf("temperature in f: ");
float f = GetFloat();
float c = f / 9.0 * (f-32);
printf("%f if F = %f if c\n", f, c); // Missing ';' is the most common syntax error
return 0; // Here is the return
} // Do not forget to close your brackets
When you do:
gcc string.c -o myprogram
It will tell you what is wrong in your program. Once you have fixed all the errors you can run the program with:
./myprogram
Understand that you cannot run a C-file: the .c contains human-readable instructions for the machine. You have to compile it, i.e. translate it into a language that your computer will understand : it is roughly your compiled myprogram (and you do not want to open it to look what it contains, it will burn your eyes :p).
Just adding to the below answer your compiler should throw the below error also:
undefined reference to `Printf'
check case of your printf()
I am using Microsoft Visual Studio 2010 to write a C code. This is the piece of code that I defined :
#define setImagVal_Matrix(matrix,type,x,y,val) \
(getImagVal_Matrix(matrix,type,x,y) = (val))
Then I am using it inside this function :
for(bands=0; bands < no_of_bands; bands++) {
outputmatrix[bands] = new_Matrix(yrange,xrange,getDataType_Image(inputImage),getDataFormat_Image(inputImage));
for(r=0; r < no_of_rows; r++) {
for(c=0; c < no_of_cols; c++) {
if(c<x1 || c>x2 || r<y1 || r>y2)
{
continue;
}
else
{
setImagVal_Matrix(outputmatrix[bands],getDataType_Matrix(outputmatrix[bands]),c-x1,r-y1,123);
}
}
}
}
However, it shows me this error on setImagVal_Matrix function call:
"Error:expected an expression"
And when I build the solution , here is the output which shows a syntax error on the same line:
1>c:\cviplab-net-2010\cviplab\crop.c(50): error C2059: syntax error : ')'
After spending couple of hours, I still cannot find the cause of the error . Any idea how to fix it?
EDIT:
I analyzed the pre-processed file and found the syntax error but still I don't know how to fix it. Here is the line which makes the error :
((((((outputmatrix[bands])->data_type) **)((outputmatrix[bands])->iptr))[r-y1][c-x1]) = (123));
the error is for the ) after **
Just stop using macros as functions. Try this:
inline void setImagVal_Matrix(int matrix, int type, int x, int y, int val) {
getImagVal_Matrix(matrix, type, x, y) = val;
}
Change the int types as appropriate, and you'll get the compiler helping you with useful error messages instead of cryptic ones. Heck, it'll even help you figure out the argument types.
Within your macro, you do an assignment to something that does not look like an lvalue. Do you mean ==?
I would like to propose at least to use brackets for the macro arguments:
#define setImagVal_Matrix((matrix),(type),(x),(y),(val)) \
(getImagVal_Matrix((matrix),(type),(x),(y)) = (val))
Also, to see if it is really bracket issue, you can try to look at preprocessed file.
But, actually using macro for this purpose is not a good one practice. If you care about function call you can make the function inline:
inline void setImagVal_Matrix(matrix,type,x,y,val) {
getImagVal_Matrix(matrix,type,x,y) = val;
}
In spite of the fact that inline is only recommendation for the compiler, this will allow to avoid a lot of compilation errors and hardly debugging run-time errors.
Since yesterday, I've been facing a compiling error for my C project. The project itself consists on creating a service that will make some tasks.
I don't what has changed since yesterday, but this morning, my code can't compile anymore.
Here are the errors I have :
c:\path\main.c(56): error C2275: 'SERVICE_TABLE_ENTRY' : illegal use of this type as an expression
c:\program files\microsoft sdks\windows\v7.0a\include\winsvc.h(773) : see declaration of 'SERVICE_TABLE_ENTRY'
c:\path\main.c(56): error C2146: syntax error : missing ';' before identifier 'DispatchTable'
c:\path\main.c(56): error C2065: 'DispatchTable' : undeclared identifier
c:\path\main.c(56): error C2059: syntax error : ']'
c:\path\main.c(57): error C2065: 'DispatchTable' : undeclared identifier
c:\path\main.c(57): warning C4047: 'function' : 'const SERVICE_TABLE_ENTRYA *' differs in levels of indirection from 'int'
c:\path\main.c(57): warning C4024: 'StartServiceCtrlDispatcherA' : different types for formal and actual parameter 1
Here's the code concerned by these errors (from lines 45 to 58) :
int main(int ac, char *av[])
{
if (ac > 1)
{
if (!parse_args(ac, av))
{
aff_error(ARGUMENTS);
return EXIT_FAILURE;
}
}
SERVICE_TABLE_ENTRY DispatchTable[] = {{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
StartServiceCtrlDispatcher(DispatchTable);
return EXIT_SUCCESS;
}
And here's the code of my ServiceMain function :
void WINAPI ServiceMain(DWORD ac, LPTSTR *av)
{
gl_ServiceStatus.dwServiceType = SERVICE_WIN32;
gl_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
gl_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
gl_ServiceStatus.dwWin32ExitCode = 0;
gl_ServiceStatus.dwServiceSpecificExitCode = 0;
gl_ServiceStatus.dwCheckPoint = 0;
gl_ServiceStatus.dwWaitHint = 0;
gl_ServiceStatusHandle = RegisterServiceCtrlHandler(MY_SERVICE_NAME, ServiceCtrlHandler);
if (gl_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
return;
gl_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
gl_ServiceStatus.dwCheckPoint = 0;
gl_ServiceStatus.dwWaitHint = 0;
SetServiceStatus(gl_ServiceStatusHandle, &gl_ServiceStatus);
}
I couldn't manage to find some answers that fit my problem, could anyone helps ? Thanks !
When you name your source files *.c, MSVC assumes it's compiling C, which means C89. All block-local variables need to be declared at the beginning of the block.
Workarounds include:
declaring/initializing all local variables at the beginning of a code block (directly after an opening brace {)
rename the source files to *.cpp or equivalent and compile as C++.
upgrading to VS 2013, which relaxes this restriction.
You might be using a version of C that doesn't allow variables to be declared in the middle of a block. C used to require that variables be declared at the top of a block, after the opening { and before executable statements.
Put braces around the code where the variable is used.
In your case that means:
if (ac > 1)
{
if (!parse_args(ac, av))
{
aff_error(ARGUMENTS);
return EXIT_FAILURE;
}
}
{
SERVICE_TABLE_ENTRY DispatchTable[] = {{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
This error occurred when transferring a project from one installation to another (VS2015 => VS2010).
The C code was actually compiled as C++ on the original machine, on the target machine the "Default" setting in Project Properties\C/C++\Advanced\Compile as was somehow pointing to C even though the source file was of type *.cpp.
In my small program, errors popped up regarding the placement in code of certain types e.g. HWND and HRESULT as well as on the different format of for loops , and C++ constructs like LPCTSTR, size_t, StringCbPrintf and BOOL. Comparison.
Changing the "Compile as" from Default to Compile as C++ Code (/TP) resolved it.
This will also give you "illegal use of this type as an expression".
WRONG:
MyClass::MyClass()
{
*MyClass _self = this;
}
CORRECT:
MyClass::MyClass()
{
MyClass* _self = this;
}
You might be wonder the point of that code. By explicitly casting to the type I thought it was, when the compiler threw an error, I realized I was ignoring some hungarian notation in front of the class name when trying to send "this"
to the constructor for another object. When bug hunting, best to test all of your assumptions.