Question about "main" and functions (from Scope introductory lesson)

Why is it that when i run this program, only the printf inside main works?
What is the difference between main and another function?
Isn’t “main” just a naming convention for what will be the main function of a program?

#include <stdio.h> // global scope int b = 10; void myFunc() { // myFunc() local scope printf("Inside myFunc, b value is:%d", b); } int main() { // main() local scope int a = 200; printf("The value of b is %d", b); }

The example comes from this lesson.

That’s not just a naming convention.
When a program is executed, the main function is the function that runs.
Any other functions that you want to run would be called in main, or called by a function that’s called by a function that is called in main, or some kind of similar chain of function calls that always starts with main.

2 Likes

Thank you
How can we rewrite the code so that main calls “myFunc”? (So that we get both printf in output)

You could do something like

int main() {
  // main() local scope
  int a = 200;
  printf("The value of b is %d", b);
  myFunc();  // myFunc is called inside of main
}

so that myFunc is called in main.

1 Like