Place Your Query
-
C Programming
-
- Control flow-based C Programs
- Enjoy Conditional Programming in C using If...else and switch statement.
- Good, Now write a C program to display "Hello World" on the screen.
- Write a C program to display Your Name, Address and City in different lines.
- C program to find sum of n numbers
- Write a C program For Addition of two numbers using Function.
- Write a Program to Find Simple Interest and Compound Interest.
- Write a Program to Convert Celsius To Fahrenheit and Vice Versa.
- Write a Program to Find Area and Perimeter of Circle, Triangle, Rectangle and Square.
- Write a Program to Swap Two Numbers Using Temporary Variables and Without Using Temporary Variables
- Write a C Program to Design a Simple Menu Driven Calculator
- Simple Expression Based C Programs
-
- 7. Components of C language
- 1. Introduction to C Programming Language
- 10. Operator Precedence and Associativity
- 11. Comments in C Programming
- 14. Fundamental Control Structure Statements in C [Part-1]
- 15. Fundamental Control Structure Statements in C [Part-2]
- 16. Looping Statements [Fundamental Control Structure Statements in C. #Part-3]
- 17. Keyword break, continue, return and exit [Fundamental Control Structure Statements in C. #Part-4]
- 2. Computer Languages
- 3. Interpreters vs Compilers vs Assemblers in programming languages
- 4. C Program Structure
- 5. Compile and Execute C Program
- 6. Errors in C Program
- 8. C Datatypes
- 9. Operators in C
- Control flow-based C Programs
- Demystifying Bit Masking: Unlocking the Power of Bitwise Operators in C-Language with best Examples.
- 18. Fundamentals of C Functions
- Show Remaining Articles (3)Collapse Articles
-
-
Java
-
AI ML
-
FAQs
- A Program to find Tokens in C code?
- What are the Common Coding Mistakes.
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
-
Python Interview Questions
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
4. C Program Structure
The program structure in any programming language is to organize the code into different sections that make it easier to read, modify, and understand. It also helps to avoid repetition of code, improve modularity, and enhance clarity. Program structure can vary depending on the programming language, but in C language, some common elements are documentation, preprocessor, definition, main function, and subprograms.
Quick Links
What is Program Structure
A C program’s structure organizes the code into sections for easier reading, modification, and understanding. A typical C program structure consists of the following parts:
Program Sections (parts of a program)
- Documentation Section: This is where you write comments to describe the purpose, author, date, and other details of your program. Comments are ignored by the compiler and are only for human readers.
- Link Section: This is having two major parts. First, Preprocessor directive which start with a # symbol and are processed before the compilation. Second, where you include header files that provide access to standard or external libraries and define macros or constants that can be used throughout the program. So, we can say that link section is nothing but a Preprocessor Directive and Header file inclusion.
- Definition and Global declaration Section: This is where you declare global variables and functions that can be used by any part of the program. Global variables have a scope that extends to the entire program, while functions are blocks of code that perform a specific task and can be called from anywhere in the program.
- The main() function Section: This is the entry point of the program, where the execution begins. Every C program must have a main function, which can have either a void or an int return type. The main function can take arguments from the command line, and can also return a value to the operating system.
- Subprogram Section: This is where you define user-defined functions that are called from the main function or other functions. User-defined functions can have parameters and return values and can be used to modularize the program and avoid repetition of code.
Here is an example of a C program structure that prints “Hello, Bharat!” to the standard output:
/* Documentation
This program prints "Hello, Bharat!" to the standard output
Author: Milind Bhatt
Date: 27 Dec 2023 */
// Link section
#include<stdio.h> // header file for input/output functions
// Main function
int main()
{
printf("\n Hello, Bharat!"); // print the message using the printf function
return 0; /* return 0 to indicate successful execution */
}
// Subprograms
// No user-defined functions in this program
Explanation:
The first six lines are the comment, defining the objective of the program and author details.
The seventh line of the program#include
The lineint main()is the main function where the program execution begins.
The line /*…*/ or //will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.
The lineprintf(…)is another function available in C which causes the message “Hello, Bharat!” to be displayed on the screen.
The next linereturn 0;terminates the main() function and returns the value 0.
Detailed example having all the sections:
/* sample Program By: Milind Bhatt */
#include<stdio.h>
void msg();
int x=10;
void main()
{ int i=5;
int b= i+ 2*x;
printf("\n var b = %d", b);
msg();
}
void msg()
{ printf(" You have made first C program");
printf(" Bye and take care!!");
}
Explanation:
The following code of C programming language is a program that prints the value of a variable b and a message to the standard output. Here are some bullet points to describe it:
- The first line is a comment that starts with /* and ends with */ and gives the name of the author of the program.
- The second line is a preprocessor directive that includes the header file stdio.h, which provides input/output functions such as printf.
- The third line is a function prototype that declares a user-defined function named msg, which takes no parameters and returns nothing.
- The fourth line is a global variable declaration that defines an integer variable x and assigns it the value 10.
- The fifth line is the main function, which is the entry point of the program. It takes no parameters and returns nothing.
- The sixth line is a local variable declaration that defines an integer variable i and assigns it the value 5.
- The seventh line is an expression that calculates the value of b as i plus 2 times x, and assigns it to a local variable b.
- The eighth line is a function call that prints the value of b using the printf function and the %d format specifier, which indicates an integer value.
- The ninth line is another function call that invokes the user-defined function msg.
- The tenth line is the end of the main function, indicated by a closing curly brace.
- The eleventh line is the definition of the user-defined function msg, which takes no parameters and returns nothing.
- The twelfth line is a function call that prints “You have made first C program” using the printf function.
- The thirteenth line is another function call that prints “Bye and take care!!” using the printf function.
- The fourteenth line is the end of the user-defined function msg, indicated by a closing curly brace.
More About C coding
#include
The part of the compiler which actually gets your program from the source file is called the preprocessor.
#include
#include is a pre-processor directive. It is not really part of our program, but instead it is an instruction to the compiler to make it do something. It tells the C compiler to include the contents of a file (in this case the system file called stdio.h).
The compiler knows it is a system file, and therefore must be looked for in a special place, by the fact that the filename is enclosed in <> characters
stdio.h is the name of the standard library definition file for all Standard Input and Output functions.
Your program will almost certainly want to send information to the screen and read things from the keyboard, and stdio.h is the name of the file in which the functions that we want to use are defined.
The function we want to use is called printf. The actual code of printf will be tied in later by the linker.
The “.h” portion of the filename is the language extension, which denotes an include file.
void
This literally means that this means nothing. In this case, it is referring to the function whose name follows.Void tells to C compiler that a given entity has no meaning, and produces no error.
Main
In this particular example, the only function in the program is called main. A C program is typically made up of large number of functions. Each of these is given a name by the programmer and they refer to each other as the program runs.C regards the name main as a special case and will run this function first i.e. the program execution starts from main. A parameter to a function gives the function something to work on.
{ (Brace)
This is a brace (or curly bracket). As the name implies, braces come in packs of two – for every open brace there must be a matching close one. Braces allow us to group pieces of program together, often called a block. A block can contain the declaration of variable used within it, followed by a sequence of program statements. In this case the braces enclose the working parts of the function main.
;( semicolon)
The semicolon marks the end of the list of variable names, and also the end of that declaration statement.
All statements in C programs are separated by “;” (semicolon) characters. The “;” character is actually very important. It tells the compiler where a given statement ends. If the compiler does not find one of these characters where it expects to see one, then it will produce an error.
scanf
In other programming languages, the printing and reading functions are a part of the language. In C this is not the case; instead they are defined as standard functions which are part of the language specification, but are not a part of the language itself. The standard input/output library contains a number of functions for formatted data transfer; the two we are going to use are scanf (scan formatted) and printf (print formatted).
printf
The printf function is the opposite of scanf. It takes text and values from within the program and sends it out onto the screen. Just like scanf, it is common to all versions of C and just like scanf, it is described in the system file stdio.h. The first parameter to a printf is the format string, which contains text, value descriptions and formatting instructions.