Lets write our first C program – the typical “Hello World!” program.
In this video tutorial lets learn the structure of a basic C program:
1. Preprocessors – include directive.
2. Main method/function.
3. printf method/function.
4. Semicolon syntax.
5. Indentation for readability of code.
Page Contents
Source Code: A Simple C Program
#include<stdio.h> int main() { printf("Microsoft\n Apple\n Oracle\n Google\n Yahoo\n"); return 0; }
Output:
Microsoft
Apple
Oracle
Google
Yahoo
Structure of a basic C Program
[youtube https://www.youtube.com/watch?v=3Yu6u3tmBS4]
Preprocessor statement
The “include” directive which we see in the first line is a preprocessor directive. Here we are including a standard library file which has some set of useful functions in it, which we are using in our “Hello World” C Program.
stdio stands for “Standard Input Output”. As the name suggests this library file has functions to read and write data to console window, along with other many useful functions. For example, printf() is a function present in stdio.h library file. We simply use it in our program to print data on to the console window. We need not know the implementation details of printf method/function.
main() method
Main method or function is part of all ‘C’ programs. It’s entry point of any C program execution. Function is a way of grouping some code together. We can write any logic/statements inside a function.
It’s a standard that main always returns a integer value. Thus main is preceded by a keyword called int, which means integer. It’s a keyword or reserve word. We’ll know more about keywords(or reserve words) in a separate video tutorial. Since main method needs to return a integer value, we explicitly return 0 at the end.
Readability of code
It’s very important that we write code which is readable. It helps in maintaining the code. Large programs get too clumsy very easily and reading and understanding code often becomes difficult. So we need to indent the code and make sure its readable as far as possible.
Note:
Main method/function doesn’t take any argument.