ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Two C question: definition of veriable and key word "static"
Question 1.
To my very limited knowledge of C lang, in a func, the veriable must definition before any operation, right?
that is to say, this code is wrong:
Quote:
#include <stdio.h>
int
main ( int argc, char* argv[ ] )
{
int a = 1;
int b = 2;
a = a + b;
int c;
printf ("Hello,world!\n") ;
return 0;
}
Can some compile flag make gcc compile this code successfully, and the bin can be runned flawless?
Question 2.
Why make function static?
Such as:
Quote:
static void foo(void);
Last edited by powerplane; 04-30-2003 at 05:21 AM.
Q1) I dont think there is a gcc flag to avoid this error. There is one way though, i dont know if it would suit you. You simply change the the extention of your '.c' files to '.cpp' and use gcc to compile it. Then you wont get errors for those declarations because the compiler will treat them as c++ files.
Q2) If you declare a function 'static' then the scope of this function will set to this file only. Only other functions in the same file will be able to call it. You will not be able to call this function from other files by declaring its prototype in header files etc.
Thanks,yrraja. I finally figure out.
I did some experiment, and here is my found:
I will give some example to illustrate.
Allowed
Quote:
Code:
int
foo_a(void)
{
int a =100;
a++;
{
int b;
}
}
int
foo_b(void)
{
int a=100;
int b=200;
a++;
{
int b=50;
printf("%d, %d\n", a, b); /*print outside a,100 and "inner" b, 50*/
}
printf("%d\n",b); /*print, "outside" b,200 */
}
Not Allowed
Code:
int
bar_a(void)
{
int a =100;
a++;
int b;
}
int
bar_b(void)
{
int a;
{
a++;
int b; /*operation before definition*/
}
}
int
bar_c(void)
{
{
int a = 100;
}
print(a); /*wrong, the outside a is not declared */
}
That means definite a variable in a "if() { }", "for() {} ", "while() {}",etc, is allowed, but must put at the very beginning in the scope of "{}", before any operation.
Strictly speaking, the latest ANSI C standard does allow variables to be declared in the middle of a function (and local to for loops, etc.), but gcc 2.x doesn't implement this standard, so it's not a good idea to take advantage of it yet.
Yes powerplane your experiments are absolutely correct. You can declare variables at the begining of a scope. But only thing that you need to keep into consideration is that you cannot use this variable after the closing bracket of this scope.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.