Linux C Programming Success Essentials For Absolute Beginners | Cary Huang | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Linux C Programming Success Essentials For Absolute Beginners

teacher avatar Cary Huang, multidisciplinary online entrepreneur

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

64 Lessons (10h 3m)
    • 1. Introduction

    • 2. Course Outline

    • 3. Set up Ubuntu 18.04 Linux Environment

    • 4. Set up Eclipse IDE for C/C++

    • 5. How Compiler Works

    • 6. Import and Create New Project

    • 7. Basic Element - Source and Header Files

    • 8. Basic Element - The Comments

    • 9. Basic Element - The Semicolon

    • 10. Basic Element - The include

    • 11. Basic Element - The Preprocessor

    • 12. Basic Element - The Makefile

    • 13. Create Your First Function

    • 14. Basic IO with "printf" and "scanf"

    • 15. Add Command Line Arguments

    • 16. If Statement

    • 17. Types of Loops

    • 18. Infinite Loops

    • 19. Break vs Continue

    • 20. Switch Statement

    • 21. The "goto" keyword

    • 22. Bit vs Byte - Decimal vs Binary

    • 23. Common Data Types and Variable Declaration

    • 24. Scope and Lifetime of a Variable

    • 25. The C Structure

    • 26. Type Casting

    • 27. The "static" variable

    • 28. The "static" Function

    • 29. The "extern" Storage Class

    • 30. The "const" Keyword

    • 31. Global Constants

    • 32. The "typedef" Keyword

    • 33. The Enumeration

    • 34. Pointer Basics

    • 35. A Look Inside Memory

    • 36. Static vs Dynamic Memory Allocation

    • 37. Deallocate Memory

    • 38. Function Taking Pointer Arguments

    • 39. Function Returning a Pointer

    • 40. The "void" Pointer

    • 41. Array Basics

    • 42. Pointer Array and "memset"

    • 43. Pointer Arithmetic, buffer and "memcpy"

    • 44. Function Pointers

    • 45. C String Basics

    • 46. Get The Size Right - "sizeof" vs "strlen"

    • 47. Copy Data - "memcpy" vs "Strcpy"

    • 48. Compare Data - "memcmp" vs "strcmp"

    • 49. Format Data With "sprintf" and "snprintf"

    • 50. Array of Strings

    • 51. String Concatenation - "strcat" vs "strncat"

    • 52. Check for Substring - "strstr"

    • 53. Escape Special String Characters

    • 54. Break a String by a Delimiter - "strtok"

    • 55. C Operator Basics

    • 56. Arithmetic Operators

    • 57. Assignment and Equal Operators

    • 58. Increment and Decrement Operators

    • 59. Relational Operators

    • 60. Logical Operators

    • 61. Bitwise Operators

    • 62. Bitwise Operator Examples

    • 63. Conditional Operators

    • 64. Bitmask Techniques

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

The C language has been around for a long time… and it is commonly considered as a low level programming language because it tends to bring your application closer to the computer hardware, requiring you to manage system resources manually and carefully.

No other programming languages have such granularity, which makes C a unique and desired programming language for embedded device,  firmware, drivers and infrastructure software such as a database.

Instead of learning C from text book or from a traditional university teacher, I believe the best way to learn C programming language or any other languages is to learn from someone who has been in the industry, made expensive mistakes and finally done something that made an impact.

In the past 10 years, I have built many innovative solutions and applications in the smart metering, communication, database and security industries and at the same time made expensive mistakes in order to become better.

For this reason, I will show you the fundamentals of C programming based on my real industrial experience. In addition to just show you how certain things work, I will also show you what to do and what no to do in order to avoid deadly and expensive mistakes in your career.

In most of the lectures, I will guide you through the C codes that we will be building together step by step with combination of some power point presentations.

If you are a beginner or someone who knows a little about C programming and would like to further enhance your skillsets. This course will help you jump start your career with C programming.

What Will You Learn From This Course?

  • The basics of C programming
  • Tips and tricks of writing C code
  • Strategies to avoid deadly coding mistakes
  • Become qualified C programmer
  • Integrated development Environment setup
  • Linux and Build environment setup
  • Software Compilation
  • Makefile basics
  • Virtual machine basics

Some Requirements

  • Laptop with internet connection
  • Very basic Linux commands such as 'cd', 'ls', 'mkdir'
  • Desire to learn

This Course Is Great For:

  • Beginner C programmers interested in building C applications
  • Intermediate C programmers interested in reviewing  C principles
  • C programmers interested in building embedded software
  • C programmers interested in building infrastructure software
  • C programmers interested in building database software
  • C programmers interested in building Linux applications
  • People who are interested in learning basics of programming
  • People who want to have a software career
  • People with computer science background
  • People who enjoys problem solving with computer software

Enroll today and see you inside!

Meet Your Teacher

Teacher Profile Image

Cary Huang

multidisciplinary online entrepreneur



Cary here, I am a multidisciplinary online entrepreneur with over 10 years of software engineering experience in smart metering, cyber security, database solution, and embedded systems

In addition to my technical background, I have been a very active side-hustler experimenting, learning and building multiple streams of income by taking advantage of the internet.

I have run an Etsy store selling handcrafted goods, built trading bots to trade the Forex market automatically, launched an Amazon store selling baby products, done affiliate marketing promoting other people's products, converted leads to sales using email marketing, and helped many other businesses scale their sales using funnels…

I enjoy teaching a lot. I have taught at Peking University's... See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Introduction: Hey, what's up guys It's Cary, your instructor here and welcome to C programming success essentials. I am super excited that you're here, and I hope you are excited as well. So C programming has been around for a long time. And it is often considered as a lower level language because it tends to bring your application closer to the computer hardware, requiring you to carefully and manually manage systems resource. And this is why a lot of C software application written in C that tends to have the best performance when comparing to other programming languages. Okay, this is what makes the language unique, because no other programming languages have such capabilities and granularity, okay, For that reason, C language is often the most desirable language of choice. When working with embedded systems, firmware development, drivers, operating systems, or even database software. All right. And in your career path, it is likely that, you may come across some of these development categories. And this is one makes C language very important because it is literally everywhere. Okay? So a little about myself. I have been working with C language in the past 10 years, building and designing and managing different software solutions written in C, in the fields of smart metering, security, database, and communication. Okay? And during my career, I've also made some expensive mistakes that caused a lot of money to recover. And for that reason, I want to take this opportunity to show you the fundamental of C programming and such that the same mistakes that you don't make in your career path. Okay, so instead of just showing you how certain things work in C programming, I will show you from my real industrial experience, what to do and what not to do in order to avoid those mistakes. And also for you to become a better programmer, okay, I believe the best way to learn C programming language is to actually code it out with a guidance, which is me. And so for that reason, for this course, most of the time we'll be working in building and writing C code together on my code editor, okay? And of course there'll be some other PowerPoint presentations to show you some concepts and some fundamental information. Okay? So if you are a beginner or someone who knows a little bit about C programming and would like to further strengthen. Your skillset. This course would be for you because it will actually help you jumpstart in your tech career, okay? So please enroll today and I will see you inside. 2. Course Outline: All right guys, welcome once again to this course. And today in this video I want to tell about the objectives of this course and some agenda about what you will be learning throughout my course. All right, so what will you learn? So of course, you will learn about the fundamentals of C programming from a real-world experience rather than from textbook. So because I'm coming from an industry where I build other C programming to make an impact on several industries related to smart metering, security, and even data. All right, so I want to bring that expertise from the real-world experience and present to you as a industry expert. Okay, So instead of teaching from textbook, I will tell you what we were using and how we do certain things, alright? And you will learn the industry's best practice with C programming. So it's just like I said, instead of just teaching you what certain things work, I will show you what we use and what people nowadays use and why we don't use. All right, so it's not like everything about C, you have to know and apply them. That's not really true. So in the real-world, we tend to only stick with certain things and you will learn about them in this course. All right? And while we're learning C programming, of course, we're also developing our logical thinking because C programming is a lower level language. So there's more consideration to be done in C programming when comparing to higher level languages such as Java and Python. All right? So C programming really prompt you to be really careful and to really apply your logical thinking when you are solving a problem. Okay? And of course, when you have the best practice will see you will, you will of course be a qualified C programmer, right? Again, I want to show you the demo that the mistakes to avoid, because I have made these mistakes when I develop the real-world applications and they cause a lot of loss, a lot of money to recover, okay, so certainly you won't be really, really careful with your C programming. And normally those things that's so small and those tends to be the mistake now you can ever make in your career, okay? So keep that in mind, right? And this course is very good for a beginner or intermediate C programmers who are interested in building C applications. So whether or not you're just started or you have learned a bit of coding from school. This course is for you because you are basically review your mind and basically teach you from, you know, step-by-step, how to do the programming from simple to intermediate and then to complex. And it's also good for people who want to have a career in software engineering. So again, if you want to have, if any questions about the career, the software engineering career, or even resume that you'll want to consult with me. You can definitely send me a message and I'll be really happy to help out. Alright? And we're coming from a computer science background or you're interested in going into computer science. Again, this course is really good for you because it gets you started. It just show you what the world of computer science is before, before you even entered, right? So it's very, very good preview of what you're about to enter. And of course, if you enjoy problem-solving with computer software, again, this course is really good for you. Alright? So let's talk about the agenda for this course. We'll be covering several topics and including sending IV environment. And we'll be using Ubunto Linux 18 for us, our environment to build and to run our C program. Okay? And we'll be building the C code, will be writing code on Eclipse IDE. Ide stands for Integrated Development Environment and is one of the most popular choice of editor to write code. Okay, well, we'll talk about GCC compiler, which is a popular compiler to compile C programs. So most of you were ever be using GCC in your entire career. We see. All right. And then you will learn how to use to write a basic makefile and how to compile a C program using the mic utility. So we'll cover that as well. And again, the rest of this is just the basics of programming from the basic elements to the basis of control flows if, while, loop, et cetera. And which we'll talk about variables and data type. And then you will go to a little bit advanced to talk about C Pointers streams in operators. So all these are what I believe is the mass no for any new programmers. So whenever you're ready, you can move on to the next module and I will see you there. 6. Import and Create New Project: All right, welcome to this module. And today I want to show you how to import a new project based on existing code to your Eclipse workspace. All right, so before we do that, I need you to go to my course page and download the my program dot zip file. Okay? So it is a zip file that will contain some, some simple makefile and example C file and header file. And these are the starting point for our course today and will actually be building on top of this example program as we learn more about C programming. Okay? So to import, we simply go to File and then we hover over to New and select makefile project with existing code. So in here you want to go to browse and go to the path that contains your, your folder for my program. And of course, the downloaded zip you have to extract first and you'll get a folder. Okay, so assembly select folder and hit Select Folder. And as you can see, the project name will be default to be the same as your folder name. So which is all right, but you can actually modify the project into your liking. So I'm going to call this carries example program. And you can call this anything you want. And this is just a name that will be appear in the project explorer to the left. Alright? So I'll just say periodic swap example program. And in terms of languages, you can leave C and C plus plus checked for a torch. And it actually doesn't matter here because we will not be using the compiler. Actually, the tool chain is just a fancy word for the compiler and the built environment. Okay? Eclipse already comes equipped with several popular compilers. And I'm going to leave it non because it will be actually using a command line prompt on the Linux environment to actually carry out the compilation and build. So whenever using Eclipse to build our program. So I'm going to leave as none and I'm going to hit Finish. And that is it. You or your project will not be appearing in the Project Explorer on the left. And that's how you can import a new project to Eclipse. And when you're done, you can move on to the next module and I will see you there. 7. Basic Element - Source and Header Files: All right guys, So welcome back to another module. And today I want to talk about some basic element of the example program. So after you have downloaded my example program and import it into Eclipse, you can hover to your project explorer and expand the project. All right? And then here you will see there are four files, okay? So two dot C files or one dot H file and one makefile. Okay? And today I want to just mainly focused on does C and H, and we'll talk about makefile later in this course. All right, so a dot c file is also called as a source file, which contains the source code. And when you hear the term source code, you just basically means the implementation of your program. Okay? And the dusty file contains implementation. And implementation could simply just a collection of functions. So which contains the implementation of each function. For, for example, this is a function called print hello world, and you will do certain actions, which is to print hello world. Okay? So here's another function that there's different things. Okay? So C files contains functions and functions implementations, okay? So when you can basically understand a C program is just a collection of functions calling each other. And that's really what a C program is. Actually, it's what many programs are our consist of. They are just a bunch of functions and you just call each other. Alright? And so that's a source file, and there's also a header file, okay, so that H stands for a header file. And the header file does not contain too much of the implementation, but rather it contains prototype. It contains declaration of variables. Okay? So in here I have two function prototypes declared. It basically will tell the compiler how many functions are available to be used. So you are telling the compiler I have two functions right here and it takes a void. Void is a keyword that, that means nothing. Okay? So a function that takes no input and returns no output. And I have two of them, and these are their names. So you're telling compiler, okay, what do you have? You basically declare a function, okay? Util the compiler, something exists over here in your project. And guess what the implementation is. The implementation is in the myfunc star C. Okay? So here it contains not just the name, not just that, the name and input and output. You also contains the implementation. It is defined with the curly bracket right here, right? So in the header it just contains the first line here. And that tells compiler what is to be expected to be fine until we found, okay. So in your digital functions, normally in here will also put a variable's declaration. Maybe you are poor structure declaration or some macro damage decorations. So all these are telling the compiler what's available, okay? And then you will go to your C program, C file to implement what you say is available such that the whole program can run together. All right? And lastly we have another C file, which is again another source file containing another function. And this is actually a special function called main, and we'll talk about that later in the course, okay. 8. Basic Element - The Comments: All right guys, welcome back to another module and today I wanna talk about comments in your C program. All right, so comments are something that the compiler will ignore when compiling your program. Okay? So something that's like this, on the very top of each of these files, each CFO and header files. Okay? So what's up here? This is called a common block, okay? So these lines right here, the words that were enclosed between the block will not be compiled by the compiler. So it's basically here too, just to show humans, to us, to developers what this file is about. Okay? You can create a common using a block like this, using a forward slash and asteroid to enclose, to begin enclosure and use asteroid slash to end the, the common block. So everything in between will be comments and they will not be compiled. All right, And normally on the very top of these each file, normally I will put some author information like who wrote this program. And if you is, if you write program for a company, you can also put copyright information right here for XYZ company. So we can put things like this. All right, and within your functions. So for example, in the main function, you can put comments using also using a double slash characters to indicate a start of a comment. So that will make this whole line as a comment. So everything you put after will not be compiled by the compiler. So normally we'll put that in front of something to basically explain what that something does. So I could just say this function prints Hello World to the terminal. Okay, so that's a common describing what this function would do. And again, I can put another carbon right here, saying this function from user for an input. Okay? Things like this. You can put a common noun. And this is another way to write a comment which is using a forward slash, asterisk, asterisk forward slash. So that can also be done in one line. So in addition to having crossing multiple lines, you can, you can put that in one line and again, put your comments in there. All right? And lastly, you can also use a shortcut. This go to my fantasy. He used a shortcut common on multiple lines at once. So first you have the highlight, you are lines that you want to comment and you hit control and then forward slash. Okay? So only includes, you automatically put a double slash in front of each line to common the entire block. Okay, So you have highlighted. So that's a shortcut for commonly multiple lines. Again, you can highlight again and do Control for slash again to common the lines. All right, so that's how you can write comments in your program. 9. Basic Element - The Semicolon: All right guys, welcome back to another module. And today I wanna talk about the semicolon at the end of the each lie in your source code or in your header file. Okay? So whatever you write in your source code or header file, you have to end it with a semicolon to tell the compiler that you have finished writing a statement. Okay? So without these, you will cause the compiler to have some parsing errors. You actually continue to checking what else do you have. But in fact you are already done. So good practice. Whatever you finish, you finished writing one line, you put a semicolon. Okay, So that's the way for compiler to know you're done for one statement. So because of that, you can actually put all these lines together into one line like this. Okay? So something like this. So this compiler will still understand as for statement, because you have four threes for semi-colons, right here indicate the end of the statement. So this technically can still compile, but I will not recommend you to write your Codec this because it is very hard to read, okay? So you want to actually separate that into a new line so it is easier to read. But in some cases, you may want to do this, you know, having multiple statements in one line for very rare occasions, or you may want to do that. But generally for most of the time, you'll basically have a, you know, put them in a new line. Just, just a very easy to see and read your code, okay. 10. Basic Element - The include: All right guys, welcome back to another module. And today I want to talk about the include keyword started with a hash character right here. Okay? So we normally use hash include to include other header files to complete our program. And other header files will provide additional utilities and functions that are your program can use in order to complete whatever you want to complete. Okay? So for example, I have to include files right here, std IO and Std Lib dot h. So these are actually the standard C libraries that provide one of the most basic utilities for your programs such as print f, okay? And also scan f. So these things are provided by the standard libraries to allow you to do some basic IO with your users. Okay? So these two include a standard library. We normally use the this kind of bracket is sharp bracket to indicate this is a system. Include the file. Okay, So by the system, I mean, your program were actually go to your system folder where you are C compiler and torture and environment are located and trying to find these two standard header files. Okay? Another way to include this is using something called the My program in here by using a double court to indicate your header file to include. So when you use a double quote in the include clause, you eat. It basically means this header file is within your own program. So this is actually your own header file that you have to define yourself, which is right here, right? So a rule of thumb, if you want to input the system library, you use this bracket right here. And you want to include your own header or your own utility, your own functions. You want to use a double quote, okay? 11. Basic Element - The Preprocessor: All right guys, welcome back to this module and today I want to talk about preprocesses, okay? So what a preprocessor is, is simply a series of actions or things that a compiler have to do first before you actually start the compilation. Okay? So one of the most well-known thing that the preprocessor would do is remove your comments, okay, because that will not be part of the compiled nation. So before the compiler will compile, it will move all the comments before he feeds to the compiler, okay, before you actually does a compilation. But in addition to removing comments, these are actually what a preprocessor will have to do. You will have to resolve all these paths, make sure they are, they exist. All right? And this hash psi is an indication that these lines should be read by the preprocessor, okay? And the rest should be read by the actual compiler. So it makes that distinction right here. So whenever you start with hash, thus for a preprocessor, in addition to the comments, Okay, and now I want to show you one of the most common preprocessors that we precede program and always, always use, which is called a macro. And America can be defined using the defined keyword. And then you want to give it a name, such as my number. And then you give a number, for example, 200. And of course you can define another macro code and my text, which you can give a, some text here. Texts. Okay? So these are actually, they are also a preprocessor, okay? But they work very much like a variable which we'll be talking about later in this course. So what is, what do is you actually search your source code, replace these words with these values, okay? And replace these words with these texts values. So that's actually a preprocessor Java. You actually scan this file and replace this with that and that with that. Okay? And then once he does the replacement, you will feed into the compiler to actually compile. Okay, So basically swapping values, okay? So that's another popular way to use a preprocessor. We said define keyword, okay. 12. Basic Element - The Makefile: Okay, So in this video, I want to talk more about the makefile and show you how this Makefile is structured in your example program. So first, let's go ahead and open the makefile. And this is something that you will see, okay, so it's not very long, it's very straightforward. It's nothing complicated. And I will show you how it is done. So the very first thing you see is up here, this is actually a comment. The makefile uses a different syntax to indicate a common, you actually use a hash character. So this is like a Unix script style commenting character. So whenever there is a line starting with a hash, that's a comment, which means you will not get one by the make utility when this file is read. Okay? So this is different from the source file, the C source file here, in which we use a double forward slash and a forward slash star to enclose comments like this. So makefile will use a hash character, okay? This file is now really a program that will get compiled is merely a series of instructions that tells the mic utility what to do and how to do it. Okay? But it does share some similarities as a program. For example, the first line here, which is a variable assignment, my product, all caps. This is the variable's name. And it equals to some value. Okay? So that's the value. So whenever I reference my Prague, he actually means my program. That's the name. Okay? And all these are actually variable assignment, okay? The first four lines. So this is actually the output program, the name of the output that I wanted to be. Okay, I want it to be called my program. If the compiler is successful, that file should be produced. Okay? And CC is a common variable name we used to stand for the compiler that we want to use. And because we're using C programming, we want to use GCC compiler. And we already, we have already set it up in the previous module, right? And this GCC allows people to give some special see flags that kind of configures how the compiler nation should be done. So what I have here is a very common C flags that we use, which is, which means I want to use, I want to be the program that enable debugging. So that's enabled by dash g. So maybe NIH in this course, but in some other course I will actually, that'll teach you how to debug your program. And one of the requirement to debug is that you have to compile your program with the dash g flag so that the debugging symbols, adresses, function names, all those things will be included in the RPA program. So when we run the debugger, we are able to see where the program is and see the name of the variable and to do the debugging, right? And, and another flag is the dash capital O flag. So this is actually something we use along with the dash g flag. This means compiler optimization, OK? And I said the optimizations level to 0. You can say that 1 or 2 to the higher the number, the more optimization the compiler will do to your program. So why am I setting that to 0? That is because I wanted to be a bugging version of the program. So by setting the two optimization 0, I'm able to see the true function flow through program flow. Okay? So line by line, I can see very clearly if I were to enable optimization, you will see that using a debugger your program with Jump, jump around. Because some part of the code is not actually compile them because compiler can optimize certain executions to make your program work more efficient. So and therefore, not every single line is executed as you thought it should. You actually could jump in-between places. And that's what makes compiling a sorry, that's what makes debugging difficult if you have optimization. So that's something that the two of the flags I wouldn't normally used together when I'm debugging my program. And the last flag is dash W, stand for warnings. And I want to see all the warnings, okay? And we'll talk about warnings later in the course. And the next variable is my objects, okay, my OB JS. And as you can see, I make it equals to my fonts dot o and my program dot o. These are actually represents my C files. But with now without the, without the C extension but rather a dot o extension. Remember in the previous module we talked about the compilation and how compiler works. We first have to compile each C files into something called an object file. And these are actually object file I want to compile first. And if you remember, after we compiled object files or actually say all of your object files, then the last stage is to link, go through a linker process, actually link all of your object files and finally produce a as accountable as the output. Right? So in this makefile, that's my linkers output. And these object files are the input to the linker so you can produce the final product. Okay? And now that's just a variable, variable assignment so far. And what happened after is what makes it very interesting over what makes this makefile unique. Okay? So makefile is consist of, I would say stages. And the stage is declared using this general format. So first, I'm going to put in comments here so you can understand. So first is the name, name of your stage, followed by a colon and dependencies. I'll explain that in a bit. So none of your stage colon dependencies and when the dependency are met, okay, So that means everything is satisfied, whatever you require is done. Then the line immediately after starting with a tab. Okay. You have to be to indent with the tap, Okay? And this is where you put your build commands, you know, with GCC, blah, blah, blah, right? And you can also put, again in there with a tab, some Unix commands. So unix command is something like RM for remove, ls for list, cp for COBie, things like this. So that's the general rule, how you declare a stage. Okay? And now here's our examples. So I have several stages below that. I have, I have declared for this example program. So first one you see is 0 or colon, okay? So all is a special stage. This stage is triggered when you issue you a make command without specifying a stage. Okay, So you can consider that as a default stage. So the compiler, so the Makefile works like this. You can define a bunch of stages. And when you use a make command, you can actually select what stage you would like to build or you would like to run. And if you don't specify any stage, okay, then you will use stage or instead, okay? So when you come into all, you first have to satisfy these dependencies, right? We put up here as the dependencies. And the dependency is my Prague. And guess what? My product is a variable, which means my program. So what this means is if you wanna do, if we want to execute stage, all you have to first finish this stage, which is called my program. And guess what? We have a declaration for that as well. So my program is right here. That's actually another stage called my program. And guess what? In order to run my program stage, I have to first finish my objects stage. My objects are these two guys right here. So actually have multiple dependencies. In this case, I have two objects I need to finish first. My fonts dot o and my program dot o. And guess what? They are already here, right? My program dot o is right here. And that's a special stage where it doesn't have any dependencies. In order to compile my program dot o because he has, has no dependencies. You can just run this program. You can run these lie immediately after that is indented. So you actually run this line and this command I would say, to compile or to finish this stage, which is GCC. The flags. We have our peer dash C for compile, compile relation, only. One compiler we want to compile the C file. So you see that will be done first because that's part of the dependencies to build my program. And that's just one of dependency, right? And there's the second one, my function, which is also down here, the declared as a stage as well, and no dependencies. And sometimes people will put the C file here in here as the dependencies. That's also fine because you, if you want to compile that all these file must exist, right? So the main utility you actually find is C files on your same folder. But if you can leave it as empty, that's also fine. So again, if I want to compile this dot o file, okay, you have to run this GCC commands, which is gcc, c flags and then the source file, right? So have two, these two object files are built as part of the dependencies. Then this stage can finally as a cue, right? You have to wait for these guys to finish. So there's the order of execution right here. And this one is actually what the will produce the final output. So again, compiler GCC might see flags and over my object files. Okay, so what is line means is I want to turn off my object files, which is least two guys, which I have already built using these two stages. And I want to produce dash 00 for output on the produce a final program, which is called my program. And this will as a queue, right? And that's it. That's the end of this compilation at these lists, What's this Makefile is describing? Okay? And you may also notice there's also a clean stage. Remember, when you issue the main utility, you can select a stage on-demand manually by yourself when you issue the command. So when you select the clean stage, when you execute a command, you actually run this command, which is to clean whatever you have built. And by clean, it will just means to remove. Rm is the Unix command for removal, dash RF. So it's recursive, recursively removing and force removing all the binary file that you generate right here and all of your object files that you also generate. So that cleans, you will build, right? I know emus are very still confusing. Maybe it's a little bit, maybe. So now I'm actually going to show you what I mean by okay. Hold on. What I mean. To demonstrate the makefile pay, I'm going to put my terminal right here. So actually I have already the program, so I'm going to do a make clean. Okay. So when that do make clean, you see you as Italy's command, R M dash RF, my program dot o file, my program dot o. So that matches this line here. Right? Because I am now selecting a stage to run by using, by passing the stage name right after the make utility. So when I do make clean, you will just clean whatever I wanted to clean, right? So this, I can just keep doing that. And to build. Let's clear the screen. To build, I can just do make. And guess what? When I type make, it first has to run this abuse the myfunc step C, which produced the dot o file. And then you build the my program.cs, which produces my program dot 02 object files are generated first. And then finally. The last bit is the linker process. You actually take all of your object files and produce the final output, my program. So that's exactly what happened in here. Because of the dependencies, even though I say I want to make which defaults to all. And you said if you want to be all, all means to be my program. And my program depends on two objects. So you actually run these two dependencies, stage first before running the final target. So in fact, I can also do something like make my program, which is the same as make without any argument because I have the default to make my program, right? So this should result in the same thing. Oh yeah, you can see that my program is up to date. That means it's already built. There's no change. So what I wanna do is make clean, clean everything up, deleted again. And you can do might make my program, which does the same thing as you can see right here. It is the same thing because we say they are the same right here, okay? And now after that, I have some dot O files here and the final program. So let's clean that up a little bit. Once again. Since we can select stages, I can also do make, let's say my program dot o. So I just, I'm just interested in making my program dot o. So I can just do that as well. And it will just run this stage and produce me a dot o file, right? And I also do the other file, which is my ufuncs that oh, like so. And it will just run that line. And then I'll just do make my program without the OH, which links two of my dot o file and produce my program. Okay? So that's my program. So that's the basics of the makefile. And I hope this has been useful. I know, you know, this Makefile can, can get really complex. But what I'm showing you here is the basics. And I think if you understand the basics, how the basic stages are setup, then you'll be easier for you to understand the makefile in general. Okay. 13. Create Your First Function: Okay, So in this video, I want to talk to you more about a C functions. I know we have just briefly talk about functions in the previous modules, but now I want to just diving a little bit more about functions. And again, in a later function we will talk in even more details. So right now, I just want to talk to you about some basics of functions and how to write a function. How to declare, how to declare a function in the header file. And how do we call a function? And what is the argument for the function and what is the return values. So just some basic stuff about the functions. And so let's get started. So as you can see, we, this is our example program and we talk about that. Everything starts from the main function, right? And you can understand a C program is just a collection of different functions. It's either your function or somebody else's function. And we call each other, okay? We are calling each other in order to complete a certain task, right? So you can understand the C program is just a bunch of functions calling each other to produce an output. In fact, I think that's what exactly what it is C programs. So we have already have two functions in the example. One is called the print helloworld, which doesn't do but she just print the hello word. And actually I just want to show you again. You can go to a function's declaration very quickly with Eclipse by right-click on the function name like that. And click Open Declaration, which will take you right to the implementation of that function. And from the implementation of the function, if you right-click again and select again open decoration, you actually take you to the prototype where you declared in the header. So I think that's pretty handy. I use it all the time to navigate through functions. I think that's one of the things I like about Eclipse is that you will just go to function declarations very quickly and effectively. And if want to go back and click on this back button, boom and boom, right? So it's very simple to navigate between the function that you have visited with these tools. So keep that in mind. Okay, So let's go here. So in order to create a function, you need to have a function name like this, or that. You can name it anything you want and followed by a bracket like this, okay? And inside the bracket you need to put the arguments required for this function. Arguments like input parameters to this function in order to do something. And void is a special keyword in C, which means nothing. So if you put a void inside the bracket, that means the function requires no input. Okay? And now there is another parameters that we put in front of the function name, which stands for the return value type. Okay? So if a function is doing something and it is supposed to return the result back to the colder. You need to specify in here the type of that return value. And again, void is a special keyword that means nothing. So this function takes nothing and returns nothing. So is this function. Okay? So that's a really interesting, right? So we will actually be creating a new function that actually take something and returns something. But before we do that, let's go back to these two functions, colder, which is in my program. So in here, in the main program, he calls those two functions that we have in there. In order to call a function that takes nothing as the input, we simply put the function name and the empty bracket here. You don't have the void here. You only need a void in the decoration and implementation. In a color. You can just leave as empty, okay? And he doesn't return anything useful. So you don't have to capture any of the return values in here. Okay? So same thing for that and same thing for this function. Both are doing the same thing. Okay, so now I want to make another function that is go to my folks that see, am I actually going to make this function called to return an int? And you will call our code is add one to my number, which takes a number, say integer. So later in this module we will actually talk more about data types. Data types such as the integer, and I think more about functions in general. So this is a very simple form, very simple function that just, I'm just making right now, that takes a number and returns a number. And remember if you have a return data types specified, you must have a return statement. Returns another keyword in C. Now, suppose to return the value of your computation. So what I want to return is I want to return number plus one, right? That's exactly what this function should do. You would take whatever number I give to the function, add one and return, and also as an integer, right? So that's my function's implementation. And now in order for my program to have access to this function, I need to register, or in other words, declare the same function in the header file. In here. I will just simply copied and paste the function, the name, argument and return type. That's it. Without implementation. Remember you just need to end with a semicolon. Okay? So let's go back to my folks that see. And now my function is ready. So let's go back to my program. Actually, I'm going to copy this, go back to my program and actually call that function. Okay? And in order to call the function, I need to give it a input, right? I need to put something here. So I can put something like 35, 350 for example. And it should return 351. But here's a problem. I need something to store my return value. Okay? If I just call the function like this, you will still work. It will still return 351. But you don't see anything because we didn't put any logic as to what to do with the return value. So what you can do right now is called declare a variable called my results. For example. Ok. And make my result equal to the return value of this function that takes this input. Okay? So after this function is run, my results should contain the number 351. And to find out, we can actually borrow the print f utility in which we'll talk a little bit more about print f later in this course. So I'm just going to print my result equals to whatever the variable holds, right? Like that. And percent d, Again, that's just telling print f to interpret this variable is integer. And again, we'll talk more about PNF later in the module. So just bear with me right now. I just want to show you how functions work, right? So that's it. That's how I can call my function that takes an integer, returns an integer. So let's try this out. So let me just pull over my hold on 1 second and pull them my terminal. Hey, right here. So this is my terminal. And I'm gonna do a make. Okay? So now I have the function, I have the program compiled. Now what I need to do is I want to run my program. And on Linux to run the program, you can just do dot slash. Dot slash represents current directory. And I want to run current direct fees my program. All right, Thank You. Just do that. And guess what? It prints my results equals to 351. And as you can see, you also promised me, prompt me to enter the integer please. So I will just enter 40. And it prints you enter 40. That's actually this function right here, prompt and print. Since we put this function in the front, you will get executed first, return this result. Second, we call print hello world. Here he did less well. And the third, let's take a look. Yaks, to take, ask the user to enter the integer and which I did. And which will use scan f to store the integer into this variable and prints the variable. So again, we will talk about print f and scan f later in the module. Because they are kind of the standard IO. Standard IO for C programs, right? Ask user to prompt enter something. So that's how, how are we gonna do it in the next module. But right now I just want you to know about functions, right? And above is 350. You don't have to put 350 here. You can also again declare another variable called my input and initialize that to some number and put that variable here instead. Right? So you want to change the input, you'd have to change this variable. And you want to compile again. So let me just do the compiler again, make, clean, make, and run the program to 44. Okay? It should still works the same way, right? So that's how you can define and create a very simple C function. 14. Basic IO with "printf" and "scanf": Okay, welcome back. In this video I want to talk about print f and scan f and how you can use these to standard utilities to do some basic IO with the end-user. Okay? And in fact, we're already using print f and scan f in one of our functions, which is called prompt and print, right? So in here, I think we have seen how it works already. So let's just go over that again. So in this function, you will first print a message to the user telling them what to do. And in this case, you would want them to enter an integer, right? So in that case we use print f. And the first argument to print f is a stream or a message. And a message or a stream is normally enclosed with a double quote in C programming language. So therefore, you see it is a double quote and you will enclose your message in between, right? So that's a very simple message that we want to print to the user. And print f has a special property, which is, you can actually include after this coded message, you can actually include as many variables as you would like to kind of customize this stream a little bit better, I would say. So for example, I can just simply say enter the integer please. Or I can say, for example, percent d, percent d or person B. So what is does is percent d is a special format specifier that print f will recognize. So before it prints, it will actually replace this percent d character with a variable or a value that you have provided after the first argument here. So for example, I can put here 34 or negative 25, or a variable called my number. My number doesn't exist yet, so I can just know I can also declare another integer with my num equals to 999 for example. So that value 99, which is stored in my number, we put it here and you actually get replaced to this third% d. So the ordering here is important. Then you want to make sure this specifier, number of these format specifiers matches the number of variables that you supplied after the first argument. So in this case, they both match. I have three specify here, and I have three numbers or values right after. So you actually print these three numbers. So just an example, right? So that's, that's a print f. And now after the user has see this message from your program, you will, you need to prompt the user to enter a number, right? And that's where we will use the scan f utility immediately after the print f scan f is a blocking function call, which means your program will actually be waiting here. Inside this function, waiting forever for the user to actually put some number. Okay? You will not go anywhere until the user has put a number. So it's a blocking co-housing. And again, since we're expecting the user to enter an integer, we want to put a formatting specifier also in the scan f percent d. And you see in the scan f, We didn't say anything else. We just want to put as formatting character because scan f is not to print a message to use it is to receive a message from user. And after he receives, it wants to store the message or the value into a variable that you have declared, which is I caught his eye number. Okay? So what it does is once the user enter some number, it will actually convert that number. Sorry, let me rephrase that a little bit. On the terminal, whatever the user enters the terminal, they are always strings, okay? They're always characters, okay? They are not really a number yet. Not yet, at least. So whatever user type is a string, it's a character. So what scan Edward do is you would take that character and convert that into whatever type you specified here using the format specifier. In this case is percent d, which is integer. So what it will do is convert the string into an integer. Stored in this variable as you specify it. And now keep in mind here. When I put the variable here, I number, I didn't just put as I number like I did here with print f, I actually take the address of this variable, i number right here. And to take it address, I have to put a special character right in front, which is called the ampersand character. So then person is means to take the address of sum, sum variable. So why do we do this? Is very simple. Because you want to take the address of the variable such that you can override its value. Okay? So this is actually where the C programming language is very powerful because it's all about address and memory and pointers. In this addressing part, we'll talk in more detail in a later module where we introduced the idea of pointers. But right now you just have to know that in order to store a value to a variable using the scanner utility, we need to take the address of the variable so that it is able to actually modify that number. Okay? And after it does that, we print it again. And we, and this time we'll print the value of i number and again as the integer. Okay? So that's how, that's basically how to use print f and scan f, f2 to do the basic IO operation. And right now I will actually just create all this. I'll actually do a demo of whatever we just written there. So just hold on a second. I'm covering my files and let's do it again. So again, I will just issue is my program to run the program. And as you can see, you will not prompt me to enter an integer, please. For example, 34, negative 25 over 99, exactly the same as whatever I have put in here, right? And I can just do 2000. And again it prints 2 thousand, right? Okay, one thing to keep in mind that in here, the program actually go to a new line after enter 2000. But when it prints 2011, go to the new line. And instead, my terminal prompt is still in the same line as the output of the program. That is because in print f, there is a special character like this, which stands for new line character. So if you want to make your print more pretty, normally you can put a backslash n right in the end of the print f string to indicate that this is a new line character, please, when you print, you print a new line in here, since I don't want to enter the new, I actually want user to stay in the same line. I will not have. I want to include a backslash n by n. Say I should put here a colon so it cannot distinguish my message and uses input. So let's try this again. Okay, So this is again b. And I will make again and run the program. And now you will not have a colon. So I can kind of easier to see my input and the message. I'll just do Enter and I entered this number. And see this time you actually go to the new line. You know, it's actually press Enter for me basically. So that's a very simple way of doing a print f and scan f. And this another thing I want to show you as the extra, I want to show you three macros that is provided by standard C library. I think you will come in very handy if you are debugging your program. So angelic gonna go ahead and type them and I will explain later. So before I print this, actually want to print something a little bit more about this program. So I'm going to do that. Do that again and this again. I'll explain in a bit, okay? And maybe a pipe character to kind of separate everything. So before I print my message and some example number, I want to briefly things and you see it's a little bit different. Now, what I have here is percent S standing for string and percent d standing for integers we know. So what I want to print here is actually some properties of this program using a special macro, neck this file. Okay? So this is a special macro. This is two there, two underscores here. Okay, So underscore, underscore 00 capital file, followed by two more underscore. So first is file, second is function, okay? And the third is line. Again. These are some special macros or you can use in Europe, actually anywhere you want that will tell you the filename, where the print comes from, and the function name where the print Cosmo and a line number where it is printed. Okay? So this is quite handy when you are debugging your program and you see another prints, but you don't know where they are. Well, you're lucky. You can print where they are exactly using these three macros. And because filename is a string, so you have to put percent S to format as a stream. And because function name is a string, you also have to put this person S to format it as a stream and line number is an integer. You can put person D like this. Nice guys. Okay? So let's try this again. Just a couple of my program polynomial terminal. I will do the program and no mistakes, and I will run the program again. So now what do you see? Filename is my fox does C and the function name is prompt and print and a line number is 19. So from this location, this message gets printed, right? And these exactly will match here, my 19, actually the last line here. So that's something I want to tell you and I hope you find this useful and I'll see you next in the next module. 15. Add Command Line Arguments: Okay, So in this video, I want to talk about how to add and process command line argument to your example program. So first of all, let's talk about what is a command line argument. Let me just briefly show you here. Okay, So this is my Linux machine. So the command line argument is when you run the program like this, you can actually specify additional argument to kind of influence how the program will work. So for example, I can give additional argument like arg 1, 2, 1, 2, 3, for example. So when this program is run, you will take these five digital arguments and trying to do something different depending on the parameter that you use applied. So basically, these arguments control how the program will behave based on your definitions, of course. So let's just go ahead and trying to add some command-line argument to our example program here. So the first thing you have to do is go to your main functions. Main function, which is right here. And instead of void as the input to the main function, we are going to change this into something like this. And I'll explain it a bit, okay? Okay. You have to change it to something like this. So what this is saying is the main function when it begins, it will pass two additional arguments to the main function in which we can use. And these two functions represent the command line argument that the user puts in. So the first argument is normally named arg C, which stands for argument count. So this integer holds the number of arguments that user has inputted. So which means number of arguments. And keep in mind, this is also counted as an argument. So the program name itself is also counted as argument and plus additional argument that you, the user decides to put in. So in total here they are 1, 2, 3, 4, 5, 6, six arguments in total. And when I run programs like this, these are xC should hold six, okay, So that's the number of arguments. And then the second argument, which is a char, star. Star is a pointer In C. And pointers we have a separate chapter to talk about just the pointers. So right now you just have to know that this arg v and square bracket, which is an array, again, we'll talk about later in the course. So this is an array of char stars or char pointers. Array of char pointers. And it doesn't have a number in between inside the square bracket, which normally we will put a number to indicate the size of the array. But in this case it doesn't have a size because the size is dynamic. And what I mean by that, because it is dynamic to how many arguments user wants to put in. And it's actually controlled by RFC because we cannot defy a finite arrayed here because you never know how many arguments the user will enter on the command line. So this is why the square bracket is left as empty. And instead of putting a number, it uses our exceed to track the number of argument dynamically. And char pointer is another representation of a stream in C. So this is essentially means an array of strings. And the rate of streams. And these additional arguments are exactly a rate of streams. And this program will give you all the additional arguments. So you can do some processing here. Okay? Okay, so now we have all the user's input arguments and the number of them given to our main function. Okay? So now we can do some simple processing and maybe some verification of. These arguments. And one of the most common checking of this argument is to actually, to make sure the number of argument is correct. So for example, your program may be expecting three arguments from the user, three additional argument from a user. And you want to make sure you get all three arguments. Not less, not more, right? So to, to do this checking is very simple. You just add a if statement right here in the early stage of your program. So you want to do this as early as possible because if this is wrong, there's no point of continuing the program, right? So you can simply check the value of R xy and say if he does not equal to 3, if he does not equal to three, then this program cannot continue. And you want to give you an error and say invalid number of arguments. Expect to. Okay? And you want to return some error code like negative, negative one. And the program will stop right here. And now you may be asking why a Y2, Y2, very simple is because these are exceeded, includes the program name itself. Remember, I just talked about this. So in fact, these three means the program's name. Pay them. Just show you once again the program's name. That's 1 first argument. That's to the third argument that's free. So in fact, these programs expect two arguments, right? So that's why in my error message, I say I expected to to, to additional arguments from the user, okay, even though I have these three right here, but that's including the program name, right? So we can give a quick test. So let me quickly just compile again. And I can just run my program. And guess what? You just give me an error right away saying I have the invalid number of arguments expect to. So I give it an even one more, still an error. And I'm going to give one more. And now the program continues to run. Okay? So that's control C and quit the program. So that's how you check the number of arguments from the user. And of course, the better checking is checking the value of the arguments the user supplies, right? So the number, you make, the numbers right now you want to make sure the values are right. And something to keep in mind that whatever the user enters on the command line via, well, I'm a command line and with those arguments, they are all strings. Okay? These commands, they are all strings. They may appear to be a number, but really they are strings, okay? So when they are given to your program, they are given to you as stream. So therefore this type is char star, and char star is stream in C, right? So if, if you want to work with numbers, so for example, you want to take a number from the user. And that number, you want to put a limit to the number. It has to be larger than ten and less than 30, for example. In order to do the arithmetic operation, to do the comparison, you need to work with number. And so this is something that you have to do some conversion from string to a number, right? In order to compare if that number is valid. All right, So what you wanna do is I'm going to declare one more variable. Let's call this age my age, okay? And it'll default to 0. So now born yet. And once the argument, the number of arguments right there, I'm gonna go ahead and check. If oh, sorry. Before we do that, we need we need to convert. So my age, I want to have a value of a to i, arg v one. Okay? So what is line saying? Arg v at index one. That is the first argument the user supplies. Okay, So this is array. Remember, and I say one because arg v 0 is the program name. Remember, and that's something that we don't want. We already know our program. So whatever user inputs source from index one, right? So I want to take index one, which is a string. I want to feed it to this function right here. This is a standard C function that would convert. Ascii. Ascii is string characters into an integer, okay? And you will return an integer. So you see in the definition, and I want the return value to be put in this my age variable. Okay, So now this my age has the number. Now I want to work with. And of course I can do if statement right after saying if my age is first, it has to be a positive number, it can't be a negative number right here, my age is a negative number. You also want to say some error. All right? You want to say in valid age needs to be a part. So tip number, something like this. And then you also return. And if this if this checking pass, then you want to check, then you want to make sure my age is less than, or I should say, is not greater than. Oops, maybe 50. And you can also print a message saying invalid age 20 to write something like this. And then you'll return negative one and the program ends. Right? So we can give it a try. Okay? So let me pull up my terminal ray here once again. Just boom a program and remember we two arguments we have to, okay, let's start with 30. 30. Okay? Now the program continues right? Now. If I put this argument as negative 54, and you will say invalid age needs to be a positive number exactly as we want here. We want to make sure that's a positive number. And if we put something that's greater than 50, maybe 51. And again, you will print the error saying, Hey, invalid age, this is twofold, right? So that's how you can check a number based on a string input. And so actually let's do one more thing here is just print out whatever we have, whatever US has given to us. So once he passes all these checking, then I can declare another variable called I. This is my iterator. Iterator is something that I use to do a loop because I'm going to print out all the argument that user has supplied. And I want to do this in a for loop. And I want to do for I equals 0, I is less than arc c. And I plus, plus I want to increment I by one whenever the loops. So what I want to print is I want to print the counter no, arg v as a string, right? And maybe give a newline character. And I want to give the I variable in, inside the bracket as my index. So once he passes all the checking, it will print out all the arguments and the user has supplied, including the program name because we start from 0 and broken, we'll continue. Right? So let's just give it. Give it a try, shall we? Okay. So let me build my program again and run it. Oh, this tool steel. So let's do 43 and bank. So here it prints out my program as the first argument to the program, which is the program name itself. And the first argument the user supplies in a second. And then the programs are continuous because they pass all the checking, right? And of course, we were not checking anything for the second argument. So technically can be anything. You will still work, right? So that's how you can add a very simple command line argument to your program and do some basic conversions to do some checking to see the validity of the input argument. 16. If Statement: Hey, welcome back to another video. And today I want to talk more about IF statement. So IF statement is something that we have actually been using quite a lot in the previous videos. So that is the if statement. That is another if statement, and there's another one. So basically what it is is a conditional block, okay? So this block inside will only be executed if this condition is met, or in other words, evaluates to true. Okay? So I'm in, I'm going to actually create another function here is to do our if statement example. So I'm going to be creating a new function called IF statement example. And I'm not taking any arguments or expecting any return value for now. So I'm going to copy that and go to the header file first. We're going to be creating a new function. We talked about how to do this already. So it shouldn't be something, something new for you. And we'll go to the C file and add the implementation here. So it's going to return a void and takes a void. Okay? So throughout the course, I'm going to be creating more the functions. Each function is designed to illustrate a certain concept within a C program. So this is only will be continuous doing. And as we learn a new concept and have some example, we will actually build within my program and have them run each time so you can really see and learn how each C concept is being practiced, right? So let's go back to our if statement example function. So I wanna talk about a simple if else block. So let's just create some simple variables like this. And I can have my simple if statement. If a is larger than two, then I'm going to do a simple print. A larger than two. And not printing anything. Actually, I can print a equals to some value and have a right here. So if this is R1, this will evaluate to true because five is greater than two. So you will come inside and print out this message right here. So what if it is not greater than two, then nothing will be done. Okay? The program will actually come here and jump to here. Or alternatively, you can have something called an else block. Okay? So the else block is if, if this evaluates to false, doesn't matter what it is, it will always come here, else will be executed. So in that case, I can have another statement and say a is not greater than two because a is this value. So we will enter in the else block. Okay? So that's a very simple If and Else block. And there's a little trick here. If the if statement or I should say whatever you want to do after the if statement is can only be done. It can be done in one line. For example, a print f, that is one line of code. You can actually omit this curly bracket to you like that to make your program a little bit shorter. That's also a valid syntax, okay? But only if whatever you wanna do after the if statement can be accomplished within with one line of code. Same thing for else. The moment you have a second line of code like that, you see the equilibrium oil automatic dividend to make sure that you know that these two are not part of the same block. So these will get wrong if, if a equals to 2, but this will always get wrong. Okay? I should put it down here. Oh, this should we always be run because that's not part of the if and else. Okay, so be careful with with the bracket when you omit them, you want to you don't wanna, you know, misplace your statement at the wrong place. And another way to check the if statement is not using any operators. And again, we'll talk about operators later in the course. So right now we will just follow along and everything will be explained later of the course. So another way to check variable, you can simply type this, okay? If a. So what this means is if a is not 0, okay? So you see I'm not using any operator to compare the value of a. I simply type if a. So that simply means if a is greater than 0, because if it is not, if it is 0, then it is false. Okay? So use this with caution. Okay? So in this case, a can be a negative number or a positive number. Because this is, the statement is saying if a is not equal to 0, okay? As you put a comma here, if a is not 0. So this is another short, shorthand for writing a condition. If you want to check 0 or non-zero, or in other words, check the Boolean value. So there's only two possible value for this, either 0 or non-zero. Okay, So that's basically what it means. It is mostly most commonly used to check a pointer, something like this. A pointer to know. And again, we'll talk about pointers in later chapters. So a pointer can point to something or nothing, nor is nothing. So sometimes we want to check if a point is valid. We can do something like if B and I can print something like B is valid or B is not valid or B's no. Right? So this is very handy because when we go to the pointer chapter, we almost always will have to check if a point is valid, because working with pointer, You just have to be careful if it's not valid and you try to use an invalid pointer and most likely you end up crashing new program. So that's why it is a common practice when you're working with pointers. And say you want to allocate some memory to the pointer and you want to make sure the allocation is successful. And after the allegation you wanna do if pointer check. So if b, so if it is not 0, then it is valid. Otherwise, it is not valid or it means you allocation failed. Okay? So I'm going to put another print f here in say a is not 0, it is, Is this. Okay? So that's how we use the condition field without a operator like this. Okay? But again, using operator, that means you have some particular value that you want to compare to. And with those operator, you can actually, you know, combine multiple operators together. For example, you want to check arrange numbers range. For example, I want to check if a is greater than 0 and less than less than three, for example. And that's a condition two. And you can go on and go on if you have more conditioner you want to include, okay? And again, this double n is a logical operator, which, which means these two conditions must be met. Both, both of them. Okay? So n is logical and operator. And once again, all of these operator, we have a separate chapter for this. So right now just follow along and we will review this operator in the later section, okay? And of course this logical OR operator, right? We have an OR operator. And that is denoted as a double pipe, okay. If you're not sure how to type this, it is located right above your Enter key and below your backspace key. So you want to hold down Shift and click on that. Pipe character. And you want to type two of them that signals that represents a logical OR operator in C. So what this means is as low as one of these evaluates to true, then this will run. And this is both of them have to be evaluated to true. Or if you have more condition, they're all three must be evaluated to true in order for this if statement to run. Okay. So I'm going to just fill it out this blank with the condition that we know if he enters or not. Okay. I'm just typing out some random conditions and I'm not sure if they were actually enter. But the point is to show you how to write your if statement, right? As you can see if statement, you can have a lot of variations using different operators, right? So again, we'll learn this later. So right now, just follow along. And there's another handy operator that we normally use in the if statement, which is the exclamation mark, okay? And you may or may not know the exclamation mark means the opposite means not a logical not operation. So let me put this here. Logical NOT. So why? Where you can put this? You can put this right in front of your variable to check if we're checking the opposite condition. So for example, right now I have a pointer be pointing to nothing. So if he points to something, it will print it. And if it points to nothing, you will print this. No. So I can reverse this logic like this. To say, if not in B, then it is no. Right? Because when you know something, if it's not something that is already nothing, then you will get something. I know that's probably not a good way to explain, but if you have two negatives, double negatives, you'll get a positive. That's simply what it means. So if we do not be and you actually evaluate to true, so you will come to this block and else B is valid. So you can reverse your condition like this using the NOT operator like that. Okay? And what is I wanna show you. Ok, else. Okay. You can have more IF statement that's belonging to part of the else block. Because sometimes if you want to check multiple conditions, but you wanna make sure the first condition, only when the first condition does not evaluate to true, then you will check your second. I'll give you example right now. So for example, I have a less than 0. Okay? So I'm going to evaluate that to negative 20, for example. So if, if a is less than 0, sorry, I should say large vasogenic 0 is okay. And you want to make sure, okay. You want to have another if statement right here. Okay? If it is not greater than 0, then it must be a negative number, right? Then you want to see if a is greater than negative 10, for example. If that is true, then you will print a is greater than equal to 10. Else, if you have another one, you can have as many as you want. If a is less than negative or equal to negative 20, then we'll put another condition here. So one thing is when you're IF statement hits the else block, you can put another if statement right? After like that to check your second condition in case the first doesn't meet your condition, right? And you can do that multiple times by concatenating more if blocks right after, if. It is a good practice that at the end of your, let's say your blocks of if statements, you'd normally, you normally want to end it with another else block without additional IF statement, because there's a chance that all this r does not satisfy. You are conditions. And if that's the case, we want to put the last else statement right here to capture all the remaining conditions. So nothing gets loose, you know? So you capture every single possible condition. So you can say nothing matches. So something like that. Right? That's just a good practice. And okay, Another thing I want to show you is declaring a variable inside a if statement. Okay? So for example, I can make a equals to 10. Again. If a equals to 10, okay? Then in that case, what will be inside here? There are cases when certain condition is met, you actually want to do a whole lot of new different operations. And those would normally require to declare another variable like this. Let's call it c equals to 100. And you want to do something with that. So it's like saying that I don't have to declare all my variables unless this condition is met. So we can save some memory space by not declaring them right? Right in the front. Okay? But you have to be careful here. Some people program like this. They declared a variable inside the if statement like that. And then I'll print c equals to something to see, print the value. And you see they declared inside the if statement, but they want to actually print out or use that variable outside of the if statement. So I can have that C printing moved to here. And guess what? Eclipse give me a warning already. Seen that this C cannot be resolved. Why? Because it is outside of the if statement. So that's not a good practice because some, some compiler which is not compile at all because he cannot get, it cannot guarantee that this if statement will always be satisfied. Because what if you don't enter this if statement, whatever this evaluates to false, then this variable never declares. And then you're trying to print out the value which is not declare, and I will end up being a crash, right? So this is something that we don't encourage you to do. So be careful where you declare your variable. So in this case, if it's declared within an if statement, it is better to finish whatever you want to do inside the if statement and don't go out of scope. Okay? Okay. So I think that's a lot of variation of the if statement. So I'm going to be trying to, to build a program to see if you can run and to show you if any of these get actually get printed out. Okay, so let's pull up my terminal screen 1 second right here and do a make clean, make an error, okay? 90, okay. It shows me online IT I have too many arguments. Of course, because I didn't say I print something. So if I do this, then you expect the second argument and that errors should be gone. Okay? Just give me one more second. Okay. So the program builds and it's just skip it run. Oh, yeah. Yeah, that's from the previous example. I need to give some argument. Okay? Okay, so that's the printing example, okay? And another types on the number, okay, so, so these are the output. Okay? And all these should match the condition that we have defined here along the way along our example. So I'll leave that for you to examine if this will make sense. But they should, because they should be very straightforward, right? And there's another thing we wanna do. And it's better that we do this now is we want to make this to actually happen first. Because we'll be adding more functions in this example, right? And normally we want to, we want our new function to happen first, because all these are from previous examples and I want to leave them here on purpose so we can have everything together in one program that shows you kind of everything, right? So I'm going to move that to there. So instead of doing all these other things, we can just pay it. We can just draw things first. Okay? So that's the output from from, from the if statement example, okay? And I think one of the prince doesn't have the new line character or pay here, or actually in here as well. So because I missed a newline character, why when you see the output is like this. When it prints the last C equals to 103, it doesn't change to a new line. Okay? So I can quickly, quickly fix that. Pages. Hold on a second here. Okay, So let me make again, make. Okay, So now that's much, much better. Okay? So as our if statement example, I hope you have learned something of value and we'll see you next time. 17. Types of Loops: Okay, welcome back to another video. And today I want to talk about loops or looking for loops are, I would say, one of the most important elements in any programming language that controls the execution of your program. Okay? So as the name suggests, instead of going, going through the code line by line, you actually loop around certain number of lines or within a block according to some conditions. Okay? It's super common and super important in all kinds of programming languages today. So pay close attention here. So what I'm gonna do is I'm going to create another function to illustrate the looping function within C programming. So I'm going to call it loop example. Okay? So I'm going to implement this loop example and show you some of the most common loop techniques within C programming. Okay, so just give me a moment here. Let me create all these function prototype. So we'll be doing this all the time. So we probably will see me creating functions here and there all the time. But it's actually good for you to see it all the time so you really understand how, how to really edit it. Add a function, right? So that's our testing function. So the first loop I want to talk about is a while loop using the keyword while. And the way to use this is first you type the keyword and we say bracket and your condition. For, for example, that's my condition. My condition is why I got, I declare a variable a. So the star from 0, for example. So what this line is saying is, if a is less than 10, that would evaluate to true. So while that is true, he will enter this curly bracket and SEQ the logic inside. And you continue to do so until this condition no longer mess. Okay? So we can do some like will first print out the value of a, okay, as he enters. And then we won't do a plus plus. Again, this is the operator to perform the increment. Increment it means add by one. So what it means is or add 1 to a, and then that's it. So what is, what is going to happen is this is going to evaluate to true because a is 0 and it is indeed less than 10. So it comes in, it will print out 10 and you will plus 1 to a, and a becomes 9. So as long as the program hits the end of this curly bracket, you will go back to on top and check this condition again to see if it is still satisfy the condition. So after the first loop, a becomes one, right? We add one to that and is still less than 10, so we can continue to execute whatever that is in here again. And then you add one again, it will become two and then still valid enter again. So technically, this while loop, we're last or will loop ten times before it breaks out of loop, right? So we can quickly develop test. So just give me 1 second to compare the programs I can show you. It is indeed looping ten times. And I will also show you some of the technique or some other keywords that you can actually break in the middle during certain condition. So you don't have to actually loop 10 times. Okay. Hold on a second here. Okay. So let me just compile this program and okay, my program, okay. So I'm going to give the argument because we have some test. Okay, we're going to fix the print here. So let's add a new line here so it's easier to see something like that. Okay, let's try again. Make clean and make run a program. And as you can see, it prints the value of a ten times from 0 to nine, okay, that's 10 temperance. So that means it is actually. Sticking in there, loop for ten times, right? So sometimes you don't want to loop 10 times. Sometimes you encounter another condition within your while loop. So for example, if you don't want to loop 10 times and you don't want to really change. You don't really want to change this condition here. And you can have another condition in the middle and say if, if a equals to 5, I'm going to break. So break is another key word to break out of the current loop. So if you are in a wild loop and you have a break statement, you will actually jump out of the loop right away, regardless if this condition is met or not. So this is something that allow you to jump out of loop, given that you have another condition that, that is not necessarily related to the first one. Okay? So we can give a try again. And this time we should see it prints five times, right? And indeed it does print six times 025, okay? Because we start from 0, right? So that's one of the most simple loop technique we can use. And of course, you can have multiple conditions here, right? It doesn't matter. So you can have multiple conditions using the AND, or, OR operator that we kinda talk about aria in the if statement lecture. So that's the while loop. And another type of loop which is also very common, is called a for loop. Okay? For and to use the for loop, you declare like this. For a equals 0 semicolon, a less than 10 semicolon, a pause. So the decoration is a little bit are kind of more complicated in while loop. So what is saying is we want to start a loop from the condition a equals to 0. Okay? So similar to this declaration here, this simply kind of evaluate or they'll give it a value again. Okay? And we want to start from 0. And I want to stop when a is less than 10. Or I should say a, we want to stop. If a is less than 10 evaluates to false. So if it becomes larger than 10, then we're done. And every loop I want to increment a by one. So we have the a plus plus operator here. So this is, this is like combining this and this and this all into one line and have a loop that defines all your conditions all at once. Okay? So same thing here. It will print about ten times, right? And if I do that, I'm actually going to come in desert so we don't get confused. Or you know what? Let's, let's declare another variable b. So we can still keep the first one running. Sorry. That, change that to B and B plus, plus, and we want to print the value of B instead, right? So we can give that a try. So what we will see is the first one will break when a equals to 5 second loop, the for loop will actually loop for ten times because we didn't put any break in the middle, right? So I'm going to run my program again and see. So as you can see, a is printing up to five MB. Again, prints 0 through 910 times Exactly. So both are doing the same thing, but it's just two different style depending on sometimes your requirement. One is easier than, than the other. And actually I will show you later in the course. In some cases we want to use a while loop, and some other cases you want to use for-loop. Actually, there's one more type of loop that I have not shown you guys yet, which is called a do while loop. Okay? So it's similar to a while loop, but a little different. And I'm going to get into the difference later. Okay? So to use a do-while loop, first, you have typed that do keyword followed by a curly bracket, like that. In the end of the curly bracket, you want to put your while and your condition inside. Bracket. And don't forget, you have to put a semicolon in the end of the while condition in a do-while loop, like so. And I'm going to declare another variable called C. Okay? And have the loop does the same thing, again, just to print the value of C. And when you finish doing that, you want to increment the variable. So something like that. That's how you write a do while loop. So let's do a quick test. So let's compile this program once again to see how it runs, right? Okay, so let's examine the output here. A is pulling from 0 to five as expected, because we have a break in the middle and B prints from 0 to Pi. Like that. No problem. And see, does the same thing prints 0 to nine. Okay? So you may be asking, so what's the difference between a do-while loop and the while loop? They are the same thing. Why should I use do and not while? Or why should I use YL instead of du? Because it has a major difference here. So while loop and a for loop, these are also called an entry controlled loops. Okay? Entry controlled. And do while loop is called a look different, it is called an exit controlled loops. So what do I mean by those? So simply, it's simply means in order to enter this while loop. In order to enter this loop, you have to satisfy this condition. Like nice no exception, you just have to satisfy. Okay? If you don't satisfy, you don't enter, so nothing gets run, okay? Same thing here. You have to satisfy these conditions in order to enter. So therefore, these two are entreat controlled. Ok. And a do-while loop, on the other hand, does not have an entry requirement. You can enter regardless if you have satisfy this condition or not. But rather, if you want to exit, you are conditioned to what determine if you should accept or not. So the difference is you can always enter a do-while loop once, okay? And I think it's better to showing in example where all these condition does not meet the while or looping condition and you will see the difference. So first we will make a equals to 20, which does not satisfy this, right? Is not less than 10 or less than 20 is equal to 20, right? And again here we're gonna make it be 20. And 20 is not less than 10, so you wouldn't run. And we'll make a C also 20. So it does not satisfy this. So we'll try this and compile the program and see what happens. Okay? So there's Loma program and guess what? A is now printing. Right? B is now printing as well because they can now enter the loop to print, but see prints at least once. It prints c equals 20 here. So which means this do while loop is entered once. Because remember this is called an exit controlled loop. It doesn't control the entry, controls the exit. So you can always enter the loop. But once you enter, unfortunately, you do not satisfy the condition, so I have to kick you out. So therefore you will break in right away, 100 and how does it right away, but right after whatever the operation inside the brackets are finished. So that's the major difference between a while loop for loop and a do while loop. Thank you. 18. Infinite Loops: All right, welcome back. And today in this video I want to talk about infinite loop. Okay? So in infinite loop can be a wild loop. It can be a for loop or it can be a do-while loop. It doesn't matter. It can be any kind of those. As long as it stays in the loop forever or infinitely, right? And therefore the name infinite loop. So it's just a program that's gets stuck in a loop almost forever, I would say. Okay, so to illustrate an infinite loop, so let's go back to our program here. I can simply do something like while, one and bracket, something like that. That is an infinite loop because one is always evaluated to true, okay? And as long as it's true, the program will come inside the while loop a block and will never exit. So if I put this here, that see what will happen. So just give me 1 second to build this program, and I will show you what, what infinite loop is. Okay? So let's make here and that's run. So what do you see? My program is kind of get stuck at this point, right? It's not doing anything. And it prints c equals to 20 because we enter this loop example and there's a do-while loop, you will always enter four ones and the condition does not satisfy, it breaks, and then followed by a infinite loop right here. So what this is doing right now is essentially nothing, okay? Because we didn't put any logic within the while block. And so it's doing nothing and you will not continue. You are not break out of this loop. It will just be stuck here forever, of course, until you terminate this program by force, right? And in the meantime, I want to show you something else. Let me just pop another terminal here. And on Linux, you can type something called a top command. Okay? So the top command will show you all of your programs running in the background and the memory usage and the CPU usage, and of course the user, right? And I was, you want to show you something about this. So as you can see, there's a program that consumes 100% or almost 100% of my CPU resource. And guess what? That is, my program, the program with an infinite loop right here, running, doing nothing right now. So in other words, my program right now is consuming all of my resource, my CPU resource, doing nothing. Right? I know it sounds funny. Doesn't. That's actually what it is. So my PC right now is be very slow because there is a program right here taking all of my CPU resource, doing nothing stuck in a while loop. It's trying so hard to, to do this loop by doing nothing. Okay? So that's what happens when you have a while loop. And it is not necessarily a bad thing because a lot of application actually have infinite loop. Or in other words, or I should say that they don't call this infinite loop. They call this a control or a main loop. Okay? So you may use some application that when you run the application, you will be stuck in the kinda of like a main loop. And keep checking if the user has input, some subcommands. Or to keep checking if there's some message being received. And in order to do that, it needs to be inside a loop or infinite loop, or you want to call it as a main loop to keep checking user input, for example. But with a catch in those applications, you probably won't see that they can see 100% of your CPU, right? Because that would be a bad programming practice. Okay? So infinite loop is not a bad thing. A lot of application, the application is have some sort of infinite loop. But in order to make this not consumed 100% of your CPU, we normally would do something like a little bit of delay. Okay? So for example, delay of 0.50.2 seconds. So with that delay, and of course you can add some logic here to check your user input or to check if your program has received some message from the network, for example. So why you're doing is checking, you know, infinitely you, you always want to put some sort of delay in the end or in the, in the beginning. It doesn't matter. This delay allows CPU to have a break, right? And in order to use sleep, I need to actually add one more include, because that's part of the standard library as well, is from you. Nist d dot h. Okay? So I'm going to put them up here and I'm going to do is add asleep statement. And I'll show you what happens when you add a sleep. So just let me pull up my program again. Okay, so I'm going to shut this down and compile my program once again and run it. So again is still doing nothing and it's still stuck in an infinite loop. But this time with 0.22 delay. And guess what? You see my program up here. It is no longer consuming 100% of the CPU. And right now it's 6.6% CPU resource. That is still quite a lot, okay? Because sleep is very short. Okay? So if you increase the sleep to something like 0.5, okay? And we're gonna give it a try and see how much resource you will consume this time. So let me clear that again and rebuild my program. And run is again, still doing nothing, right? And now let's go back to top labor, lower LSA, but still it is better than 100%, right? So what does that tell you? So with some sort of a delay here, you're actually allowing the CPU to have a break, to do some time slicing, to do other task, okay? So the CPU is over by instructions and time slicing to process all these tasks in parallel. And by having a sleep right here is a standard practice for many applications that has some sort of main loop in place. Okay? So this is something that I want to show you today. And thank you, and I'll see you in the next module. 19. Break vs Continue: All right, Welcome back. In this video, I want to talk about two important keywords that you can use within your loop to make a good control of us occlusion. And the first keyword is break. And in fact, we have just used break in our while loop example, which under some condition, we will break out of the loop such that the loop will not SEQ anymore. You will actually be outer loop and continue the program, Right? And the second keyword is continue. Continue is kind of opposite to break. So instead of breaking out of the loop, you will actually go back to the beginning of the loop and try to do the same thing again within the loop. Okay, so still in the loop, but continue to the next iteration. That's what a continuum keyword do. And it is better to give you an example. Okay? So we had an infinite loop here. So in order for our program to actually continue, I am going to put another, let's say D equal to five. And we'll break pay. And we'll make a new variable d, like that. And everything loops. We will add d by one. Okay, So I think this is very clear for you. We literally just had a similar example back then. So this is something by accept that the loop is a infinite loop. But within the infinite loop, as long as this condition is met, we'll break out of the loop. Okay? And was delayed zero-point five seconds every time it loops. Okay? And now I'm going to give you another example about continue keyword. So to illustrate that. To illustrate the continue keyword, I'm going to declare another variable called e equals to start with 0. And I'm going to be using a for loop. And I'm going to start from E equal to 0 and less than, say, 20. And every time it loops, I want e to be incremented by one. So like that. So I have a for loop that we'll go 20 times. All right? And again, I'm going to add the print E equals to whatever value it is. And here's what I'm gonna do a little differently. Okay? So I'm going to skip some of the value. So in other words, I'm, I want to control, say if e equals to 5, I actually don't want to print out five. And rather I want to continue to the next iteration of the loop. So that's where you want to use a continue keyword to accomplish that task along with IF statement, of course. So if e equals to 5, we don't want to print out the value. And we can simply go continue. Okay, So what is, does is when we loop to the fifth time and hit this condition and evaluates to true, it will hit Continue. And you will just go back to top of the loop, increment it by one, and continue to the next iteration, where equal to 6. Okay? So when E equals to five, it will not come down here to print the value. Okay? We can do like this. We can add more condition. So we actually want to skip, let's say, three values, okay? So if any of these is true, it will continue. It will go back to here and not print the value. So let's give it a try and see the trend. Let's just compile this program and see how it works. Okay? So unless there's clear my screen and Let's make and run. Okay, so first we have 20. Okay? So that's from a do-while loop earlier here, actually, right here, okay? And then we have E going from 0 to nine, or actually E from 0 to 19. And you may see that you can notice that it doesn't print out five, right? It goes like this, 01234. There's no five because there's a continuum here. Actually skipped printing five, right? And let's keep going. And there's also a 10 missing because we put the right here that we want to skip ten in the printing. And again, 15 is missing, right? And there you go. So that's how you can use a continue to go back to the top of the loop without going further within the loop. Loops logic. And you want to use brake when you break out the loop under certain condition. 20. Switch Statement: All right, Welcome back. In this video I want to talk about the switch statement. The switch statement is, it's very similar to the if statement that we have already covered in the previous module, but with a completely different structures and with a completely different purposes. And I will show you, in some cases we want to use a switch statement. In some other cases we will use IF statement. So let me first go ahead and create a new function in our example program called switch example. And inside that function is where we will illustrate the switch statement syntax and explain to you why this is important. Okay, So let me just quickly and is function prototype. Of course we have to add the implementation in the C file like that. Okay, so let me first, by giving, giving you the basic structure for, for a switch statement and our explain after. So first I want to declare a variable called selector. And I'm going to evaluate that to, let's say letter a. And then I'm going to start writing out the syntax for the switch statement. So which we will begin by putting the switch keyword and with a bracket. And that's where you want to put your selector variable inside the bracket. And then followed by a curly bracket to enclose a block. Inside the block, you want to use another keyword called case. Okay? So this keyword is where you want to have some evaluation of that selector. So for example, case a followed by a colon, colon sign another curly bracket. And normally I would do indentation to just to make it very clear. And with case a, I want to do some printing. I'm going to bring Apple when it's a. And then I will put another keyword, break. And we'll talk about break later. It essentially means, okay, if this case is evaluated, get out of the switch statement because we're done, okay? And you can put as many as this case block as you wish, as you required. And in this case, I'm going to put four cases from case to case B, C, and D. And each case, I'm going to bring a different characters, different words before banana, c for cars, for example, and D for dog. Okay? So what this is telling you is very similar to a if, if statement. So if the selected equals to a, i will do this. If a equals to B, I will do this. See you, I'll do that. And d o print dog. Okay? So normally, at the end of these possible cases, it is a very good practice to put additional block. And it's a special type of blog called default. Okay? So when it hits this default, that means the selected does not match any of these cases, okay? So therefore you will fall through to the default block. And this is where we'll print unknown letter, for example, because it doesn't match whatever we were looking for. Therefore, we will say unknown error or no letter and followed by a break. Okay, so we're done with the switch block. Please exit. So that's a very simple structure of switch statement. So let's give it a try and see how it works. Okay, just give me a second to pour my terminal right here. And let's do a compilation. Pay and compilers is done. And let's run it. And as you can see, it prints the apple, right? Because my selector is letter a. And if I change that to let us see and compile once again and run this program. And you are now say car. Okay? So it's very straightforward, right? And now I'm going to tell, and I'm going to show you the importance of this break keyword within a switch statement, okay? So this is where you have to be careful with switch statement with, with the use of this break keyword. Because something bad can happen if you misplace or misuse the break keyword. Because within a switch statement is the idea of something called a follow-through. Okay, I'll explain what that means in a second. So now let me add a couple more conditions, okay? F, g and h, and k I k. So now I have four more conditions. And like before, I'm going to print something with a break statement. And this time I'm going to print, let's say fish, gorilla, hat, and igloo for words that represent this single letters. So if my selector is one of these, f, g, h, i, let's say g. It will come here. But you may be asking, Okay, if the selector is G, there's no logic defined for G because we didn't have a block like this that tells what to do when the case is G. Okay? That's what a lot of people think. What will happen? There was. Nothing will happen because we don't have a definition for g. But no, no, that is actually wrong because we think a switch statement, there is a concept of four through. Okay? So basically what this means is that if you select a is g, Okay? You actually, the control we actually move to here and forth, x2, which means it's going to drop down and do whatever you have within below the case g before it reach a break keyword. So I'll show you what that means. So let me just copy this again to my Linux machine and let's compile. Let's see. Let's run the program. And guess what? It prints fish, gorilla had an igloo. It prints this. Even though my case is not, I write because of something called a fourth X2 will actually start from case g and literally drop the control. And you will execute whatever that is in here below until it sees, it sees a break keyword. Okay, so it's one of the unique property of switch statement. And this is something that you have to be careful and best practice is always, always use the keyword break if you are done with the evaluation. Okay? So for example, if I will move this break, okay? According to the property is going to fall through. So will fall from g and h. And I am print this and forced to default and print this until it hits the break, which will get out of this loop. So that's Give it a try. Do you want to show you what I mean by fourth rule? Okay, Oops. Make my program. So now you can see you actually prints fish, gorilla had igloo, and also the unknown letter. You see four through the default as well. It just keep falling and doing whatever you tell it to do, even though the condition is not really what you are looking for. You want to process when e is equal to g by actually does more than that, you actually follow through and process everything down below G. So this is only have to be careful with break. And the best practice is use break anywhere. You see necessary. Unless you intentionally want them to fall through. For example, f, g, h, i. You can have the same processing. For example, this print when all these cases satisfies, okay? But when it comes here, you don't really know what selector really equals two. You only know is either F, G, H, or I. You don't really know what exactly it is. So if you do need to know, you will need to put an if statement here. If selector equals to, I don't know, h, and then you do something, right? So that's not something that you would actually use because you have the if statement. So you have the switch statement and you can simply put another case down here for H, down here for g like that. So someone like that. Though. Yeah, just in summary, I just want to show you the importance of the fourth root property within the switch statement. And a good practice is used to break whenever you require. Okay. And socially, under every single case. Okay. Okay, Now that's the switch statement. And right now I want to show you and I want to talk a little bit more about why we want to use a switch statement over the if statement, okay? So the switch statement, as you can see, is it does not based on the evaluation of true or false. Because remember in the if statement right here, the evaluation inside the if bracket is either can either evaluate to true or false. It can only have these two possible outcomes, true or false. If true, run this. If not, I will run this. But with a switch statement is actually more than that. It's actually based on the fixed data values. Since there is no such as, such thing as true or false, is actually looking at the actual value. If your values a do that, if it's B would do that even see we do that. All right. So that's one of the major difference. So depending on your condition, if you if you just have a simple condition like this and you're just checking simple values, simple fixed values, then switch statement is more suitable. Okay? So if condition is more complex, it's not just checking fixed values. Maybe you want to check. A more complex checking. Maybe you want to use the logical OR logical end. Maybe you have multiple conditions concatenating together. Then in that case that's more complex. And that's where you want to use a if statement. Okay? And switch statement has another property is that in terms of speed, speed, I mean, how much does it take CPU to compute a switch statement? It is generally faster than an if condition, because if requires more checking from the CPU. And whereas switch statement each other simple value lookup. And it can be easily done with a hash table lookup by the, by your computer, right? So in general, switch statement tends to work a little faster. And especially when you have a lot of cases, okay. They say you have more than five, let's say you have 25 or 50 cases with different simple values. Then if you have that many cases, then switch statement can really take advantage of the hash lookup and you can actually process a little bit faster. Okay? And another advantage of the switch statement is the readability. And as you can see, is well-structured. And it's easy to see what logic to implement under what condition, right? So in general, is, is more friendly to know to developers because it easier to see and understand what your logic is. And if statement on the other hand, if you have a lot of these, if else, if else, if else, maybe you can get really long, right? Because your condition can be a lot of things in there. So it's not as friendly in terms of readability. Okay? Okay, So that's pretty much all the basics about switch statements. And I hope it's been really helpful to you and hope you have learned something of value. And I will see you in the next module. 21. The "goto" keyword: All right, Welcome back. Today in this video I want to talk about the go-to keyword in C programming. So the goto statement or the go-to keyword, allows you to do a unconditional jump from the location of the go-to keyword to a specific labeled that you have defined within a function. Okay? And this kind of an unconditional jump, I think it's normally discouraged to be used in many programming languages because it makes it difficult to trace the flow of the program because you are just basically jumping around between the code. So it makes harder to trace the program and maybe make it harder to understand or to modify, okay? And it is generally can be replaced. So you can write a program without using any of the goto statement. But today, I want to show you what it is so you understand. And I will actually show you one of the, one of the many uses, die personally use with the goto statement. That makes my, you know, my function well flow a little better according to my preference, of course. So let's go right ahead and do an example of a goto. So similarly, I'm going to make another function called go-to example. So it's the same deal as we have done before. So every single feature in C, we are going to be creating a new function to illustrate the functionality, right? So we go to the same deal, but this time I'm going to return an int instead of void and out. I'll show you why in a bit. So let's go ahead and add the function prototype. And there we go. Okay, so we have the function defined. So I'm going to be writing out a simple program that actually checks this function, checks a user identity, for example, okay? And in this function, I will be using the go-to keyword to illustrate the way I process errors. Okay, so let's go ahead and create a new variable called name. It's a char star pointer. And again, we'll be talking about these datatypes in other modules. So name is Keri, that's my name. And I'm going to define another variable called age 32. And maybe another variable called occupation. Say programmer. Okay? So I have three kinds of variables that I need to check if they are valid, okay? And the result, I'm going to be storing the result in the variable called red, red for return. And I'm going to initialize to negative one. Okay? And I'll show you why in a bit. So this function, I want to check the identity of this person. So first I want to check if name is valid. Okay? Or I should say, I want to check if the name is not valid, okay? For example, the guy doesn't have a name or the variable name does not point to any values. So I'm going to be using the NOT operator to check for the error case. So if the name is no, meaning it doesn't point to anything. With a nod, you will actually enter the loop. Sorry, sorry, not a loop. The if statement block, okay? So if he enters, that means this name is not valid. So I'm going to be print, print an error saying, Hey, actually I'm not going to print anything. So I'll show you why in a bit. I'm going to say red equals negative two and go to error. So I'm going to make an unconditional jump to error. But this label has not been defined. I'm going to be defined down here. And you can define a label like this with just a name and a colon. And normally we will put this all the way to the left without any indentation. So like that. And I'm gonna return red. Okay? So in the case this error is not captured, meaning the control doesn't go inside. You actually continue processing the program. And if you continue to this point, I'm going to set return value equal to 00, means no problem. Yeah, I should make it more clear here. And returns 0. Success. Otherwise, failures with different return codes, or I should say error codes. It's very important because this is one of the use case that I actually like using go to and some label because it makes the control flow much better. And again, I'll explain why in a bit. So let's check couple more things here. So if name is now valid or say the return value to negative two, and we'll go to error. If the age is not valid. For example, if age is less than 0, then we'll set the return value to negative three and again go to error. And if the occupation is not valid, then we will said the return value to negative four and go to error again. So what am I doing here? Okay? I have this function that does a couple of things, okay? And right now, and for example, I'm just checking the value of this. So if at any point any of this results in error, because I'm only checking for error. If there's no error, I'm not interested. I want the program to continue. If any of these resulting error, you will update the error code depending on the type of error that he has encountered. So negative two means name is not valid. Negative three means ages Nevada. And while negative four means occupation is not valid. And I am going to set these red return value to the corresponding error codes. And I'm going to go to error which jumps right here. And you actually skip this line, which makes the return equal to 0, which is assess, right? If we count here, there's no subset. So i'm, I'm, I'm just gonna go to here error and return the return value, whatever we have said. Okay, so, so why are we doing, why are we using a go-to keyword? Well, you don't have to. But for me, I like to use go-to because actually returns, this function actually returns in the end. And I like to put the return statement at the end of the function because e increases my code coverage. I know we haven't talked about code coverage, but there are some tools out there that's kind of checking your program's code coverage to kind of evaluate how, how good you are programming coding style is. Because generally we want as many or as much coverage as possible in terms of co-writing. And if you have the return statement right here, no, you don't have to use go to, you can just put return negative two right here. And none of these will get run, right. You can do this instead of settled error, error code and put a goto statement, you can just return negative right here. Yeah, it's quick and there's less execution. But you're skipping this big chunk of code to be processed because he literally just in the middle, you exit. And these parts, they are not covered and therefore resulting in a lower code coverage rating, right? So both works. And I just want to show you what the style I like to structure my function. And especially function with error checking. So that's the way I like. And at the end of and when this function returns, okay? So this is color, this is where I'm calling a function. I can make another variable called red equals negative one and make that equals to the output of this go to example. And in here I can do a switch statement. Switch on red, right? We talked about switch Saman in earlier module. And k is negative one, negative two, negative three. So we can capture the type of error that results from that particular function like this. So in other words, you can fine tune your error codes, okay? And default, default, I'm going to make a success. So I'm going to be printing. Okay, let me just quickly finish the coding. Is return 0, so that's success. Okay? So I'm going to be printing error message here. So negative one, if I remember, is invalid name, right? And negative two is invalid age. And last but not least, this is invalid occupation. All right, so that's how I would structure a function that does a lot of error checking and with the goto statement to make the control flow better, to have the full the full 100 percent code coverage. Actually, I may have misplaced the error code because we actually start from negative 2 and up to negative four. So I need to go back to my program does C and kind of fix this error code because I didn't start from the right number. So now I actually start from negative two, which is invalid name and so on, right? So I need to go back here to make sure they are matching the error checking and return value. Okay, so they should be matching. So if I were to change any of these to a invalid value, for example, a law for occupation. You will actually get captured here and you will return the proper error code and also have the full coverage with the goto statement, right? So let's give that a try. Okay. So let me pull up my terminal once again. Let's rebuild our program and that's run. And now you should see that it prints invalid occupation right here, because we have an invalid occupation, NDI capture the error and it will return a negative four. So he's captured and handle right here at the college side. Okay. So this is one of the many cases of to, and I know a lot of places using our resources, they don't encourage you to use Goto at all. And saying some Malaysia avoid Goto at all costs or something similar to that. So I wouldn't say it's completely useless. And I actually enjoy having a go to in a function that checks for errors because he makes this for actually better rather than make it not clear, right? So I'm just going to share with you what I like to use. So I hope you found this lecture useful and let me know if you have any questions, right. 22. Bit vs Byte - Decimal vs Binary: All right, Welcome back to another module. And in this video I want to talk about some basic stuff like how the computer represents numbers. Okay? This is super important because in a later module we'll be talking about data types and the storage size of each data type. So it's super important to understand how the computer represents numbers in the first place. So when we are talking about sizes, you have a good understanding of what I'm talking about, right? So let's start with the basics here. Bid, this is a byte, okay? So a bit stands for binary digit. That's the basic unit of information in order the computer season we know today. And it can only be a one or 0. Okay. May have heard that your computer is a binary system because he only recognizes one or 0. And when you translate that to the actual hardware. And you also know that your computer is consist of trillions gazillions of transistors, right? And transistors and output two values, one or 0. So it corresponds directly to also the hardware part of your computer system. So this whole thing, we also call this as a binary system, okay? Because it says either one or 0 and bi means two. So 100. So for example, a one that is one bit. And there are two possible combinations, right? So 1, 0, 0, so two possible numbers, okay? And I have another example here with 11, that is two bits, okay? And with two bits, you have four possible combinations. And which four does that? It can be a 0, 0, 0, 1, 1, 0, or 1, 1. So with two bits, you are able to represent four possible combinations, or I should say numbers. Okay? So that's the basic of bit, okay, Just 100. And now we are talking about byte pain. That's the most common measurement that we're using in today's world. A byte contains eight bits, okay? So eight of these forms one byte. And just saying example here, for example, 0, 1 0, 0, 1, 1, 0, 0. That's eight bits consisting of zeros and ones. And these as a whole is referred to as one byte. Okay? And when you have one bite, you are allowed, you are able to represent 256 possible combinations. So in other words, if you have a data of size one byte, the most, the biggest number that can represent is 256. You cannot go larger than that, okay? If want to go larger, then you have to add one more bite, which I have in the second example, a two byte value, okay? So to base consists of 16 bits in total, as you can see right here. And with two bytes, you are able to represent up to 65000, 535 possible combinations. Okay? So as you can see, the more bytes or more bits you have, the larger the number you can represent. And this is the fundamental of all its data type that we're about to cover in the upcoming modules. Okay, so now I want to talk about some common measurements. You may have seen this before. I'm sure you do. If you have a computer, you have seen all this before. So we storage, we normally say a kilobyte, megabyte, gigabyte, terabyte, right? It's very common today. And one kilobyte is 124 bytes. Okay? And one megabytes is 124 kilobase, one giga is 124, mega, one Tera is 124 Giga. Okay, So you probably have seen this already. And that's storage we refer to represent how big your computer is able to store data. You have one TB, one terabyte hard drive, or 1000 terabytes, right? And we just storage. You probably also have seen a measurement for transfer speed. For example, you want to copy some files from your computer to a USB drive. Then there's a window that will pop up and saying how fast it is transferring the data. And you may have seen something like one kilobyte per second, one megabyte per second, or 200 megabytes per second, one gigabytes per second, for example. Just a representation of how fast your computer is transmitting data. And like before, when k is 1024 bytes, one megabyte per second is 1024 kilobytes per second and so on, right? So you notice and what I want to talk about network throughput, okay? It's a little different than what we have seen so far. And you may have seen this before with your internet provider, Right? So I don't know if you are using optical network or a cable modem provider. And they will offer you some packages. And you want to 150 gigabit package or 300 megabit package. So what they mean is the network throughput. Or you want to call this the speed of your internet. All right? And of course, the bigger, the faster the network is. So you may have seen one kilobit per second, one megabit per second, or 300 megabit per second, or one gigabit per second. So as you can see the units a little differently. So it's not like slash S represent per second. He actually uses k bps kilo bit per second. And also note that the B here is a lowercase b. If you see a lowercase b, that means bit, okay, not a byte. If you want to refer to a byte, normally we will put a capitalized B to represent a byte and lowercase b for a bit. And we use megabit, Some, most common measurement to measure your network speed or how fast you are getting with your Internet. I am using 600 megabit per second network. So my Internet is pretty fast to today's standard. But of course, there's optical network. We can go even, even more faster, right? So that's the three common storage, measurement of speed and storage and throughput. Ok. And now I wanted to talk about a little bit more about the numbering system that a computer use and we humans use. So there's comes to the idea of a decimal and binary. Okay? So the decimal system is the measurement system that we are using today and the one that we understand. So for example, when I say one kilometer, I mean they are 1, 0, 0, 0, 0. Okay? But that's just the way we represent numbers. But your computer. And so is my computer or everybody's computer, most computer system, they are of binary systems. So which means late, don't really use the decimal system to represent numbers. Instead, they use something called the binary numbering system, which is base two. Because the one or two, right? When you have more, you want to jump into next digits. But we're using a base 10. So that introduced a discrepancy right here. Because two are humans. When k is 1, 0, 0, 0, 0, 0. But to the computer one k is 124. Right? So why is this important? I will give an example. Let's say you buy a one terabyte hard drive when I hooked up to your PC. And, but it turns out the PC says, Hey, you only have 952 gigabytes in this hard drive. So you may be wondering, Hey, how come it shows less than what I have purchased. I purchased one hundred ten hundred byte, but my computer is showing me 950 to do somebody. Still some of the bias from my hard drive. It's been around for a long time and actually nobody actually steals bytes from your hard drive. What happened was the different methods of representing the number. Because you hardware manufacturer, when they say one terabyte, they actually mean 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0. Okay? But to the computer, it represents the entire number of bytes and represent that using the binary system, which is based on when k is 124 mega wonderful and 24. So it's not really 1000. So after that, after this conversion, it will appear a little bit less than what you have purchased. But in fact, you are getting the exact number of bytes you manufacturer provides, okay? It's just representation of this number is different and therefore appears that you have lost some bytes. But in fact you didn't lose anybody, just that little representation. So this is again very important to understand these base 10 numbering system in a way we're using today and a base two system that computer is using today. So you understand why they are sometimes when k is 1, 0, 0, 0 sometimes is 1 third and 24. Alright? So that's all for today and I hope is useful to you. And if you have any questions, feel free to ask. 23. Common Data Types and Variable Declaration: All right, all right, welcome back to another module. And today in this video I want to talk about the common data types in C programming languages, okay? And so you can see right here, I have a table of all the integer data types supported in the C programming language. And they come in different sizes and different value ranges, okay? The size here represent how many bytes you will occupy in memory, okay? And int, which stands for integer, is the most commonly used data tape that we have been using to talk about in our previous examples. Okay? And so you can see it has a value range from a negative number, the big number, to a positive number, also quite big. So which means if you declare a variable with type int, it can be either positive or negative. Okay? But I want to show you that this Unsigned version for this integer datatype, which is controlled by the keyword outside that you put in front of the int, which also has a four bytes storage volume. But it has a different range. As you can see right now, it comes from 0 to a much bigger number than this, about twice as much. So why is the range like this? Okay? So it has to come down to our previous module about bits and bytes. So we know that there are only four bytes of space to store. The integer datatype. Signed or unsigned, Doesn't matter. They all take four bytes. But with the sign version, which is int, we actually have to reserve one of those bid to store the sinus of the number. So we, we actually are left with 31 bit to store the number because one of them is a sign, right? And that's the reason why with a signed version, the range tends to be, well, a little bit smaller, but in fact they are the same range, but it comes from negative to positive. It's still the same range, which if you take this number minus this, you probably will get this same range, but including half to the negative space and the other half to the positive space. So whereas with the unsigned version, everything is in a positive space. So you can actually go to a larger number then this, okay? So that's the difference between signing outside. And there are data types also. So there's a short which occupy two bytes, and again, signed version and the outside version. And there's a char and unsigned char. We use this before to represent characters. Remember, and I will tell you how these numbers can actually be used to represent characters later. And of course, there's a long. Long is the largest datatype that you can hold eight byte of data. So again, you can see it is a huge number that you can support in the value range. And unsigned long or even bigger, right? So these are the integer types. And now I want to show you the floating datatypes, which can include decimals places. And in C we normally use the float datatype, which occupies four bytes, and the double, which occupies, oops, eight bytes, okay? And it has different ranges. And you can support our two different decimal places. Okay, So we'll talk more about this later. So now let me pull up my, my code here. Our example program that we have been building together in a course. And I actually went ahead and add a new function called datatype example right here, and which is already implemented in the myfunc step C, like that. And I have also added the prototype in the header file. We've been doing this for other course, so it's shouldn't be now familiar to you, right? So in this function, I actually go ahead and declare all those variables that we have seen in the PowerPoint, starting from the end, too short to char and long, and actually a couple floating point values. So what I want to see, what I wanna do right now is actually compile this program and print out the storage sizes, okay? Using the size of operator. So this is a very handy operator that you will be using. Well, you should be using in your career with C because this tells you the size of whatever you give in, like how many bytes they occupy in the memory. So let me pull up my terminal right here. And that's brutal program. And as one. So you can see right here, I have the int size of int equals two for four sets of shorter 2 and 2, 1 and long equals eight, float equals to 4. And my double has size eight bytes. So they match exactly to the PowerPoint, the PowerPoint here. Now we will talk about, right? So now there's one thing I want to mention, something about char, okay? If you remember, we use char to store a character like this. For example, a, and we use a single quote to represent a single character. Remember, single quote is a character, double quote is a string, okay? And we'll talk about string later in the course. So now a single coil represent a character. But I did not tell you that. You can actually make that equals to a number. So one number you may be asking. So for example, 65. I'll show you the significance of 65 later when I show you something called the ascii table. Okay. So I was just want to quickly mention one thing. So you see sometimes I will declare my variable like this, and sometimes I will declare my variable and initialize to some value. Okay? So again, I just want to share an experience from my side is that we always want to declare a variable. And if you can initialize to some initial value like this, okay? If you don't know, you can give me some rent, some negative number or 0. And you don't have to initialize all the time or always. Because as long as you will be using this variable and give it some value, then that's okay. But it's just my practice. I like to initialize all my variables to some initial values. Because if you don't do this, then they will have some random initial value that, well, sometimes you may forget to use your two to give us some values, and you forget about that and you start using the variable and you get some random valuing crash your program. So with initial value, oh sorry, that's not a long, sorry. This is the I initialize this a little differently. I use 0 because these are floating point numbers, then they tend to store decimals. So I want to make sure my decimal is also included like that, right? So now back to this chart. Why do I make that equal to 65? So let me show you this. I want to print out this chart in two different ways, okay? So I'm going to print my char to integer value. Remember the print f. We can format a variable using this person operator. So percent d represents integer. So which is saying that please print my variable, my char as a integer. And guess what now will print 65, right? But at the same time you can also print my char is. Okay, let me correct that. By char as a character is percent c. And again, the variable is still my char as integer. So what's going on here? I want to print my char as an integer, but at the same time as a character. I know you may not make sense to you right now, but let me just go ahead and run this program. And I will show you what's going on. Okay, so let me clean this program, make again and my program. Okay. So now what do you see? My char as integer is 65. That's exactly what it is as we initialized. But you also see my char as a character equals to a. Okay? Let me fix that quickly. Let me use a newline character so it's easier to see the character. Okay? So it's just one more time and run. Okay, now, so my char as a character is a. How is 65 equals to a? Because remember, they are the same variable with the value 65. And how can this is a character a y is 65 Eight. Well, let me tell you why. There's something called the ascii table right here. Let me resize this a little bit. Okay? So ascii table stands for American Standard Code for Information Interchange. So basically every single character that you have no or you see on your keyboard is actually a number or represented by a number, okay? So for example, for letter capital a is it is represented by a decimal and decimal numbers 65. You see that? So 65 is a basically. So this means that whenever you work with streams, streams are just seeing a rate of characters. When you are working with streams, they are actually a array of numbers. Still wish number, these numbers. And each number represents a character that you have in the stream. Okay? So this table, during my work, I referred to most of the time, all the time actually, when I was, when I'm, when I'm working with streams. Because streams, they are numbers. And these tells you what number they are. Do you have a good reference, right? So that's why this can be a number and it can be a character. And normally we use that with a char because char only occupy one byte, right? And each character, you see right here, they're exactly one byte. All right? So if you have a stream of five characters, you will take our five bytes in your memory. Okay? So that's what I want to talk about today is regarding to data types. And I will see you in the next module, okay. 24. Scope and Lifetime of a Variable: All right, all right, welcome back to another module. And in this video, I want to talk about the lifetime and the scope of a variable. Okay? So, so far we have been talking about declaring and initializing a variable. And then we'll just start using it, right? And it is also very important to understand the scope of that variable and the lifetime of that variable. So you know, when it is valid for and who has access to that variable, right? So to give you an example, well, we're actually using the same example in previous module called datatype example. So you see we have a bunch of variables here are declared. And these variables are declared inside this function. Okay? So when you declare a variable inside a function, these are referred to as a local variable. Local to this function, okay? So which means within this function, which means these variable can only be accessed and modified within this function. And when this function ends, meaning executed the end of the line here, this variable will become invalid. Or in other words, the, the operating system will recycle these memory spaces so they are no longer valid. So the lifetime, well, it starts here, the moment they are declared and it will end soon as this function returns. Okay? And this scope is within this function. Alright, I think that's very clear. And now I want to talk about another case. When you have variables does actually pass inside a function as an argument. So there's a my int. Okay, Let's use another name, my, my arc. Okay? So if you have a variable and that is, that is passed into a function from the color. So for example, here in my program, when I call datatype example, I want to give that say my input. Okay? So I actually pass my input to this function. So now the question is, what's the scope of this variable? Is it within this function? Or It's still valid because it's declared from the coders function. Well, the answer depends. So this is something in C that, that is very important about passing an argument. You can either pass an argument by value or pass by reference. So that's, so I'll explain. Right now. I have an integer called my input, which has a value of 350. Now I want to pass that variable. So when I do this, I am passing by value, Okay? So just the value 350. So which means when this function receive this value, you will actually make a copy of that value, which is 350 and stored inside another integer variable called my arc. You see the names are even different, right? This is called my input here. Inside a function is called my arc. So if I simply print my arc, okay, just hold on. Let me type this out. Okay, I'll print the value out. My art. You'll be 315, okay? But these 350, and that's 350 are different. Okay? So which means if I make changes to this variable, so for example, I want to make my arg equals to a new value of 500, and I will print my arc, I will get 500. Okay? But back to the colder, the color. Who pass this variable. If you print out the value, it will stay as 300. And the reason for that is we are only passing by value. So the function itself is not able to modify the value of this. Okay? So let's stop right here and I will give it, give it a try. I'll run a program just to show you that these two variables are indeed different. Okay? So this just a second to pull up my terminal here and make the program, oops. Oh, okay, so I get an error because I just changed the function's declaration. So I need to also change the header to also take an argument like that. Okay? So let me just compile again. Oops. Okay, now the error is gone. So that's run the program. Clear my program. Okay, now, okay, So this, you can see I have here my program which prints my arg equals 500. So first, first of all, the function will come inside is the program will come inside this function and prints my r equals 500, right? And I would do this from a previous example. And then when the function returns, it will print my input equals some value, right? And so 500, and a bunch of sizes from previous example, and my input is 350. So as you can see, these two are completely different. They are in different scope, okay? And, and the moment is function returns. This variable is also invalidated. Ok? And remember it has make a copy of this and take the value and it's gone when the function returns. Okay, so now I want to talk about a different type of variable declaration, which is called a global variable. Which means instead of declaring a variable inside a function, you can actually declare a variable outside of the function or on the very top of the source file. So I can declare a variable here as well, and my global in, for example, and as equal to 200. So these, by doing this, you will make this variable global to the entire C file. So all the function in here, they have access to this global variable and they are able to make changes to this variable. Okay? And there will be the only one copy. And the lifetime of this is always, is always valid until the program exit. Okay? So it's a little bit different from a local variable. You declare inside a function where you will be destroyed when the function returns. This was stays as long as the program is running. All right, so you will just disappear during running, during runtime. That's, that's just not how it works. So global variable. So which means I can have somebody. For example here, datatype example. When it comes to this function, I can make a change to to that global variable. Actually, let's make this equal to 0. And I want to change the global variable to 200 and inside the function. And I will print this. Actually our print functioning as well. So we know who is changing this variable. So this function will make my global int equals to 200. Setting my global in equals to a new value which is 200, right? So I'll print that. And when the program executes finished SQ nice function, you will, you will call the function write the go-to example. And this guy is also in the same source file as the datatype example, right? So in here I want to print out the value up here when it comes inside. So I would say, let's not do that. I'll just simply print reading my global int a equals to 0 value. Let me fix this. Should be called my global int, right? Because we're referring to the global variable right now instead of a local. So, so the, so when this function finishes the value assignment, this function is able to read the new value, okay? So they are actually referring to the same copy of the variable. Okay? So let's run the program and give it a try. Make my program. All right, So let's interpret this. So first of all, my program and datatype example, setting my global int to 200. Remember it was initialized to 0 and now this function says that to 200. And then another function go to example for function reading the same variable and also get to a 100. Alright, so it's the same. So that means the scope of that function is global within this source file. And they are actually referring to the same variable. All right. 25. The C Structure: Okay, welcome back. And today in this video I want to talk about a C structure. So what is the structure? Is? It is basically a collection of variables that kind of related together. And therefore it makes sense to be packaged as one structured type. So, well, let's just go ahead and give you an example so you can understand better. So a C structure, it is not really a variable yet. It is rather a datatype, user-defined datatype. So we can put it as many variables as you wish. And I normally put this in the header file here because all of my source file includes the header file. So whatever I define in there, both of these C files have access to, okay? And normally I would defy in the very top. So also the function below can have access to the sea structure. So to declare a structure, you first use the struct keyword like that, space and give it a name, let's say user. Okay? So what does user have? Well, this is totally up to you as example, I want to say the user has a user ID, for example, and the user has a username. Username is string, array of character, char, username. And you want to give it a size, a suitable size. So that's a 64, okay? So I have an array of character used to store the username field, okay? And the username also have, may also have some vague age and having a type unsigned int, okay, because we don't want the age to be negative, right? And maybe you can have one more record. Occupation. No. What does this user do? An occupation again, size 64, right? So right now I have a structure called user, and that structure contains four variables and who are closely related together. So iss kinda like organize multiple variables having the similar nature into one big datatype called struct user. Keep in mind that this is not the variable yet. This is just a data type declaration. So you are telling the compiler what shock user means, okay, it means these four variable. So, so which means after you declare the data type, you need to declare a variable using that datatype. So let's do that. I'll go to my function and I will go back to this example that we have been working with. And I'm going to, I'm going to declare a variable called struct user. And I'm going to call this user info. So now I have a variable user info, having the type struct user, okay, so similar to the way you declare variables, this is a type, okay? And this is the variable. So same thing here, except that the type is now struck the user. Okay, so in order to access those values inside the struct user, you need to refer to the variable like this. So first we type the name of this variable, having struct user data type and present dot. And Eclipse should automatically show you all the possible variables inside these structures. And you can refer to any one of these. So for example, age, I'm going to give it a value, let's say 32. And using four, let's type it again. And let's say user ID equals to 1. Okay, so that's actually rearrange this, be the same order. And use the info again and username. Okay? Now, username is not integer, it is a string. And normally when I want to write a string value, I use something called a string copy. And again, right now just to show you what I use, but later we'll have an entire module on strings in C. So this is something that we'll revisit. So right now I just want to show you quickly how I populate a string with, with some value. So I want to shrink copy to username with. Maybe that's my username with the string. And the length of the string is whatever, however long it is. So again, this is underworld revisit to string using the string copy or actually string and copy. So so we include the size as well. It's missing a bracket, okay? So that's how I populate a string. And then there's also a occupation field, right? So I'm going to use string and tabby again, but this time copy to occupation. And occupation is, let's say teacher and string. Then we'll return the size of this string. Okay? And that's it. That's how I populate my structure user with values. And if you want to print out this, the content of this structure is the same thing. So first we can have user ID equals, that's integer, right? And username is string. User age is unsigned. So I'm going to go with you, you for anxiety. And then occupation user, our queue patient. Q patient is string. All right? And then you want to fill it in with your variables. So again, you want to start with your user info first and use a dot and select the variable inside. And so user ID, you will go with user ID, username, you will select user name. And age. Again, select age, which is this one. And occupation, again, this guy right here. So that's it. That's how you declare a structure. Fill it, fill the structure with values, and print out the values inside its structure. And one thing I want to encourage you to do is when you declare a structure like this, you see it is not initialized like these guys. And remember in previous module I said I like to initialize my variables. So they all starts from a star somewhere instead of star rending random. Because if you don't initialize to something, it is some random value and sometimes they cause trouble, right? So what I would normally do is the moment I declare a structure, I will set this to 0 using the men set tool from the standard C function to set a memory block to 0. So I want to set the address of this. Again, it takes a pointer, okay, pointer to some block. And so you want to give that address of your structure and on the set that to 0. And the third argument is how big? Okay? How, how, how big is your structure? And this is where I will use the size of operator and given my variable here like that. So you see in C, I keep emphasizing that size is important, is important to know your sizes because in C, we have to be careful with sizes. So we are working, building our program. We know how much memory or how much storage space we're working with. So same thing here. You want to say that structure to 0. You wanna give it a pointer to a block and what you want to set it to, and how big you want to sit. Said that structure two and size of user info will give you the size of this entire structure that you have defined. So basically you can just simply do some math. This is four bytes and that's 64 bytes is another four by another 64. So your app add up to a 100 and something bytes, right? So you don't have to manually add it up, you just have to call the sizeof operator. And I will initialize the structured to all 0 with the proper size. So I like to use this and I will encourage you to do so. So you have something clean when you start working with structure. Okay, so before we end, let's just quickly run this program just to show you that everything is set up properly and the structure is printing out. Whatever we wanted to print out, right. So make clean, make okay. So I got some warnings. That's because I didn't include the proper header, but I think I should still be able to run. Okay. So let's see. Okay, here we go. So user ID is one. Yeah, we set it to one and username is C when there's my name and age 32 and use arcuation teacher. So again, it's printing the structure is like the US how he said it, right? So that's how you declare a C structure. 26. Type Casting: Okay, welcome back to another module. And today in this module I want to talk about something called a type casting. Okay? So that's just a fancy word. That means to change a datatype. So for example, you have an integer like this guy and you want to convert that type tool something different. For example, a short or short, okay? So when you want to do a type conversion, you will use something called a type casting in C. As you can see right here, I have a casting example already written out for you. And of course, I have the function declaration for the implementation here and a declaration in the header file in the main program, I call this casting example function. So actually went ahead and write all this out because I want to save some time. Because, you know, we'll be doing this will always be creating new functions to illustrate a new concept in C. So I actually went ahead and Dennis for you. So I will just explain what was going on here. So I have declared two variables up here, having the type int and each giving a different value. So first one is 80000 and the second is 5000, okay? And also I have a character variable char, and the charter can be signed or unsigned. So I'm going to give you a negative one, okay? And you see you have a common here, two's complement to represent negative numbers. I will explain that in a bit. Okay? So right now I just want to show you what is printed out does, okay? So we all know print f and f. You can do a type casting when you supply your variable after the comma. And to use a casting, you simply use a bracket and put your destination, your target data type inside. And you put this whole thing in front of the variable name. And that will signal a typecasting to the C compiler. So what is line is doing is I'm going to print the integer cast too short as an integer. But we do a little casting here by putting a short, okay? And same thing here. I want to print out the new value after I cast the integer two short. Okay? So these two are doing the same thing, but different variable, okay? And these two are exactly the same. I want to do a typecasting from, again my integer to a unsigned int variable. So I use an unsigned int in the casting bracket. And another example of the unsigned long. All right, and finally, I have a sign char variable. I want to cast that to a Unsigned version. And you want to print out a number. And that's how you do it using a casting operator here. So typecasting is very straightforward, but I have structured the sprints to show you two very important things that you have to be aware of to be careful when you're doing casting. Because well, let me just show you how these terms are first and then I'll explain what you see. Because a lot of people, if they're not careful, they will get unexpected results after they do the casting, okay? And I will show you what, what that means exactly. Okay, so let's compile the program and then let's just run. Okay, So after we run the program, we are outputs from the casting example. So I think you can see immediately, get us move over here. The first one. My task to short, it says 14,464. But prominent is the number we had is 80000. So after casting, my number becomes smaller, much, much smaller in fact. So what happened here? And with the second example, I have the 5000 and I cast that to a short. A is still 5000. No change. You see it's still fine. And same thing. I want to cast that to a unsigned. Okay, no problem. I still get 80 thousand. The value doesn't change. Cassian do on-site law. Again, same thing. Value has not changed. So these three are they expected case. So the values stay the same, but the type change, okay, just changing type. So that's the ideal case we want. So what happened to this guy? Then I will add something I want to show you right now because that's something to do with the storage. Size of integer is four bytes, short is two bytes. So you're casting from a larger data type to a smaller data type. But that's the case where the small data type can now hold all the data values that you have. So then pull out my calculator here. So 80 thousand. As you can see, it occupies 2.5 bite or 20 bits in order to represent 80 thousand. But as we know, a short datatype can only hold 16 bits, okay? Or two bytes. So by Reynolds number uses 2.5 bytes. So what happened is you would actually discard this bit right here when you cast to a short. So this is two bytes. So when you do a casting, you will actually only looking at this, this value right here, and you will not look at, you will not consider this bit right here. So when you cast to a short, it becomes this. Okay? And guess what? That is equivalent to the value 14,464. Exactly the same as you showing here. So again, just something that you have to be careful about is data type conversion to a smaller storage size datatype, okay? So you don't want, sometimes you do a casting and the u value suddenly changed a, you have no idea why. That's not good. That's, that's the seed to problems. Okay. And let's continue looking at one more line here. You see, I haven't might have a character called myChar casting to a sign chart and I get 255. Or what's going on here, my number is negative one. But after casting to the outside version, which I'm trying to remove the negative, I get to 55. And a lot of people, when it's due these, they expect to get one positive one for negative one to positive one bigger. That makes sense to them, right? You want to cast a type okay, From negative one. And if you cast that to go to the unsigned version, actually get possibly one, right. What you get wrong, because you have to be to understand how the C program represents negative numbers in memory in order to answer this question. So he actually use something called a two's complement. Complement here. And what two's complement is. It is a very popular computer science technique to store a negative number in memory. So let me show you how it works, okay, I will show you in the comments section here. So how do you represent one in binary? Okay? Very simple. You, that is one, that is one in binary, okay? You are only using one bit, okay? Which is that, and the rest is 0. And I want to include a bit here because that's how much a char can store. A char is one byte and one byte is eight bits, right? So that's what it is with a positive one case. And now I haven't negative. So I need to do a two's complement. So the complement two's common works like this. First, we have to convert all the zeros to one and all the ones to 0, which will become like this. Okay? So convert 0, 11 to 0. This is also called a complement. Okay? To make it opposite, okay? When you see a one, you make it 0. When you see a 0, you make it one. Okay? So that's the first part. You do a complement and then you add one to it. You add one to this outcome. So when you add one, okay? That's what it becomes. And then add one to the outcome. Okay? So that is how the C program represent negative number. So this is how the program represent negative one in C. Okay? So when you do this declaration and initialization in the memory, is this, that byte is all one. And that represent a negative number. And guess what? The value of that is in the sky being assigned version. That's actually to 55. Right? And pull up my calculator here. So if you have 11, 11, 11, 11 in binary, that's That's equivalent to the value of 255. And that's exactly what you see here. When you want to convert that to the outside value, okay? You will actually see that integer representation of 255 rather than negative one. That's why we see 255 because it is in fact or indeed stored as a 255 in the memory. Okay? And you may be wondering, how can the casting change my value? Well, let me tell you this. Casting does not change anything in memory. Okay? So when you declare your variable here and initialized to a value, that value is set in memory. Okay? So when you do this, it is already stored as eight ones in memory. And that's to represent negative number. It's already done like that. But each just the representation is different. And with these print f and the casting, you are basically trying to interpret the same bit differently. Whether or not it's assigned version or outside version is a long int or long unsigned, short or whatever you are. You are trying to represent the number differently, even though the storage space is the same, right? So this is why this is much smaller. Then we expected, because we interpret only the first two bytes of memory space, right? So keep in mind, casting does not change your values, but you may appear that it has changed, but it doesn't change. It's still what it, what it is the moment you declare. It just is all about interpretation and representational numbers, right? And I hope that's very, I hope you have learned something of value in his lecture. And if you have any questions, feel free to ask, okay. 27. The "static" variable: All right, Welcome back to another video. And today in this video I want to talk about the static storage class. Okay? So static is a keyword in C programming that you can append to any existing variable declaration or initialization that will change the property of that storage to be static. And I will go ahead and show you what it means when you append the keyword static. And actually, depending on what you append the static keyword to, he actually has some different purposes and functions. So right now, I'm just going to show you by example. And I want to show you the first example, which is the static keyword in front of an integer declaration like this. So you recognize this because we always declare a variable in an initialized using this syntax, right? And now what's different is I added a keyword static in front of it, and which makes this variable static. So when you do that in a local variable inside a function stifled key or would do is it will persist the value of this integer variable every time you come in to this function. So for example, you see if you don't have the static every time it comes inside x function, this myInt will always be initialized to 0 to begin with, right? So every time you come in you will be 0, it'll be set to 0 always all the time. But with the static keyword, the value of this integer will only, or I should say the initialization of this equal to 0. We're only be done once. And now was the first time you enter this function. And after that, the value will be persisted throughout the consequent calls to the function. So let me show you what it means. So I have a static variable called my end, and I'm going to increment my end equals my end plus 15, okay? So that it becomes 50. And I'm going to print the value of myInt. My end is now this value, okay? Okay, say I put my in here of course. And to be clear, I'm going to also want to put the function name here so we know it's coming from this static example, okay? And of course I've added this function to the header and also to the main. The main function, right? So let's see what, how, how it behaves. Okay? So again, just give me a second to pull up my terminal and I still do a build. So the screw up here where there's a start, okay? Okay, it starts from here. My end is now 50. So that's okay. With oil. Without static, you will always print 50, right? Because we only come inside the function once. Okay, now, what if I call this function multiple times? Let's say for time, alright? Now, let's see how it behaves differently this time. Okay? So again, let's make clean, make, and run the program. And now what do you see? The first line calls is 50 and then one, and then 150 and 200. So you see the value of the previous call is persisted. After first call. The variable is 50. And when you enter the function a second time, it will start from 50 instead of 0, right? So that's the static keyword in place. And if you remove the static keyword, okay? And doing the same thing again, every time you would just say 50. Because whenever he enters, you will initialize to 0 and 50. So every call results in the same results, right? So that's the difference of the static and what it does to your function. Okay? So now I want to show you one of the most popular interview question regarding the use of the static keyword to your variable. So let's say I have another function called Example 2. And it does all the stuff similarly except that it doesn't add the 50, you will just print whatever this value is, okay? And let me quickly add this new function to my header. Okay? And at the color coding process in my main function, okay, So I will call that function also. Okay? So the interview question is, after I call the first function four times and then call the second function one time. What do you think the value here we'll print out to be? That's a question. And a lot of people will say it will print 200 because your variable is declared as static. And we know the property of a static is you will persist the value that it contains. So since they have the same name and both are declared as static, this value will be persisted from the first function, which happened to be 200. And after we call the function four times. And then the moment you call the second function, it will print 200. Because of the variable is persisted by the static keyword. If you, if the answer is 200 and then you will, you'll be wrong. That's not the right answer. That is incorrect. And the reason is due to a very basic fundamentals that we have just talked about. Something to do with the scope of a variable. So remember, when you declare a variable inside a function like this, that is a local variable to this function. So same thing here. This is a local variable to function too. So even though they have the same name and same datatype in same static operator, they have different, okay? I'll always maybe confusing. They are exactly the same, same everything but different. Different. Why different? Because they are declared by two different functions. Okay? So in this case, you will print 0 instead of 200 because this variable has nothing to do with this. So the static keyword, it doesn't work cross functions, okay? So when you do like this, okay, they are in fact two different variables. So you'll print 0 in this case. Okay? So that's one of the trickier static interview questions I get all the time. And I just want you to know that don't fall for the trap, okay? Okay. So let me quickly show you how the program runs. Okay, so again, we will review map, rebuild program, and quickly wrong the program. As you can see, it prints 201st by calling the first function four times. And then the second function, you just print 0, exactly as we thought, right? The static keyword also be applied to a global variable declaration as well. So for example, we have a global variable declared from our earlier examples. And I can go ahead and add the static keyword to in front of that decoration. And if I do this static keyword in here, this global variable, it does not mean to persist the values every time he calls this because he doesn't really apply when it's global, right? So what it does instead is it will limit the scope of this variable within this C file. So within my function C. So it's like restricting access to this variable. So if there's any attempt that from other source files such as my program.cs, who has any attempt to access this global variable inside my function C, you will not be allowed. And probably the compiler will not even work, okay? Because keyword guarantees that this variable is only accessible within this source file here. And since we're already using the global variable within the source file, so it doesn't impose any errors right now. So with that there, it just tells the user and tells the compiler there's no way this variable will be accessed from other files. So that's another very, very good practice to put in front of your globals to tell the developers that whether or not this variable is used everywhere by other source file or only within this current source file, okay. 28. The "static" Function: All right, Welcome back to another video. And today we will continue to talk about the static keyword. But this time we will use the keyword in front of a function like this. And like that. In addition to using it against a variable that we have been using in the previous example. So when I declare a function like this as static, one really doing is I'm limiting the scope of this function, its axis. So just to remind you, in all of our previous examples, like this, with a static example or like casting example. They are declared and they are meant to be used by other source files. So this is why when we declare all these previous examples and the functions, we put in the header file, okay? Okay, we put, we put in here and have the, another C file and not a source file called my program, who includes the header file and gain the access to those functions decoration. So really it's trying to use the function defined in another CIF C file called my funds, right? We want them to be visible to other source files. So therefore we declare them regularly and put it in the header, right? So we said static keyword I and limiting the access to these two functions within only this source file, myfunc snazzy. And they are not meant to be used by, for example, my program dot c because I'll be out of scope. Okay? So it's very similar to a global variable declared as static is also meant to be used within this source file. Okay? So as you can see, I have here to static function declared, declared. And I'm going to create a third function called static Example 3. And what he's gonna do is this will be a public function. So which means we actually want this function to be accessed by the my program dot c. So we have to put them in the header here. And we already, well, as just call, call the function first, okay, and just to show you how a static function can be used along with a public function like this. Okay? So let's say I have a function called this static Example 3. And in order to complete the operation inside, normally it will call other functions. Right? Remember, a C program is basically just a bunch of functions calling another bunch of functions. And together they form logic and do whatever you wanted to do, right? So in order to complete, it may declare a variable which is local. And we'll rely on some other functions like this helper function one in order to complete some task. So first, we'll do that. In a second. You will rely on the second function to do something and get some results. So we can decode like this as well, which will populate a and B. And you have to do that through calling to additional helper functions within this source file. So this will be legal, you see, because I'm using these two function which is defined within the same source file. And in the end maybe want to return a plus b, return the result of both operation together. So that's Monteverdi's at the bit to make. This example returns an integer. And also here return an integer. And instead of calling like this, we're actually going to wrap this function with a print f and simply just print the result of this function, right? So we can do like that, right? So we are skipping to have a variable to store the value. We can just put the entire function call here and print the return value. So if I run this, if I put all this together, oops, let me move this and make my program. And of course, running a program, I should see result. Result is 300, so which is 100 and add 200, right? So that's how you can make them static. So we may be asking why, what's, what's good about this? Why, why we add a static? Can I also make these to a public function and used within this function? The answer is yes. You don't have to use static here. You can declare these two function in the same way. Now we declare this and add this to your header, which is here. And it will also work. So why do we add a static? Okay? So it also comes down to, you know, what's the practical practice when you're writing a C program, okay? The reason we do this is we're telling the compiler and the developer that these two functions are owned. Our local function's local to the source. So there's no way that other source file can have access to this. If we want to have access to this, you have to do it through a public function like this starting example, which we'll use these two functions to complete some task. So it's again, just a practice in your programming style. You want to make a distinguishing about local function and public functions. Okay? So it's very similar to C plus plus public and private keyword. You want to specify your scope here. So this is done using the static keyword in C. And another thing to notice that I only have the implementation of these static functions. I don't really have a function prototype like this, where it comes consists of the name and return value and the input. I don't have none of these. And just put a decoration here. These would be fine as long as you use these two functions in the functions that proceed this, okay? So what do I mean by that? So if I put this function, this public function, and cause these two, if I move all this code in front of this, then let me tell you and our work because, because I'll show you where the compiler will say. You will say static declaration. Helper function follows now static declaration. It means I can, I don't know what this is. I don't know what this means. Okay, compiler is having trouble looking at, looking for the definition of this. And this is because you are calling a static function before you even declare the the definition, the implementation here. So if you do like this, you enter in compilation error like this. Okay? So how should we fix this? So it's also a good practice that you always, always put you in your static functions prototype on top of this source file in here. And you just say the same thing, similar to what you put in the header. But we said static keyword. So when you do this, the compiler will know, oh, okay, so within this source file there are two static function, helper function 12. And so if you have these two lines, then it doesn't matter where you are using them. And it doesn't matter where you define the implementation. You always have access to because you already told you a compiler, Hey, I have two static functions, so remember to use them. Okay? So if I do it like this, and I'll rebuild, you'll see that the errors are gone. And we run a program, you still get the same result. Okay? So that's the power of a static keyword in front the function. And I want to remind you that because the static keyword limits the function's scope. So this is reason why we almost never, ever put a static functions declaration inside a header file. Okay? Because it doesn't make sense, the function are not meant to be exposed to other source files. So just remember that don't put static function declarations or any static keywords within your header file because they do not make too much sense and will make your programming style now looking as good, okay. 29. The "extern" Storage Class: Okay, welcome back to another video. And today I want to talk about the extern storage class. Okay? So extern is a keyword that you can put in front of your global variable or a function declaration to extend its scope. Okay, so what do I mean by that? So if you remember our global variable module, previously, we normally will declare the function of the variable on the very top of the, of a source file, right? For example, my global int. And if you remember, we put the static to limit its scope to be accessible within this file, right? And if you have, let's say I declare another variable, global variable without the static keyword. So I'm going to call this my external, my external int, for example. And I'm going to initialize this to, let's say 100. Okay? So these two variables, they are both global, okay? And, but the first one is strictly axis. We'll accessible within this file. And if I have the declaration like this, this is also accessible within this file also. Okay. So how can I extend the scope of this external variable such that my other source files can also access, for example, my program. Okay? So if I were just to, you know, to print my external end equals some value, which is this, because that is supposed to be global. If I just do that, I will get a compilation error, okay? Because it will say, Hey, this is not defined, even though it is defined here within the program. So let me quickly show you this error. And L. I will also show you how you can use extern to solve this problem. So let's make it a compilation else you see, it says my external end undeclared. It is happening in my program.cs. Because right now these external, or I should say this global variable right now doesn't have a scope extended beyond this source file. So what we need to do right now is use the external keyword. And we only want the type and the declaration. The declaration part, not the initialization part. So I want to just going to copy this and go to my program dot c. So this is where I'm getting the error undergo top, I'm going to say external int, my external int. So basically I'm doing another declaration here in my program, does C, except that I put that extern keyword here. And it's very important when you put the extern keyword, you cannot say this equals to something. You can initialize this variable because it is supposed to tell the compiler, Hey, this variable is defined externally somewhere. Please go find it and get the value. So there's no point. And if you initialized to some value, you also get a compiler error anyway. So normally we just put this extern int, my external int. So this is the same as this, okay? Except that I do not initialize to 10000. So that this line gives access to this variable within this my program dot c. So I can print that variable and also get the value 1, 0, 0, 0, 0. So let me just quickly show you how it works. So I mean, I will just compile program again. And as you can see, Rana, it compiles just fine. So I will just run my program and I scroll to the very top. And indeed it prints my external ID equals to 1, 0, 0, 0. So that's how I use extern to get access to a global variables. Other C files. And you only make sense when you have multiple C files within your program. And the lifetime of these will last the entire program's execution. So it's always there. Okay? And right now I want to show you the extern can also be put in front of a function. So for example, let me go back to here my ufuncs. So I will create a new function. Just to show you. You can also make some external function. So let's just make a very simple function called my extern font, okay? And it takes no arguments and returns no arguments. And it's just going to print calling my extern fun. So just prints something that to tell people that we are here. Okay? And instead of putting this function declaration in the myfunc step h, like we normally do. And I'm just going to go ahead and call this function right here from my program dot c. For example. Here. If I just, if I do this and normally I will get another error saying this function is not found. Okay? So let me quickly show you that. Okay. You'll get a warning saying This is not found. Okay? So that is because we never, we never actually did declare this function. We don't have a function prototype like what's in the header, like these guys. So in this case, again, you can do this. You can say extern, and then followed by the function's prototype. So basically you are declaring the entire function's prototype here and plus a extern keyword. So that tells compiler, okay, you don't have this implementation. That's fine. Age is somewhere, it's defined somewhere else externally. So if I do this, that function will, will, will be executed and it will be called without any problem. And the compilation error will, or the warning where disappear. So let me just show you once again. And then it's gone with the extern declaration, right? And then I just go ahead and run the program and see where it starts. So it's getting a little messy. Okay? So there you go. So my external int, when I'm calling my external func. So that's how you use the external keyword in your C program. 30. The "const" Keyword: Okay, welcome back to another video. And today in this video I want to talk about the const storage class, okay? So counts is again another keyword that you can put in front of a variable to make that variable read only. Okay? So read only variable means it can only be read and you cannot modify the value of that. Okay? So let me show you some of the cases where we will use const. Okay? So let me just create another function in here. And normally with the const there to usage basically. And the one I normally use, or most often used is put in, put it as a function's argument. So for example, I have, say, a function called const example. And I want that function to take two arguments, okay? First one being the name and second being the occupation. And what's different is I want the name to be a cost variable. Which means that when, when the function is color gives a pointer to name, it will not be modified within this function. So it's like a guarantee that this function will not modify the value of this pointer and is protected by the const keyword. Okay, so whereas the second argument against a pointer to a character called occupation by in this case, there's no guarantee that is function will or will not modify the value of this pointer. So it's a good practice to make this distinction. So your users or your other colleagues or your customers when they see, you know, you will use sorts go. And they, they will say, okay, this is good brackets like I know was what gets changed and what doesn't get changed, right? So in here I just gonna do a simple print out. I print the name and occupation. So the very simple example, just to basically print whatever, namely occupation color gives, right? So I'm just going to do that. And again, I want to add that to my header. Okay? And I want to make my main function to call this new function. And this shouldn't be any problem with this. Okay? Let me just modify this a bit. Smes, carry and occupation, teacher, for example, just like this. And let me just show you how this program runs. Okay, so, so far there's no, there's no tricks. Should be very straight forward. We have done this many times. And once we print, once we run a program, it will put in the name is Carrie and occupation is teacher. So now let me show you something. Say for some reason I want to change my name. I want to change my name such that it started with k, carry with the K, little k instead of C. So for example, I want to do this, right? Okay, so when i'm, I'm, I'm using something called a dereferencing a pointer. And I want to take the first character of this name and change that to another value, right? We'll talk about pointers and de-referencing later in this module. So in this course. So right now, just bear with me. Let's say I want to make these attempt to modify name. Okay? Oh, actually say I want to also modify the occupation. And just for the sake of simplicity, I just want to change the first character as well. Same thing, synchronous with my name, right? So I'm going to do that and I'm going to compile. Okay? And I will show you what will happen if I do this. So let me quickly. We'll compile the program. As you can see, I'll get a compiler error. And the error is assignment of read-only location name. So this guy name, because we put the const keyword so that the compiler knows this variable is read-only. It is reciting, it is stored in a read-only section of the memory. And if your data is in a hurry only section of the membrane you can now modify, and therefore it gives you this error. Okay? So, so that's a protection for your program. So you will not modify by accident because the compiler will catch that and we'll give you an error. These, on the other hand, it doesn't give you an error. The error doesn't come from here. So as soon as I comment this line out and recompile the program, again. Like that, I will no longer get a program. But guess what? If I run the program now, I will get something called a segmentation fault. Okay? So this segmentation fault is one of the most common, I would say, era that we'll see in Linux in all the C programming, this basically means your program has crashed, Okay? And we'll talk about crash information in this course. And this crash is due to a memory access violation. Okay? It's called valleys line because I'm trying to change the occupation, the first character of occupation to the letter K. But you might be asking, but we never set this occupation to be a const, variable. So how can it doesn't let us change the value, right? We said a name because, and we'll get caught by the compiler. But we didn't say these occupation to cost. And therefore we should be able to modify these, right? That's what it means. Yes, that is true. But the problem is not here. The problem is in fact, in my program does see the way we pass our variable in to this function. You see the way I pass a variable like this. Where do you think these carry and teachers Stream are stored? These are also reciting in the read-only section of the memory, okay? And you know, if we want to modify something that's in the read only section of memory, we will crash. Okay? So why, why, why, why should we do, What can we do to make these not crash? Well, it's very simple. We simply have to allocate a new variable with a valid memory space to store the value teacher. Such that we can pass the reference to this function, which is okay to be modified, right? If I pass just a teacher like this with a double quote, that is we only and changing them will result in crash and we don't want that, right. So we can declare an array of char like this. I know we have not talked about strings in C. Strings in C are basically a array of characters like this one right here. And normally we will allocate the size of this array to be whatever size you want, plus one. And the one is for the storage of something called a terminating nor characters. So this is something C Programming use to determine the end of the year and the end of the stream. So we have teacher, that's your string. And we will have to put a 0 or terminating nor after the last character of your string to signal that this is the end of it. So when you want to do a print f, print out the string, you actually stop printing stream until when he sees the terminating non-zero. So it doesn't just keep on printing. Okay? So if we put this teacher inside this array of char, then we can instead pass the pointer reference to the first character of this char array, right? So when we call inside this cost example program, this pointer will point to the first character or the first location of this char array which points to t. And thus, if that's the case, then this line will make that T equals to k one, give it a new value. So let me just print this. Print f right after the modification so we know the multiplication actually takes place and it doesn't crash, right? Okay, so let me try compiling this program and have it run again just to show you your work, right? Okay, and run the program and scroll to the very top. Okay? So as you can see right here, it prints, oops. Okay. Okay. So you can see right here it prints name is two equals to carry occupation teacher, and secondary prints. The name is to carry read-only, and occupation is now Keyczar with the k, right? So that means without the const keyword, this pointer value can be modified. And again, you'd have to be careful when you have something that's to be modified by the fire function, you want to make sure these modifying the correct value of memory space, okay. 31. Global Constants: All right, welcome to this module. And today I want to show you how to declare a global constant using either the const keyword or using the hash defined keyword. Okay? So what is a global constant? So a constant could be something like the number of continent in the world, which turned out to be seven. And it's not like the change in the near future. Okay? Another example would be the number of states in the USA, which is 50, right? Again, something that doesn't get changed. And in C programming, we normally define these constants as global, such that in our functions or in our main programs, we can refer to these constants where necessary. For example, the array having the size equals to number of continents or number of stays in USA. So things like that. And in order to declare them, they are two ways, okay? You can use the const keyword that I have shown you in previous modules. And you can declare an integer with the cost storage class. And we can call this number of states in USA equals 250. So we declare an integer and initialized to 50 and protected with the const keyword. Okay? So this is fine. That's your constant and you can use this constant throughout your program, globally. Okay? And of course, if you are using another C file, you can use the extern keyword, right? We talked about before, right? And another way to define this is to use something called hash define macro, okay? So things that start with hash define. You may have seen this from somewhere because this is actually one of the most common way to define a constant. So we can use the defined keyword like this, followed by the name of your macro. And this is called the Macro, another variable, and I'll tell you, show you the difference later. And normally with the defined, we want to keep the characters all capitalized to indicate that this is a macro, not a variable. Okay? So number of states in USA for example. And again 50. So let me tell you what's the difference between these two. So first we have to understand what a macro is. What is these guys? So the macro is something that I should say. There are several stages in your compiled nation, Okay, I think we talked about this in a previous module. So before any compounding is done, there's a stage called the preprocessor stage. And what a pre-processor would do is to actually replace all of your, it would replace all of your macros that have this name, number of states in USA. Replace that with the value 50. Okay, So you actually go to your program, search for these macro here, we said was that name and replace 250. And then you will go to the compilation and have a compiled. Okay? So this is actually not part of the compiler, but rather before compiling. So we can consider preprocessors a text replacement tool to actually replace these texts to this value. Right? And same thing with this. This is something the preprocessor will do. You will try to check it with this file exist and make sure you have all the includes for your compilation and all these preprocessor macros. They are all starting with the hash character, okay? And I think the most important difference is this is not a variable. So which means this does not take up memory space. Okay? And this, this is a membrane. This is a variable. So you will take up some space in memory. Okay? So four residues for this reason, it is very normal. I would say 90% of the time that people were used to define way to define the global constant. And it will use the constant like this. Let's say my states. And there were actually put the macro in here. So the size of this array equals to the size of this macro, whatever you define here. And we always, always, normally used the macro to define our constants. So I'll actually nine encourage you to use this. Okay? So I'm gonna come in and out. So instead, use the macro away, okay. 32. The "typedef" Keyword: All right, Welcome back to another module. And today I want to talk about the type def keyword. Okay? So the type def keyword can be used to give a datatype a new name. And this name, you can name it anything you want, whatever that works for you. Okay? So for example, what I have here, I have added a bunch of typedef keywords. So basically, I want to rename the type int into int 32. Okay? So the syntax is very simple. You start with the type def keyword and the data type that you want to rename the new name of that data ten of that datatype. Okay, so what I have here is a list of datatype now we have worked with or we have talked about in the previous module. And here I am giving them a new name. So you see I have a convention to follow here. The way I like to name this data types is to include, well, their native type, which is an int, and their sizes. So 32 is for 32 bits, okay, so that's four bytes. Integer is four bytes long, okay? And similarly for unsigned int, except I add you in front to indicate that this is an unsigned integer of 32 bits, okay? So semi for short, which is two bytes, and again is in the int, int family. And 16 is the size. And the same thing for char for long. So I'm calling all this int or you int. And it just differ by sizes, okay? And you, you will probably see a lot of open source software. They use a convention like this to tell the user or the developers what type of variable they are working with and their sizes. So again, you probably have heard me emphasizing on the size of the data, size of whatever. Very much in the C programming because it is absolutely important when you're working with C, because C is considered a lower level programming language. So whatever you are doing, it is always better to have an idea of how big of the data you're working with. Okay? So that's for the regular variable renaming with typedef, okay? And typedef is also most commonly used to rename a structure like this. Because if you remember, if I define a structure like this, struct, user becomes a type and that's like two words, right? That's a little bit too long, right? We want one word that represents data type. So in that case you can put a type def in front of you, a structure definition. And this whole thing is a datatype which we want to rename or we want to rename to, let's say UserInfo. Actually this solvers capital letter user info. See what I can also do like that. So which means I want to remove this whole structure to user info. So if I can go back to, oh yeah, one more thing. You see I have this typedef stuff defined in the header file and all of my C files includes this file. So, so they were all have access to the new names as I have defined here. Okay, it's very important. So let's go back to our datatype example in my fonts does see right here. So we can actually replace all these datatypes to the new names that we have defined. So instead, I'm going to put int 32 and instead of outside in, I'm going to replace U int 32. And short is 16. And you get the idea, right? So I'm not going to do all this, but you get my idea. And for the structure. And again, instead of saying shop user, we can say user info, okay? And if you hover your mouse over the new name, you will say, okay, this is int, this is type int, okay? Hover over 2, 16. And this is a short, and again, hover over to using it is this structure right here. So that's how you can use typedef to rename your data type and use them in your program. 33. The Enumeration: All right, Welcome back to another module. And today I want to talk about enumeration in C programming. So enumeration is basically a user-defined datatype in the C language that is used to assign names to the integral constants, which makes the program easy to read and maintain. Ok. And you can use the keyword enum to defy and declare an enumeration. Okay, so that's jumping for example. So as you can see right here, I have defined a enumeration called day of week, okay? And it consists of seven possible values. The value for Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday. Okay? So this enumeration represents the day of week. Okay. So I think this is very clear because there are seven days in a week and these are the seven days. And in fact, all these values here, they appear to be words, words that we can understand Sunday, Monday, Tuesday. But in fact, they are actually number, okay? In a C program, they are actually represented, represented by numbers internally. But to you as a programmer, you can use enum to represent those numbers with a text that we recognize is the recognize. And as you can see, if I hover my mouse over, it says 0 and hover to another value is three. So here's how the numbering works. If you don't give these names any value, the first value will default to 0, and the next one will default to 123 and so on. Okay? So the moment you give this to a value, let's say one, okay? If you make the first value equal to one, then you will say one. And we hover over to the next value USA to you actually increment by one automatically, internally in the program for you. So you can also start with negative one if you wish. And if I hover over again, you say negative 10123. All right? So you can make the first value to equal to some number. Of course, a number now makes sense to you, okay? So I'm going to put a 0 for Sunday, and Monday will be one, and 23 and so on. Okay? So that's how you can declare a enum like this. And of course, you can wrap all this. This whole thing is a data type, by the way, just like a struct. Okay? So of course you can use a type def keyword again to wrap all this, to say a typical day. So if you declare a variable having the type day, you actually means this entire enum structure. And it can only be one of these seven values. Okay? So let's jump into an example that shows you how e1 and can be used. So I have here a function called ener example. And of course I have added to the myfunc, myfunc stage and also I added to my main function. Okay, So it's being called. So let's jump back in here. I'm going to declare a new variable called day. And I'm going to name this today, and I'm going to initialize that to Wednesday. Okay. Because today is Wednesday. So as how you can declare a enough and normally will use enum with the switch statement to define what to do with each possible values. So I'm going to paste here my switch statement, which is quite lengthy. I don't really want to take the time to write it out. So I'm going to paste it right here. So as you can see, I have the condition here, switch and with a variable. And I have each possible enum values in my case. Okay, So 14 Sunday, one for Monday, Tuesday and so on. And what I'm gonna do is simply to print the day they represent and also the E naught value that's actually stored in the program. And down here, I have a default, a case which will print invalid day in case it is none of the seven possible values. Okay? Because there's only seven days in a week. If you give a number that is like 10, then last a invalid day. All right, so let's run these quickly. And just to show you one of the most common use case with enum and switch statement. Okay, so I'm going to compile the program and run the program and see USA Today is Wednesday, exactly as we have coded our example, right? So that's how you, that's how to use enum and how it can be used with a switch statement to make your program easy to see and easy to maintain. 34. Pointer Basics: Alright, alright, welcome back to another module. And I'm super excited that you have made it this far to this module. Because starting from this module and the module after, we're gonna be talking about the most interesting and the most powerful aspect of the C programming, which is called a pointer. Okay? This pointer idea, it may seem very daunting at first. But once you get a handle, us become very interesting and you will help you and your path as a C programmer. Much, much easier once you get handled there, of course. So let's get right into it. Okay. So first of all, what is a pointer? Okay, you heard the term already. So a pointer is an object in C, The stores a memory address. Okay? So a memory address is basically a number represented by a number. So for example, this huge number, and I mean, I have not shown you this, but this is actually a hazard hexadecimal number. So it's a common way to represent a memory address. We use hex number representation. Well, we haven't talked about this hex number in this course, but I will highly recommend you to refer to this Wikipedia page that will give you a very thorough explanation of hex numbers, okay? And a pointer. What it does is it references your references a location in memory. This keyword is very important. That's the terminology I like to use. And some people say a pointer points to a location in memory. It means the same thing, but I like to use reference as a keyword as the term to indicate the point in part. Because when you want to obtain the value of pointed by a pointer in memory, that action is actually called dereferencing the pointer. Okay, so there's a reference or referencing and there's also de-referencing. So they actually make sense, right? So basically undo what is references doing. So you dereference that and you get a value instead of an address. Alright? And in C programming, every single variable and every single function, they occupy some space in memory. So this means all of these things can be referenced using pointers. Okay? That's basically what a pointer is. And again, pointer has a lot of advantages in USE programming. And another task can be done much easier with pointers in this module and the module to come, I will show you some examples how you can use pointers to complete a certain task. Faster and easier, okay? And again, pointer is a requirement when you want to perform dynamic memory allocation. And once again, we'll talk about dynamic memory allocation later in the course. But right now you just have to remember this term, dynamic memory allocation. And pointer is a requirement. And last but not least, pointer is a must. In order to become a good C programming, you need to know pointers. And a lot of people hate pointers because they cannot. They fail to visualize how pointers work. And in this course, the way I will teach you, I'm going to teach you from my experience, okay? So I will not teach you from textbook because that's where people get confused, is from textbook. And I'm going to teach you from my point of view, the way I learn pointers, how I learned, and how I learned they applied the pointer into the real application. Okay. So how do you declare a pointer? All right, you may have seen these before in my earlier or earlier modules. The way you point you declare a pointer. You first declare a datatype, okay? For example, in integer int, and then you put the asteroid, or they call it star. Okay? So this asteroid after datatype indicates a pointer declaration, okay? And then you're going to give you a pointer, a name. So right here you put the name and you can also initialize this pointer to reference something. All right? And normally in my practice, if I don't know what it points to or I don't know what issue reference to. I will initialize to pointing to nor. Nor is a special macro that you can use that indicates nothing. It has a value of 0 and a pointer pointing to 0 is like pointing to nothing. All right? So that's the initial value I would normally give my pointer declaration. All right. And another way to declare a pointer is by using the ampersand operator. Again, you may have seen me using this ampersand in earlier module. So this m percent can be applied to a regular variable like this. So for example, I have an integer a equals to the value of 90. That's a regular variable declaration. We use low all the time, right? But the moment I put the ampersand operator in front of this variable name. Now we'll evaluate to the memory address where this variable currently resides, resize in. Okay. So he will evaluate to a, a big number. That's the address of this memory block, or I should say, the starting point of this memory block. And the my pointer, my PTR variable, I can evaluate, sorry, 90 value. I can assign the pointer to point to the value which is the address of a. So wherever a is located, this my PTR, we're points to the start of that memory block. Alright? And in other word, you, I'm trying to make my, my PDR reference to the address that contains the value of 90. Okay, so that's what this is doing. That's another way to declare a variable, to get a pointer using the ampersand. All right, so now let's talk about dereference a pointer. So again, dereference a pointer. You, you also will want to use the asteroid operator, but you want to use it a little bit differently. All right, so let's take a look at this example here. So I have an int pointer called my PDR, and I have a regular point, a equals 90, and my PTR points to the address of a. All right, so let's say I want to print the address of the of my, my PDI where it is pointing to currently. So I'm going to print like this using the print f. And with the percent x operator that will print the numbering has hexa decimal, alright? And in order to print a pointer address, I will just put my variable, my PTR right here. And now will give me the address. But I say I want to print the value pointed by this pointer. In other words, I want to dereference this pointer and get its value. Then in that case, I will print as an integer because I know the point that has a type int. And then I'm going to put the asteroid in front of my pointer. So star pointer or asteroid pointed these. What de-reference the pointer to get the actual value stored in memory. Okay, so that's how you dereference a pointer. So if I run this program, I will get the outputs of something like this. The first one being the address, which is huge number, because I put the pointer itself now will result in a huge number and not the address of the memory. But the second line, I dereference that pointer and our get the value 90, right? So keep in mind on D-Rev, dereference a pointer. It actually depends on the datatype of you, your pointer. Because a pointer is just a reference, you can only reference to a location 1 location membrane, and that is the starting location, okay? And you are variable, for example, this int a, E occupies four bytes, four bytes in memory. So what a dereference, what do you actually look at how? Well, how big your data will occupy, depending on the data size here you put in, whatever you put in here. An integer occupies four bytes in memory. All right, so when you dereference my PDR, the program will evaluate four bytes from the current memory address it is currently pointing at. Okay? So the pointer, right now it points to this location. In order to evaluate, in order to dereference and get your value, you actually look four bytes starting from this location and evaluate that four bytes, which will get you 90. Okay? So dereference is very important, is size dependent on data type dependent, OK. And now pointer arithmetics. So what I mean by this is the arithmetic operation can be applied to a pointer. Really? Yes, because appointed just a number, normally very big because that is to store memory address. So because it is a number, it is possible to do arithmetic operations on a pointer. And here's some example. You can make the pointer to whatever, whatever it is plus 10 or whatever it is minus 20. You can use the increment operator to increment my pointer value by one, the decrement operator. So all these will work. So this basically changes the reference, the pointer points to. Okay? But you have to be very careful with this, okay? In C is a lower level language. You can do a lot of things. And the compiler, normally, well, we will try to tell you how you doing something wrong. But most of the time you have to be careful with what you're doing with pointers. Because with a pointer in C, it is possible to make a point of reference to any memory address. So what do I mean by that is you can make my PDR equals to, you know, some random number. Okay, So when the number in memory, now that would be, now be legal C1 assays. There's something wrong with that because there's nothing wrong with that, you can point to anywhere. But when you try to dereference that pointer from wherever you make a reference to some location, then you, you may not be allowed to operate on that memory space. And in that case, the operating system will terminate your program due to membrane violation. And when this happens, this is also known as a program crash. Okay. And let me tell you most of the crash that happened in today's software application, whatever you're using, most of them are related to memory violations, okay? And when the program, when the application gets big, this kind of issue or get no more difficult to deal with. And so when you program crashed, mostly there's a memory validation. And that basically means you are trying to access some memory that you shouldn't be accessing, and your operating system will detect that and you will shut you down. All right, so this is the basic basics of pointer, and I will see you in the next module, okay. 35. A Look Inside Memory: All right, Welcome back to another video. So, so far we have been talking about pointers and how they reference a location in memory, right? So today I want to show you the inside look of the memory. Basically to show you how the memory is divided up into several sections. And they have their own purposes and how they are being used. Cases is very important. Fundamental knowledge that I think you have to know Before you move on and play with pointers. Okay, So let's begin. So what is up with memory? As we know, all computer programs require memory to run because we need memory to store our variables, to store our functions and all that stuff, right? And most of the program crashes are involved in memory violations. So as you can see, we need memory to run our program. But at the same time we have to be careful with these memories, especially in C, because C is a lower level language, which means we have to be careful what, how much resources are being allocated and how much are being deallocated such that we don't overflow or exhaust all the system resource, right? We also know that the memory address is normally represented by a hazard hexa decimal values. We talk about that, right? And now I want to show you the three sections or three levels of memory that we will actually be using throughout the course. And each of these sections have different purposes and different usages. And so I think it's a very important fundamental knowledge about memory before we actually start playing with pointers. Okay, so pay close attention here. So first let's take a look at stack memory. Okay? So a stack is a data structure having a continuous blocks of memory. So continuous means if you start from one and next block we have are just two. So it's one big block. So it's continuous. So it's not like separating different blocks, okay? So they are one piece in memory, okay? So in other words, when I say linear memory, all right, So everything is in one place. So stacks works in a last in first out fashion. Okay, And that's where, that's why it gets the name stack. Because basically you are stacking things together on top of the previous thing that you have inserted. So as you can see right here, the first thing we put in will go to the bottom of the stack. And then the second thing you put in, you will be on top of that. And when you pop something out of the stack, the last one inserted gets pop out first, okay? So it has like a reverse order. So stack memory allocation happens automatically in the function call stack. So when I say function calls DAG, that means is for example, in fund a main function, I'm calling function one. Inside the function, I'm calling function to. Doing so form a stack as illustrated in this graph. Okay? So stack first, the first function call is function one down here. And there's some variables in the function being declared. And then function tool. So this thing is a call stack, okay? And allocation happens automatically, all right? And the function's local variables are also stored in stack memory. So these guys right here, into XYZ, ABC, these are local variables inside their respective functions, and they are stored in the stack as well. And de-allocation happens also automatically when the function returns. All right, so let's go to this example once again. So I am calling function 1 first and these function one, the function itself also occupies some space in the stack memory. And because that's the first function I'm calling, you will be on the bottom of the stack memory. And then inside function, I declare three integers, a, b, and c, and it's added on top of above the function one storage, right? And then it calls function two, which is right here, another stack added on top of ABC and inside function to have a rather variables x, y, z, and another a, and a pointer. So all these, even a pointer itself, which is just a number, is also in the stack memory. All right, So when this function returns 0, these get popped out or they are de-allocated and recycled so you can use them again. All right, so this data structure is very efficient. So that's why it, unless way, the speed is normally very fast. So when you're doing coding, doing function calls, they are very fast. These happen very fast, okay? And the stack, the size of the stack is normally quite small. And it's actually operating OS dependent. Okay? So normally the stack size range from one to eight megabyte. These are some typical range. And of course, when you have a multi, multi thread, when you're doing a multi-threaded application, you can set the stack size to the individual thread. But this is an advanced topic that I will cover in the actual separate course. All right. So with this stack memory, you may have heard the term called Stack Overflow. Okay? So this happens when all of the stack memory is exhausted. Always or four. And because four it can accommodate any more variables. So this can happen if you continuously calling a function inside a function, and you just keep going on in INR and never returned all of these functions and local variables, they will occupy space in a stack. And if you declare a huge array of something, now we'll also take up space, okay? So we have a function that keep calling another function. And again, again, again, that's normally, normally not a very good way to program things. And if, if it goes out of control, you will have a stack overflow and random things can happen, even crash. Okay, Next let's talk about heap memory. So what is a heap memory? So it is basically a pie of memory space available to programmers like us to allocate and deallocate. And the size of this memory space is determined by how much RAM you have. Okay, I have down here. So the more RAM you have, the more heap memory space you have, and the spacing here. They, they are allocated by programmers manually during program run-time, normally using malloc and pointers. We'll talk about this again later in the course. And that the allocation is also done by the programmer manually during program run. Didn't poke around pi. And normally we use the free function call to deallocate a piece of memory from the heap. All right, so as you can see the keywords manually, okay, In opposed to automatically, which is like a stack memory, everything is automatic. With the heap, everything is manual. So you allocate some memory space and then you have to remember to free them. And this is why heap memory requires more care to manage. Otherwise, something called a memory leak can happen to the program. And if we have a program having memory leak, you actually affect other programs in your system as well because they will have less heap space to use. Because you just keep allocating and freeing, freeing the memory back to the OS. Memory leak basically means you just allocate memory, but you never return them back to you operating system. And that is a memory leak. And there are tools to detect this. But again, just third rule of thump. Let's say I have an example right here on the right. So, so when you malloc a piece of memory, so for example, I have an integer pointer, okay? And initially points, no. So it doesn't point to anything. And now I will want to use the malloc function call to allocate four bytes of memory in heap and assigned to this pointer. Okay, You see I'm doing a little casting here. So I want this to be a integer pointer, okay? And size of int is four. So you will occupy four bytes in the heap memory. And then of course you can go ahead and operate on the memory. You can make that equal to 8 or some other value. That's no problem. Inside the heap memory, you will have the value of eight here. And the pointer int PTR is pointing at the beginning of this memory space, okay? And when you're done with this pointer, you have to remember, you want to call it a free into PTR. Okay? So these basically tell the OS, okay, I'm done with this memory space. I have these four bys finished with these four bytes. You may take them away. So these get recycled back to the OS. All right. And because he has the, have to manually allocate them and free them. And even these action take up space because he has communicated with the kernel to request memory. So generally, doing this is a live is slower than stack memory because DAG is a simple data structure designed to be very efficient, but heap, on the other hand, is a little slower. Okay? And again, we talked about this. How much heap memory is available is depend on how much RAM your computer have. Okay? So that's the heap memory. And now last but not least, the Read Only Memory. So what is a read only memory? Well, as the name suggests, is a special area in memory. Now you can only read. Okay? So you are proving program can only read and not to modify any of the value. And again, this is also allocated automatically. And let's go to this example here. So we talk about these in a previous module. So you can declare a char pointer like this chart. Our story, my name equals some string, carry Huang. So this period Hong here actually resize in read-only memory because we never actually have a storage space to save this. Okay. I know you may be asking By have a pointer pointing to this carry Huang. Yes, but pointer is just a reference. Okay. Is it not necessary a reference to a storage space? We need to have that prepared separately. So what is Ming is my name actually points to this block of memory that resides in read-only section of the memory. So these can change its only initialize the first time we declare this. Okay? So if you change, this program will crash, right? And we'll also talk about counts. You can declare a variable using the const keyword. And now we'll make this variable read-only as well. And the initial value is set for the first time, and that's it. No more change after. And you don't know if you want to change this, the compiler will give you a warning, okay? And a 75 also in the re only read-only memory space. And again, if you want to past some streams into a function like this, you don't store these stream in the variable with the valid memory space. You just pass in like that right here. That is also in a real nice section of the memory. So Bobby here. So be careful with this when you are using a variable like this, especially with streams, they are read-only and you do not want to modify them, otherwise, your program will crash. And you know, when your program crash, U USC, the segmentation for error for your program, and that basically means your program crashed. And 90 percent of the crash is involving in memory validation. And this is a typical case of a memory of validation. All right? And for this, you normally do not have to manually manage these memory space. This is again happening automatically. And you just have to be careful not to do any modification, okay. 36. Static vs Dynamic Memory Allocation: Okay, Welcome to this module. And today I want to talk about static versus dynamic memory allocation. Okay? These two terms, you may or may not have heard of them, but in fact, we have been using these throughout this course to show you different aspects of C programming, okay? And in the previous module we talked about, we briefly mentioned dynamic memory allocation, which it requires a pointer to allocate some memory from the heap section of the memory, right? So today I want to show you how that is done and also show you the static memory allocation in the example program Part 2. So as you can see, I'm starting a new example program and I name a part two because starting from this chapter is going to be major using a new interesting concept but with pointers. And later we'll introduce a raid and streams. And the first part is a little bit lazy, so it makes a lot of sense to start a new program just to demonstrate what we're about to learn, right? And again, you can download this example program part to you from the course page as well. All right, so this is fairly simple. This example program Part 2, I have here the main function calling a memory alloc example, which of course has nothing, will be filling this out today, right? And of course I've added that to my header file as well. Okay, So let's start from a simple declaration of variable. For example, in a equals 50. This is nothing new. We have seen this, but I never talked, but I've never told you that when you do this, this is a static memory allocation. We've been doing this, but I never tell you that this is also known as a static memory allocation. So why is it static? Because as soon as you declare and initialize a variable like this, you will be stored in the stack memory. We remember stack memory, right? And that is done automatically. So really I don't have to do anything other than declare a variable and give it a value. And the memory allocation is done automatically. Okay, So this is a static memory allocation. And now I want to show you how to do the same thing with dynamic memory allocation way, okay? And as you recall, to do a dynamic memory allocation, we need a pointer. That's a pointer B equals no, I like to initialize it to null because I don't know what it should point to as of now. As you recall, again, we need to use the asteroid to use to declare a variable as a pointer, right? So it is supposed to be referencing and address. All right? So in order to allocate a piece of memory to hold this integer and have be pointing to that block of memory. We need to do something like this. We need to make b equals two. Okay? Pass to an integer pointer and we need to use the malloc function, okay? So something like this. We need to use a function like this. All right, so what is this saying is malloc stands for memory, memory allocation, okay? Allocate memory or whatever. And you will take one argument which is the size. How many bytes of memory do you want to allocate? And once it's a seed, let's say your system has enough free heap memory. And then you will return a pointer. As you can see, you will return a void pointer. We'll talk about void pointer later in this module. And this is something that we need to cast that to an integer pointer because we're working with int, right? So void pointer is a special type of pointer that I will explain to you later in the course. All right, so that once it returns, it should give you a memory address of that will occupy four bytes, because the size of int is four bytes and have be pointing to the beginning of that four bytes in memory. Okay? So let me put a comment here. This is called the dynamic memory allocation. Okay? And then we put a little more information here. This is in the stack memory, okay? A is in stack, B is in stack as well. And these four bytes of memory that we just allocated, this is in heap memory, okay? So they are in different sections of your memory. All right? And normally what we would like to do after allocation, we want to do a check B, because this line could fail if your system run out of memory. All right, you know, this is merely to request a memory from your operating system, and it may or may not give you the memory you requested depending on if the system has enough. All right. So normally would do something like this to do a NOR checking on B, because we need that initialise B to know if this seed you will return a valid pointer. If he does not see the will return a null pointer. So we could do a check-in here and say if b, if b is known and we do a NOT operators who actually come inside this. If we want to print some error message, right? We wanna say failed to allocate memory. Men more from heap. Right? So you will want to print our error. If your program fails to allocate memory and you want to return right away, you don't want to continue. Okay? And then after that, after that, if this doesn't get executed, that means B is valid. If it's not valid, you will come inside and then your return. So everything will end, right? So after that, then of course you can do something with it. You can make the value of B equals 250, right? Only, only after you do this, okay? I mean, only after you allocate a memory and then you can use that memory block by assigning a value to the memory address. Otherwise, it will crash your program because there's no memory available, right? And now after that it's the city. I want to print my values, okay? So a equals to some number and b equals to some other number and it should be the same. And if one or point a, we simply put a here, like we have always been doing. But if we want to print the value of b, the actual value that b pointer points to. We need to de-reference that. Remember? So we need to do a asteroid B. And asteroid is a dereferencing symbol as well. Okay? So that will print both values. Okay? So let's give it a try. And as you will try it, let's compile this program. And just to show you how it works, Okay? So again, let's make our program. And then there's one. So as you can see, it prints a equals 50 and 50. So that's how you use a static memory allocation. And dynamic memory allocation. 37. Deallocate Memory: All right, welcome to this module. And today I want to talk about memory deallocation. Okay? So in the previous module we talk about static memory allocation, you know, like in this line right here. And we have also talked about dynamic memory allocation, which is right here using the malloc function and with a pointer, right? And after we declare and allocates memory, we went ahead and used the variable and print their values right here. So after we're finished with what we wanna do with these pointers and memory. We normally want to do some clean up, okay? And memory clean up or memory deallocation is one of the most common cleanup operations. Of course there's others. Right now we only worry about a memory, okay? So now in order to free up the memory that's allocated by static memory, you actually do not need to do anything. Okay? Because if you remember, stack memory is managed automatically by the program, Okay? So when this function returns, okay? When this function returns all these stack memory and its local variables and the memory space that they occupied, they will automatically be freed. So which means they can be reused by subsequent function calls. Okay? So for a we don't have to do anything which led the function return and we clean, clean up the memory automatically. But now with the point a, b, which points to a piece of memory that we have allocated in the heap. Using the malloc function, we need to do some manual clean up, Okay, once we're finished with it. And to do that, we normally would do a NOR checking this pointer B. Because what we want to make sure BY is actually pointing to some valid location in memory. So valid means anything but anything that is not 0, okay? And Nor noise 0. And we want to make sure it is not 0. So we just simply put if b is greater than 0 or you will come inside. And if that is valid, we will just call the free function call and give the point a b that will tell the program to, to deallocate the memory that has been allocated using malloc. And free, you don't need to specify a size because the system knows how much you allocate from this location in memory, okay? So all you have to do is to give the beginning of the memory address which the point a b will contain. So we just put that in the free function and you will know how much to free, okay? And you will free four bytes after you call free. Okay? And after we call free, it is again, a very common, common practice to put b equals know once again. And the reason for this is the free function call right here is simply invalid. It the memory pointed by b wherever that is. But it doesn't really change where b is pointing. Okay? So b is still pointing at some value, some location in memory. But this guy is imbedded anymore. But it's still there. It's still has an address value. So which means if you call three and after free you have another condition like this. If B to check the validity of B, then the condition will not be valid anymore because you have already free the memory by the pointer is still valid. So this is why we have a common practice to also invalidate a pointer after you do the free. Just to make sure when you do a condition like this, you will actually be a correct condition, okay? So that's how you can deallocate the heap memory that you have allocated using malloc. 38. Function Taking Pointer Arguments: All right, Welcome back to another module. And today I would like to show you one of the most common way to use a pointer in your function's input argument. Okay? I know you may know some very special or very fancy because we have worked with functions and we know what the function's argument is. And we also know that you will produce a return value, right? So nothing special about that. But with pointers, it gets a little bit interesting, Okay? And it is better for me to show you an example right here. So as you can see right here, I have an example function called pointer argument example, okay? And you will return an integer, and you will take three input arguments, okay? So first one being a regular integer, like we normally know. And the second, third, they are pointers. Okay? Second one being an integer pointer, 2 out number of days. And the third one is a char pointer 2 out month name. So why do I put in and out as a prefix for these variable names? Because with pointers, an input argument to a function can also be used as an output. Okay, hold on radio. He is the first time you heard this MSR. A little bit confusing because should a function input argument be just inputs and whatever it returns as output. Yes, that is true. But this is where C programming is a little bit, is unique. It's kinda stand out from other programming language because with the use of pointers, okay, which is a reference to somewhere in memory, you can actually put the output in your input arguments. And for that reason, I put the prefix out to indicate that these two parameters, they were actually be served as outputs when these function as accuse. Okay? So in this example function, what we wanna do is we wanna take the input month, okay? And depending on the value of month, for example, if is one, then we would write January, the string January in the month name output. And we'll also at the same time put the number of days in January and safe in these variable, or saving this pointer wherever he points to. Okay, so that's what this function would do and that's implemented together, okay? And notice that I also have a return value here, okay? So this is something to note. Notify the colder about these functions execution status. So for example, if this function returns, let's say 0. If it's successful, you will return 0. Then the color will know, okay, there's some value in here, and there's some value in this pointer as well, right? If this function fails, you will return negative one. And that tells the color, okay? The function fails to execute. So there's nothing in this pointer and nothing in his pointer. So don't look at those places and print an error or something. So that's what is returned value would normally do. So let's go ahead and implement this function. So first, we need to do in input check on the month variable because they are 12 months in a year. And we wanna make sure this in month value cannot be less than 0 or greater than 12, right? It has to be from one to 12. So I think that makes sense, right? So that's a very typical input checking. So it can be done with a simple if statement right here. So that's if the month is less than 0 or greater than 12. Now, that's an error, will return negative one. Tell the caller, Hey, don't print anything, don't get any value of around these two pointer because the function has failed. Okay? And the second checking is we want to make sure these two pointers are actually valid. Okay? I think it is also a very good practice before you interact with any kind of pointer. Like before you do any interaction, you want to make sure they are actually valid. And very soon there's a very simple way to check the validity is to use the NOT operator and followed by the pointer. So if any of that is not valid and not valid, it means the pointer is pointing to know, okay, nowhere in memory. Then in that case, we will also return negative one to tell the caller, Hey, something wrong with you input. So we're done. Okay? So that's my basic checking of the inputs and also the upper pointers, okay? And after that, then we'll just return 0, right? Because there's no other ways that you will result in failure. So we'll return 0 right here. Okay, so now I'm going to copy this big switch statement. Okay, just 1 second here. So I will just copy right here and I'll explain what's going on here. Hey, so we're using a switch statement. It shouldn't be something new for you. But we talk about switched given in the previous module. So we do a switch statement on the month variable. And if that equals to 1, okay, then we will make the value K. We use de-referencing pointer dereferencing operator here. We want to make the value pointed by this p