C Programming Language
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:-
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 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:-
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.
Features of C:-
Small
Speed
Modularity
Case sensitive language
Middle level language
Structured
Memory Management
Rich library
Pointer & Recursion
Extensible
Fast
Small:-
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:-
Case Sensitive:-
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:-
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:-
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:-
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”.
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.
Ken Thomson developed BASIC type of UNIX Operating System, while the B
After all this, by further developing the B language, Dennis Ritchie developed the C
After developing the C language, Dennis Ritchie and Ken Thompson together
Committee.
Low level languages are used to write programs that relate to the specific
architecture and hardware of a particular type of computer.
Assembly language
Machine language.
Ex:- “Machine language” and “Assembly language” are the examples of Low level
languages.
Machine language:-
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.
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:-
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.
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.
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.
Advantages
Disadvantages
1. Preprocessor Commands
2. Functions
3. Variables
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:-
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>
#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.
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.
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.
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.
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.
int %d
char %c
double %lf
float %f
signed char %c
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
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
#include <stdio.h>
int main()
{
int testInteger;
float testFloat;
double testDouble;
char testChar='9';
return 0;
}
Output
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
Interpreter:-
Time taken to analyse the source code is less but the overall execution time is
slower.
Continues translating the program until the first error is met, in which case it stops.
Interpreter Compiler
It takes more time for translation. It takes less time for translation.
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,
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.
1.Dynamic Linker:-
2.Static Linker:-
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.