Can you only declare variables at the beginning of a C script? Also, can anyone tell me how you declare a char array and how you copy things into that char array?
Many thanks.
Can you only declare variables at the beginning of a C script? Also, can anyone tell me how you declare a char array and how you copy things into that char array?
Many thanks.
You can declare them at the start of any opening brace.
If you're referring to your other post, there is no such thing as a variable length array in standard C.
So
char myArray[someComputedSize];
is not allowed.
The way to do this is
char *myArray = malloc( someComputedSize );
Don't forget to include stdlib.h to use malloc.
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.
Quick question on this. I'm new to C, and only just recently saw people saying that you can only declare variables at the beginning of blocks. I hadn't been doing this, and never experienced a problem. I did a little test and found the following code to compile and run fine. I'm using gcc, and I don't think it's using C99 because I can't do some other C99-ish things like for(int blah;...).
I'm sure there's an obvious explanation I'm missing; could someone please enlighten me?
(Here is my little goofy test code where I declare variables all over the place, in case you were wondering.)
Code:int main (int argc, const char * argv[]) { int blah; blah = 3 + 2; while (blah > 3) blah--; int poop; poop = 7; printf("%d\n", poop); int hi = 4; printf("%d\n", hi); return 0; }
Declaring variables in the middle of blocks is an extremely common compiler extension. So is for(int x; ...), but there you have it. Single-line comments are also a common C99 and C++ feature that are commonly implemented by C89 compilers.
If you really want to tell if something is ANSI-standard or not, enable strict ANSI compatibility.
Here's a recent thread about this: http://cboard.cprogramming.com/showthread.php?t=95876
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Thank you for clarifying
In this day and age, as someone learning C "recreationally" (that is, not professionally using it), how "evil" would it be to write C99 code?
GCC 4.1.2 compiles your code without warnings or errors with no options (I added #include <stdio.h> on top), but with the -pedantic option
gcc -pedantic foo.c
foo.c: In function ‘main’:
foo.c:8: warning: ISO C90 forbids mixed declarations and code
foo.c:11: warning: ISO C90 forbids mixed declarations and code
It's a good idea with GCC to compile with "-W -Wall -ansi -pedantic":
gcc -W -Wall -ansi -pedantic foo.c
foo.c: In function ‘main’:
foo.c:8: warning: ISO C90 forbids mixed declarations and code
foo.c:11: warning: ISO C90 forbids mixed declarations and code
foo.c: At top level:
foo.c:3: warning: unused parameter ‘argc’
foo.c:3: warning: unused parameter ‘argv’
Notice that 3 is a constant, so the size of a is not variable.
As in the C standard library is not available? I find that rather unusual. I am no expert, but my guess is that you probably cannot code in C to begin with and have to use the assembly language for that system.And what happens if you do not have a C library? (nor malloc)
Or... you get a better compiler.
Last edited by laserlight; 11-18-2007 at 03:05 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
yes, sorry, I have this bad habit to read to fast... I had understood the exact contrary.
well you can write your own libc functions without linking to the c library, even if of course something will be written in assembly.As in the C standard library is not available? I find that rather unusual. I am no expert, but my guess is that you probably cannot code in C to begin with and have to use the assembly language for that system.
Free standing implementations often come with no library at all, or a very rudimentary library specific to the hardware you're running on (say driving an attached LCD display).
Writing C for such systems is no different, except that you have to write everything yourself.
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.