- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
C - Program Structure
A typical program in C language has certain mandatory sections and a few optional sections, depending on the program's logic, complexity, and readability. Normally a C program starts with one or more preprocessor directives (#include statements) and must have a main() function that serves as the entry point of the program. In addition, there may be global declarations of variables and functions, macros, other user-defined functions, etc.
The Preprocessor Section
The C compiler comes with several library files, having ".h" as an extension. A ".h" file (called a "header file") consists of one or more predefined functions (also called "library functions") to be used in the C program.
The library functions must be loaded in any C program. The "#include" statement is used to include a header file. It is a "preprocessor directive".
For example, printf() and scanf() functions are needed to perform console I/O operations. They are defined in the stdio.h file. Hence, you invariably find #include <stdio.h> statement at the top of any C program. Other important and frequently used header files include string.h, math.h, stdlib.h, etc.
There are other preprocessor directives such as #define which is used to define constants and macros and #ifdef for conditional definitions.
The following statement defines a constant PI −
#define PI 3.14159
Example
Once a constant is defined, it can be used in the rest of the C program.
#include <stdio.h> #define PI 3.14159 int main(){ int radius = 5; float area = PI*radius*radius; printf("Area: %f", area); return 0; }
Output
On executing this code, you will get the following output −
Area: 78.539749
You can also define a macro with the "#define" directive. It is similar to a function in C. We can pass one or more arguments to the macro name and perform the actions in the code segment.
The following code defines AREA macro using the #define statement −
Example
#include <stdio.h> #define PI 3.14159 #define AREA(r) (PI*r*r) int main(){ int radius = 5; float area = AREA(radius); printf("Area: %f", area); return 0; }
Output
Area: 78.539749
Macros are generally faster in execution than the functions.
The main() Function
A C program is a collection of one or more functions. There are two types of functions in a C program: library functions and user-defined functions.
There must be at least one user-defined function in a C program, whose name must be main(). The main() function serves as the entry point of the program. When the program is run, the compiler looks for the main() function.
The main() function contains one or more statements. By default, each statement must end with a semicolon. The statement may include variable declarations, decision control or loop constructs or call to a library or another user-defined function.
In C, a function must have a data type. The data type of return value must match with the data type of the function. By default, a function in C is of int type. Hence, if a function doesn’t have a return statement, its type is int, and you may omit it in the function definition, but the compiler issues a warning −
warning: return type defaults to 'int'
Example
A typical example of main() function is as follows −
#include <stdio.h> int main() { /* my first program in C */ printf("Hello, World! \n"); return 0; }
Output
On executing this code, you will get the following output −
Hello, World!
The Global Declaration Section
This section consists of declaration of variables to be used across all the functions in a program. Forward declarations of user-defined functions defined later in the program as well as user-defined data types are also present in the global section.
Example of global variable declaration −
int total = 0; float average = 0.0;
Example of forward declaration of a function −
float area(float height, float width);
Subroutines in a C Program
There may be more than one user-defined functions in a C program. Programming best practices require that the programming logic be broken down to independent and reusable functions in a structured manner.
Depending on the requirements, a C program may have one or more user-defined functions, which may be called from the main() function or any other user-defined function as well.
Comments in a C Program
Apart from the programming elements of a C program such as variables, structures, loops, functions, etc., the code may have a certain text inside "/* .. */" recognized as comments. Such comments are ignored by the compiler.
Inserting comments in the code often proves to be helpful in documenting the program, and in understanding as well as debugging the programming logic and errors.
If the /* symbol doesn’t have a matching */ symbol, the compiler reports an error: "Unterminated comment".
A text between /* and */ is called as C-style comment, and is used to insert multi-line comments.
/* Program to display Hello World Author: Tutorialspoint Built with codeBlocks */
A single line comment starts with a double forward-slash (//) and ends with a new line. It may appear after a valid C statement also.
int age = 20; // variable to store age
However, a valid statement can’t be given in a line that starts with "//". Hence, the following statement is erroneous:
// Variable to store age. int age=20;
Structure of the C Program
The following code shows the different sections in a C program −
/*Headers*/ #include <stdio.h> #include <math.h> /*forward declaration*/ float area_of_square(float); /*main function*/ int main() { /* my first program in C */ float side = 5.50; float area = area_of_square(side); printf ("Side=%5.2f Area=%5.2f", side, area); return 0; } /*subroutine*/ float area_of_square(float side){ float area = pow(side,2); return area; }
Output
On executing this code, you will get the following output −
Side= 5.50 Area=30.25