Sie sind auf Seite 1von 37

Introduction to Computer Programming

(CSE – 101N)
Unit 2
Overview of C: Elements of C, Data types; Storage classes in C;
Operators: Arithmetic, relational, logical, bitwise, unary, assignment
and conditional operators, precedence & associativity of operators.

Input/output: Unformatted & formatted I/O function in C.

Control statements: if statement, switch statement;

Repetition: for, while, and do-while loop; break, continue, goto


statements.

By
Ashish Shrivastava
Assistant Professor
Computer Science and Engineering
PIET
Different Types of Software's in a Computer

 There are two things every computer has in common: software and
hardware.
 Computer software consists of one or more computer programs
which tell the computer what to do. It can be described as a set of
instructions and data that are stored inside the memory to enable it to
perform different functions.

2
Different Types of Software's in a Computer

 Hardware refers to the physical components of a computer. Items


that form part of the computer and have a physical structure can be
regarded to as computer hardware. Good examples are the mouse and
keyboard. Other hardware’s are
Motherboard: The main board of the computer that consists of
the CPU, RAM, firmware and internet/external buses.
Power Supply: Device that supplies power to the other
components in the machine.
Removable media devices: Such as the CDs, DVD drives, floppy
disk that are used for storage.
Hard disk drive
3
Different Types of Software's in a Computer

Application Software
Database
Word processor
Paint
System Software
Games Operating system Tools & Utility
Hardware

Skype
Language Compiler like C, C++, Java

Calculator

4
Different Types of Software's in a Computer
There are two main types of computer software: System software and
Application software.

System Software – They are responsible for the operation of


Computer hardware. This software sync the work of hardware and
other types of programs. It acts as a middle layer between hardware
and Application software. System software includes programs like –

Operating system – It is a well-known example of system software.


This software interacts with the hardware and provides the capability
for running various types of programs. Desktop uses operating
systems, like Windows, Linux and MacOS, whereas Android and
Windows are commonly used operating systems for mobiles. There
are different types of operating systems like real time, distribute,
embedded, etc., It is necessary to consider the hardware specifications
before deciding the operating system.
5
Different Types of Software's in a Computer
System Software

Functions of Operating system –

CPU management – allocation of CPU to programs


Memory management – loading and unloading of programs, creating
free memory space
Input Output management – managing printer, scanner, and other
devices
File management – creating file in secondary memory (HDD),
managing reading, writing, and updating operations on file.

6
Different Types of Software's in a Computer
System software

Language translators – It includes Assemblers, Compilers and


Interpreters. These programs have been designed for programming
languages like C, Cobol, Pascal, etc. Machine language is translated
into machine code with the help of an Assembler. Compiler translates
the code written in a high level language to a lower level. An
Interpreter is capable of executing programs directly. It executes the
program in a line by line manner.

Common Utility Programs – These programs are designed specifically


for managing the computer device and its resources. It includes
programs like communication tools and disk formatter. They are
more focused on the operations of computer infrastructure. For
example – Virus scanner is a kind of common utility programs which
provides protection to the system from unwanted guests like Trojans
and viruses. 7
Different Types of Software's in a Computer

Application Software – System Software are more concerned about


the management and working of a computer system. However, an
application software performs some specific task on a system. There
are numerous types of application software. They deal mainly with
solving out some specific computing types of problems. These types of
software are also known as apps in short. Some of the common
application software are –

Word Processors – These software programs are used for creation of


documents. In addition to basic activity of creating a document, it also
assists in storing and printing that document. Changes can be easily
made in the document through these word processors. It is one of the
most commonly used application software. MS-Word is an example of
a word processor program.

8
Different Types of Software's in a Computer

Application Software

Database software – This software is capable of creating databases. In


addition to it, it also manages the data or information residing inside
the database. It is also known as DBMS sometimes. They help in
organizing data. Some of the examples of database software are
Access, Oracle and Sybase.

Multimedia software – These software have been devised to work with


different types of media which are connected with each other, they are
usually used in multimedia presentation.

9
Different Types of Software's in a Computer

Application Software

Graphic software – These software programs assist in working with


graphics, as it enables the user to edit or manipulate visual images. It
includes image editors and illustration software. Adobe Photoshop and
Corel Photo paint are two popular examples of graphic software. These
types of software usually work on either bitmap or vector images.

Web Browser – This is a software program which is used for locating


and retrieving files or resources on the World Wide Web. Internet
explorer like web browser comes integrated with the operating system.
Mozilla FireFox is another example of an application software
program. It is an open source and multi-platform software. Other are
Google, Internet explorer.

10
What is C ?

The C programming language is a structure oriented programming


language, developed at Bell Laboratories in 1972 by Dennis Ritchie
C programming language features were derived from an earlier
language called “B” (Basic Combined Programming Language –
BCPL)
C language was invented for implementing UNIX operating system
In 1978, Dennis Ritchie and Brian Kernighan published the first
edition “The C Programming Language” and commonly known as
K&R C
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.

11
What is C ?

Features of C programming language:


Reliability
Portability
Flexibility
Interactivity
Modularity
Efficiency and Effectiveness

12
What is C ?

Uses of C programming language:


The C programming language is used for developing system
applications that forms a major portion of operating systems such as
Windows, UNIX and Linux. Below are some examples of C being
used.

Database systems
Graphics packages
Word processors
Spreadsheets
Operating system development
Compilers and Assemblers
Network drivers
Interpreters
Satellite programming

13
What is C ?

Which level is C language belonging to?


S. High Level Middle Level Low Level
n
1 High level languages provide Middle level languages Low level languages
almost everything that the don’t provide all the provides nothing
programmer might need to do built-in functions found other than access to
as already built into the in high level languages, the machines basic
language but provides all building instruction set
blocks that we need to
produce the result we
want

2 Examples C, C++ Assembler, Machine


Java, Python language

14
What is C ?

The C language is a structured language


Sn Structure oriented Object oriented Non structure

1 In this type of language, large In this type of language, There is no specific


programs are divided into small programs are divided into structure for
programs called functions objects programming this
language
2 Prime focus is on functions and Prime focus is in the data N/A
procedures that operate on the that is being operated and
data not on the functions or
procedures
3 Data moves freely around the Data is hidden and cannot N/A
systems from one function to be accessed by external
another functions
4 Program structure follows “Top Program structure follows N/A
Down Approach” “Bottom UP Approach”

5 Examples: BASIC, COBOL,


C, Pascal, ALGOL and Modula- C++, JAVA and C# (C sharp) FORTRAN
2

15
Execution of a C Program

The compilation and execution process of C can be divided in to


multiple steps:

Pre-processing - Using a Pre-processor program to convert C source


code in expanded source code. "#includes" and "#defines" statements
will be processed and replaced actually source codes in this step.
Compilation - Using a Compiler program to convert C expanded
source to assembly source code.
Assembly - Using a Assembler program to convert assembly source
code to object code.
Linking - Using a Linker program to convert object code to executable
code. Multiple units of object codes are linked to together in this step.
Loading - Using a Loader program to load the executable code into
Memory for execution by CPU.
16
Execution of a C Program

Here is a simple table showing input and output of each step in the
compilation and execution process:
Input Program Output

source code Pre-processor expanded source code

expanded source code Compiler assembly source code

assembly code Assembler object code

object code Linker executable code

executable code Loader execution

17
Basic C Program

1. #include <stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. clrscr();
6. /* Our first simple C basic program */
7. printf(“Hello World! “);
8. getch();
9. return 0;
10. }

18
Basic C Program
Sn Command Description

1 #include <stdio.h> This is a pre-processor command that


includes standard input output header
file(stdio.h) from the C library before
compiling a C program
2 #include<conio.h> This is a pre-processor command that
includes standard console input output
header file(conio.h) from the C library
before compiling a C program
3 void main() This is the main function from where
execution of any C program begins.
4 { This indicates the beginning of the main
function.
5 clrscr(); Built-in library function used to clear
screen
19
Basic C Program
Sn Command Description

6 /* Our first simple C whatever is given inside the command


basic program */ “/* */” in any C program, won’t be
considered for compilation and execution. It
is called comment line
7 printf(“Hello World! “); printf command prints the output onto the
screen. printf is a built-in library function

8 getch(); This command waits for any character input


from keyboard. It is a built-in library function
defined in conio.h header file
9 return 0; This command terminates C program (main
function) and returns 0

10 } This indicates the end of the main function

20
Variables and Data Types

 As we know a program is a group of Instructions and data


In C language variables are used to store data
A variable is a reserved memory location in memory(RAM)
In real life data can be categorized in three types, non decimal
(integer), with decimal (float), and characters (char)
 A data type is used to define variables, data type tells about the
nature of data like integer, float, or character
Data type is a rule to store, read and edit data in variable Data
types are used to define a variable before to use in a program.
Size of variable, constant and array are determined by data types.

21
Variables and Data Types
S.no Types Data Types

1 Basic data types int, char, float, double

2 Enumeration data type enum

3 Derived data type pointer, array, structure, union

4 Void data type void

22
Variables and Data Types
Modifiers in C
The amount of memory space to be allocated for a variable is
derived by modifiers.
Modifiers are sufixed with basic data types to modify (either
increase or decrease) the amount of storage space allocated to a
variable.
For example, storage space for int data type is 2 byte. We can
increase the range by using long int which is 4 byte.
There are 4 modifiers available in C language. They are,

short
long
signed
unsigned

23
Variables and Data Types
S.N C Data types storage Size Range

1 char 1 –127 to 127

2 int 2 –32,768 to 32,767

3 float 4 -2147483648 to 2147483647

8*8 8*8
4 double 8 -2 to (2 - 1)

5 long double 10

6 long int 4

7 short int 2 =int

24
Variables and Data Types
S.N C Data types storage Size Range

8 unsigned 2 0 to 65,535
short int
9 signed short 2 –32,768 to 32,767
int
10 long int 4

11 signed long int 4 –2,147,483,648 to 2,147,483,647

12 unsigned long 4 0 to 4,294,967,295


int
13 unsigned char 1 0 to 255

25
1. #include<stdio.h> a b
2. #include<conio.h> c d
3. void main()
4. { variables in RAM
5. clrscr();
6. int a; float b; char c; double d; long int e; long float f;

7. printf(“\n integer=%d \n float=%d \n char=%d \n \

double=%d \n long int=%d \n long float=%d”, \


sizeof(a),sizeof(b),sizeof(c),sizeof(d),sizeof(e),sizeof(f));
8. getch();
9. }

26
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. clrscr();
6. int a,b,c; a b c
7. a=5; b=7; a 5 b 7 c 12
8. c=a+b;
9. printf(“\n Sum is =%d”, c);
10. getch();
11. }

27
printf() and scanf() library functions

 printf() and scanf() functions are inbuilt library functions in C


which are available in C library by default. These functions are
declared and related macros are defined in “stdio.h” which is a
header file.
 We have to include “stdio.h” file in C program to make use of
these printf() and scanf() library functions.

28
printf() and scanf() library functions

printf()

printf() function is used to print the “character, string, float, integer,


octal and hexadecimal values” onto the output screen.
We use printf() function with %d format specifier to display the
value of an integer variable.
Similarly %c is used to display character, %f for float
variable, %s for string variable, %lf for double and %x for
hexadecimal variable.
To generate a newline, we use “\n” in C printf() statement.

29
printf() and scanf() library functions

printf()
Format specifiers in printf() and scanf()
%d got replaced by value of an integer variable
%c got replaced by value of a character variable
%f got replaced by value of a float variable
%lf got replaced by value of a double variable
%s got replaced by value of a string variable
%o got replaced by a octal value corresponding to integer variable
%x got replaced by a hexadecimal value corresponding to integer
variable
\n got replaced by a newline.
30
printf() and scanf() library functions
printf()
1) #include <stdio.h>
2) #include<conio.h>
3) void main()
4) {
5) clrscr();
6) char ch = ‘A’;
7) float flt = 10.234;
8) int num = 150;
9) double dbl = 20.123456;
10) printf(“Character is %c \n”, ch);
11) printf(“Float value is %f \n”, flt);
12) printf(“Integer value is %d\n” , num);
13) printf(“Double value is %lf \n”, dbl);
14) printf(“%lf %c %d %f”, dbl, ch, num, flt);
15) getch();
16) } 31
printf() and scanf() library functions
printf()

1. print your Name, Father’s name, Address, Mobile num. and


you course name on screen.
2. Write a program to addition, subtraction, multiplication, and
division of two integer numbers.
3. Write a program to implement
(A+B) *(C-D)
X=----------------------
(A+(B/D))
where the values of A, B, C, and D can be taken as per your
choice.

32
printf() and scanf() library functions
scanf()

scanf() function is used to read character, string, numeric data from


keyboard
printf() and scanf() both library functions are defined in “stdio.h”
header file.
getch() and clrscr() both library functions are defined in “conio.h”
header file. RAM

Variable
Data
Keyboard scanf()

33
printf() and scanf() library functions
scanf()
1) #include <stdio.h>
num
2) #include<conio.h>
3) int main()
format specifier
4) {
address of operator
5) clrscr();
6) int num;
7) printf(“Enter any number\n”);
8) scanf(“%d”, &num);
9) printf(“Entered number is %d \n”, num);
10) getch();
variable
11) }

34
printf() and scanf() library functions
scanf()
Write a program to add two numbers, get numbers from user
through keyboard
1) #include <stdio.h>
2) #include<conio.h>
3) int main()
4) {
5) clrscr();
6) int num1, num2, num3;
7) printf(“Enter first number\n”);
8) scanf(“%d”, &num1);
continue

35
printf() and scanf() library functions
scanf()
9) printf(“Enter second number\n”);
10) scanf(“%d”, &num2);
11) num3=num1+num2;
12) printf(“Sum is = %d”, num3);
13) getch();
14) }

36
printf() and scanf() library functions
printf() and scanf()
1. Write a program to print square and cube of a number, get
the number from keyboard.
2. Write a program to addition, subtraction, multiplication, and
division of two integer numbers. get the numbers from
keyboard
3. Write a program to implement
(A+B) *(C-D)
X=----------------------
(A+(B/D))
where the values of A, B, C, and D are taken through
keyboard.
4. Write a program to find sum of series of four numbers, get
numbers from keyboard.
(num1*num2) + (num2*num3) + (num3*num4)
Sum= ----------------- ------------------ -------------------
num4 num3 (num1+num2)
37

Das könnte Ihnen auch gefallen