C is a general-purpose programming language that was developed in the early 1970s by Dennis Ritchie at Bell Labs. It is widely used for system programming, developing operating systems, and embedded systems due to its efficiency and control over system resources.
C is a procedural programming language, meaning it follows a step-by-step approach to solve problems, where the program is structured as a sequence of procedures or functions.
C is case-sensitive language, meaning that uppercase and lowercase letters are treated as distinct characters.
C follows top-down approach, where the program is broken down into smaller functions.
C programs are typically compiled into machine code, which makes them very fast and efficient.
Structure of C Program
Files Inclusion Section
Definition Section
Global Declaration Section
int main() {
// Local Declaration Section
// Executable Statements
return 0;
}
Sub-Functions Section
This section contains comments and documentation about the program. For comments, use // for single-line comments and /* */ for multi-line comments. Comments are not executed by compiler.
This section includes header files that provide necessary declarations and functions for the program. For example, #include <stdio.h> is used to include the standard input-output library.
Here, # is called the preprocessor.
Use #include, also import existing C-programs. For example, create following files in a same working directory:
variables.cint num2=10;
add.c
#include "variables.c"
int main(){
printf("%d", num1+num2);
return 0;
}
multiply.c
#include "variables.c"
int main(){
printf("%d", num1*num2);
return 0;
}
This section is used to define constants and macros using #define directive.
Syntax:Where, MACRO_NAME or CONSTANT_NAME is the name of the macro or constant being defined, and expression or value is the value or expression to be substituted than only you can evaluate.
For Example:#define SQUARE(x) x*x
int main(){
printf("%d", SQUARE(2+3));
return 0;
}
Explanation: In the example, the macro SQUARE(x) expands to x*x. When SQUARE(2+3), x is 2+3, is called, it becomes 2+3*2+3, which evaluates to 11.
This section is used to declare global variables and function prototypes that can be accessed throughout the program.
This is the entry point of the program where execution begins. The main function typically returns an integer value to indicate the program's success or failure.
- Program executes successfully, return 0;
- Program encounters an error, return non-zero value;
We can use the following arguments for main, if needed:
- int argc: argc - argument count. It represents the number of command-line arguments passed to the program.
- char *argv[]: argv - argument vector. It is an array of strings that holds the actual command-line arguments.
- char *envp[]: envp - environment pointer. It is an array of strings that holds the environment variables.
This section is used to declare local variables that are only accessible within the main function or other functions.
This section contains the main logic of the program, where the actual operations and computations are performed.
This section contains additional functions that are defined outside the main function to perform specific tasks.