A simple C program basically consists of the following parts −
- Preprocessor Commands
- Functions
- Variables
- Statements & Expressions
- Comments
Basic structure of C program
#include
1 2 3 4 5 6 7 | #include <stdio.h> int main() { /* first C program */ printf("Hello"); return 0; } |
Above shown the basic structure of C program. For every program we have to write above mention basic structure.
How to write or print
We can write or print in C language by using printf() function. printf() function is a predefined function. for example: code to write “hello” is as follows:
printtf(“hello”);
output: hello
Note: any thing written within double code in a printf() function will be write.
A simple C program to print “Hello”
1 2 3 4 5 6 7 | #include <stdio.h> int main() { /* first C program */ printf("Hello"); return 0; } |
Description
- First line #include <stdio.h> is a preprocessor, which instruct to C compiler to include stdio.h file. stdio.h (slandered input output ) is used for printf function.
- int main() is the main function from where the program execution begins.
- /*………..*/ is a “comments” in the program. This comments will be ignored by the compiler during execution of program
- printf() is a predifned function in C which is used to print message.
- line return 0; terminates the main() function and returns the value 0.return 0 tells the compiler that everything is ok and the execution of main() function ends. Because 0 is the code for successful execution, while values greater than 0 (integers) is an indication that something went wrong.
Compile and Execute C Program in Unix
How to save and compile the source code and run it:
- Write the above-mentioned code on a text editor.
- Save the file as hello.c
- Open a command prompt and go to the directory where you have saved the file.
- To compile your code: type gcc hello.c and press enter.
- If there are no errors in your code, the command prompt will take you to the next line and generate a.out executable file.
- To execute your program, type a.out and press enter.
- Output “Hello” will print on the screen.
COMPILATION
1 2 | $ gcc hello.c $ ./a.out |
OUTPUT
1 | Hello |