Introduction to Programming - (C Language) - Unit : 1 – COMPILATION AND EXECUTION - FIRST C PROGRAM
Compilation and
execution are fundamental processes in computer programming and software
development. They are crucial steps in converting human-readable source code
into machine-executable instructions, allowing computers to perform tasks as
directed by the programmer. This introduction will provide an overview of these
two processes and their significance in the world of programming.
1.
Compilation:
Compilation is
the process of translating high-level programming code, written in languages
like C, C++, Java, or C#, into lower-level code known as machine code or
bytecode. This transformation involves several steps:
Source
Code: Programmers write human-readable source code using a high-level
programming language. This code is easy to understand and write but cannot be
directly executed by a computer.
Compiler: A compiler is a
software tool that takes the source code as input and converts it into
machine-readable code. The compiler performs syntax checking, optimization, and
code generation to produce an executable file or bytecode. Different
programming languages have different compilers.
Executable
File or Bytecode: The output of the compilation process is often an
executable file (e.g., .exe for Windows) or bytecode (e.g., .class for Java).
These files contain the instructions that the computer's processor can
understand and execute.
2.
Execution:
Execution is the
process of running the compiled or interpreted program to achieve the intended
task. Depending on the programming language, there are two primary methods of
execution:
Compiled
Languages: In languages like C and C++, the compiled code is directly executed by
the computer's processor. When you run the program, the operating system loads
the executable file into memory, and the CPU processes the instructions,
producing the desired output.
Interpreted
Languages: Languages like Python and JavaScript are interpreted. In this case, the
source code is not compiled into machine code beforehand. Instead, an
interpreter reads and executes the code line by line at runtime. This can make
development and debugging more straightforward but may result in slower
execution.
Efficiency: Compilation
optimizes code for faster execution, as it can make various performance
enhancements.
Portability: Compiled code
can be distributed without revealing the source code, enhancing security.
Interpreted languages often require distributing the source code or bytecode.
Cross-Platform
Compatibility: Compiled code can be platform-specific, while interpreted languages are
often more platform-independent, making them suitable for web applications and
cross-platform development.
Error
Checking: Compilation performs extensive error checking at the early stages,
reducing the likelihood of runtime errors.
PROCESS OF WRITING PROGRAM
1. Problem analysis: We should analyze the
problem to be solved initially. In this process we clearly understand the exact
problem, finding best solution among various solutions, using the suitable
concepts for solving problem.
2. Writing of code: Write the program code
3. Compiling: Now compile the code. In
this process we will get the errors and correct them and then convert into
machine code(binary code).
4. Execution/Running: Then execute the machine
code.
5. Output: Now check the output.
Components
needed to write a program?
To write a program we need the
following requirements.
è
Editor
è
Preprocessor
è
Debugger
è
Compiler
è
Linker
è
Standard object Library
è
Header files
è
Help Document.
Turbo C++ IDE (Integrated Development
Environment) is an integration of
several tools.
You can download Turbo C++ (which is useful
for C and C++) from internet from many sites.
Structure of a ‘c’ program :
Header files
preprocessor commands
global variable declarations
main()
{
local variable declarations ;
statements ;
}
function(arguments)
{
local declarations ;
statements ;
}
·
The ‘C’ program has a free
formatted structure.
·
Every statement must be
terminated with a semicolon ;
·
A program is a collection of
functions. There may be a lot of functions but at least one function must be
there that is main(), where the execution starts.
·
C has case sensitivity. All the
keywords are defined in lower case.
·
So better to write entire
program in lower case.
Preprocessor
commands:
The commands which start with a hash(#) symbol are
called Preprocessor commands.
Ex :
# include <stdio.h>
# include <conio.h>
# define PI
3.14159
Global
declarations :
To use any variable it must
be declared with its data type before the first executable statement. The
variables which declared in a block are available in that block only.
To use the variable in the entire program with same
effect it must be declared as global.
printf() scanf() in C
The printf() and scanf()
functions are used for input and output in C language.
Both functions are
inbuilt library functions, defined in stdio.h (header file).
printf() function
The printf() function is used for output. It prints the
given statement to the console.
The syntax of printf()
function is given below:
printf("format string",argument_list);
The format string can be %d (integer), %c (character),
%s (string), %f (float) etc.
scanf() function
The scanf() function is used for input. It reads the input
data from the console.
scanf("format string",argument_list);
Sample
Program
/*Sample program in C
language*/
#include
<stdio.h>
#include
<conio.h>
void main()
{
clrscr();
printf("Hello
C Language");
getch();
}
Program
explanation:
Ø /*..*/ is comment
line. It is used to give additional information about the program.
Ø #include
<stdio.h> includes the standard input output library functions. The
printf() function is defined in stdio.h .
Ø #include
<conio.h> includes the console input output library functions. The
getch() function is defined in conio.h file.
Ø void main() The
main() function is the entry point of every program in c language. The void
keyword specifies that it returns no value.
Ø printf() The printf()
function is used to print data on the console.
Ø getch() The
getch() function asks for a single character. Until you press any key, it
blocks the screen.
// Sum of two numbers
#include <stdio.h>
int main()
{
int num1 = 10; // Initialize num1 with the value 10
int num2 = 20; // Initialize num2 with the value 20
int sum = num1 + num2; // Calculate the sum of num1 and num2
printf("Sum: %d\n", sum); // Display the result (sum)
return 0;
}
In this program:
int is a data type and num1,num2,sum
are the variables
int num1 = 10; initializes num1
with the value 10.
int num2 = 20; initializes num2
with the value 20.
int sum = num1 + num2; calculates
the sum of num1 and num2 and stores it in the sum variable.
printf("Sum: %d\n",
sum); prints the result (the sum) as an integer.
return 0; indicates successful
execution of the program to the operating system.
Comments
Post a Comment