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

Programming in C Basics Unit-1

A computer system consists of hardware and software components that work together. The main software is the operating system, which manages other programs. A computer system has input, storage, processing, and output components. The central processing unit controls the system and includes an arithmetic logic unit for processing and a control unit for managing operations.

Uploaded by

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

Programming in C Basics Unit-1

A computer system consists of hardware and software components that work together. The main software is the operating system, which manages other programs. A computer system has input, storage, processing, and output components. The central processing unit controls the system and includes an arithmetic logic unit for processing and a control unit for managing operations.

Uploaded by

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

What is a computer system?

A computer system consists of hardware components that have been carefully chosen so that
they work well together and software components or programs that run in the computer. The
main software component is itself an operating system (OS) that manages and provides services
to other programs that can be run in the computer.

In its most basic form, a computer system is a programmable electronic device that can accept
input; store data; and retrieve, process and output information.

Computer systems consist of three components as shown in below image: Central Processing
Unit, Input devices and Output devices. Input devices provide data input to processor, which
processes data and generates useful information that’s displayed to the user through output
devices. This is stored in computer’s memory.

Central Processing Unit


The Central Processing Unit (CPU) is called "the brain of computer" as it controls operation of
all parts of computer. It consists of two components: Arithmetic Logic Unit (ALU), and Control
Unit.
Arithmetic Logic Unit (ALU):
Data entered into computer is sent to RAM, from where it is then sent to ALU, where rest of data
processing takes place. All types of processing, such as comparisons, decision-making and
processing of non-numeric information takes place here and once again data is moved to RAM.
Control Unit
As name indicates, this part of CPU extracts instructions, performs execution, maintains and
directs operations of entire system.Functions of Control Unit
Control unit performs following functions −

 It controls all activities of computer


 Supervises flow of data within CPU
 Directs flow of data within CPU
 Transfers data to Arithmetic and Logic Unit
 Transfers results to memory
 Fetches results from memory to output devices

Memory Unit
This is unit in which data and instructions given to computer as well as results given by
computer are stored. Unit of memory is "Byte".
1 Byte = 8 Bits

Computer Language:
A computer language is a formal language used to communicate with a computer. Types of
computer languages include:

 Construction language – all forms of communication by which a human can specify an


executable problem solution to a computer
o Command language – a language used to control the tasks of the computer itself, such as
starting programs
o Configuration language – a language used to write configuration files
o Programming language – a formal language designed to communicate instructions to a
machine, particularly a computer
o Query language – a language used to make queries in databases and information systems
o Transformation language – designed to transform some input text in a certain formal
language into a modified output text that meets some specific goal
 Data exchange language – a language that is domain-independent and can be used for data
from any kind of discipline; examples: JSON, XML
 Markup language – a grammar for annotating a document in a way that is syntactically
distinguishable from the text, such as HTML
 Modeling language – an artificial language used to express information or knowledge, often
for use in computer system design
o Architecture description language – used as a language (or a conceptual model) to
describe and represent system architectures
o Hardware description language – used to model integrated circuits
 Page description language – describes the appearance of a printed page in a higher level than
an actual output bitmap
 Simulation language – a language used to describe simulations
 Specification language – a language used to describe what a system should do
 Style sheet language – a computer language that expresses the presentation of structured
documents, such as CSS

Creating and Running programs

The programs created using programming languages like C, C++, Java, etc., are written using a
high-level language like English. But, the computer cannot understand the high-level language. It
can understand only low-level language. So, the program written in the high-level language
needs to be converted into the low-level language to make it understandable for the computer.
This conversion is performed using either Interpreter or Compiler.To create and execute C
programs in the Windows Operating System, we need to install Turbo C software. We use the
following steps to create and execute C programs in Windows OS.
Step 1: Creating a Source Code

Source code is a file with C programming instructions in a high-level language. To create source
code, we use any text editor to write the program instructions. The instructions written in the
source code must follow the C programming language rules. The following steps are used to
create a source code file in Windows OS…

 Click on the Start button


 Select Run
 Type cmd and press Enter
 Type cd c:\TC\bin in the command prompt and press Enter
 Type TC press Enter
 Click on File -> New in C Editor window
 Type the program
 Save it as FileName.c (Use shortcut key F2 to save)

Step 2: Compile Source Code (Alt + F9)


The compilation is the process of converting high-level language instructions into low-level
language instructions. We use the shortcut key Alt + F9 to compile a C program in Turbo C.
The compilation is the process of converting high-level language instructions into low-level
language instructions.

The compiler first checks for the Errors. If there are any Errors then compiler returns List of
Errors, if there are no errors then the source code is converted into object code and stores it as a
file with .obj extension. Then the object code is given to the Linker. The Linker combines both
the object code and specified header file code and generates an Executable file with
a .exe extension.

Step 3: Executing / Running Executable File (Ctrl + F9)


After completing compilation successfully, an executable file is created with a .exe extension.
The processor can understand this .exe file content so that it can perform the task specified in the
sourcefile. We use a shortcut key Ctrl + F9 to run a C program. Whenever we press Ctrl + F9,
the .exe file is submitted to the CPU. On receiving .exe file, CPU performs the task according to
the instruction written in the file. The result generated from the execution is placed in a window
called User Screen.

Step 4: Check Result (Alt + F5)


After running the program, the result is placed into User Screen. Just we need to open the User
Screen to check the result of the program execution. We use the shortcut key Alt + F5 to open
the User Screen and check the result.
Execution Process of a C Program
When we execute a C program it undergoes with the following process.

Process:

 Type the program in C editor and save with .c extension (Press F2 to save).
 Compile the program.
 If there are errors, correct the errors and recompile the program.
 If there are no errors, then run the program.

Preprocessor:

The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your
program before it is compiled. These transformations can be the inclusion of header files, macro
expansions, etc.

All preprocessing directives begin with a # symbol. For example,

#define PI 3.14

Including Header Files: #include

The #include preprocessor is used to include header files to C programs. For example,
#include <stdio.h>

Here, stdio.h is a header file. The #include preprocessor directive replaces the above line with the
contents of stdio.h header file. That's the reason why you need to use #include <stdio.h> before
you can use functions like scanf() and printf().
You can also create your own header file containing function declaration and include it in your
program using this preprocessor directive.

Macros using #define

A macro is a fragment of code that is given a name. You can define a macro in C using
the #define preprocessor directive. Here's an example.

#define c 299792458 // speed of light

Here, when we use c in our program, it is replaced with 299792458.

What is a compilation?

The compilation is a process of converting the source code into object code. It is done with the
help of the compiler. The compiler checks the source code for the syntactical or structural errors,
and if the source code is error-free, then it generates the object code.

The c compilation process converts the source code taken as input into the object code or
machine code. The compilation process can be divided into four steps, i.e., Pre-processing,
Compiling, Assembling, and Linking.

The preprocessor takes the source code as an input, and it removes all the comments from the
source code. The preprocessor takes the preprocessor directive and interprets it. For example,
if <stdio.h>, the directive is available in the program, then the preprocessor interprets the
directive and replace this directive with the content of the 'stdio.h' file.
The following are the phases through which our program passes before being transformed into an
executable form:

o Preprocessor
o Compiler
o Assembler
o Linker

Preprocessor

The source code is the code which is written in a text editor and the source code file is given an
extension ".c". This source code is first passed to the preprocessor, and then the preprocessor
expands this code. After expanding the code, the expanded code is passed to the compiler.

Compiler

The code which is expanded by the preprocessor is passed to the compiler. The compiler
converts this code into assembly code. Or we can say that the C compiler converts the pre-
processed code into assembly code.

Assembler

The assembly code is converted into object code by using an assembler. The name of the object
file generated by the assembler is the same as the source file. The extension of the object file in
DOS is '.obj,' and in UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then the
name of the object file would be 'hello.obj'.

Linker

Mainly, all the programs written in C use library functions. These library functions are pre-
compiled, and the object code of these library files is stored with '.lib' (or '.a') extension.
The main working of the linker is to combine the object code of library files with the object
code of our program. Sometimes the situation arises when our program refers to the functions
defined in other files; then linker plays a very important role in this. It links the object code of
these files to our program.

Therefore, we conclude that the job of the linker is to link the object code of our program with
the object code of the library files and other files.

The output of the linker is the executable file. The name of the executable file is the same as the
source file but differs only in their extensions. In DOS, the extension of the executable file is
'.exe', and in UNIX, the executable file can be named as 'a.out'. For example, if we are using
printf() function in a program, then the linker adds its associated code in an output file.

You might also like