0% found this document useful (0 votes)
47 views12 pages

Pps Using C r20 - Unit-1

The document discusses the basics of computer programming using C language. It covers key topics like computer hardware components, computer software categories, computing environments, computer languages evolution, and the process of creating and running C programs. The process involves writing code in a text editor, compiling the code using a compiler, linking the compiled code, executing the generated executable file, and checking the output.

Uploaded by

SRI RAMYA B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views12 pages

Pps Using C r20 - Unit-1

The document discusses the basics of computer programming using C language. It covers key topics like computer hardware components, computer software categories, computing environments, computer languages evolution, and the process of creating and running C programs. The process involves writing code in a text editor, compiling the code using a compiler, linking the compiled code, executing the generated executable file, and checking the output.

Uploaded by

SRI RAMYA B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PROGRAMMING FOR PROBLEM SOLVING USING C

UNIT I
 Computer Systems:-
A computer is a system made of two major components: hardware and software. The
computer hardware is the physical equipment. The software is the collection of programs
(instructions) that allow the hardware to do its job.

 Computer Hardware: - The hardware component of the computer system consists of


five parts: input devices, central processing unit (CPU) ,primary storage, output
devices, and auxiliary storage devices.

The input device is usually a keyboard where programs and data are entered into the
computers. Examples of other input devices include amouse, a pen or stylus, a touch screen,
or an audio input unit.
 The central processing unit (CPU) is responsible for executing instructions
such as arithmetic calculations, comparisons amongdata, and movement
of data inside the system.
 The output device is usually a monitor or a printer to show output. If theoutput is
shown on the monitor, we say we have a soft copy. If it is printed on the printer, we
say we have a hard copy.
 Auxiliary storage, also known as secondary storage, is used for both input and
output. It is the place where the programs and data are storedpermanently. When
we turn off the computer, or programs and data remain in the secondary storage,
ready for the next time we need them.

 Computer Software :-
Computer software is divided in to two broad categories: systemsoftware
and application software.
 System software manages the computer resources .It provides theinterface
between the hardware and the users.
 Application software, on the other hand is directly responsible forhelping
users solve their problems.

 Computing Environments:-
Computing Environment is a collection of computers / machines, software, and
networks that support the processing and exchange of electronic information meant to

1|Page
support various types of computing solutions. With the advent if technology the
computing environments havebeen improved.

Types of Computing Environments:-


1. Personal Computing Environment
2. Time sharing Environment
3. Client Server Computing Environment
4. Distributed Computing
1. Personal Computing Environment:-
Personal means, all the computer stuff will be tied together i.e computeris
completely ours, no other Connections.
2. Time sharing Environment:-
In computing, time-sharing is the sharing of a computing resource among many
users by means of multi programming and multi-tasking at the same time. Mam users are
connected to one or more computers.
3. Client Server Computing Environment:-
A client/server system is “a networked computing model that distributes processes
between clients and servers, which supply the requested service.” A client/server network
connects many computers, called clients, to a main computer, called a server. Whenever
client requests for something, server receives the request and process it.
4. Distributed Computing:-
A Distributed Computing Environment Provides a seamless integrationof computing
functions between different servers and clients. the servers are connected by
internet all over the world.

 Computer Languages:-
To write a program for a computer, we must use a computer language. Over the
years computer languages have evolved from machinelanguages to natural languages.

1940’s Machine level Languages


1950’s Symbolic Languages 1960’s
High-Level Languages

 Machine Languages:-
In the earliest days of computers, the only programming languagesavailable were
machine languages. Each computer has its own machine language, which is made of
streams of 0’s and 1’s.
Instructions in machine language must be in streams of 0’s and 1’s because theinternal
circuits of a computer are made of switches transistors and other electronic devices that
can be in one of two states: off or on. The off state is represented by 0 , the on state is
represented by 1.
The only language understood by computer hardware is machine language.

 Symbolic Languages:-
In early 1950’s Admiral Grace Hopper, A mathematician and naval officer
developed the concept of a special computer program that wouldconvert programs
into machine language

2|Page
Computer does not understand symbolic language it must be translated to the machine
language. A special program called assemblertranslates symbolic code into machine language.
 High Level Languages:-
Symbolic languages greatly improved programming effificiency; theystill required
programmers to concentrate on the hardware that they were using.
Working with symbolic languages was also very tedious because each machine
instruction has to be individually coded. The desire to improve programmer
efficiency and to change the focus from the computer to the problem being solved
led to the development of high-level language.

 Creating and running Programs:-


Generally,the programs created usingprogramming languages like C, C++, Java, etc., are
written using a high-levellanguage likeEnglish. But, the computer cannot understand
the high-levellanguage.
It can understand only low-level language. So, the program written in high-level
language needs to be converted into thelow-level language to make it understandable
for the computer.This conversion is performed using either Interpreter or Compiler.
Popular programming languages like C, C++,Java,etc., usethe compiler to convert high-
level language instructions into low-level language instructions.
To create and execute C programs in Windows Operating System, we need to
install Turbo C software. We use the following steps to create and execute C
programs in WindowsOS…

Step 1: Creating Source Code


Source code is a file with C programming instructions in high level language. To create source
code, we use any text editor to write the program instructions. The instructions written in the
source code must follow the C programming language rules. The following steps are used to
create source code file in Windows OS…

Click on Start button


Select Run
Type cmd and press Enter
Type cd c:\TC\bin in the command prompt and press Enter
Type TC press Enter

3|Page
Click on File -> New in C Editorwindow
Type the program
Save it as FileName.c (Use shortcut key F2 to save)

Step 2: Compile Source Code (Alt + F9)


Compilation is the process of converting high level language instructions into low level
language instructions. We use the shortcut key Alt + F9 to compile a C program in Turbo C.
Whenever we press Alt + F9, the source file is going to be submitted to the Compiler.
On receiving a source file, the compiler first checks for the Errors. If there are any Errors then
compiler returns List of Errors, if there are no errors then the source code is converted into
object code and stores it as file with .obj extension. Then the object code is given to the
Linker. The Linker combines
both the object code and specified header file code and generates an Executable file
with .exe extension.

Step 3: Executing / Running Executable File (Ctrl + F9)


After completing compilation successfully, an executable file is created with .exe extension.
The processor can understand this.exe file content so that it can perform the task
specified in thesource file.

We use a shortcut key Ctrl + F9 to run a C program. Whenever we press Ctrl + F9, the .exe
file is submitted to the CPU. On receiving .exe file, CPU performs the task according to the
instruction written in the file. The result generated from the execution is placed in a window
called User Screen.

Step 4: Check Result (Alt + F5)


After running the program, the result is placed into User Screen. Just we need to open the User
Screen to check the result of the program execution. We use the shortcut key Alt + F5 to open
the User Screenand check theresult.

Execution Process of a C Program


When we execute a C program it undergoes with following process…

The file which contains c program instructions in high level language is said to be source code.
Every c program source file issaved with .c extension, for example Sample.c.

4|Page
 Whenever we press Alt + F9 the source file is submitted to the compiler. Compiler checks
for the errors, if there are any errors, itreturns list of errors, otherwise generates object code
in a file withname Sample.obj and submit it to the linker.
 Linker combines the code from specified header file into object file and generates
executable file as Sample.exe. With this compilation process completes.
 Now, we need to Run the executable file (Sample.exe). To run a program we press Ctrl
+ F9. When we press Ctrl + F9 the executable file is submitted to the CPU.
 Then CPU performs the task according to the instructions writtenin that program and place
the result into UserScreen.
 Then we press Alt + F5 to open UserScreen and check the result ofthe program.

Overall Process:-

 Type the program in C editor and save with .c


extension (Press F2 to save).
 Press Alt + F9 to compile the program.
 If there are errors, correct the errors and recompile theprogram.
 If there are no errors, then press Ctrl + F9 to execute / run the
program.
 Press Alt + F5 to open User Screen and check the result.

 Computer Numbering System:-


The technique to represent and work with numbers is called numbersystem. Decimal
number system is the most common number system. Other popular number systems
include binary number system, octal number system, hexadecimal number system, etc.
Decimal Number System:-
Decimal number system is a base 10 number system having 10 digits from 0 to 9. This
means that any numerical quantity can be represented using these 10 digits. Decimal
number system is also a positional value system.

Binary Number System:-


The easiest way to vary instructions through electric signals is two- state system – on
and off. On is represented as 1 and off as 0, though 0 is not actually no signal but signal at
a lower voltage. The number system having just these two digits – 0 and 1 – is called
binary number system.

Octal Number System:-


Octal number system has eight digits – 0, 1, 2, 3, 4, 5, 6 and 7. Octal number system is also
a positional value system with where each digit has its value expressed in powers of 8, as
shown here −

5|Page
Decimal equivalent of any octal number is sum of product of each digit with itspositional
value.

7268 = 7×82 + 2×81 + 6×80


= 448 + 16 + 6
= 47010

Hexadecimal Number System:-


Octal number system has 16 symbols – 0 to 9 and A to F where A is equal to 10, B is equal
to 11 and so on till F. Hexadecimal number system is also a positional value system with
where each digit has its value expressed in powers of 16, as shown here −

 Storing Integers:-
Integers are commonly stored using a word of memory, which is 4 bytesor 32 bits, so
integers from 0 up to 4,294,967,295 (232 - 1) can be stored.
Below are the integers 1 to 5 stored as four-byte values (each rowrepresents
one integer).

0 : 00000001 00000000 00000000 00000000 | 1


4 : 00000010 00000000 00000000 00000000 | 2
8 : 00000011 00000000 00000000 00000000 | 3
12 : 00000100 00000000 00000000 00000000 | 4
16 : 00000101 00000000 00000000 00000000 | 5

This may look a little strange; within each byte (each block of eight bits), the bits are
written from right to left like we are used to in normal decimal notation,but the bytes
themselves are written left to right! It turns out that the computer does not mind which
order the bytes are used (as long as we tell the computer what the order is) and most
software uses this left to right order for bytes.7.3

Two problems should immediately be apparent: this does not allow for negative
values, and very large integers, 232 or greater, cannot be stored in aword of memory.

 Real numbers:-

Real numbers (and rationals) are much harder to store digitally thanintegers.

Recall that k bits can represent 2k different states. For integers, the first state can
represent 0, the second state can represent 1, the third state can represent 2, and so
on. We can only go as high as the integer 2k - 1, but atleast we know that we can
account for all of the integers up to that point.

Unfortunately, we cannot do the same thing for reals. We could say that thefirst state

6|Page
represents 0, but what does the second state represent? 0.1? 0.01? 0.00000001?
Suppose we chose 0.01, so the first state represents 0, the second state represents 0.01,
the third state represents 0.02, and so on. We can now only go as high as 0.01 x (2k - 1),
and we have missed all of the numbers between 0.01 and 0.02 (and all of the numbers
between 0.02 and 0.03, and infinitely many others).

 INTRODUCTION TO ‘C’ LANGUAGE:-


 C language facilitates a very efficient approach to the development and
implementation of computer programs. The History of C started in 1972at the Bell
Laboratories, USA where Dennis M. Ritchie proposed this language. In 1983 the
American National Standards Institute (ANSI)
established committee whose goal was to produce “an unambiguous
and machine independent definition of the language C “ while stillretaining it’s spirit
.
 C is the programming language most frequently associated with UNIX. Since the
1970s, the bulk of the UNIX operating system and its applications have been
written in C. Because the C language does not directly rely on any specific hardware
architecture, UNIX was one of thefirst portable operating systems. In other words,
the majority of the code that makes up UNIX does not know and does not care
which computer it is actually running on.
 C was first designed by Dennis Ritchie for use with UNIX on DEC PDP-11computers.
The language evolved from Martin Richard's BCPL, and oneof its earlier forms was
the B language, which was written by Ken Thompson for the DEC PDP-7. The first
book on C was The C Programming Language by Brian Kernighan and Dennis
Ritchie, published in 1978.
 In 1983, the American National Standards Institute (ANSI) established a committee
to standardize the definition of C. The resulting standard is known as ANSI C, and it is
the recognized standard for the language, grammar, and a core set of libraries. The
syntax is slightly different from the original C language, which is frequently called
K&R for Kernighan andRitchie. There is also an ISO (International Standards
Organization) standard that is very similar to the ANSI standard.
 It appears that there will be yet another ANSI C standard officially dated1999 or in
the early 2000 years; it is currently known as "C9X."

 BASIC STRUCTURE OF C LANGUAGE:-


 The program written in C language follows this basic structure. The sequence of
sections should be as they are in the basic structure. A C program should have one
or more sections but the sequence of sectionsis to be followed.
1. Documentation section
2. Linking section
3. Definition section
4. Global declaration section
5. Main function section
{
Declaration
sectionExecutable
section

7|Page
}
6. Sub program or function section

8|Page
1. DOCUMENTATION SECTION : comes first and is used to document the use of logic or
reasons in your program. It can be used to write the program's objective, developer and
logic details. The documentation is done in C language with /* and */ . Whatever is written
between these two are called comments.

2. LINKING SECTION : This section tells the compiler to link the certain occurrences of
keywords or functions in your program to the header files specified in this section.
e.g. #include <stdio.h>

3. DEFINITION SECTION : It is used to declare some constants and assign them some value.
e.g. #define MAX 25
Here #define is a compiler directive which tells the compiler whenever MAX is found in the
program replace it with 25.

4. GLOBAL DECLARATION SECTION : Here the variables which are used through out the
program(including main and other functions) are declared so as to make them global(i.e
accessible to all parts of program)
e.g. int i; (before main())

5. MAIN FUNCTION SECTION : It tells the compiler where to start the execution
frommain()
{
point from execution starts
}
main function has two sections

1. declaration section : In this the variables and their data types are declared.

2. Executable section : This has the part of program which actually performs the task we need.
6. SUB PROGRAM OR FUNCTION SECTION : This has all the sub programs or
thefunctions which our program needs.

SIMPLE ‘C’ PROGRAM:

/* simple program in c */
#include<stdio.h>
main()
{
printf(“welcome to c programming”);
} /* End of main */

IDENTIFIERS :-
 Names of the variables and other program elements such as functions,array, etc,
are known as identifiers.
There are few rules that govern the way variable are named (identifiers).

9|Page
1. Identifiers can be named from the combination of A-Z, a-z, 0-9,
_(Underscore).
2. The first alphabet of the identifier should be either an alphabet or an
underscore. digit are not allowed.
3. It should not be a keyword.Eg:
name, ptr, sum
After naming a variable we need to declare it to compiler of what data type itis .
The format of declaring a variable isData-

type id1, id2, ................... idn;

where data type could be float, int, char or any of the data types.
id1, id2, id3 are the names of variable we use. In case of single variable nocommas are
required.
Eg float a, b, c;
int e, f, grand total;
char present_or_absent;

DATA TYPES :-
To represent different types of data in C program we need different data types. A data
type is essential to identify the storage representation and thetype of operations that can
be performed on that data. C supports four different classes of data types namely
1. Basic Data types
2. Derives data types
3. User defined data types
4. Pointer data types
BASIC DATA TYPES:
All arithmetic operations such as Addition , subtraction etc are possible onbasic data types.
E.g.: int a,b;
Char c;

DERIVED DATA TYPES:-


Derived datatypes are used in ‘C’ to store a set of data values. Arrays andStructures are
examples for derived data types.
Ex: int a[10];
Char name[20];

USER DEFINED DATATYPES:


C Provides a facility called typedef for creating new data type namesdefined by
the user. For Example ,the declaration ,
typedef int Integer;
makes the name Integer a synonym of int.Now the type Integer can be used in
declarations ,casts,etc,like,
Integer num1,num2;
Which will be treated by the C compiler as the declaration ofnum1,num2as int
variables.
“typedef” ia more useful with structures and pointers.

POINTER DATA TYPES:-


10 | P a g e
Pointer data type is necessary to store the address of a variable.

 VARIABLES :-
A quantity that can vary during the execution of a program is known as a variable. To
identify a quantity we name the variable for example if we arecalculating a sum of two
numbers we will name the variable that will hold the value of sum of two numbers as
'sum'.
 CONSTANTS : -
A quantity that does not vary during the execution of a program is known as a
constant supports two types of constants namely Numeric constants and
character constants.
NUMERIC CONSTANTS:
1. Example for an integer constant is 786,-127
2. Long constant is written with a terminal ‘l’or ‘L’,for example 1234567899Lis a Long
constant.
CHARACTER CONSTANTS:-
A character constant is written as one character with in single quotessuch as ‘a’. The
value of a character constant is the numerical value of the character in the machines
character set.
 INPUT AND OUTPUT STATEMENTS :-
The simplest of input operator is getchar to read a single character fromthe input
device.
varname=getchar();
you need to declare varname.
The simplest of output operator is putchar to output a single character onthe output
device.
putchar(varname)
The getchar() is used only for one input and is not formatted. Formatted input refers
to an input data that has been arranged in a particular format, forthat we have scanf.

scanf("control string", arg1, arg2,...argn);

Control string specifies field format in which data is to be entered.arg1, arg2...

argn specifies address of location or variable where data


Eg scanf("%d%d",&a,&b);

%d used for integers


%f floats
%l long
%c character
for formatted output you use printf
printf("control string", arg1, arg2,...argn);

/* program to exhibit i/o */

#include<stdio.h>
main()
11 | P a g e
{
int a,b;
float c;
printf("Enter any number");
a=getchar();
printf("the char is ");
putchar(a);
printf("Exhibiting the use of scanf");
printf("Enter three numbers");
scanf("%d%d%f",&a,&b,&c);
printf("%d%d%f",a,b,c);
}

12 | P a g e

You might also like