declare

C Programming Tutorial - 55 - Global vs Local Variables



Sharing buttons:

but pakka the only thing I've ever

wanted to do was learn about global and

local variables that's what everyone

always tells me you know since they're

from you know England or ASHRAE whatever

the heck the accident was so to all the

people who asked that let me go ahead

and teach it to you right now so there

are two main types of variables or

scopes of variables that we're going to

learn this tutorial one global to local

what's the difference between them

well don't let the words global and

local confuse it because I'm going to

explain it real quick this is the rule

for using variables in your program

whenever you make a variable like int

tuna outside and above all of your

functions any of your functions are

allowed to use that variable so main can

use tuna just like a normal variable

print something can use tuna just like

normal so when it's above your functions

any function can use it now

whenever you make it inside a function

only the function that it's created

inside can use it so for example if

print something wanted to use tuna it

wouldn't be allowed to okay so when it's

inside a function only that function can

use it when it's outside any function

can use it now just so it sticks in your

brain let me go ahead and make like a

two second example so enough of the tuna

let's feel like words so we'll set words

that how many words do I have 23 and

that's only on my forehead believe it or

not true story 23 big one size of

baseballs alright

so remember I told you that anytime you

make outside any function can use it so

let's do it right now so printf

I have its integer so percent D warts

and might as well kick that to newline

and for this we're going to put warts

looking good and also I got rid of the

print something so right now if we just

ran this arm print something would never

be called and that's remember that told

you that whenever you want to use the

function I'll put the name parentheses

and then a semicolon and that pretty

much goes down to here and cause

whatever's in there well whenever I say

calls it's just a technical term for

using the function so you know just a

some little programming lingo so right

now I'm going to call print something

and we'll see if that can use words as

well all right so I have 23 warts I have

23 warts it was able to be used from

each of these functions so what happens

if we accidentally made it right in here

well what would happen is it would say

okay I am main in this warts variable

belongs to me and me alone no one else

can use it

so when print something tries to use

warts right here and check this out if

we look it said on a clear Devender and

a little I can't even refer can talk

pretty much it's saying hey dude we

don't have a words variable and there's

none out here to use so you can't use it

and again one last time whenever a

variable is declared out here it's

called a global variable and whenever

it's created inside a function it's

called a local variable local to that

function so now you know about global

and local variables congratulations of

see you later