Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

BE SEM - I

C- Programming
Unit 2: Introduction to c

Syllabus Unit: - 2

Introduction
C is a general-purpose programming language that is extremely popular, simple and flexible.
It is machine-independent, structured programming language which is used extensively in
various applications.

In 1972, a great computer scientist Dennis Ritchie created a new programming language
called 'C' at the Bell Laboratories. C was originally first implemented on the DEC PDP-11
computer in 1972. It was created from 'ALGOL', 'BCPL' and 'B' programming languages. 'C'
programming language contains all the features of these languages and many more additional
concepts that make it unique from other languages.

'C' is a powerful programming language which is strongly associated with the UNIX
operating system. Even most of the UNIX operating system is coded in 'C'. Initially 'C'
programming was limited to the UNIX operating system, but as it started spreading around
the world, it became commercial, and many compilers were released for cross-platform
systems.

Today 'C' runs under a variety of operating systems and hardware platforms. As it started
evolving many different versions of the language were released. At times it became difficult
for the developers to keep up with the latest version as the systems were running under the
older versions. To assure that 'C' language will remain standard, American National
Standards Institute (ANSI) defined a commercial standard for 'C' language in 1989. Later, it
was approved by the International Standards Organization (ISO) in 1990.

C has now become a widely used professional language for various reasons −
• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms

Facts about C
• C was invented to write an operating system called UNIX.
• C is a successor of B language which was introduced around the early 1970s.
• The language was formalized in 1988 by the American National Standard Institute
(ANSI).

Teksan Gharti
BE SEM - I
C- Programming
• The UNIX OS was totally written in C.
• Today C is the most widely used and popular System Programming Language.
• Most of the state-of-the-art software have been implemented using C.
• Today's most popular Linux OS and RDBMS MySQL have been written in C.
Advantages of C
• C is the building block for many other programming languages.
• Programs written in C are highly portable.
• Several standard functions are there (like in-built) that can be used to develop
programs.
• C programs are collections of C library functions, and it's also easy to add functions to
the C library.
• The modular structure makes code debugging, maintenance, and testing easier.

Disadvantages of C
• C does not provide Object Oriented Programming (OOP) concepts.
• There are no concepts of Namespace in C.
• C does not provide binding or wrapping up of data in a single unit.
• C does not provide Constructor and Destructor.

2.1 Historical development of c

• The history of C programming language is quite interesting.


• C is a general-purpose programming language which features economy of expression,
modern control flow and data structures, and a rich set of operators. C is not a "very
high level" language, nor a "big" one, and is not specialized to any particular area of
application. But its absence of restrictions and its generality make it more convenient
and effective for many tasks.
• C programming language is a structure-oriented programming language, was
developed at Bell Laboratories in 1972 by Dennis Ritchie
• C language features were derived from earlier language called “B” (Basic Combined
Programming Language – BCPL)
• In earlier days, programs were written in assembly level language. So, it had
happened to write very big programs to perform specific tasks using assembly code.
• But, ‘B’ language could perform the same task in few lines of program and it was
faster than assemble language code.
• But, B language did not support some features like data types and structures etc. So,
this was a drawback of B language. So, Dennis Ritchie developed C language by
keeping most part of the B language and adding many features that produced
powerful and effective outputs.
• So, C language was invented for implementing UNIX operating system. Most of the
UNIX components were rewritten in C.
• In 1978, Dennis Ritchie and Brian Kernighan published the first edition “The C
Programming Language” and commonly known as K&R C

Teksan Gharti
BE SEM - I
C- Programming
• In 1983, the American National Standards Institute (ANSI) established a committee to
provide a modern, comprehensive definition of C. The resulting definition, the ANSI
standard, or “ANSI C”, was completed late 1988.
• C89/C90 standard – First standardized specification for C language was developed by
American National Standards Institute in 1989. C89 and C90 standards refer to the
same programming language.
• C99 standard – Next revision was published in 1999 that introduced new futures like
advanced data types and other changes.
• C11 standard adds new features to C and library like type generic macros, anonymous
structures, improved Unicode support, atomic operations, multi-threading, and
bounds-checked functions. It also makes some portions of the existing C99 library
optional, and improves compatibility with C++.
• Embedded C includes features not available in normal C like fixed-point arithmetic,
named address spaces, and basic I/O hardware addressing.

Let's see the programming languages that were developed before C language.

Language Year Developed By

Algol 1960 International Group

BCPL 1967 Martin Richard

B 1970 Ken Thompson

Traditional C 1972 Dennis Ritchie

K&RC 1978 Kernighan & Dennis Ritchie

ANSI C 1989 ANSI Committee

ANSI/ISO C 1990 ISO Committee

C99 1999 Standardization Committee

2.2 Importance of c
• C is robust language and has rich set of built-in functions, data types and
operators which can be used to write any complex program.
• Program written in C are efficient due to availability of several data types and
operators.
• C has the capabilities of an assembly language (low level features) with the feature of
high-level language so it is well suited for writing both system software and
application software.

Teksan Gharti
BE SEM - I
C- Programming
• C is highly portable language i.e. code written in one machine can be moved to other
which is very important and powerful feature.
• C supports low level features like bit level programming and direct access to memory
using pointer which is very useful for managing resource efficiently.
• C has high level constructs and it is more user friendly as its syntaxes approaches to
English like language.

• C language is well suited for structured programming, this requires user to think of a
problems in terms of function or modules or block. A collection of these modules
make a program debugging and testing easier.
• C language has its ability to extend itself. A c program is basically a collection of
functions that are supported by the C library. We can continuously add our own
functions to the library with the availability of the large number of functions.

2.3 Basic Stricture of c programs

Documentation section:
The documentation section is the part of the program where the programmer gives the details
associated with the program. programmer usually gives the name of the program, the details
of the author and other details like the time of coding and description. It gives anyone reading
the code the overview of the code.

Example

Teksan Gharti
BE SEM - I
C- Programming
/**
* File Name: Helloworld.c
* Author: teksan
* date: 01/11/2020
* description: a program to display hello world
*
*/
Link section:
This part of the code is used to declare all the header files that will be used in the
program. This leads to the compiler being told to link the header files to the system libraries.

Example
#include<stdio.h>

A header file is a file with extension .h which contains C function declarations and
macro definitions to be shared between several source files.

Some of C Header files:

• stddef.h – Defines several useful types and macros.


• stdint.h – Defines exact width integer types.
• stdio.h – Defines core input and output functions
• stdlib.h – Defines numeric conversion functions, pseudo-random network generator,
memory allocation
• string.h – Defines string handling functions
• math.h – Defines common mathematical functions

Definition section:
The definition section defines all symbolic constants. In this section, we define different
constants. The keyword define is used in this part.

Example
#define PI=3.14

Global declaration section:


There are some variables that are used in more than one function. Such variables are called
global variables and are declared in the global declaration section that is outside of all the
functions. This section also declares all the user-defined functions.
Example
float area(float r);
int a=7;

main () function section:


Every C program must have one main function section. This section contains two
parts; declaration part and executable part

Teksan Gharti
BE SEM - I
C- Programming
Declaration part: The declaration part declares all the variables used in the executable part.

Executable part: There is at least one statement in the executable part. Both the declaration
and execution part are inside the curly braces. The program execution begins at the opening
brace and ends at the closing brace. The closing brace of the main function is the logical end
of the program. All statements in the declaration and executable part end with a semicolon.

Example
int main(void)
{
int a=10;
printf(" %d", a);
return 0;
}

Subprogram section:
The subprogram section contains all the user-defined functions that are called in the
main () function. User-defined functions are generally placed immediately after the main ()
function, although they may appear in any order.

int add(int a, int b)


{
return a+b;
}

Writing C Programs: Sample Program

The C program here will find the area of a circle using a user-defined function and a global
variable pi holding the value of pi

/**
* File Name: areaofcircle.c
* Author: teksan
* date: 2020
* description: a program to calculate area of circle
**/
#include<stdio.h>//link section or Preprocessing Directive
#define pi 3.14;//defination section
float area(float r);//global declaration
int main()//main function
{
float r;
printf(" Enter the radius:n");
scanf("%f",&r);
printf("the area is: %f",area(r));

Teksan Gharti
BE SEM - I
C- Programming
return 0;
}
float area(float r)
{
return pi * r * r;//sub program
}

Executing a c program

Debugging a C Program

What is Debugging?
Debugging is a methodical process of finding and reducing the number of bugs (or
defects) in a computer program, thus making it behave as originally expected. There are two
main types of errors that need debugging:

Compile-time: These occur due to misuse of language constructs, such as syntax errors.
Normally fairly easy to find by using compiler tools and warnings to fix reported problems.

Run-time: These are much harder to figure out, as they cause the program to generate
incorrect output (or “crash”) during execution.

A typical lifecycle for a C/C++ bug is:


• a program fails a unit test included with the source code, or a bug is reported by a
user, or observed by the programmer.
• given the failing input conditions, a programmer debugs the program until the
offending source code is located.
• the program is recompiled with a source code fix and a regression test is run to
confirm that the behaviour is fixed.

Compiling
Compiling is the process of transforming a high level language into a low level
langauge. A high level langauge is closer to English. A low level language is closer to what
the computer understands.

Compiling a C Program
• Compiling is the transformation from Source Code (human readable) into machine
code (computer executable). A compiler is a program. A compiler takes the recipe
(code) for a new program (written in a high level language) and transforms this Code
into a new language (Machine Language) that can be understood by the computer
itself.
• The compiler also ensures that your program is TYPE correct. For example, you are
not allowed to assign a string to an integer variable!

Teksan Gharti
BE SEM - I
C- Programming
• The compiler also ensures that your program is syntactically correct. For example, "x
* y" is valid, but "x @ y" is not.
• The compiler does not ensure that your program is logically correct.
• The compiler we use is the GNU (Gnu is not Unix) Open Source compiler.

1) Running a C Application Program

use Turbo C/C++ to run your C program


Turbo C is one such compiler for windows operating system. If you are running a Linux
operating system, you can use the GCC compiler(GNU Compiler Collection). A compiler
does the job of converting codes written in C language to machine language, so that it can be
executed.
Note: - GNU stands for GNU's Not UNIX.

How to Compile a C program in Turbo C ?


The first step is compiling. Compiling makes sure your program is free of syntax
errors. However, compiler won’t check for any logical/algorithmic errors. There is a lot of
process that happens while the compiler compiles a program. To do compiling – Select ->
Compile from menu and click-> compile. See the image below.

After compiling, you will see a dialog box as shown below. If the compilation is success –
you will see a “success” message. Else you will see the number of errors. Both are shown
using screen shots.

The screen shot of a “success” compilation

Teksan Gharti
BE SEM - I
C- Programming

The screen shot of an “Error” compilation.

The 2 errors are seen because here we removed semicolons from the 2 lines

You may run the program only after a “Successful” compilation. Make your program free of
errors before you RUN the program.

How to RUN a C Program in Turbo C compiler?


To RUN the program – you may select ->Run from menu and click -> Run (as shown in the
image below).

Teksan Gharti
BE SEM - I
C- Programming
Now you will see the output screen as shown in the screen shot below.

======================== End unit 2 ==================

Teksan Gharti

You might also like