C Program Basic Program[1.0.3]
Posted by Parth Makadiya on 12:03 with No comments
Basic commands in C programming to write basic C Program:
Below are few commands and syntax used in C programming to write a simple C program. Let’s see all the sections of a simple C program line by line.
S.no | Command | Explanation |
1 | #include <stdio.h> | This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program |
2 | int main() | This is the main function from where execution of any C program begins. |
3 | { | This indicates the beginning of the main function. |
4 | /*_some_comments_*/ | whatever is given inside the command “/* */” in any C program, won’t be considered for compilation and execution. |
5 | printf(“Hello_World! “); | printf command prints the output onto the screen. |
6 | getch(); | This command waits for any character input from keyboard. |
7 | return 0; | This command terminates C program (main function) and returns 0. |
8 | } | This indicates the end of the main function. |
1. C Basic Program:
#include <stdio.h> int main() { /* Our first simple C basic program */ printf(“Hello World! “); getch(); return 0; } . |
Output:
Hello World! . |
2.Steps to write C programs and get the output:
Below are the steps to be followed for any C program to create and get the output. This is common to all C program and there is no exception whether its a very small C program or very large C program.
3. Creation, Compilation and Execution of a C program:
Prerequisite:
- If you want to create, compile and execute C programs by your own, you have to install C compiler in your machine. Then, you can start to execute your own C programs in your machine.
- You can refer below link for how to install C compiler and compile and execute C programs in your machine.
- Once C compiler is installed in your machine, you can create, compile and execute C programs as shown in below link.
C – Environment Setup Using IDE tool
C – Environment Setup Using GCC compiler
4. Basic structure of C program:
Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned below.
- Documentation section
- Link Section
- Definition Section
- Global declaration section
- Function prototype declaration section
- Main function
- User defined function definition section
Example C program to compare all the sections:
You can compare all the sections of a C program with the below C program.
/* C basic structure program Documentation section Author: fresh2refresh.com Date : 01/01/2012 */#include <stdio.h> /* Link section */ int total = 0; /* Global declaration and definition section */ int sum (int, int); /* Function declaration section */ int main () /* Main function */ { printf (“This is a C basic program \n”); total = sum (1, 1); printf (“Sum of two numbers : %d \n”, total); return 0; } int sum (int a, int b) /* User defined function */ { /* definition section */ return a + b; } . |
Output:
This is a C basic program Sum of two numbers : 2 . |
To Get Output AT GCC Compiler(type this code at terminal)
$ gcc programename.c
$ ./a.out
Description for each section of a C program:
- Let us see about each section of a C basic program in detail below.
- Please note that a C program mayn’t have all below mentioned sections except main function and link sections.
- Also, a C program structure mayn’t be in below mentioned order.
S.No | Sections | Description |
1 | Documentation section | We can give comments about the program, creation or modified date, author name etc in this section. The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation. Example : /* comment line1 comment line2 comment 3 */ |
2 | Link Section | Header files that are required to execute a C program are included in this section |
3 | Definition Section | In this section, variables are defined and values are set to these variables. |
4 | Global declaration section | Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section. |
5 | Function prototype declaration section | Function prototype gives many information about a function like return type, parameter names used inside the function. |
6 | Main function | Every C program is started from main function and this function contains two major sections called declaration section and executable section. |
7 | User defined function section | User can define their own functions in this section which perform particular task as per the user requirement. |
C programming tutorial reference E-books & research papers:
- [LET US C] This is a nice book if you want to learn C.Yashavant Kanetkar is a nice Writer.I like this Book. Download Here
- [ANSI C] This is good Book If you only want to get Exam Preparation and So ON..
- [ANSI 89] American National Standards Institute., American National Standard for Information Programming Language C, X3 159-1989
- [Kernighan 78] B. W. Kernighan and D. M. Ritchie, The C Programming Language, Prentice-Hall: Englewood Cliffs, NJ, 1978. Second edition, 1988.
- [Thinking 90] C* Programming Guide, Thinking Machines Corp.: Cambridge Mass., 1990.
0 comments:
Post a Comment