Docsity

CS 449: Understanding Scope, Functions, Pointers, and Variables in C, Study notes of Computer Science

Toggle sidebar
bg1
1
CS 449 –Scope, Functions and
Pointers
David Kyle
Scope
•Scope: the region of code in which a symbol is
legal and meaningful
•Symbol: a name which represents a constant,
literal, or variable
•Scope serves two purposes:
–restrict symbols to logical divisions
–allow usage of same symbol for different purposes
Lifetime
•Lifetime: time from allocation to deallocation
of memory
•Scope is determined at compile time
•Lifetime is run-time, but two categories:
–Static: location and size of memory allocation
determined at compile time
–Dynamic: not static
Local Variables
•Are local variables static or dynamic?
•They are dynamic; we don't know how many
instances will be needed
•What about globals?
•They are static
Scope and Lifetime in C
•In C, scope is defined by:
–Files; be default all variables are limited to the file
they are declared in
–Blocks { }; a variable defined within a block has
scope limited to that block
•Variables of the same name may not coexist
within the same scope
Shadowing
•A variable may share the same name as
another in an enclosed scope
•This is called shadowing
int main() {
int shadowed = 4;
{
int shadowed = 6;
printf("%i\n",shadowed) /* prints 6 */
}
printf("%i\n",shadowed) /* prints 4 */
}
pf3
pf4

Partial preview of the text

Download CS 449: Understanding Scope, Functions, Pointers, and Variables in C and more Study notes Computer Science in PDF only on Docsity!

CS 449 – Scope, Functions and

Pointers

David Kyle

[email protected]

Scope

• Scope : the region of code in which a symbol is

legal and meaningful

• Symbol : a name which represents a constant,

literal, or variable

• Scope serves two purposes:

– restrict symbols to logical divisions

– allow usage of same symbol for different purposes

Lifetime

• Lifetime : time from allocation to deallocation

of memory

• Scope is determined at compile time

• Lifetime is run-time, but two categories:

– Static : location and size of memory allocation

determined at compile time

– Dynamic : not static

Local Variables

• Are local variables static or dynamic?

• They are dynamic; we don't know how many

instances will be needed

• What about globals?

• They are static

Scope and Lifetime in C

• In C, scope is defined by:

– Files; be default all variables are limited to the file

they are declared in

– Blocks { }; a variable defined within a block has

scope limited to that block

• Variables of the same name may not coexist

within the same scope

Shadowing

• A variable may share the same name as

another in an enclosed scope

• This is called shadowing

int main() { int shadowed = 4; { int shadowed = 6; printf("%i\n",shadowed) /* prints 6 / } printf("%i\n",shadowed) / prints 4 */ }

Global Variables

• Global variables have scope throughout the

entire program, and lifetime throughout the

execution of the entire program

• In C, variables declared outside functions have

file-scope – they have scope within all

functions in the file

• These variables are often called global

Automatic Variables

• Automatic variables are created and

destroyed automatically – usually at the start

and end of a block, respectively

• In practice, all of the variables in a function

are generally created at the start of the

• By default, all variables declared in functions

are automatic (have the auto keyword)

Static Global Variables

• A file-scope variable may be imported into

other files via the extern keyword

• If a file-scope variable has static in front of

it, extern may not be used on it

int x = 9; /* defined in one file / extern int x; / imported in another / int main() { printf("%i\n", x); / prints 9 */ return 0; }

Static Local Variables

• A variable declared static within a function

retains its value between executions

• It is essentially a global variable with scope

only in that function

int inc() { static int x = 0; return x++; }

Functions

• Functions in C are much like functions in Java

• Main difference: aren't part of classes

• Also, can separate declaration from definition

• A function can be declared with its prototype :

used in the program, the defined later:

int add(int l, int r); int add(int l, int r) { return l+r; }

Functions with no Arguments

• Unlike in Java, where you would define as:

you should use:

• The former actually defines a function which

can take any parameters

• It can allow logic errors which would

otherwise be syntax errors

int print_header(); int print_header(void);

Pointers and Arrays

• We can treat pointers as arrays:

int array[5]; int array_ptr = &array[0]; array_ptr[2] = 4; printf("%i\n",array[2]); / prints 4 */ int array_2nd_ptr = array_ptr + 2; printf("%i\n",array_2nd_ptr); (array_ptr + 2) = 6; printf("%i\n",array[2]); / prints 6 */

Command Line Arguments

int main(int argc, char *argv[]); … 0x argc 3 0x argv 0x4004 0x 0x100C … … 0x argv[0] 0x8000 0x argv[1] 0x8010 0x argv[2] 0x8020 0x400C … $ ./example arg1 arg "./example" "arg1" "arg2" printf("Program Name: %s\n", argv[0]); printf("First argument: %s\n", argv[1]);