if you run it in devc++ it will work.Code:#include <stdio.h> int main() { int bil; scanf("%d", &bil); int kotak[bil]; return 0; }
can someone explain why this can be happening?
my lecturer also doesn't know about this
if you run it in devc++ it will work.Code:#include <stdio.h> int main() { int bil; scanf("%d", &bil); int kotak[bil]; return 0; }
can someone explain why this can be happening?
my lecturer also doesn't know about this
C99, VLA ( variable length arrays ). gcc supports ( in some way ) VLA's for quite a while already.
Kurt
If you use the -pedantic option with gcc, it will issue the following warnings (see gcc's documentation):
foo.c: In function ‘main’:
foo.c:9: warning: ISO C90 forbids variable-size array ‘kotak’
foo.c:9: warning: ISO C90 forbids mixed declarations and code
C99 also allows you to write ugly code variable declarations throughout your program, instead just at the beginning.
Quzah.
Hope is the first step on the road to disappointment.
Can someone explain me in a very easy explanation? I don't get it at all..
If I'm able to use that then I don't have to use malloc again?
Like has been said already, it's a feature new in C99.
But most everyone uses C89 if they want to write portable code.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
No I'm afraid it's not a replacement for malloc. I'm pretty sure a variable sized array inside a function will be allocated on the stack (hence of limited size). But even worse if you should return a pointer to this array, the pointer would be invalid and writing to it would likely crash your program. It's kind of equivalent to the old alloca() function I think.
However if you only need the array for your current function then it is pretty nice. The array will get 'freed' when the function returns (by virtue of your stack pointer being restored to the callers context.). So you need never call free in that case. Now trying to find a fully compliant C99 compiler on the major platforms though might be an issue. Thus C99 code could not be considered particularly portable.
What's the politics behind it taking so long for compilers to support C99? Didn't C++98 have reasonably complete support years ago? And is gcc progressing towards complete support in the near future? I know it can be run in (not fully implemented) C99 mode with -std=c99.
For microsoft they probably feel there is not enough of a market to make it worthwhile implementing it. Their flagship IDE supports C++ and then some of their .net languages like C#. The only real hope is for C++ to decide to support C99 as a subset of the language.
Frankly there are very few C99 compliant compilers out there and none of them are free and maybe only one of them runs on windows. gcc claims to be close, however the two most important changes for scientific programming, variable length arrays and complex numbers are either not supported or broken for gcc
http://gcc.gnu.org/c99status.html
.
C99's strength is really in the scientific computing area, which I guess compiler writers see no use for.
VLA's are essentially useless candy IMO.
The biggest problem is what happens if your VLA no longer fits on the stack (assuming that is where VLAs get created on a given machine). Even the best scenario results in the program being killed by the OS. On an embedded system, it probably just trashes someone else's memory. Nor is there a way of finding out in advance whether it will succeed or not, just close your eyes, pray and take a jump.
Of course, a lot of naive programmers are going to take unchecked user input, and create their VLA directly from that. Most of them won't even bother to check for negative numbers, nevermind huge positive numbers.
At least with malloc, there is a clearly defined way of being told there is no more room, and from there you can take appropriate action.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
uh... you mean to say *fully* compliant, right? because:
Quincy 99
Code::Blocks
Dev-C++
Pelles C
are all free, and all run on Windows.
If you're not fully compliant, then you're not compliant. Don't they teach people anything in school any more? Didn't you ever learn that "in a true or false question, if any of it is false, then the whole thing is false"? Thus, is it compliant? No.
Quzah.
Hope is the first step on the road to disappointment.
Compliance in so much as it pertains to the more notorious of the C99 standards, such as those that make it possible for the OP's code to compile. In this context, the qualifier "fully" makes it clearer as to whether it complies with every C99 standard, as it is not uncommon to refer to a compiler as C99 even though it supports only the majority of these standards. It's a shame the english language isn't completely boolean quantifiable huh?