C Programming Fundamentals - Variable Scope
Skills:
ML Maths Basics80%
Key Takeaways
The video explains variable scope in C programming, covering global and local variables, parameter passing, and return values, using tools like C, printf, and gcc.
Full Transcript
[Music] hey guys hackersploit here back again with another video and welcome back to the c programming series in this video we're going to be taking a look at variable scope now this is actually a video that you guys requested uh essentially wanting me to explain how the variable scope works in the sense of how do you declare global variables local variables and parameters or formal parameters as they are also known as so when we're talking about uh variable declaration and initialization let me just let's start off with that before we actually move on to local variables and global variables so when we talk about variable declaration and initialization it's as simple as as the following so when we're talking about declaring a valid so when we're talking about declaring a variable we have int a and we do not give it a value all right so that means it's uninitialized so if we try and print print it out or we try and use it in a function or use it in any in any type of way it's not going to have any value assigned to it now when it comes down to initialization this is when we say uh int a equals you know e into a equal to 20 or you know we give it a value all right so that is declaration and initialization now when we talk about local variables and global variables local variables are variables that are declared inside a particular function and cannot be used by other functions so let me give you a very simple example let me say int a equals 20. so i've initialized this variable and then i say printf or i can actually just use it like so and i create another function and i say int print and i do not pass any parameters through it and i say right over here printf i want to print out the the value of int a and i just say a here now this is not going to work because as mentioned this is a local variable meaning it is declared and initialized within a function and that means it cannot be used in another function so let me just compile and run this just to see just to show you what you'll you'll see so you can see right here if you take a look at the errors you get a a is undeclared and this is the first use in this function so this this variable here is locally declared and cannot be used in any other function all right so that is a very simple example of uh of a local variable of course if i well you might you might be wondering saying well you haven't really called that function so if i just call it just to show you that it does work so if i call the function or actually that is not printf that is simply print and we try and compile and run this you'll see you'll still get the same error all right so we can get rid of the function call and that is the simple example so this will work within uh the function in which the variable was declared and initialized so if i rebuild all and let me uh there we are there we are we get the value 20 all right so that works perfectly now we talk about global variables global variables are again very simple to understand these are variables that are declared and defined outside the functions at usually at the top when i do it over here and they can be accessed by any of the functions if you actually choose to use them within the functions all right so let me show you a simple example of how this goes so i'm just going to get rid of all of this data here and we'll still keep the other function and now i'll globally declare some variables it's usually done after you've included your standard libraries or your libraries sorry so when you talk about global variables let me just initialize a one more time and i give it a value of 50 for example all right so that is a global variable meaning i can use this variable right over here uh inside any of these functions and let me show you how that is done so i'll also declare and initialize some local variables here let me just add some comments so this is a global variable and let me just give it some spacing and then right over here we'll declare some local uh variables local variables and i'm going to say int b is equal to 30. all right and another thing that you guys wanted to know is uh the state of change of the variables value as the program is executed the program is executed from the top to the bottom and of course it starts off with the with the main function but what i'm trying to say is if if the value of int a over here is 50 and i can use it in any of the functions i can give it a new value at any time so i can say over here a is equal to all i can actually just say a and a is equal to a hundred all right and then if i try and print out the value right here printf and this is an example of how i can use this variable now in any of the function and you will see indeed it does work all right so this is uh of that data type it's an integer and we're printing out int a here so let's see if this does work all right so i'm just going to compile and run this and as you can see it's going to give us the new value of a hundred now if in this case we would have just said int a and not given it any value or simply just used it right over there let me just compile and run this you can see well we get the value of 44 which is really weird and i think i know what the issue is here so if we say for example intel let me just add this uh sorry there we are and let's just compile and run this and we get an error all right yes i know what the issue is and i'll get to that in a second so when it comes down to using any of the other variables inside any of the of the other functions let me explain how this is done so what i'm going to do is i'm just going to leave that as it is and i'll explain why in a second so that i can explain this really clearly also get rid of the print function so we have our global variable and our local variable so into aced is declared globally into b is declared within the main function so what i'm going to do now is if i go into the print function right over here and we say for example printf and we say that is going to be uh and we simply call the variable a right over here and we say print for example what you will see is well first of all you have to call the function which is quite important as you would imagine so print and like so all right so now if you are going to initialize the variable or use it with a different value in in a function and this i'm talking about global variables you need to give it a new value if you want to simply use it in a calculation you see you can simply call it directly as follows you do not have to initialize it one more time that was what i was trying to explain as you can see right over here it prints it out for us we get the value of int a which is 50. so the reason i was getting an error here is because i'd initialized it and given and given it a null value i had not given it any value and so it assumed that i'm i'm not i've given it a new p a new value because again as i mentioned it's declared all the values are given as the code is executed all right so that is uh those are global variables and of course i can use as many as i want so i can say in c is equal to you know 500 or we can yeah we can use 500 here and i can simply print out right before this i can say print f and i simply use the the d command there and oops sorry about that guys uh and i simply type out the variable c like so and that should execute the value of c first and then the value of uh and then the the value of a so let me just compile and run this and there we are we get 550 or there we are so we get what is printed out first and that's the important thing so if i would have declared this first uh the the uh if i would have declared or called the function before this print statement then i would have got the value of a all right so that is essentially global and local variable declaration and initializations all right so let me just close this all of these uh currently open programs here and now let's let's take a look at parameters all right parameters are extremely important in uh in c and in any other programming language really and you might have heard me mention that we declare our parameters uh within the function initialization all right so all within the function definition as it's so called so what i'm going to do is i'm just going to get rid of all of this data here or we can leave the variables as they are and we'll get rid of the print statements here so we're going to create a very simple program we have two variables we'll get rid of the c because i'm going to use that right now or we actually won't use it but i'll show you what i'm talking about so we have a global variable local variable i'm going to show you how we can use both of them so when you're talking about um when you're talking about performing calculations well one thing i can do is let me just rename this function here to sum and we're simply going to have a simple program that will add the values of a and b so within the main function i'm going to again declare another local variable and i'll show you where parameters come into play all right now the important thing with parameters is they are declared within the function definition and they get their values when the function is called so it's very important to understand that this will really depend on function calls all right so with that out of the way you can get started so what i'm going to do now is i'm going to create another variable called int total and i'm not going to give it any value all right now in total we're going to initialize after this and we're going to use it for the function call now that's very important because after we've called the function and then we we can also uh we can get the sum from uh from the uh from the function sum and once we have passed the parameters through it i'll show you what this means so for now we leave that as it is and when we talk about uh variable sorry parameters they are declared within here so we can call upon we can say int a we're going to use that within the function and we use a comma to separate it and now we say into b but you you must be wondering well why are we actually using parameter in b if it is a local variable variable and that would not work if this function was not called by this function right over here by the main function that actually has the value of b and this is where we actually perform our function called so i'll show you this right now so uh in here we can simply return the value of a and b which is pretty much what this function will do so you can choose to do the addition here or you can simply just return a plus b all right and that is going to return the value uh to uh and when the function is called back so now we can store the the return or the the the value uh of of this function and what it returns into a variable and that's what i'm going to i'm going to say total is equal to and the function called so sum and we specify uh the uh the variables here without their data type as we already know them because they've already been initialized and we do not need to actually initialize them once more that will cause or give you an error all right and now since we have the total we can then choose to print out the total so we can say printf and we can then specify the the data type there and we provide the variable itself which is total which stores the uh the value of the function right over here the function itself just returns the value of a and b all right so that should print out all the information we need so we are expecting an answer of 50 oh sorry 50 plus 30 that is 80. all right so let me just compile and run this and as you can see the value returned there is 80. and of course these can be changed uh the values can be changed so that you can experiment with it all right so that is pretty much what i wanted to cover in variable scope hopefully this clears out all the doubts that you had about uh variable declaration initialization local variables uh global variables and parameters so now that we've actually taken a look at parameters really really well we can now move on to memory management which is quite the exciting stuff so if you found value in this video please leave a like down below if you have any questions or suggestions let me know in the comment section on my social networks or on my website and i'll be seeing you in the next video peace you
Original Description
Hey guys! HackerSploit here back again with another video, in this video, I will be explaining variable scope in C programming.
⭐Help Support HackerSploit by using the following links:
🔗 NordVPN: https://nordvpn.org/hacker
Use the link above or the code below for 77% Off your order
Promo Code: hacker
Patreon: http://patreon.com/hackersploit
I Hope you enjoy/enjoyed the video.
If you have any questions or suggestions feel free to ask them in the comments section or on my social networks.
🔗 HackerSploit Website: https://hsploit.com/
🔹 Support The Channel
NordVPN Affiliate Link: https://nordvpn.org/hacker
Patreon: http://patreon.com/hackersploit
🔹 Get Our Courses
Get a special discount on our courses:
The Complete Deep Web Course 2018:
https://www.udemy.com/the-complete-deep-web-course-2017/?couponCode=DWCBP2017
🔹 SOCIAL NETWORKS - Connect With Us!
-------------------------------
Facebook: https://www.facebook.com/HackerSploit/
Twitter: https://twitter.com/HackerSploit
Instagram: https://www.instagram.com/hackersploit/
Patreon: http://patreon.com/hackersploit
--------------------------------
Thanks for watching!
Благодаря за гледането
Kiitos katsomisesta
Danke fürs Zuschauen!
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة
دیکھنے کے لیے شکریہ
देखने के लिए धन्यवाद
#Hacking#C#ExploitDevelopment
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from HackerSploit · HackerSploit · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How To Install Kali Linux 2.0 On Virtual Box
HackerSploit
100 Subscriber Q&A! - How I Learned Ethical Hacking
HackerSploit
BlackArch Linux Review - Better Than Kali Linux?
HackerSploit
How to Access the Deep Web Safely | Deep Web Starter Guide 1.0
HackerSploit
Wireshark Tutorial for Beginners - Installation
HackerSploit
Wireshark Tutorial for Beginners - Overview of the environment
HackerSploit
Wireshark Tutorial for Beginners - Capture options
HackerSploit
Wireshark Tutorial for Beginners - Filters
HackerSploit
Complete Ethical Hacking Course - Become a Hacker Today - #1 Hacking Terminology
HackerSploit
Complete Ethical Hacking Course #2 - Installing Kali Linux
HackerSploit
Parrot OS 3.5 Review | The Best Kali Linux Alternative
HackerSploit
Nmap Tutorial For Beginners - 1 - What is Nmap?
HackerSploit
Katoolin | How To Install Pentesting Tools On Any Linux Distro
HackerSploit
Nmap Tutorial For Beginners - 2 - Advanced Scanning
HackerSploit
Nmap Tutorial For Beginners - 3 - Aggressive Scanning
HackerSploit
Zenmap Tutorial For Beginners
HackerSploit
How To Setup Proxychains In Kali Linux - #1 - Stay Anonymous
HackerSploit
How To Setup Proxychains In Kali Linux - #2 - Change Your IP
HackerSploit
How To Change Mac Address In Kali Linux | Macchanger
HackerSploit
How To Setup And Use anonsurf On Kali Linux | Stay Anonymous
HackerSploit
Ubuntu 17.04 "Zesty Zapus" Review - Bye Unity
HackerSploit
VPN And DNS For Beginners | Kali Linux
HackerSploit
Tails OS Installation And Review - Access The Deep Web/Dark Net
HackerSploit
Steganography Tutorial - Hide Messages In Images
HackerSploit
The Lazy Script - Kali Linux 2017.1 - Automate Penetration Testing!
HackerSploit
Best Linux Distributions For Penetration Testing
HackerSploit
Netcat Tutorial - The Swiss Army Knife Of Networking - Reverse Shell
HackerSploit
Gaining Access - Web Server Hacking - Metasploitable - #1
HackerSploit
Web Server Hacking - FTP Backdoor Command Execution With Metasploit - #2
HackerSploit
How To Install Kali Linux On VMware - Complete Guide 2018
HackerSploit
Q&A #1 - Best Cyber-security Certifications?
HackerSploit
Terminator - Kali Linux - Multiple Terminals
HackerSploit
Shodan Search Engine Tutorial - Access Routers,Servers,Webcams + Install CLI
HackerSploit
Q&A #2 - Mr Robot?
HackerSploit
Metasploit Community Web GUI - Installation And Overview
HackerSploit
Linux Expl0rer - Forensics Toolbox - Installation & Configuration
HackerSploit
QuasarRAT - The Best Windows RAT? - Remote Administration Tool for Windows
HackerSploit
Metasploit For Beginners - #1 - The Basics - Modules, Exploits & Payloads
HackerSploit
Metasploit For Beginners - #2 - Understanding Metasploit Modules
HackerSploit
Kali Linux Quick Tips - #1 - Adding a non-root user
HackerSploit
Metasploit For Beginners - #3 - Information Gathering - Auxiliary Scanners
HackerSploit
Spectre Meltdown Vulnerability - How To Check Your System
HackerSploit
Metasploit For Beginners - #4 - Basic Exploitation
HackerSploit
ARP Spoofing With arpspoof - MITM
HackerSploit
WordPress Vulnerability Scanning With WPScan
HackerSploit
Generating A PHP Backdoor with weevely
HackerSploit
Nikto Web Vulnerability Scanner - Web Penetration Testing - #1
HackerSploit
How To Install Kali Linux On Windows 10 - Windows Subsystem For Linux
HackerSploit
Stacer - System Optimizer And Monitoring Tool For Linux
HackerSploit
Kali Linux 2018.1 - Kernel Updates & Patches
HackerSploit
MITM With Ettercap - ARP Poisoning
HackerSploit
Password Cracking With John The Ripper - RAR/ZIP & Linux Passwords
HackerSploit
How To Detect Rootkits On Kali Linux - chkrootkit & rkhunter
HackerSploit
Channel Updates - How To Post Questions & Video Suggestions
HackerSploit
Web App Penetration Testing - #1 - Setting Up Burp Suite
HackerSploit
Web App Penetration Testing - #2 - Spidering & DVWA
HackerSploit
Cl0neMast3r - GitHub Repository Cloning Tool
HackerSploit
Kali Linux On Windows 10 Official - WSL - Installation & Configuration
HackerSploit
DoS/DDoS Protection - How To Enable ICMP, UDP & TCP Flood Filtering
HackerSploit
Web App Penetration Testing - #3 - Brute Force With Burp Suite
HackerSploit
More on: ML Maths Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
The Billion Dollar Business of Making You Forget Passwords
Medium · Cybersecurity
Your ChatGPT History Is a Liability. I Fixed That With a $80 Chip and a Pi5.
Medium · Cybersecurity
Aikido buys Root to patch open source in place, without the upgrade dance
Dev.to · Leo
5G Security: Why Most Operators Are Underprepared for the Threats Standalone Architecture Introduces
Dev.to · 5gwolrdpro
🎓
Tutor Explanation
DeepCamp AI