Introduction to C
Sun, 03 Nov 2024
"No matter which field of work you want to go in, it is of great importance to learn at least one programming language."
― Ram Ray
Programming is the process of creating instructions for a computer to follow, to perform specific tasks/goal. Let us understand via a simple real-life example:
(a)
(a)
can be considered as precise for that language.Feature | High-Level Language | Low-Level Language |
---|---|---|
Programmer Friendliness | It is programmer-friendly. | It is machine-friendly. |
Memory Efficiency | Less memory efficient. | High memory efficient. |
Ease of Understanding | Easy to understand. | Tough to understand. |
Debugging | Debugging is easy. | Debugging is complex comparatively. |
Maintenance | Simple to maintain. | Complex to maintain comparatively. |
Portability | Portable. | Non-portable. |
Translation Requirement | Needs a compiler or interpreter. | Needs an assembler. |
Usage | Widely used for programming. | Not commonly used in modern programming. |
The structure of a C program is divided into six main sections, each of which serves a specific purpose. This organized approach makes programs easier to read, modify, document, and debug.
The documentation section provides information about the program, such as its description, author name, and creation date. This information is placed in the form of comments at the beginning of the program. Documentation is ignored by the compiler and serves as a guide for anyone reading the code.
// Program: Sum of two numbers
// Author: Your Name
// Date: YYYY-MM-DD
/*
Program: Sum of two numbers
Author: Your Name
Date: YYYY-MM-DD
Description: This program calculates the sum of two numbers.
*/
Header files contain declarations of standard library functions, macros, and types. Including them in a program enables access to predefined functionality, such as input/output operations, mathematical functions, and memory allocation.
Example:
#include <stdio.h> // Standard I/O library for functions like printf() and scanf()
#include <stdlib.h> // Standard library for memory allocation and process control
#include <math.h> // Math library for functions like sqrt(), pow(), etc.
Preprocessors are programs that process source code before compilation. There are multiple steps involved in writing and executing a program. Preprocessor directives start with the #
symbol. The #define
preprocessor is used to create a constant throughout the program. Whenever this name is encountered by the compiler, it is replaced by the actual piece of defined code.
Example:
#define PI 3.14
The global declaration section contains global variables, function declarations, and static variables. Variables and functions declared in this scope can be used anywhere in the program.
Example:
int num = 18;
Every C program must have a main
function. The main()
function of the program is written in this section. Operations such as declaration and execution are performed inside the curly braces of the main program. The return type of the main()
function can be either int
or void
. The void main()
tells the compiler that the program will not return any value, while int main()
indicates that the program will return an integer value.
Example:
void main()
User-defined functions are called in this section of the program. The control of the program is shifted to the called function whenever it is invoked from the main()
or outside the main()
function. These functions are specified according to the requirements of the programmer.
Example:
int sum(int x, int y)
{
return x + y;
}
Example: Below is a C program to find the sum of two numbers:
// Documentation
/**
* file: sum.c
* author: you
* description: program to find sum.
*/
// Link
#include <stdio.h>
// Definition
#define X 20
// Global Declaration
int sum(int y);
// Main() Function
int main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}
// Subprogram
int sum(int y)
{
return y + X;
}
Output:
75
Explanation of the Above Program Below is the explanation of the program, describing its meaning and use.
Sections | Description |
---|---|
/** * file: sum.c * author: you * description: program to find sum. */ | This is the comment section and part of the documentation for the code. |
#include <stdio.h> | This header file is used for standard input-output. It belongs to the preprocessor section. |
#define X 20 | This is the definition section. It allows the use of the constant X in the code. |
int sum(int y); | This is the global declaration section, which includes the function declaration that can be used anywhere in the program. |
int main() | The main() function is the first function executed in a C program. |
{…} | These curly braces mark the beginning and end of the main function. |
printf("Sum: %d", sum(y)); | The printf() function is used to print the sum on the screen. |
return 0; | Since int is used as the return type, we must return 0 , indicating that the program executed successfully without errors. |
int sum(int y) { return y + X; } | This is the subprogram section, which includes the user-defined function that is called in the main() function. |
#include <stdio.h> // Includes standard I/O library
int main() { // Main function starts here
printf("Hello, World!\n"); // Print statement
return 0; // Program ends, returning 0 to the OS
}
#include <stdio.h>
: Includes the standard input-output library for using printf
.int main()
: Declares the main function where execution begins.printf("Hello, World!\n");
: Prints "Hello, World!" followed by a newline character.return 0;
: Indicates successful completion of the program.This is the part where our code written in C, is converted into computer understandable format. In short, it does the translation from C code to binary code. The steps are:
Writing the Code
.c
extension.bit2byte.c
with the following content(No need to understand now):#include<stdio.h>
#define PI 3.14159
int main(){
float angle;
// Taking the angle input from user in degree
scanf("%f", &angle);
// Converting into radian
float rad = (PI / 180) * angle;
printf("Angle in radian is %f\n", rad);
return 0;
}
Preprocessing
//comment
and /*comment*/
are removed.// Taking the angle input from user in degree
#define
are replaced with it values in the program.float rad = (PI / 180) * angle;
float rad = (3.14159 / 180) * angle;
#include<stdio.h>
stdio.h
is added into the bit2byte.c
file.void printf(const char *format, ...);
int scanf(const char *format, ...);
#ifdef
, #endif
are part are processed here based on definition.#define DEBUG
#ifdef DEBUG
printf("Debugging information.\n");
#endif
DEBUG
is defined).printf("Debugging information.\n");
bit2byte.i
)# 1 "bit2byte.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "bit2byte.c"
... ... ...
int main(){
float angle;
scanf("%f", &angle);
float rad = (3.14159 / 180) * angle;
printf("Angle in radian is %f\n", rad);
return 0;
}
.c
file:gcc -E bit2byte.c -o bit2byte.i
Compilation
bit2byte.s
) .file "bit2byte.c"
.text
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
... ... ...
.LC1:
.long -1525816720
.long 1066524485
.ident "GCC: (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0"
.def scanf; .scl 2; .type 32; .endef
.def printf; .scl 2; .type 32; .endef
gcc -S bit2byte.i -o bit2byte.s
Assembly
bit2byte.o
)d† ~ .text p , $ P`.data @ PÀ.bss € PÀ.rdata 0 œ @ P@.xdata Ì @ 0@.pdata Ø ` @ 0@/4 @ ä @ P@UH‰åHƒì0è HEøH‰ÂH
è óEøóZÈò òYÁòZÐóUüóZEüfH~ÀH‰ÂfHnÊH‰ÂH
è ¸ HƒÄ0]Ã%f Angle in radian is %f
pâ
¥Eß‘? RP j GCC: (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
-
V
[ .file þÿ gbit2byte.c main .text j .data .bss .rdata ( .xdata .pdata ? __main scanf printf .rdata$zzz .rdata$zzz
gcc -c bit2byte.s -o bit2byte.o
Linking
output.out
on Unix/Linux or output.exe
on Windows.output.exe
)MZ ÿÿ ¸ @ € º ´ Í!¸LÍ!This program cannot be run in DOS mode.
$ PE d† J!g h œ ð '
8
gcc bit2byte.o -o output
Loading
output.exe
file, the loader is invoked.output.exe
Execution
main()
function, and interacts with the system until it completes or is terminated.Termination
main()
function is executed at the first.This is a comment 1.
This is a comment 2.