Transcript for:
Introduction to Writing Code in C

all right what's going on everybody it's you bro hope you're doing well and in this video i'm going to explain how we can get started writing code in c so sit back relax and enjoy the show if you wouldn't mind please like comment and subscribe one like equals one prayer for the youtube algorithm i'm going to tell you why you need to learn c c is a middle level language that originated in the 1970s and it is one of the most widely used programming languages to date on a spectrum of high-level languages to low-level languages c is a middle level language low-level languages are efficient they work closely with machine architecture they consume less memory and they're fast as however they're difficult to understand and it takes more time to write code that's in a low level format high-level languages are easier to work with easier to understand they allow for more abstraction but they're slower they use more memory and they're abstract so it's difficult to work with low level hardware and systems a major advantage of middle level languages is that they can act as a bridge between high-level software and applications as well as low-level hardware and embedded systems most compilers kernels and operating systems are written in c nearly all programming languages are influenced by c in some way c is what provided many of the original programming concepts such as variables data types loops arrays functions etc in fact the python language is written with c the default implementation is known as c python if you're already familiar with the programming language learning c will give you an even deeper understanding of how those operate c is literally everywhere from databases to self-driving cars operating systems to embedded systems it's been around for so long and used for so many purposes here are some important notes before we get started c is not an object-oriented language it's procedural not abstract c plus plus is an object-oriented extension of c if you know c you already know some c plus plus however c is a difficult language for beginners don't get discouraged you can do it so what you'll need you'll need an ide an integrated development environment which is basically a fancy text editor to help us write c code as well as a gnu compiler collection which we abbreviate to simply gcc this compiles or otherwise converts c code to machine code you know all those ones and zeros that a machine can read let's begin by downloading an ide i recommend vs code it's flexible and you can use vs code for more than just c all right what you're gonna do is head to code.visualstudio.com and then look for this drop down menu to install for your operating system i'm running windows i'm going to install for windows and then i will simply just open when done okay accept the license agreement next you can create a desktop icon and add to path next then install and then give it a second or a couple minutes then you can launch this if you prefer i think i will okay we are now within visual studio head to the left hand menu for extensions we're going to install two extensions c c plus plus that contains intellisense and a couple other useful things so install that and then next we will install code runner code runner install after installing these extensions you may need to restart vs code okay then we are going to add a new folder go to the left hand menu add folder i'll create a new folder on my desktop so i'll right click go to new folder i'll name this c files then add i think you might have to click within the folder okay we now have a folder named c files then to create a new c file go to new file after clicking this folder i'll name this hello world and make sure that it ends with the c extension hello world dot c and we now have a c file that we can work with and on this tab at the top this says hello world dot c now the next thing that we'll need is that gcc compiler to convert c code to machine code now if you're running windows this is how to check to see if you have a gcc compiler already installed so you're going to open command prompt and enter this command g plus plus dash dash version i already have a gcc compiler already installed if you're getting an error then you'll probably have to download one here's an interruption from future bro i traveled from the future to the past to deliver you this message so if you need to install gcc on a mac operating system what you're going to do within a terminal window is enter the following command c lang dash dash version if c lang isn't installed enter the following command xcode dash select dash dash install and that's all there is to it if you need additional assistance you can always visit this webpage code.visualstudio.com docs slash cpp and if you're running on linux within a terminal window you'll enter this command instead gcc dash v if gcc isn't installed run this command sudo apt-get update and then next type in this long command and if you need any additional assistance or documentation you can always visit this webpage so google this min gw dash w64 dash install exe and the first link is for sourceforge so click on that then you can find this underneath home browse development compilers mingw64 for 32 and 64-bit windows and the download should start automatically so click next change the architecture to x8664 next and then copy this path for the destination folder it's going to be relevant later then next next and finish now what we're going to do is add our path to our gcc underneath environment variables so open up control panel then go to system and security then system scroll down to advanced system settings underneath the advanced tab go to environment variables underneath path we are going to edit i already have this path configured so i'm going to delete this you probably won't have this setup and then new paste that file path to the gcc compiler then add slash min 64 slash bin then okay okay and then you can close out of everything and now we need to configure our build task so go to a terminal configure default build task if nothing appears within the search box you may need to restart vs code i think i do so i'm going to restart it and then let's try that again terminal configure default build task and there it is i will select that this creates a json file that tells vs code how to compile the program and with that out of the way we can begin coding now before we do start coding anything i'm going to increase the font size because as you can see this font size is really small so within vs code to change the font size go to file preferences settings and you can change that here let's try maybe 20. you can also change the font family too if you want and everything uh but let's close out of that and try that again okay that isn't too bad i'll stick with this font size for now i also recommend enabling autosave that's going to save you a lot of headaches later in the future so go to file auto save okay the first thing that we're going to include within our c program is the word hashtag include so this is a preprocessor command that tells the compiler to include the contents of a file and the file that we would like to include is within angle brackets std for standard io input output dot h this file contains some useful functions related to input and output so we'll need that now the entry point of our program is the main function type int main parentheses curly braces anything within our main function is read procedurally starting from the top and working its way down so anything within this set of curly braces is within the main function and at the end of our main function we're going to add the statement return 0 semicolon a semicolon is used to terminate statements kind of like a period at the end of a sentence at the end of our main function we have this return zero statement this returns the exit status of our program we return a zero if our program runs successfully with no errors if there is an error then we'll return a 1. so now we can add anything that we want within this main function but we'll need return 0 at the end to check for any errors so let's print something to our console as output so to display something we're going to type print f parentheses semicolon because we end our statements with a semicolon and within the parentheses add a set of double quotes because we would like to literally print something and you can type in whatever you want let's say i like pizza then to run this code you can either right click then go to run code alternatively there is a run code button in the top right corner of the s code so after running this code this displays my output i like pizza so what if i would like to add a second line well i would just follow these steps again so i need another printf statement print f parentheses semicolon any text i would like to display i'll place that within a set of double quotes this time let's add a second line i like pizza it's really good and then save all right and this is what this looks like i like pizza it's really good so this is all one long line what if i would like my second line of text on the next line well i can add an escape sequence for a new line character so at the end of my printf statement within the double quotes i'll add backslash n for a new line character and let's try that again so i'm going to clear my output make sure i save and then run this again i like pizza it's really good and then we have that extra space at the bottom because we added an additional new line character which is optional also take notice too that we have this message exited with code equal zero so if there are no errors and your program runs successfully this function will return a zero if there is an error well then this will return one so let's misspell something let's say instead of printf we just have print so save and then run this again okay it looks like we have an error exited with code equals one all right people well that's your first c program in the next video we'll cover escape sequences and comments i'll post this code to the comment section down below and pin it to the top if you would like a copy for yourself but yeah that is your first c program hey you yeah i'm talking to you if you learned something new then help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you