What does int argc, char* argv[] mean?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is Paul in this video I'm going to be explaining the Arg C and RV that are sometimes pass in as parameters to a C or C++ main function so you may have written C or C++ code before and wonder to yourself what is Arg C and what is arc V why are they there well in a nutshell Arg C represents the number of things that you entered into the command line when running this program on a terminal Arg V is an array that holds the string values of the things that were entered on the command line when running this program so if that doesn't make sense don't worry I'm going to explain each of these step-by-step so first let's take a look at argh C so since this is a C file I'm just going to print the contents of arc C by calling the printf function and then I'll say Arg C is equal to some integer so this percent D right here in the side of the printf function represents that we're going to be supplying this string with an integer value and then we specify what integer we want to supply it after this end quote here by saying comma and then the name of the variable that holds the integer value that we'd like to place right here so in our case we want to see what's inside of Arg C we'll end that with a semicolon and then we'll go ahead and give it or madding by putting a new line here we'll save this and now notice here I'm in a directory which I've named args and if I type the LS command we can see the inside of the directory I've got Arg C and our GC is this file that I have open here so we're going to compile our GC by saying G CC and then the name of the C file we want to compile which is Arg C and then we're going to give it a - o flag which allows us to give our executable file a unique name and I want to give our executable the name add so I'm going to press ENTER now and now if i type LS we can see we now have our new executable named ad inside of our directory so let's go ahead and run this new executable and see what happens so a dot /add and here we get the output Arg C is equal to 1 and that's because we entered one thing when running this program we entered the prompt itself that is the one thing that we entered to run the program so I could enter more things so I'll do dot /add and I'll say halt programming so here I'm entering one thing here's a second thing I'm entering and here's a third thing so when I push enter we should see the value three inside of arg C and sure enough we get the value three right here so if I wanted to modify this a bit and just take off the programming here we should get the value to the prompt to run the program would be the first art and Paul would be the second argh so we've got two arms right here so arc C just represents the number of things that we entered into the command prompt when running our executable so now let's take a look at this Arg V here so our V is an array and in the C programming language a char star is a string so you can think of our V as an array of strings and the strings that are V holds are essentially going to be the things that were entered onto the command prompt here so let's go ahead and write some code so we can see what that looks like so I'll just go ahead and do another printf function here so here I'll just say let's see what is in Arg V then I'll put a new line here and that with a semicolon and I'll want to go ahead and just run a for loop here so I'll just do four and actually before I do that I'm going to go up here and create an integer variable called I and I'll use that as my index for the for loop in the C programming language you can't say int I as the beginning of your for loop you can do this in C++ but and see you've got to get rid of this declaration right here you can't declare a variable inside a for loop one you're writing C code that will work in C++ but not in C so we're going to start at the index I and as long as I is less than the value that stored in Arg C then we're just going to do the stuff in the for loop and then we're going to increment or add 1 to the value of I so inside of the for loop what we're going to do is we just want to see what's inside of our r.v array so I'll say Arg V and then I want to print out a particular index and so I want to see what index I'm printing so I'm going to put a integer placeholder here and each index of the array is going to hold a string value so I'll put percent s to let the printf function know that we're going to be placing a string value here and so we'll just go ahead and put a new line here now let's go ahead and specify where we're getting this integer and where we're getting the string value so the integer value is coming from I it's just going to represent the index that we're currently looking at and the string value is going to be the thing that's inside the eighth element of our r.v array so I'll just say RV and we're going to look at the element of that so now I'll go ahead and save this I'll go ahead and type the clear command here now we're going to recompile our program so I'll say g CC args dot c minus o ad and now we'll go ahead and run our executable one more time and here we've got one thing it's the prompt to run the executable and we can see that we've got the value of arc C is 1 and then in arg V index 0 we have the thing that we entered here to run the program so that's pretty cool let's look at this again with some more arguments so let's say add 4 6 8 so now we can see that arg c is for we've got one two three four things that we entered onto the command-line when running our ad program and now we're going to see what's inside of the arc v array well our V index 0 holds the prompt to run the executable our v1 holds the for our v2 holds the 6 and our v3 holds the 8 so you can see that each of these just got placed into the indexes of the Arg V array now just to kind of help demonstrate what we can do with this stuff let's go ahead and have our program add the values that are stored in our V index 1 and greater so to do that we don't really want to look at what's in index 0 anymore we're going to start with index 1 because those are going to be the numbers we're going to want to add up here and so we can go ahead and continue to print out what is inside of our r.v indexes but let's create another integer variable here we'll call it sum and we'll initially set it to 0 and what we're going to do is every time we enter this for loop we're just going to say sum is equal to its self plus whatever is contained in the 8th index of our V but the ithe index of our viii is actually a string and sum is an integer so we're going to convert this string to an integer by just passing it into the a to I function so now we're going to get the integer value corresponding to the string that's stored in the I element of RV we're going to add that to the value of sum and then store that result inside of the variable sum we can actually do this in a simpler way by saying sum plus equals a to I of the I RV and so this right here using the plus equals is the exact same thing as what I wrote above we'll go ahead and delete this part right here since they both mean the same thing so now our code is a little bit more simplified we're just adding the next integer value to our sum total so let's go ahead and just do a really quick check here we don't really want to do this calculation if we're only dealing with the 0 with our gear so we'll just go ahead and just do a quick if check right here will say if Arg C is greater than 1 then we want to go ahead and do the logic that I put here otherwise we'll just go ahead and skip this whole section so we'll go ahead and shift this over for formatting so as long as we have an index 1 or greater we're going to go ahead and add the values of all the things that I enter here on the command line and then we'll go ahead and print a total of that when we're done so let's go ahead and write the print function to print the total we'll just say total equals and it's going to be an integer value we'll put a new line and let's go ahead and tell it where we want that integer value to come from and we want it to come from the some variable I don't have anything really checking to see if I'm entering integer values here I could enter a string or a floating-point or something like that but if you are concerned about making sure that these were actually integers here you'd want to put some more checks into your code to make sure that's the case but I'm not going to worry about it for this program let's go ahead and expand this window a little bit since we're kind of cutting off the code right here and we'll modify this window size also so let's just go ahead and save this file now and see what happens we'll say clear and now we'll just compile it one more time GCC args dot c minus o ad now if I do dock /add and push enter it says let's see what's in our V but goes into checks and it sees okay argh see is not greater than one so it skips this whole section so that's working fine so now let's go ahead and run our program and we'll give it some integer values to total up for us so we'll say three and two and five so that should give us a value of 10 so we'll push enter you can see the value of Arg C is four there's four things here let's see what's an RV this time we're not printing index zero because we change this to one so we start with that index start with index 1 here and we print index 1 2 & 3 you can see 3 2 & 5 or stored in those indexes and if you add 3 + 2 + 5 you get the total of 10 so we can try it with one other thing right here so let's just change this to a negative 2 so 3 plus a negative 2 would be a positive 1 plus the 5 would give us a positive 6 push enter and we can see everything looks fine arg c is for rv1 holds 3 our two holds the negative 2 that we changed here and our v3 holds the value 5 and the total is 6 when you add all three of these together so anyway that's an introduction of what Arg C and RV mean when you're using a C or C++ program thank you guys for watching have an excellent day and if you haven't already don't forget to subscribe
Info
Channel: Paul Programming
Views: 226,127
Rating: 4.9526582 out of 5
Keywords: argc, argv, argv[], int argc, char* argv[], char *argv[], What, is, are, argc and argv, argc and argv[], what are argc and argv, what is argc and argv, what is argc, what is int argc, what is char* argv[], what is char *argv[], the, args, in, main, c/c++, C program, C++ program, C Programming, printf, example, paul programming, paulprogramming, paul, programming, computer science, computer, science, code, sublime text, parameters, explaination, tutorial, lesson, meaming, of, %d, %s, %d printf function
Id: aP1ijjeZc24
Channel Id: undefined
Length: 10min 10sec (610 seconds)
Published: Sat Jan 10 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.