You are on page 1of 37

Introduction to C Programming Language

Dr. Ajay Pratap

Assistant Professor

Department of Computer Science and Engineering

Indian Institute of Technology (BHU) Varanasi, India

ajay.cse@iitbhu.ac.in

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 1 / 20
Introduction to C Programming Language

Content of the lecture

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 2 / 20
Introduction to C Programming Language

Content of the lecture


What is C ?
Types of programming languages
Features of C
Structure of a C program
Executing a C program
Characteristics and applications

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 2 / 20
What is C ?

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 3 / 20
What is C ?

C is a programming language developed at AT & T’s Bell


Laboratories of USA in 1972.
Designed and written by a man named Dennis Ritchie.
Late seventies C began to replace the more familiar languages of that
time like, PL/I (IBM, 1964), FORTRAN (John Backus and IBM,
1957), ALGOL 60 (1960), CPL (Cambridge, 1963), BCPL (Martin
Richard, 1967), B (Ken Thompson, 1970), etc.
It wasn’t made the ‘official’ Bell Labs language.
Reason for popularity of C: reliable, simple and easy to use.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 3 / 20
Types of Programming Languages

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 4 / 20
Types of Programming Languages

Generally two types of computer programming languages are defined:

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 4 / 20
Types of Programming Languages

Generally two types of computer programming languages are defined:


Low level language
High level language

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 4 / 20
Types of Programming Languages

Generally two types of computer programming languages are defined:


Low level language
High level language
Low Level Language: Close to machine code (0s and 1s).
Instructions are written in binary forms. For examples:

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 4 / 20
Types of Programming Languages

Generally two types of computer programming languages are defined:


Low level language
High level language
Low Level Language: Close to machine code (0s and 1s).
Instructions are written in binary forms. For examples:
Machine language
Assembly language

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 4 / 20
Types of Programming Languages

Generally two types of computer programming languages are defined:


Low level language
High level language
Low Level Language: Close to machine code (0s and 1s).
Instructions are written in binary forms. For examples:
Machine language
Assembly language
Machine Language (1 GL): Machine language is a collection of
binary digits orbits that the computer reads and interprets.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 4 / 20
Types of Programming Languages

Generally two types of computer programming languages are defined:


Low level language
High level language
Low Level Language: Close to machine code (0s and 1s).
Instructions are written in binary forms. For examples:
Machine language
Assembly language
Machine Language (1 GL): Machine language is a collection of
binary digits orbits that the computer reads and interprets.
Easy for machines but difficult for human being.
Fast because no translation program is required for the CPU.
Machine dependent, difficult to modify, difficult to program.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 4 / 20
Types of Programming Languages
Assembly Language (2 GL): A program written in assembly
language consists of a series of instructions mnemonics that
correspond to a stream of executable instructions, when translated by
an assembler, that can be loaded into memory and executed.
Advantages: Easy to understand and use, easier to locate and
correct errors, easy to modify, efficiency of machine language.
Disadvantages: Machine dependent, knowledge of hardware
required, machine level coding.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 5 / 20
Types of Programming Languages

High Level Languages (3 GL): Allow us to write computer code


using instructions resembling everyday spoken language (for example:
print, if, while) which are then translated into machine language to be
executed.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 6 / 20
Types of Programming Languages

High Level Languages (3 GL): Allow us to write computer code


using instructions resembling everyday spoken language (for example:
print, if, while) which are then translated into machine language to be
executed.
Some programming languages use a compiler to perform this
translation and others use an interpreter.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 6 / 20
Types of Programming Languages

High Level Languages (3 GL): Allow us to write computer code


using instructions resembling everyday spoken language (for example:
print, if, while) which are then translated into machine language to be
executed.
Some programming languages use a compiler to perform this
translation and others use an interpreter.
Example of high level languages: ADA, C, C++, JAVA, BASIC,
COBOL, PASCAL, PHYTON.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 6 / 20
Comparison

1
PROG0101 Fundamentals of Programming, FTMS College, Kuala Lumpur,
Malaysia.
Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 7 / 20
Other Types of Programming Languages

The fourth generation languages,or 4GL,are languages that consist of


statements similar to statements in a human language. Fourth
generation languages are commonly used in database programming
and scripts.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 8 / 20
Other Types of Programming Languages

The fourth generation languages,or 4GL,are languages that consist of


statements similar to statements in a human language. Fourth
generation languages are commonly used in database programming
and scripts.
The fifth generation languages, or 5GL, are programming languages
that contain visual tools to help develop a program. A good example
of a fifth generation language is Visual Basic.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 8 / 20
Features & Characteristics of C

Simple
Powerful
Fast and efficient
Portable
Function rich library
Easy to extend
Variety of datatype and powerful operators
Portable
Pointers
Recursion
Extensible

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 9 / 20
Structure of C Language

3
Yashwanth Kanetker, “Let Us C”, BPB Publication, 2005.
Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 10 / 20
Structure of C Language

Basic C program has following parts:

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 11 / 20
Structure of C Language

Basic C program has following parts:


Preprocessor Command: Includes all standard input-output files before
compiling any C program.
Functions: Function is a group of statements that together perform a
task.
Variables: Name given to a storage area that our programs can
manipulate
Statements & Expressions

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 11 / 20
Structure of C Language

Basic C program has following parts:


Preprocessor Command: Includes all standard input-output files before
compiling any C program.
Functions: Function is a group of statements that together perform a
task.
Variables: Name given to a storage area that our programs can
manipulate
Statements & Expressions
Comments: /*Multi-line comment */
// single line comment
A simple description can also be found: https://data-flair.
training/blogs/basic-structure-of-c-program/

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 11 / 20
Structure of C Language

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 12 / 20
Hello World Program

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 13 / 20
Compile and Execute C Program

Open a text editor and write the code


Save the file as x.c

4
Other compilation and execution procedure can be learned during the lab hours.
Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 14 / 20
Compile and Execute C Program

Open a text editor and write the code


Save the file as x.c
Open a command prompt and go to the directory where you have
saved the file.
Type gcc x.c and press enter to compile your code.
If there are no errors in your code, the command prompt will take you
to the next line and would generate a.out executable file.
Type a.out to execute your program.
The output printed on the screen.

4
Other compilation and execution procedure can be learned during the lab hours.
Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 14 / 20
Compile and Execute C Program

Open a text editor and write the code


Save the file as x.c
Open a command prompt and go to the directory where you have
saved the file.
Type gcc x.c and press enter to compile your code.
If there are no errors in your code, the command prompt will take you
to the next line and would generate a.out executable file.
Type a.out to execute your program.
The output printed on the screen.
GCC on Ubuntu 18.04 LTS:
https://www.youtube.com/watch?v=oLjN6jAg-sY

4
Other compilation and execution procedure can be learned during the lab hours.
Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 14 / 20
Compile and Execute C Program

Open a text editor and write the code


Save the file as x.c
Open a command prompt and go to the directory where you have
saved the file.
Type gcc x.c and press enter to compile your code.
If there are no errors in your code, the command prompt will take you
to the next line and would generate a.out executable file.
Type a.out to execute your program.
The output printed on the screen.
GCC on Ubuntu 18.04 LTS:
https://www.youtube.com/watch?v=oLjN6jAg-sY
Command Prompt ( cmd ) on Windows 10:
https://www.youtube.com/watch?v=zQtCsiaeiIE
4
Other compilation and execution procedure can be learned during the lab hours.
Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 14 / 20
Compile and Execute C Program

3
E. Balagurusamy ”Programming in ANSI C”
Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 15 / 20
Applications of C

Due to portability and efficiency, C is used to develop the system as


well as application software.
System Software:
Operating system, interpreters, compilers, assemblers, editors, loaders,
linkers.
Application Software:
Database systems, graphics packages, spread sheets, word processors,
office automation, scientific and engineering applications.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 16 / 20
Addition of two numbers

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 17 / 20
C Input Output (I/O)

The printf() is a library function to send formatted output to the


screen. The function prints the string inside quotations.
To use printf() in our program, we need to include stdio.h header file
using the #include < stdio.h > statement.
We use %d format specifier to print int types. Here, the %d inside
the quotations will be replaced by the value of variable.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 18 / 20
C Input Output (I/O)

The printf() is a library function to send formatted output to the


screen. The function prints the string inside quotations.
To use printf() in our program, we need to include stdio.h header file
using the #include < stdio.h > statement.
We use %d format specifier to print int types. Here, the %d inside
the quotations will be replaced by the value of variable.
In C programming, scanf() is one of the commonly used function to
take input from the user. The scanf() function reads formatted input
from the standard input such as keyboards.
Here, we have used %d format specifier inside the scanf() function to
take int input from the user. When the user enters an integer, it is
stored in the variable.

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 18 / 20
Suggested Readings/Reference Materials

1st Chapter of:


E. Balagurusamy ”Programming in ANSI C”
Yashavant P. Kanetkar ”Let US C”

Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 19 / 20
Dr. Ajay Pratap (IIT (BHU), Varanasi) Introduction to C Programming Language ajay.cse@iitbhu.ac.in 20 / 20

You might also like