Skip to main content

C Programming - 4.2 The main() function

The main() function is the entry 🔑 point of a C program

Now look at the code what I wrote

#include <stdio.h>

int main()
{

     printf("I am a Programmer\n");

     return 0;
}

OK, I wrote,

int main()
here "int" represents the Integer number is, an Integer number is a whole number not a fraction one like 1,2,0,10,55 etc.

now, the question ❓ is-

Why I wrote int main()

We all know mathematical function right.
for example :
                        f(x) = bx+c

this function f(x) has a return value, let's say it's Integer, then for C it is int


C functions are quite similar to mathematical functions, the basic format of a C function is

return_type function_name()
{

}

by writing int main(), we indicate that this main() function will return an int number.

that's why we write return 0 at the end of the code because 0 is an int number

int main()
{

     printf("I am a Programmer\n");

     return 0;
}

but, C function can also return nothing
in that case, we will simply write

void main()
{

}

OR

main()
{

}

cause if we just write main() not specifying the return type,
the main() function will behave as void main() by default

we will learn more about functions in details later 

Now listen,

As I said, main() function is the entry point of a program, that means you always have to write the main function when you write a C code

The formats for writing main() function is-

int main()
{
    
     return 0;
}

OR

void main()
{

}

OR

main()
{

}


You can follow any of the above three.

I like the first one 😍
So, I will be using the first one  for writing the future codes


Happy Coding
See You in the next tutorial



Next Post:

https://coderavens.blogspot.com/2018/05/c-programming-43-header-files.html

Comments