Q:

In C, calling a function from main

In C, I tried to call a function printSum from main. But the main function isn't calling printSum, its just printing out "Hi!" which is a print statement from main. I am not sure why printSum is not being called. Thanks.
Code:
int main(void){

  void printSum(void);
  printf("Hi!\n");

  return 0;
}

void printSum (void){
  printf("Please give two integers\n");
    int x,y;
  scanf("%d %d", &x,&y);
  printf("%d + %d is %d\n",x,y,x+y);
}

Sam

A:

you defined it again..
Just remove the void from the funciton.
include a header a forward declartion so it will recognize it..
void printSum (void); <-------------------

int main(void){

  printSum();         <-------------------------
  printf("Hi!\n");

  return 0;
}

void printSum (void)
{
  printf("Please give two integers\n");
  int x,y;
  scanf("%d %d", &x,&y);
  printf("%d + %d is %d\n",x,y,x+y);
}

A:

It looks like you're just starting out with C. Hopefully you have some experience with other languages, as C has a steep learning curve. Anyway, it's important to note a few things about C. The first is what void printSum(void); really means. Breaking it down:
void printSum(void);
This declares a return signature. In other words, what the function gives back to you. In C, the word void basically means "no variable". Remember that specifically. Why? Because C has another similar word, NULL. NULL means "no value." This is another way to look at it.
Some valid variables: int, float, bool, void
Some valid values: 1, 'c', 2.0f, NULL
In reality, NULL is actually just the number 0. Literally. NULL == 0 will return true.
Moving on...
void printSum(void);
This defines the name of the item.
void printSum ( void );
The parentheses mean this is a function.
void printSum(void);
This represents a variable being passed into the system. So this could have been int, float, etc.
void printSum(void) ;
The semi-colon represents the end of a statement. This is a bit trickier of a concept to explain, but just think of it as finishing a sentence.
Now, the important bit here is the very first void. If you tell C what kind of thing a function returns, it assumes you are talking about the function, not actually calling it. Omitting the first void makes C attempt to run the function instead of define or declare it.
The difference between defining a function and declaring it is interesting and might be best saved for when you're a bit more accustomed to C.

