I have questions for lecture in week 2. In function-0.c file, I want to understand each line, so please fix me if I am wrong.

// cs50 library is necessary to use GetString
#include <cs50.h>
// stdio library is needed for printf
#include <stdio.h>
// prototype to define before int main
void PrintName (string name);

What does void do here? I know that xxx(void) means xxx does not take any input. But this case is confusing.

The string name in the parenthesis is to call name as a function, printing it. string is only the argument here.

Also, is xxx(void) the same as xxx()?

// main is set as integer with no argument
int main(void)`
    // printing
    printf("Your name: ");
    string s = GetString();
    // 's' in the parenthesis is the name of string to call GetString()

why string s was used? Can I use string without s such as string = GetString();?

I am confused why the function's name is s instead of string s. Also, I thought the name of string is just name. Please clarify it.

void PrintName (string name)`
    // implement the prototype used before main int 
    printf("hello, %s \n", name); `

What is different between %c and %s?

  • you need to watch the shorts on variables and functions! and I encourage you to google the difference between %c and %s. you can also read more about them in man 3 printf.
    – kzidane
    Sep 23 '15 at 19:24
  • I wrote this after watching variables and functions. PrintName is a function of name, and has no return values. In the main function, we set string s as GetString(). Then, I think PrintName(s) should be PrintName(string s). Sep 23 '15 at 23:25

aside from my comment, I'm gonna answer the question

is xxx(void) the same as xxx()?

assuming we're talking about function prototypes, the answer would be no.

the first explicitly tells that compiler that xxx takes no arguments. while the second, although it may seem that it does tell the same thing to the compiler and it practically does that in other languages, doesn't in this case.

try compiling the following code:

void foo();

int main(void) {
    foo(10); // produces no errors

void foo() {}
  • I compiled this code run well without any output. So, I tried printf( "%i", foo) in the code which gives me an error. I still don't get what is difference between main(void) and main(). Also, could you explain why it runs without setting return value such as return 0; or return; for the end of the code since main with no argument needs returning value? Sep 23 '15 at 23:39

You must log in to answer this question.

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