Re: abt variable storage like global static,auto ....



I don't think there are any rules about the storage of a variable
specified by the standard. What it says is that global and static
variables remain alive for the life of the program.They are initialised
before main() is called and get destroyed at the end. Implementations
use something called as the static memory to implement that.

Local variables are created where they are defined and gets destroyed
when they go out of scope. An implementation may use stack to realise
this. Talking about your code:

>#include <string.h>

>int global;
Global variable created at start and automatically initialized with 0

> main()
main() always has a return type int and nothing else.

int main()
>{
> int a ;
> int b;
2 local variables defined, created after main in called, uninitialized.
DANGER!!!

> increment(a);
You are trying to pass the value of a which is not known to a function,
Invoking undefined behaviour. Anyhing can happen from "looks like
working" to "formatting your hard disk".

>}
a and b get out of scope, destroyed.

>
>increment(int a)
A function taking argument by value. A copy of a will be passed.

functions missing return type anything and not declared, dafaults int.
You should always provide declaration for function if you don't want
your life to be miserable.

argument a is local to the function.

>{
> a++;
Value of a is not known, any operation on it leads to UB.
something happened with a, don't know.

>}
a goes out of scope, so destroyed, any changes to a are gone.

/PT

.



Relevant Pages

  • compiling kernel
    ... scripts/kconfig/qconf.h:51: error: `e' was not declared in this scope ... scripts/kconfig/qconf.h:73: error: `int updateList' redeclared as different ... scripts/kconfig/qconf.h:8: error: forward declaration of `class ConfigList' ... ConfigLineEdit' ...
    (alt.os.linux.suse)
  • Re: Toolbox initialise
    ... (the declaration and definition) ... where they are defining local variables and 'int' is one of C's defined types. ... using the typedef command somewhere in one of the header files that is being ...
    (comp.sys.acorn.programmer)
  • Re: Porting from visual C to gcc problem
    ... The declaration above defines the identifier 'DNIS_TIMER_CALLBACK' as ... of a structure type named 'struct DNS_TIMER_CB'. ... C is a scoped language, and one of the scopes is "prototype scope". ... int func; ...
    (comp.lang.c.moderated)
  • Re: Porting from visual C to gcc problem
    ... >The declaration above defines the identifier 'DNIS_TIMER_CALLBACK' as ... the compiler must have come across the typedef ... >C is a scoped language, and one of the scopes is "prototype scope". ... >int func; ...
    (comp.lang.c.moderated)
  • Re: scope and linkage rule, very confusing!
    ... >> function scope ... >> int f{ ... Labels that can be the targets of a goto statement, ... > both linkage and storage duration. ...
    (comp.lang.c)