C tutorials Structure in C
Basic Structure of C:
Ø Save the file as hello.c.
Ø Open a command prompt and go to the directory where you saved the file.
Ø Type gcc hello.c and press enter to compile your code.
Ø If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file.
Ø Now, type a.out to execute your program.
Ø Output Programming C is shown on the display
$gcc programming.c
A C program Basically builds on the following parts:
v
Preprocessor Commands
v
Functions
v
Variables
v
Statements & Expressions
v
Comments
Now write your first program
#include<stdio.h>
Int main()
{
Printf(“Hello World”);
Return 0;
}
Output:
Hello World
Structure:
Preprocessor Directions
Global Declarations
Main()
{
Local Declaration;
Statements;
}
Function1()
{
Local Declaration;
Statements;
}
Function2()
{
Local Declaration;
Statements;
}
Ø
The first line of the program
#include<stdio.h> is a preprocessor command, which tells a compiler to
include stdio.h file before going to actual compilation.
Ø
The next line int main() is the main function
where the program execution starts.
Ø
The line/*..*/ will be avoided by the compiler
and it has been put to add additional comments in the program. Such lines are
named comments in the program.
Ø
The next line printf(“…”); is another function
in C which causes the massage. “Programming C” to be displayed Programming C
Ø
The next line return 0; terminates the main()
function and return the value 0.
Compile and execute C program:
Let us see how to save the source code in a file and how to compile and following are the simple steps:
Ø Open a text editor and add the above mention codes.Ø Save the file as hello.c.
Ø Open a command prompt and go to the directory where you saved the file.
Ø Type gcc hello.c and press enter to compile your code.
Ø If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file.
Ø Now, type a.out to execute your program.
Ø Output Programming C is shown on the display
$-/a-out
Programming C
Make sure the gcc compiler is in your path and that you are
running it in the directory containing the source file programming.c
No comments