0% found this document useful (0 votes)
1 views

Week 1 Introduction to C Programs

The document explains the 'Hello, World!' program in C, detailing its structure and purpose as a beginner's introduction to programming. It outlines the compilation process of a C program, which includes preprocessing, compilation, assembly, linking, loading, and execution. Each stage is described with examples to illustrate how source code is transformed into an executable program.

Uploaded by

joshuamaingi106
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Week 1 Introduction to C Programs

The document explains the 'Hello, World!' program in C, detailing its structure and purpose as a beginner's introduction to programming. It outlines the compilation process of a C program, which includes preprocessing, compilation, assembly, linking, loading, and execution. Each stage is described with examples to illustrate how source code is transformed into an executable program.

Uploaded by

joshuamaingi106
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Hello World Program

The classic "Hello, World!" program is a simple way to start learning any programming
language. In C, it looks like this:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
#include <stdio.h>: This line includes the standard input/output library, which
provides functions like printf and scanf.
int main(): Every C program starts execution from the main function.
printf("Hello, World!\n");: This line prints the string to the console.
return 0;: Indicates that the program executed successfully.

COMPILATION PROCESS OF A C PROGRAM


Preprocessing
The first stage involves a preprocessor, which is a program that processes the source
code before actual compilation. The preprocessor directives start with a # symbol and
include commands such as #include for file inclusion, #define for macro definitions,
and more.
The preprocessor replaces these directives with actual code, and it generates an
intermediate code known as the "preprocessed code."
#include <stdio.h>
#define PI 3.14
int main() {
printf("Value of PI: %f\n", PI);
return 0;
}
After preprocessing, the code might look like:
/* Contents of stdio.h included here */
/* Contents of stdlib.h included here */
/* Other preprocessor replacements */
int main() {
printf("Value of PI: %f\n", 3.14);
return 0;
}

Compilation
The preprocessed code is then passed to the compiler. The compiler translates the
preprocessed code into assembly code or an intermediate code specific to the target
platform.
This stage involves syntax checking, semantic analysis, and the generation of
intermediate code.
Assembly
The compiler generates assembly code or machine code from the intermediate code.
Assembly code is specific to the target architecture and is human-readable.

Linking
The linker combines the compiled code with other necessary code (such as libraries) to
create the final executable file. It resolves references to external functions and variables,
ensuring that the program can access the required resources.
If your program uses external libraries, the linker includes the compiled code from
those libraries in the final executable.

Loading
The loader loads the final executable file into memory for execution. It may also
perform address binding, resolving addresses of variables and functions.
Execution
The operating system begins the execution of the program. The CPU reads and executes
the instructions in the program's memory, producing the desired output.

You might also like