[program 1]
Hello guys, today we will try the famous "Hello World" program. Here's the code:
1: #include<stdio.h>
2: void main()
3: {
4: printf("Hello World");
5: }
Okay, now lets understand the program.
- As stated earlier, "#" is a pre-processor directive. It says the compiler to perform a task before any further execution. "include" is the type of work that is to be done in pre-processing. It includes the header file "stdio.h" with the source code. "stdio.h" is a library header file which contains the codes to implement various input/output operations.
- Then comes the main() function. We know that al lfunctions are user defined functions, but this isn't entirely true. The main function is different. In C programming Language main function is user implemented (user defined) whose declaration is fixed in the compiler. hence we can say that main() is predefined and user defined too because programmer has to write function's body.
Actually
main() is an entry point of a program, from where program’s execution starts.
Program’s execution starts from main(). Main is a thread/process/function which is invoked by operating system automatically when program is being executed.
There are many flavors of main function, flavors means prototypes. One that we used commonly
int main(void)
– it means main will return an integer value after or between in the program’s execution and void represents that there are no argument passing.
You can also pass arguments while writing main the syntax is
int main(int argc, char **argv){...}
One can also use,
void main()
- it means the main function will return void to the OS. - Printf is a function, a library function at that which is provided by stdio.h header file. When declared it prints the desired information in the console's screen. Note that it is terminated with a semicolon(;), this is a terminator. It terminates the line there. Every line in a program should be terminated with a semicolon.
In case if you want to write the program using int main(),here is the code:
1: #include<stdio.h>
2: int main()
3: {
4: printf("Hello world");
5: return 0;
6: }
In case you are wondering, what is this "return 0 "and why didn't we write earlier.
Well we used void main() earlier. It is the same as int main() with return 0 . As we don't want the program to return anything to the OS, we can use void main() or use int main() with return 0.
Note -:
Void main() is only used by some compilers in C. Make sure your compiler supports it to use it.
Turbo C supports void main() for C programming.
No comments:
Post a Comment
If u need any help,