The trouble here is that I can't declare variables inside a function after the function already has some statements in it. Declaring at the start works fine, but after something, it gives a parse error. For example:
int main()
{
int b;
b = sisesta();
float st[b];
return 0;
}
I'd like to declare an array st with its size being returned by another function, but it won't let me do it! Says "Parse error before float". This is in C by the way, but I guess its identical to what it would be in other languages with the same syntax.
Any help appreciated.
In C standards before C99, you have to declare your local variables at the beginning of the function. Beginning with C99, this is no longer required.
Since Dev-C++ ships with gcc and recent gcc versions do support C99 partially, you can try adding -std=c99 to the gcc argument list in the Dev-C++ settings to trigger C99 mode.
Dude in C you have to declare all variables at the start. You can't declare between statements
you could malloc() a float* to the size you want (just remember to free() it afterwards):
int main()
{
int b;
float *st;
b = sisesta();
if((st = malloc(sizeof float * b)) == NULL){exit 1;}
/* blah blah */
free(st);
return 0;
}
It turns out that I just had an old version of DevC++ which didnt support the newer standard, with the latest release the statements work fine, thanks for the help anyway.
Even in C89, it was just a stylistic choice to do all declarations at the beginning of a function - the trouble that you hit in your code was that you attempted to declare an array on the stack of an unknown size, and that was not allowed until C99. If you were to do the same code, but replace "float st[b]" with a statement where "b" was constant, it would work, like "float st[10]"
Related
I'm new to c and I am having a hard time understanding why am I getting an error while trying to compile the following code in c I believe I tried it Java and it worked compiled perfectly without error
void f(void) {
int i;
i = 6;
int j;
j = 20;
}
In "old" C all declarations must be at the top of the functions. In later versions like C99, C declarations can be anywhere in the code. I guess you have an old compiler.
Change your code to
void f(void) {
int i;
int j;
i = 6;
j = 20;
}
The problem is that for some old compilers you need to declare the variables before any executable statement. If you do not want to have this problem, switch to a newer compiler.
If your compiler is configured to compile c code following c98 then you will get this error because following c98 standard the déclaration of variables must be done first then we can make assignment so we can't make declaration of variable in the middle of the code.
However you can select the option to compile your code following the standard c99 and in this case you can make the déclaration of variable in the middle of your code.
I wrote a simple code where I am creating array without a fixed size. I tried compiling the code in gcc and it is working fine. Please explain why this is working array size should be supposed to be known at compile time.
Here's the code I have used.
void f(int k)
{
int a[k];
.....//some operation
}
int main()
{
int i = 10;
f(10);
return 0;
}
This feature is known as VLA or variable length array. This is not supported in all C standards. In recent C standards like C11 and C99, it is supported, but not in older C Standards as 'C89'.
If you're using gcc, please have a look at the compiler documentation regarding this.
I'm hoping someone can help me out with this. I'm a Linux & Eclipse noob, but I'm pretty familiar with C/C++, though its been a while since I've used them. When I try to compile I get strange errors. No matter what I do to fix them they don't seem to go away.
You can see the there's a simple main function with a little bit of code. There's only 15 lines of code but if you look at the errors they are in external libraries, stdio.h. In main it says there's one error at line 11 but that one doesn't make sense. I assume it's an Eclipse settings problem, but I have no idea what to do to fix it. Any help would be very appreciated. By the way I'm using SciLinux and Eclipse Indigo Service Release 2. Thanks
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *ptr;
int a;
a = 20;
ptr = &a;
int b;
b = *ptr;
printf(" ptr is %d\n",b);
return 0;
}
int *ptr;
int a;
int b; //<- move to block top declaration
a = 20;
ptr = &a;
Some of the previous compiler have this weird problem relating to C they only accept variables which are declared in the beginning of the function.
So most probably the error is because you have not declared the variable b at the starting of the block , i suggest you try using a different compiler or be prepared to declare all the variables at the beginning.
As other answers say, mixing code and declarations is illegal in old fashioned plain C. See:
Variable declaration placement in C
How to enforce C89-style variable declarations in gcc?
In eclipse, the standard version used will depend on the compiler flags passed to the C compiler gcc: either -std=c89 or -std=c99. Depending on how the project is set up, will either be in the Eclipse project properties or a Makefile.
Folks I think I will throw all my modest C lore away. Look at this code:
int main(int argc, char** argv, char** envp)
{
int aa;
srand(time(NULL));
int Num = rand()%20;
int Vetor[Num];
for (aa = 0; aa < Num; aa++)
{
Vetor[aa] = rand()%40;
printf("Vetor [%d] = %d\n", aa, Vetor[aa]);
}
}
I would think that this should throw an error for two reasons - first that I am declaring both Num and Vetor after executing a command (srand), second because I am declaring Vetor based on Num, this should not be possible right? because those array sizes should not be decided at runtime but at compile time right?
I am really surprised that his works and if you guys could explain why I can actually use stuff like this would be great.
This is using GCC.
These are C99 features, and it seems your compiler supports them. That's all ;)
From Wikipedia:
C99 introduced several new features, many of which had already been implemented as extensions in several compilers:
inline functions
intermingled declarations and code, variable declaration no longer
restricted to file scope or the start
of a compound statement (block)
several new data types, including long long int, optional extended
integer types, an explicit boolean
data type, and a complex type to
represent complex numbers
variable-length arrays
support for one-line comments beginning with //, as in BCPL or C++
new library functions, such as snprintf
etc (more)
C99 supports declarations anywhere in the code, as well as VLAs. What compiler are you using?
Can someone elaborate on the following gcc error?
$ gcc -o Ctutorial/temptable.out temptable.c
temptable.c: In function ‘main’:
temptable.c:5: error: ‘for’ loop initial declaration used outside C99 mode
temptable.c:
...
/* print Fahrenheit-Celsius Table */
main()
{
for(int i = 0; i <= 300; i += 20)
{
printf("F=%d C=%d\n",i, (i-32) / 9);
}
}
P.S: I vaguely recall that int i should be declared before a for loop. I should state that I am looking for an answer that gives a historical context of C standard.
for (int i = 0; ...)
is a syntax that was introduced in C99. In order to use it you must enable C99 mode by passing -std=c99 (or some later standard) to GCC. The C89 version is:
int i;
for (i = 0; ...)
EDIT
Historically, the C language always forced programmers to declare all the variables at the begin of a block. So something like:
{
printf("%d", 42);
int c = 43; /* <--- compile time error */
must be rewritten as:
{
int c = 43;
printf("%d", 42);
a block is defined as:
block := '{' declarations statements '}'
C99, C++, C#, and Java allow declaration of variables anywhere in a block.
The real reason (guessing) is about allocating internal structures (like calculating stack size) ASAP while parsing the C source, without go for another compiler pass.
Before C99, you had to define the local variables at the start of a block. C99 imported the C++ feature that you can intermix local variable definitions with the instructions and you can define variables in the for and while control expressions.