C Programming
C Programming
'C' Programming language is a Middle level programming language. It is widely used programming language from the day it is created. It is used in developing various operating systems as well as vast range of application.
A program is a set of instructions, given to the computer by user in user friendly format.
Program are written in human understandable format, that format is called as a programming language.
Then this source code will be converted to machine understandable code by the programming compiler.
First C Program:
// single line comment is written by using "//"
/*
Multi-line comment is written in this format
*/
#include<stdio.h> // header file which includes standard input/output functions
int main() // main function is the first function to be called in the program
{
printf("Hello World\n"); // function to display the output
return 0; // return 0 value on successful execution of function statements
}
Compiler used for C program compilation : GCC
Terminal Commands to compile a C program:
>gcc program.c //terminal command to compile the program
>./a.out //terminal command to execute or run the program
// Program to declare the variable using extern keyword
//<-------- extern keyword Use -------->//
#include<stdio.h>
#include<stdbool.h>
//#include<conio.h> //not available for vscode
extern int x;
int main()
{
int age = 30;
char name[] = "Embedded";
float average = 66.78;
char gender = 'M';
bool result = true;
printf("%d\n",x);
printf("%d\n", age);
printf("%s\n", name);
printf("%f\n", average);
printf("%c\n", gender);
printf("%d\n", result);
printf("size of int is = %u\n", sizeof(int));
printf("size of char is=%u\n", sizeof(char));
printf("size of float is=%u\n", sizeof(float));
printf("size of bool is=%u\n", sizeof(bool));
return 0;
// getch(); //only used with conio.h header file to hold value on console
// The getch() is a predefined non-standard function that is defined in conio. h header file.
// It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen
// until the user passes a single value to exit from the console screen.
}
int x=5; //extern variable is declared here in
Compile Process in C:
1. Preprocessing:
- The preprocessor scans the source code and handles preprocessor directives that begin with a '#' symbol, such as '#include', '#define', '#ifdef', etc.
- The '#include' directive is used to insert the contents of header files into the source code.
- Macro expansions occur when the preprocessor replaces macro names with their corresponding definitions.
- Conditional compilation directives, like '#ifdef' and '#ifndef', control the inclusion or exclusion of certain code based on conditions.
2. Compilation:
- The preprocessed code is passed to the compiler.
- The compiler performs lexical analysis, syntax analysis, and semantic analysis of the code.
- Lexical analysis involves breaking the code into tokens (keywords, identifiers, operators, etc.) and removing comments.
- Syntax analysis checks the grammar of the code to ensure it follows the language rules.
- Semantic analysis verifies the correctness of the code in terms of types, scope, and usage of variables and functions.
- If there are no syntax or semantic errors, the compiler generates assembly language or machine code instructions.
3. Assembly:
- If the compiler generates assembly code, the assembly step converts the assembly code into machine code.
- The assembly code is transformed into binary instructions that the processor can understand.
- The output of this step is an object file, which contains the machine code instructions and some metadata.
4. Linking:
- If your program consists of multiple source files or uses external libraries, the linker combines the object files generated in the previous step.
- The linker resolves references to functions and variables across different object files by matching symbols.
- It also resolves references to external libraries by linking them to the program.
- The linker performs memory layout, determining the location of functions and variables in memory.
- It generates the final executable file, which can be run by the operating system.
5. Loading and Execution:
- The operating system loads the executable file into memory.
- It allocates memory for global and static variables.
- Registers and stacks are set up, and any necessary system resources are allocated.
- The program's entry point (usually the 'main' function in C) is located.
- Execution begins, and the processor executes the instructions in the loaded executable file.
During the compilation process, various optimizations may be performed by the compiler and linker to improve the efficiency and performance of the resulting executable code. These optimizations can include dead code elimination, constant folding, loop unrolling, and many more.
- The preprocessor scans the source code and handles preprocessor directives that begin with a '#' symbol, such as '#include', '#define', '#ifdef', etc.
- The '#include' directive is used to insert the contents of header files into the source code.
- Macro expansions occur when the preprocessor replaces macro names with their corresponding definitions.
- Conditional compilation directives, like '#ifdef' and '#ifndef', control the inclusion or exclusion of certain code based on conditions.
2. Compilation:
- The preprocessed code is passed to the compiler.
- The compiler performs lexical analysis, syntax analysis, and semantic analysis of the code.
- Lexical analysis involves breaking the code into tokens (keywords, identifiers, operators, etc.) and removing comments.
- Syntax analysis checks the grammar of the code to ensure it follows the language rules.
- Semantic analysis verifies the correctness of the code in terms of types, scope, and usage of variables and functions.
- If there are no syntax or semantic errors, the compiler generates assembly language or machine code instructions.
3. Assembly:
- If the compiler generates assembly code, the assembly step converts the assembly code into machine code.
- The assembly code is transformed into binary instructions that the processor can understand.
- The output of this step is an object file, which contains the machine code instructions and some metadata.
4. Linking:
- If your program consists of multiple source files or uses external libraries, the linker combines the object files generated in the previous step.
- The linker resolves references to functions and variables across different object files by matching symbols.
- It also resolves references to external libraries by linking them to the program.
- The linker performs memory layout, determining the location of functions and variables in memory.
- It generates the final executable file, which can be run by the operating system.
5. Loading and Execution:
- The operating system loads the executable file into memory.
- It allocates memory for global and static variables.
- Registers and stacks are set up, and any necessary system resources are allocated.
- The program's entry point (usually the 'main' function in C) is located.
- Execution begins, and the processor executes the instructions in the loaded executable file.
During the compilation process, various optimizations may be performed by the compiler and linker to improve the efficiency and performance of the resulting executable code. These optimizations can include dead code elimination, constant folding, loop unrolling, and many more.
In C, a memory leak refers to a situation where dynamically allocated memory is not properly deallocated or freed when it is no longer needed. As a result, the allocated memory remains in use even though it cannot be accessed or used by the program anymore. Memory leaks can lead to a gradual depletion of available memory, which can eventually cause performance issues or program crashes.
There are several causes of memory leaks in C programs:
1. Forgetting to deallocate memory: If you allocate memory dynamically using functions like malloc(), calloc(), or realloc(), it is important to free that memory using the free() function when it is no longer needed. Forgetting to deallocate memory can result in a memory leak.
2. Incorrect deallocation: It is essential to ensure that memory is deallocated properly and at the right time. If you deallocate memory too early or too late, it can cause memory leaks. For example, deallocating memory inside a loop without considering all iterations can lead to memory leaks.
3. Overwriting memory pointers: If you overwrite a pointer to dynamically allocated memory without freeing it first, you lose the reference to that memory and cannot deallocate it. This can happen, for instance, when assigning a new memory address to a pointer without freeing the previous memory.
4. Global variables: If you allocate memory to a global variable and forget to deallocate it before the program terminates, the memory will not be released, resulting in a memory leak. Global variables persist throughout the entire program's execution.
5. Circular references: In situations where data structures or objects reference each other in a circular manner, it can prevent memory from being deallocated even if it is no longer in use. This is commonly found in situations where garbage collection is not available, as in C.
6. Exception handling: If an exception occurs and memory allocated in the corresponding try block is not freed in the catch block, it can lead to memory leaks.
To avoid memory leaks, it is important to follow good memory management practices. Always free dynamically allocated memory when it is no longer needed and ensure that deallocation is performed correctly and at the appropriate time. Additionally, using tools like memory profilers or leak detection tools can help identify and locate memory leaks in C programs.
Comments
Post a Comment