Details about the main function

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we'll take a look at a very familiar function and that is the main function of your program so you're pretty familiar with it in the sense that you've probably written it a hundreds of times or the IDE already auto-completed it for you inside of files so you didn't pay much attention to it it was initially taught to you one way and you just kind of saw that it worked and you never really paid much attention to it and what I feel is that some people get this declaration wrong and there are some there are some materials out there that actually do use the wrong definition of the main function so let's get straight to it how do you properly define the main function well the first thing is just the name the name is main all lowercase that's for sure then you just have the main body the parameters here and then the actual codes are gonna write inside here now the return type is the one thing that many people do get wrong so the return type has to be int it cannot it cannot be void it can be float or char or anything like it it has to be int from the specifications of the C language right ice also many people use no specification here and if I try to for example let's print let's print a statement here hello right and if I try to run this it is going to build and show me on the screen the hello world or the hello world here now now not actually providing a return type is an obsolete way of declaring the main function this is allowed due to backwards compatibility with other C programs right in the past this was allowed but now it isn't and why I don't want you to get this wrong it because in the future like very far future you might see compiler that compilers that don't support this sort of return type so you have to actually specify a return type to the main function and that is int so now that we know we have to declare the return type as int for the main function what why do we not actually use a return like don't mean don't we always have to return a value when we say that okay you need to return an INT here like this program compiles just fine right it works it's no problem and I don't have a return statement well the thing is that most compilers what they do or actually the specification itself says that the compiler is going to add a an empty return statement at the end of the main if he doesn't see any return statement so this would be equivalent to our previous version which was not having any return type in here right so and then again if you have a return without any value it's going to simply return zero as a default right so you just kind of it just kind of defaults to return zero all of a sudden and that's basically the value itself so if I try to run it it's exactly the same and it does say that the program has exited with called zero if I try to change this to a five for example I'm gonna get a different value as you can see down here right with the code five so that's how you can control that exit code that exit code really what it does is it tells the underlying operating system what happened with the program so zero will mean the program executed successfully and it's alright you can continue and if anything aside from zero is an error code so we've looked at the return type we've looked at the return statement we know the name is going to be main with lower keys now let's take a look at the parameters we can have at the very least two parameters those two parameters are going to work on any environment and is to compile anything because it's in the specifications themselves those are and you might know of them int arc see and char pointer heart v4 array of char pointers which is our tree here and those are the actual parameters our past from the command line to this program itself right so for example if I try to print here let's say print percent s message and and if I do here arc V of 0 you notice that I get the path to my to my executable so that's always going to be the first parameter inside arc V now you can go to debug and then project properties here inside this is visual studio by the way by the way so if you go here and you change the inside debugging you change the command arguments I added here - - hell but you can do anything you can say hello world for example and those are like executing the program with these command arguments right it's going to append them to the execution command line and if I try to run this now and get the second argument you'll notice I'm going to get hello on here and as well the third argument is going to be world right so they are actually split at space and you have every single argument inside this RV and you actually just get a for loop like so and then if I try to run this I'm going to get every single argument inside the console here so let's all nice that's that's about all there is to these two parameters so aside from those two parameters there's a third one that is widely supported by most compilers and you can probably use it and that is n P and P is again an array of char pointers no char pointer and P array and pick like that and it can really be any name you want but it's recommended to keep the names as is inside your program now NP really what it stores is the environment variables that you usually set inside the environment variable system settings that you might remember about so basically every single program runs inside an environment with some variables set like where's your for example Java executable or where's your home directory and so on and so forth so in our case here is where you can find those environment variables and the weird part about this is the way you iterate it so it's different from Arc V because all he does have a counter right but this guy doesn't have a counter this guy is an array right it's an array that ends with an element that is a null pointer right so if for example you have three environment variables this array is going to be of length four char pointers and the fourth one is always going to be a null pointer so what you can do is say for I equals zero and here I'm going to say and P of I is different than null and I plus plus and then I'm going to print F I'm gonna copy this thing so here I'm going to say NP and if I try to now run this I'm gonna actually comment out the ArcView ones so that we don't have that anymore you'll notice I get a whole bunch of lines here on the screen and those are all my environment variables that are set on my system right so you can actually get all of them here and if you really need any of those you can just sort of let me actually run this again you can just iterate over them split at equals right and get check the left side if the left side is what you're looking for that's the key then the right side is going to be the value right so that's how you can get the environment variables inside your program and that's really all there is to it for the main function itself not much just remember the return type is going to be always int lowercase main don't don't change it to uppercase or something like that it's not going to work then you have these two parameters that are always going to be available that you should probably add them on your program because you're probably gonna use them at one point and this third one is all the environment variables and it's probably also supported by our compiler and also don't forget to return a variable here and it has to be an int don't return anything else right so it's recommended that you actually have a return statement otherwise yeah sure is going to be automatically added but just for consistency's sake you should really add a return statement because all other functions do have a return statement when they have a return type that's different and void all right so I hope you got something out of this video I hope you now fully understand how to declare main it's very simple it's just some nitty-gritty details that some people gloss over or they don't really care it works without having a return type but then again you might risk that your programs are not going to work in the future and that's that's an issue right so thanks so much for watching if you do have any questions do leave them down in the comments below or on our discord server we have a link down in the description take care bye
Info
Channel: CodeVault
Views: 2,146
Rating: 5 out of 5
Keywords: main function, main c, return of main, return 0, argc, argv, envp, codevault, main, c (programming language)
Id: 2dsSGnmbXNM
Channel Id: undefined
Length: 10min 27sec (627 seconds)
Published: Mon Sep 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.