LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2003, 05:08 AM   #1
powerplane
LQ Newbie
 
Registered: Apr 2003
Location: P.R. China
Distribution: FreeBSD 4.8 stable
Posts: 26

Rep: Reputation: 15
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.
 
Old 04-30-2003, 07:56 AM   #2
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
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.


Yaser
 
Old 04-30-2003, 08:09 AM   #3
powerplane
LQ Newbie
 
Registered: Apr 2003
Location: P.R. China
Distribution: FreeBSD 4.8 stable
Posts: 26

Original Poster
Rep: Reputation: 15
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.
 
Old 05-01-2003, 05:07 AM   #4
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
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.

Alex
 
Old 05-01-2003, 10:59 PM   #5
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
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.

Yaser
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need a Definition dudeman41465 Linux - Software 2 11-07-2005 05:07 AM
Red Hat 9 h.c.l. modem definition question. Hal Linux - Hardware 2 01-16-2004 04:07 PM
multiple definition ? lackluster Linux - Software 1 11-27-2003 05:29 PM
New terminal definition gdeckler Linux - Newbie 1 10-06-2003 11:38 PM
Alias definition question Dark_Helmet Linux - General 5 02-04-2003 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration