0% found this document useful (0 votes)
20 views30 pages

2.4 C Programing Language

About C programming language

Uploaded by

dhruvkolare04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
20 views30 pages

2.4 C Programing Language

About C programming language

Uploaded by

dhruvkolare04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 30

Programming language C

Devyani Soni
Department of Computer science and technology
C language

 C is a general-purpose programming language created by Dennis


Ritchie at the Bell Laboratories in 1972.

 It is a very popular language, despite being old. The main reason for
its popularity is because it is a fundamental language in the field of
computer science.

 C is strongly associated with UNIX, as it was developed to write the


UNIX operating system.
C Programming Language Working procedure

Execution of the C program involves 5 steps. These are:

1. Creating the Program


2. Preprocessing
3. Compiling the Program
4. Linking the Program with functions from the C library
5. Executing the Program
1. Creating the Program: Firstly, we need to create a C program. For that,
we will open a text editor and write our C program into it. Then save the
file with .c extension.
For example: hello.c The program written into the file is known as the
source code, and it serves the original form of the C program.

2. Preprocessing: Preprocessing is the stage where source code is passed


for the first time. This stage consists of the following steps:

Expansion of Macros and Comment Removal


Expansion of the included files
Conditional compilation The preprocessed output of hello.c is stored in
the file hello.i.
3. Compiling the Program: Once our source code is preprocessed in the file
hello.i. Now our file is ready for compilation, which after compilation produces
an intermediate compiled output file hello.s, which is in assembly level
instructions.

During the compilation process, the compiler checks for all the compilation
errors. If the online C compiler gives no error, then the hello.s is taken as input
and turned into hello.o by the assembler in the next phase.

This file contains machine-level instructions. At this phase, only existing code
is converted into machine language, and the function calls are not resolved.
Since the object file is not executable, the process is transferred to the linker,
which ultimately produces a .exe file.
4. Linking the Program with functions from the C library: This is the final
phase, in which all of the function calls are linked to their definitions. Linker
knows where all these functions are implemented. The linker performs
additional work and adds more code to our programe that is required when it
starts and stops. Setting up the environment, for example, requires a code, as
does sending command-line inputs. The linker connects the object code of our
programme file to the C library functions, resulting in an.exe file, hello.exe,
which is an executable file, will be created here.

5. Executing the Program: The execution of a program is a very simple task.


After giving the command to execute a particular program. The loader will
load the executable object code into the RAM and execute the instructions.
C program structure

 The basic structure of a C program is divided into 6 parts which makes it easy
to read, modify, document, and understand in a particular format.

 Sections of the C Program


There are 6 basic sections responsible for the proper execution of a program.
Sections are mentioned below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs
1. Documentation
 This section consists of the description of the program, the name of the
program, and the creation date and time of the program. It is specified at the
start of the program in the form of comments. Documentation can be
represented as:

// description, name of the program, programmer name, date, time etc.


or
/*
description, name of the program, programmer name, date, time etc.
*/

 Anything written as comments will be treated as documentation of the program


and this will not interfere with the given code. Basically, it gives an overview to
the reader of the program.
2. Preprocessor Section

 All the header files of the program will be declared in the preprocessor
section of the program. Header files help us to access other’s improved code
into our code. A copy of these multiple files is inserted into our program
before the process of compilation.

Example:

#include<stdio.h>
#include<math.h>
3. Definition

 Preprocessors are the programs that process our source code before the
process of compilation. There are multiple steps which are involved in the
writing and execution of the 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 long long ll


4. Global Declaration

 The global declaration section contains global variables, function declaration,


and static variables. Variables and functions which are declared in this scope
can be used anywhere in the program.

 Example:

int num = 18;


5. Main() Function

 Every C program must have a main function. The main() function of the
program is written in this section. Operations like declaration and execution
are performed inside the curly braces of the main program. The return type
of the main() function can be int as well as void too. void() main tells the
compiler that the program will not return any value. The int main() tells the
compiler that the program will return an integer value.

Example:

void main()
or
int main()
6. Sub Programs
 User-defined functions are called in this section of the program. The control
of the program is shifted to the called function whenever they are called from
the main or outside the main() function. These are specified as per the
requirements of the programmer.

 Example:

int sum(int x, int y)


{
return x+y;
}
Features of C language

1. Simplicity: C is designed to be simple and straightforward. It has a clear and


concise syntax, making it easy to read and write code.

2. Portability: C programs can run on different types of computers with minimal


changes. This means that once you write a C program, it can work on various
machines without much hassle.

3. Modularity: C allows you to break down your program into smaller,


manageable parts called functions. These functions can be reused in different
parts of your program, making it easier to organize and maintain your code.
4. Efficiency: C is known for its efficiency in terms of both execution speed and
memory usage. It allows you to write programs that run quickly and use
system resources effectively.

5. Flexibility: C provides a wide range of operators, data types, and control


structures, giving you the flexibility to express your ideas and solve problems
in different ways.

6. Low-Level Features: C gives you direct control over the computer's


hardware, allowing you to manipulate memory and perform low-level
operations when needed.

7. Extensibility: C can be easily extended through libraries and other languages


like assembly language. This allows you to add new features to your programs
or optimize performance as needed.
History of C
 The root of all modern languages is ALGOL (Algorithmic Language). ALGOL was
the first computer programming language to use a block structure, and it was
introduced in 1960.

 In 1967, Martin Richards developed a language called BCPL (Basic Combined


Programming Language). BCPL was derived from ALGOL. In 1970, Ken
Thompson created a language using BCPL called B.

 Both BCPL and B programming languages were typeless. After that, C was
developed using BCPL and B by Dennis Ritchie at the Bell lab in 1972.

 So, in terms of history of C language, it was used in mainly academic


environments, but at long last with the release of many C compilers for
commercial use and the increasing popularity of UNIX, it began to gain
extensive support among professionals.
Advantage of C language
 Easy: The main reason for choosing the C language is code portability; C language
is highly portable as you can write code in one system and use the code in another
system. the code compilation is faster and more efficient. Anyone can easily grasp
concepts of C language with the help of some online tutorials. Everyone can easily
understand the code; the individual requires no prior or technical knowledge to
understand this language. C language has the one of the simplest syntax in
comparison to another programming language.

 Libraries for More Practical Programming: For more Practical Programming, C


provides access to more and more libraries for better problem-solving abilities. C
Language gives lots of built-in functions consisting of system-provided and user-
defined functions. The developers optimize the libraries daily to make the
programming in c better and optimize for the user. Built-in libraries make the
work for the developer easy, as when the developer develops a function, the
function is used repeatedly. Again then, they chooses to make a library of that
function. So, they can use that library as often as possible, and in any system they
wishes.
 So Easy to Write in The Compiler: Another reason C is so popular as an
efficient language among programmers is that it allows them to create their
software without worrying about syntax errors. If you're unfamiliar with
coding, using the structured language, C will help you develop better skills.
You'll create more efficient and effective solutions with C in compare to
another programming languages.

 Faster Running Speed: Any developer who wants to execute any program or
application quickly relies on the C language for the fast execution cycle. As C
uses fewer instructions, it executes faster than any other programming
language, such as Java, Ruby, PHP, etc.
 Compile Time Speed Accuracy: The C compiler is very efficient; it quickly
produces your code into machine-level code. Lakhs of lines of code can be
compiled at one instance of a time and put in front of a developer in a couple
of seconds. The C compiler makes the code more efficient for faster and
optimized execution. as the compile time is high in the c language, the error
detections are also at high-speed rates.

 Procedural-Oriented Programming Language: Users create procedures or


functions to execute their programs in C Language. It's very easy to fit a
procedure-oriented language because of the way it works algorithms to
execute the statements the developer writes. Suppose the developer wants to
develop a program using procedure-oriented language. In that case, you need
to frame an algorithm and convert it into a function in the C compiler, as the
compiler provides better optimization and faster exception.
 Easy Debug: Debugging in the c language is easy and practical as the
language does not require any complex statements in the execution, and
the speed of compilations and execution is a bit faster than in any other
language, so debugging is a faster and easier task to manipulate the code
circumstances.

 Open Source: The language C is a free and open-source programming


language that is publicly accessible and available in all coding and
programming platforms. The code of the c language is also publicly
available. One can easily download the compiler of the language and can
start working.
Disadvantage of C Language

 Missing Concept of OOPs Technology: C is a massive and vast language, but it


does not have the concept of OOPs like Inheritance, Polymorphism,
Encapsulation, Abstraction, Data Hiding, overloading, and overriding. Due to
this it is sometimes hectic for the programmer to solve a real-world problem
easily.

 Run-Time Execution Error: In the C programming language, errors or bugs


are not detected after each line of code in the compiler for faster debugging;
the major problem is the compiler shows all the errors after writing the
program, which makes the checking of code corrections very complex in
thousands of lines of code. Thus the developer faces optimization issues in
the language.
 Missing Namespaces: For the declaration of the variable to use on any
other line of code, the concept of a namespace is missing, as we can't
declare the same name to another variable in the C language. The c
language is considered the old-school programming language for this major
problem.

 Poor Memory Management: The C language is very smart in that it


automatically allocates the memory for the Developer's program when it's
needed for the execution of the program. The problem arises when the
programmer wants to allocate the memory dynamically; the programmer
should keep in mind the memory management techniques to get allocate
the memory using the malloc function.

 Poor Exception Handling: All programming languages have the feature of


optimized exception handling; the critical character is missing from the C
language as in the results, and the response to errors is very slow in C
Language.
 No Garbage Collection: Garbage collection is a feature that automatically
reclaims memory from objects no longer needed by an application or library.
It can be used for both automatic and manual garbage collection. Automatic
garbage collection occurs when there is insufficient free space on the heap to
allocate new objects; this may occur because all available physical RAM was
allocated to other processes running on the computer system. Manual garbage
collection explicitly frees unused memory blocks with calls to functions such
as malloc.

 But in C/C++ languages, there's no such feature of garbage collection as the


culture of these languages is to leave storage management to the Developer
or programmer. Hence, it would be technically tedious and harsh on pockets
to implement a precise garbage collector for C / C++.
 No Constructor and Destructor: The major drawback in the C language is the
missing feature of constructor and destructor as the language lacks the
support of OOPS technology and concepts. Both constructor and destructor
are the member functions created when the class is created. This support
helps memory occupation by the objects as soon as the program terminates or
is created. The feature helps many programmers to perform programming
tasks with great ease.
Application of C

C programming has a wide range of applications across various domains. Here are
some notable areas where C is commonly used:

1. System Software Development


C is often used to develop system software, including operating systems (e.g.,
Unix, Linux, Windows), device drivers, and firmware for embedded systems. Its
low-level capabilities make it suitable for tasks that require direct interaction
with hardware.

2. Application Software
C is used to build a variety of application software, such as text editors,
compilers, and databases. It provides the necessary tools and efficiency to create
robust and performance-critical applications.
3. Game Development
Many video games and game engines are developed in C or C++. These languages
offer the performance and control needed for real-time rendering and complex
game logic.

4. Embedded Systems
C is widely used in the development of embedded systems found in consumer
electronics, automotive systems, medical devices, and more. Its ability to work
with limited resources makes it a preferred choice in this domain.

5. Scientific Computing
C is used in scientific research and computational modeling due to its efficiency
in handling complex calculations. Researchers often use C for simulations, data
analysis, and scientific programming.
6. Web Development (Backend)
Although higher-level languages like Python and JavaScript dominate web
development, C can be used for developing web server software and optimizing
critical components of web applications.

7. Compilers and Interpreters


C is often used to create compilers and interpreters for other programming
languages. This bootstrapping process highlights C’s role in building the
foundations of other programming ecosystems.
End

You might also like