Nothing Special   »   [go: up one dir, main page]

C Programming Language

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

C Programming Language

Basics of C programming:-
C is a general purpose high-level language most popular amongst coders, it is the most
compatible, easy to learn and effective programming language. It is a successor of B
language that came out way back in the 70s and has been the most popular programming
language ever since.

Features of C programming:-

 C is a Middle level language. 


 It is case sensitive.
 It is easy to extend.
 Highly portable.
 It is fast and efficient.
 It is more widely used language in operating systems and embedded system.
 It is the simple language.

Why C is a middle level language?


To understand this let us first understand what higher and lower level languages are
with example.

A higher level language would be the language that we humans speaks like simple
English and lower level language is that a computer can understand like binary (1s
and 0s) called machine language, while C is a middle level language that is
between higher and lower level and is also called assembly language. It uses
simple words from English like for, if, while, include and also symbols like +, ++, <,
%, & to carry out tasks and to interact with our computer.

We Used C Language in Following Areas:-

 We can create an Operating System with the help of the C language. Mostly
all operating systems are developed in C language like Unix, Windows
operating system.
 We can develop assemblers in C language.
 We can develop compilers in C language.
 Network Drivers are also created using C language.
 Text Editors are developed using the C language like Notepad++,gedit e.t.c.
 Database Management System are developed using the C language like
oracle database.

Limitations of C language:-

 No exception handling is done.


 We can not reuse the code in C language.
 In C language, no run time type checking is done.
 C language doesn’t support Object Oriented programming.

Features of C Language
The reason for the popularity of the C language is its features, these good features
make this language unique, mother language and Powerful language.
C is a procedural programming language. It was mainly developed as a System
programming language, for writes the operating system program. Its main features
are:- presence of low level access to memory,  use of simple set of keywords, this
features make suitable of C language, it is portable.

There are some features of the C programming language:-

Features of C:-

 Small
 Speed
 Modularity
 Case sensitive language
 Middle level language
 Structured
 Memory Management
 Rich library
 Pointer & Recursion
 Extensible
 Fast
Small:-

 C provide 32 reserved keywords, these keywords provide control to programmer on


language. By understanding  the use of keywords we can do programming in C
language.
 C language can easily learn in less time because, C language is small and simple
language.
 This language provide structure approach.
 In C language there are  various data types and library function provided to solve the
problems.

Speed:-

 The main feature of the C language is Speed because after assembly language C
language is considered to be the fastest.
 The compilation and execution time of the C language is very fast.
 C language is also known as Low level language.
 This language is very close to the hardware. 

Modularity:-

 C is a Modular programming language. In this feature small modules are made to


solve any problem and these small module are known as function. By dividing any
program in modules, it becomes easy to manage or debug the program.

Case Sensitive:-

 C is a case sensitive language.


 In case sensitive programming all letters are different weather they are small and
capital variables, which makes it easy for the programmer to create variables in a
program.

Middle level language:-

 This is the main feature in features of C.C language is a middle level language
because C language can make high level and low level software.
 C language is also used in programming of low level language, in which  system
applications are created.it also supports the features of high level language, that’s
why C language is called a middle level language.

Structured:-

 C is  a structure programming language. 


 In this feature small modules are made to solve any problem and these small module
are known as function. By dividing any program in modules, it becomes easy to
manage or debug the program.

Memory Management:-
 Memory Management is a good feature in features of C.
 C language supports the feature of dynamic memory allocation.
 We can allocate time memory with the help of free() function.

Rich Library:-

 This is the most important feature in features of C.


 C language provides various in-built functions, with the help of which we can speed
up any program.

Pointer and Recursion:-

 C language provides Pointer and Recursion feature.we can  directly handle the
memory with the help of Pointer.we use pointer for Array,Function,Structures.
 In C programming, we can call function inside the function.C language provides
code re-usability to us.By which we can use the code of any function again.

Extensible:-

 In the C programming language, you can add your own user defined library and use
it. Because the C language is an extensible programming language and we can add
new features easily.

Fast:-

 After assembly language C language is considered to be the fastest.


 This is also sometimes called low level language.
 C language is close to the hardware.
 Processing of applications created in the C language is fast.

History of C Language
To overcome the problems of languages such as B and BCPL, Dennis Ritchie from
The United States of America developed C language in 1972.

C was developed by Dennis Ritchie, he was born on 9th September, 1941. Ritchie
invented two great inventions, C programming language and Unix operating
system.he also co-authored a book “The C Programming Language”.
 

C is used for creating system application.Implementation of system software was


designed by C language.It is a case sensitive programming language.

Dennis Ritchie further was referred as “The Father of Modern Programming


Languages”.

C is a simple programming language in the history of computing programming


language. It is structure oriented. Nowadays, many programming languages have
arrived with many features like C++, python, Java and more but C is a base of any
language. 

Initially C was developed to be used in UNIX operating system, but nowadays it’s
compatibility has grown a lot ranging from windows, Mac, Linux, Ubuntu and
others.Many of the important working principle of C comes from BCPL, developed by
Martin Richards, although B,BCLP are typeless languages C provides a number of
different data types.

C Programming history:-
 In 1960, ALGOL was developed by the  international group.

 In 1966, Martin Richards developed BCPL (Basic Combined Programming

Language before developing C.

 Later, Ken Thomson further developed BCPL by developing the first letter of BCPL

i.e. he developed the B language.

 Ken Thomson developed BASIC type of UNIX Operating System, while the B

language was in motion.

 After all this, by further developing the B language, Dennis Ritchie developed the C

language in the Bell laboratory.  

 After developing the C language, Dennis Ritchie and Ken Thompson together

developed the UNIX Operating System.

 In 1978, Kerni Ghan and Ritchie both did further improvement in C language and

they brought the C programming language in front of the whole world.

 In 1989, ANSI C developed by the ANSI Committee.


 In 1999, The new version of C was developed, In which have new features like Data

Types- char,float,int,double was used.

 Today’s  we are using the new version of C is C11, which is developed by Standard

Committee.

Low Level Programming :-


A low-level language is the language the machine understands. The machine
language. Every machine or computer comes with a specific assembly. 

Low level languages are used to write programs that relate to the specific
architecture and hardware of a particular type of computer.

Programs and applications written in low-level language are directly executable on


the computing hardware without any interpretation or translation..Low-level
languages are appropriate for writing operating systems or firmware for micro-
controllers. They can do just about anything with a little bit of work.

Two common types of low-level programming:-

 Assembly language
 Machine language.

Ex:- “Machine language” and “Assembly language” are the examples of Low level
languages.
Machine language:-

Machine language consists of sequence of instructions written in the form of binary


numbers to which computer responds directly. The machine only understand the
binaries digit 01, this is what we call the machine language.  All programs need to be
translated into machine code before a computer can understand and execute them.

 ML(Machine language ) is a low-level language interpreted and converted from


high-level source code and understood only by the machine. Machine code is
transported to the system processor when a specific task, application or program
executes even the smallest process.

Ex:-  computer can understand only o ,1 language we convert into 0,1 form through 
high level language.
1.   24 —–> 1100
2.   14 ——> 01110

Assembly Language:-

Assembly language is simply a different way to write down machine code. It is used
instead of machine code in order to make the programmer’s job easier – instead of
reading binary values, the programmer deals with easier to read shorthand that is
equivalent to the binary. A low level programming language used to relate to the
specific architecture and hardware of a particular type of computer.

Assembly language has little semantics or formal specification, being only a mapping
of human-readable symbols, including symbolic addresses, to opcodes, addresses,
numeric constants, strings and so on. 

Ex:- MOV TH1 , #RELOAD —> Move  the reload  value of TH1

       SUB AX,BX —> Subtraction of AX,BX

       SJMP WAIT —> Wait till interrupt occurs


Working on low level Programs :-

As C is considered a low-level language, you can often do much of this type of


programming in C. But not all of it. Sometimes one must resort to assembly
language, which usually has a one to one correspondence (unless one is using
macros) to the machine instructions of the machine. Mathematically a number
system in any base works equally well. Whether or not it would gain traction among
any particular group of people is a different matter.

Why C is a middle level language?


C is a middle level language because its bind  the gap between machine level
language and high level language.it can be used for both, system programming (like
as operating system) as well as application programming (like as spreadsheet).
Middle level language are more related to machine as well as human language. So
that’s why it is called “Middle level language”.

C language merges the best element of high level language with the rule and
flexibility of assembly language.C allows the manipulation of bits and addresses and
bytes .

Characteristics:-

 It is  also supports high level language features.


 It is static typing.
 Garbage collection.
 Virtual machines.
 Middle level language are closely related to machine language as well as human
language.
 It is user friendly as compared to previous language.
 Efficiency is increased.
 It is reusable.
 We have languages that are classified in three categories:-

Higher level language:- 

A higher level language(HLL)  would be the language that we humans speaks like
simple English.HLL is more closer to humans than it is to machines.

C is only a programming language which can be used in Device drivers, to create


programs or software to access Hardware Level.

BASIC, C,C++,java are such examples of high level language.

Machine language:-

 A machine language is a computer can understand like binary (1s and 0s) or
bits.machine language is the only language a computer can understand called
machine language.this language is also known as Low Level Language. This is a
very tough language. This language was not used when High level language was not
developed.

Example:- 00101101,10101111,01111111 (binary digit) .

                  45,175,127 (Non-binary digits) .


 Assembly language:-

 Assembly language is a programming language which lies in between high level


language and binary language just like C. C uses simple words from English like for,
if, while, include and also symbols like +, ++, <, %, & to carry out tasks and to
interact with our computer.this is also known as Middle Level language. In this
language we use simple english words to write a program.
Introduction of C
C is procedural and high level structured oriented programming language.c is used
for general purpose programming.it was developed by Dennis Ritchie in between
1969 and 1973.It is simple and flexible.C was invented to write everything from
operating system like Oracle database,Git and more.

We can say that, C is a base for the programming .if you know about “C” language
,you can easily get the knowledge of other programming languages uses the concept
of C.

Some facts about “C”

 C was developed by Dennis Ritchie in between 1969 and 1973.


 American National Standards Institute (ANSI) had organized the C language in 1988.
 C was invented to write from operating system.
 Linux,Oracle database,PHP are written in C.
Application of  C language

 It is used in writing embedded software.


 It is used for creating system application,
 Database system
 Operating system
 Compilers and assemblers
 Interpreters
 The core parts of UNIX,WINDOWS are written using the C language.
 It is used to create graphical application.
 It is used in network drivers.
 Used in text editors.

Advantages

  It is case sensitive.


 It is highly portable language.
 Programs written in c language efficient and fast.
 It is easy to understand.  
 C language is procedure oriented language.

Disadvantages

 It does not have any concept of destructor and constructor.


 C++ is developed because C does not have concept of oops.
 Lack of exception handling.
 It does not have concept of namespace.

Basic Structure of C Program


A C Program has the following entites that define structure of the program –

1. Preprocessor Commands

2. Functions

3. Variables

4. Statements & Expressions

5. Comments
Basic Structure of C Program
A C Program has the following entites that define structure of the program –

Structure
Let’s take an example for exploring these sections:

In this example we will write a program to find the area of a circle with radius = 5.

/* Comments
Name of Developer    - Somya Jain
Program - To find the area of a circle using radius*/

//Preprocessor section
//May include libraries, macros etc
#include <stdio.h>
#define PI  3.1416
float area (float r);

int main(void)
{
//Example of variables
float r = 5;
printf("Area:  %.2f",area(r));

return 0
}

//Example of function
float area(float r)
{
//example of statements & Expression
return  PI * r * r;
}

Output:–

Area: 78.54
Basic structure of C language –
Documentation Section:-

The C program is divided  into several sections, the Documentation section, which


is at the top of the program. In this section, programmer can provide information
related to this section like-

 who created the program.


 what is the purpose of the program
 Which date the program was created.
 Any necessary information related to the program can be written here.

/* Name    - Somya Jain


Program-  To find the area of a circle using radius r
Created -  12/7/19
Place     - Noida */

Link Section:-

In this Link section, programmers can capture external files in their current
programs.In which the header file is primarily linked. Header files are a type of file.

In this section we write all the header files, inside which we have to use the functions
with the use of in the next program. 

Like:-

#include<stdio.h> Because of this, we use a function like printf(), and scanf().

#include<conio.h> Because of this, we use a function like clrscr(), and getch().

#include<stdio.h>
#include<conio.h>
Definition Section:-

In this section we define a symbolic constant that we have to use in the entire
program, here the value that is defined once does not change during the entire
program. These symbolic constants are also called micro.

#define PI  3.1416


Global declaration section:-

In this section we define a symbolic constant that we have to use in the entire
program. Whenever a program requires a variable that can be accessed by any
function of the program, then the programmer should define such a variable in the
global declaration section.

This section contains function declaration:

float area (float r);


Main() function Section:-
Main function is the most important section in C programming.C program is not run
without main function, so whenever a program is created in the C language, it must
have a main function.which are written in the main function section.whenever you
implement a program built in C, the compiler automatically executes the main
function and if the program does not have a main function,then the program warns
the programmer by displaying the error message. 

Within the main function,there are two sub parts:

 Declaration part
 Executable part

In declaration part we defines the local variable.which we have to use inside our
main function.while the executable part is the part of the main function where the
program processes or executes the data provided.

This part contains the main function:

int main(void)
{
float r = 5;
printf(“Area:  %.2f”,area(r));
return 0;
}
Sub-Program Section:-

If you want to use a user defined function in C program, then the user defined
function is defined in the same section. Sub-program section below the main function
section.here you can define user defined function.

This section contains sub program:

float area(float r)
{
return  PI * r * r;
}
Standard input and output functions
C programming language libraries that allow input and output in a program.
the  Stdio.h or Standard input-output library in C that has a method for input and
output.

Input:  

when we talking about the input, so it means to feed some data into a program. an
input can be given in the form the command line. C programming has a set of built-in
functions to read the given input and feed it to the program as per
requirement. scanf() is a function which is used to take input.

Output:

When we talking about the output, so it means to display something in the screen.
The data we want to print in the screen that is print as it is. C programming has a set
of built-in function to output the data on the computer screen. printf() is a function
which is used for output.
Formate Specifier for I/O

Here’s the list of commonly used data type and their formate specifier. which is used
in printf() and scanf() function for take input and print the value of any variable of
data type.

    Data type Formate  Specifier

            int               %d

            char                %c

            double                %lf

            float                %f

            short int                %hd

            unsigned int                %u


            long int                %li

            long long int                %lli

            unsigned long int                %lu

            unsigned long long int                %llu

            signed char                 %c

            unsigned char                  %c

            long double                  %Lf

C Output

As we see the printf() is an output function in C. this function sends formatted output
to the screen.

To use the printf() in our program, we need to include stdio.h header fil
using #include<stdio.h> statement. 

the return 0; statement inside main() function is the Exit status of the program.

example

#include<stdio.h>

int main()
{
    printf("learn C programming from Prepinsta");
    return 0;
}

Output

learn C programming from Prepinsta.


Formate Specifier in C output for data type 

In this section, we see commonly used data type. we see how to print data type
value.

#include <stdio.h>
int main()
{
int testInteger = 5;
float testFloat = 13.5;
double testDouble = 12.4;
char testChar = 'a';
printf("int value = %d \n", testInteger);
printf("float value = %f\n", testFloat);
printf("double value = %lf\n", testDouble);
printf("char value = %c", testChar);
return 0;
}

Output

int value = 5
float value = 13.500000
double value = 12.400000
char value = a

Here we use formate specifier to print data type. here, the %d,%f,%c,%lf inside the
quotation will be replaced by the value of testInt, testFloat, testDouble and testChar.

C input

As we see the scanf() is an input function in C this function used to take input from
the user. the scanf() function reads formatted input from the standard input such as
keyboards.

Example

here we will use commonly used, formate specifier in c input.

#include <stdio.h>

int main()
{
int testInteger;
float testFloat;
double testDouble;
char testChar='9';

printf("enter a character value ");


scanf("%c",&testChar);
printf("\nyour entered char value = %c", testChar);

printf("\nenter an Integer value ");


scanf("%d",&testInteger);
printf("\nyour entered integer value = %d ", testInteger);

printf("\nenter a float value ");


scanf("%f",&testFloat);
printf("\nyour entered float value = %f", testFloat);

printf("\nenter a double value ");


scanf("%lf",&testDouble);
printf("\nyour entered ndouble value = %lf", testDouble);

return 0;
}

Output

enter a character value g

your entered char value = g


enter an Integer value 45

your entered integer value = 45


enter a float value 4.4

your entered float value = 4.400000


enter a double value 45.89

your entered ndouble value = 45.890000

here we have used %d,%f,%lf and %c  formate inside the scanf() function to take
int,float, double and character input respectively from user. when user input integer
value, it stored in the testInterger variable. 

Notice, that we have used &testInteger inside scanf(). It is because &testInteger gets
the address of testInteger, and the value entered by the user is stored in that
address.
Difference between interpreter and compiler
While humans can only read/write in complex languages and hence the program
written by a programmer is also in higher level language which also can only be read
by a human, the computer deals in a more simple binary language which only
includes 1’s and 0’s.

Compiler and Interpreter both are language translators and both converts the high
level language into machine code but there are some differences in between
compiler and interpreter.

Compiler:-

 Goes through the entire program and translates it as a whole in a single run.

 It  takes more time to analyse the source code but the overall execution time is

faster.

 Generates intermediate object code which  requires linking, hence requires more

memory.

 It generates the error message only after scanning the whole program. Therefore

debugging is relatively hard.

 It generates the intermediate code.

 Programming language like C, C++ use compilers.

Interpreter:-

 Translates one line of a program at a time.

 Time taken to analyse the source code is less but the overall execution time is

slower.

 No intermediate object code is generated, hence are memory efficient.

 Continues translating the program until the first error is met, in which case it stops.

which is why debugging is easy.

 It does not generate the intermediate code.

 Programming language like Python, Ruby use interpreters.


In order for the computer to understand a code we are supposed to have programs
that can break down the higher level language (source codes) in the binary
language, this is done by compiler and interpreter.But they both have a number of
differences in their operation.Let’s see what these differences are:-

Interpreter Compiler

Compiler translates the entire program in a


Translates one line of a program at a time.
single run.

Interpreter tells the error of each line and only after


Compiler tell all the errors after the
the error is resolved, the interpreter moves  to
compilation of entire program.
another line.

It takes more time for translation. It takes less time for translation.

Compiler generates intermediate code,that


Interpreter does not generate the intermediate object
code is known as object code or machine
code or machine code.
code.
Programming language like Python, Ruby use Programming language like C, C++ use
interpreters. compilers.

There are chances of error in a program.


There are no chances of error in a program.
Programming language like Python, Ruby use Programming language like C, C++ use
interpreters. compilers.

Assembler Compiler Interpreter Linker Loader


Assembler

In computer science, assembler is a program which converts assembly language


into machine code. A computer doesn’t understand human languages like English or
french, but it deals in a much simpler language called binary language, but a
programmer can not write the whole program with its complexity in a binary language
therefore we need a program that can convert the human written language
(assembly language) into binary language, these softwares are called assemblers.

In assembler, a programmer can write a program into sequence of assembler


instructions, the sequence of assembler instruction is known as source code and
source program.
Compiler

A Compiler is a program that converts a number of statement of program into binary


language, but it is more intelligent than interpreter because it goes through the entire
code at once and can tell the possible errors and limits and ranges.But this makes
it’s operating time a little slower.it is platform-dependent.its help to detect error and
get displayed after reading the entire code by compiler.

In other words we can say that, “Compilers turns the high level language to binary
language or machine code  at only time once”, it is known as Compiler.
Interpreter

An interpreter is also a program like a compiler that converts assembly language into
binary but an interpreter goes through one line of code at a time and executes it and
then goes on to the next line of the code and then the next and keeps going on until
there is an error in the line or the code has completed.It is 5 to 25 times faster than a
compiler but it stops at the line where error occurs and then again if the next line has
an error too, where as a compiler gives all the errors in the code at once.if changes
are made on that code which is already compiled then the changed code will need
to  be compiled and added to compiled code or the entire code need to be re-
compiled.

Also, a compiler saves the machine codes for future use permanently but an
interpreter doesn’t, but an interpreter occupies less memory.
Interpreter is differ from compiler such as,

 Interpreter is faster than compiler.


 It contains less memory.
 Interpreter executes the instructions in to source programming language.

There are several types of interpreter:

 Syntax-directed interpreter
 Threaded interpreter
 Bytecode interpreter

Linker

For a code to run we need to include a header file or a file saved from the library
which are pre-defined if they are not included in the beginning of the program then
after execution the compiler will generate errors, and the code will not work.
Linker is a program that holds one or more object files which is created by compiler,
combines them into one executable file.Linking is implemented  at both time,load
time and compile time. Compile time is when high level language is turns to machine
code and load time is when the code is loaded into the memory by loader.

Linker is of two types:

1.Dynamic Linker:-          

 It is implemented during run time.


 It requires less memory.
 In dynamic linking there are many chances of error and failure chances.
 Linking stored the program in virtual memory to save RAM,So we have need to
shared library

2.Static Linker:-

 It is implemented during compilation of source program.


 It requires more memory.
  Linking is implemented before execution in static linking.
 It is faster and portable.
 In static linking there are less chances to error and No chances to failure.

Loader
Loader is a program that loads machine codes of a program into the system
memory.It is part of the OS of the computer that is responsible for loading the
program. It is the bare beginning of the execution of a program. Loading a program
involves reading the contents of executable file into memory. Only after the program
is loaded the operating system starts the program by passing control to the loaded
program code. All the OS that support loading have loader and many have loaders
permanently in their memory.

You might also like