In pset2 description following is said about int main(int argc, string argv[]) function:

1 : Well, recall that inside of argv[0], by default, is a program’s own name

Q1: How come argv[0] is by default program's own name? I don't remember reading it in books. Isn't argv[0] is first letter in string? For example, "This is CS50", in this case argv[0] is letter "T".

2 : But for this program you want the user to provide a command-line argument, k, in which case argc should be 2

Q2: isn't argc equals number of words in string. For example, "This is CS50", in this case argc = 3 (1 - This, 2 - is, 3 - CS50)

I'm totally confused with description of main(int argc, string argv[]) and cannot continue. Please help with description of this function.


Nothing is misleading in the lectures, you interpreted them wrong.

It should be noted that argv is itself not a string, it is collection of string type objects. Let me tell you the structure of parameter argv[](assuming that you wrote hello.c).

argv[] = {"./hello", "This", "is", "CS50"} // this is correct

and not

argv[] = "./hello This is CS50"   //this is wrong

So now the following stand for the respective objects :

  1. argv[0] for ./hello and not for '.'
  2. argv[1] for This and not for '/'
  3. argv[2] for is and not for 'h'
  4. argv[3] for CS50 and not for 'e'

I think now it would be clear. Good Luck.

  • So is this function covered in David's lecture? If so, in which one? I watched them and after couple of months started pset2, and could forgot about it. – Dauren Z Nov 17 '14 at 14:07
  • at length 6:00 , week-3m – sinister Nov 18 '14 at 0:03
  • #include <stdio.h> #include <stdlib.h> #include <cs50.h> #include <string.h> int main (int argc, string argv[]) { for (int i = 0; i < argc; i++) { for (int j = 0; n = strlen(argv[i]); j < n; j++) { printf("%c\n", argv[i][j]); } } ERROR: caesar.c:16:25: error: use of undeclared identifier 'n' But it works in video without error and "n" being declared. Why? – Dauren Z Dec 8 '14 at 14:35
  • Also, I got the function principle now. But, why do they put function description in week 3 video and refer to it in pset2? For example, I even didn't think to start any week 3 materials until I finished, pset2. – Dauren Z Dec 8 '14 at 14:40
  • somebody please help? – Dauren Z Dec 10 '14 at 14:14

Week 0 = first week of school, no pset. Week 1 = second week of class, pset0. Week 2 = third week of class, pset1. One of the objectives of cs50 is to change the way you think & how you see the world around you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .