Docsity

C programming unit 1, Study notes of Programming Languages

Toggle sidebar
bg1
KMEC/I/PPS/Unit-1
1
UNIT-I
INTRODUCTION TO COMPUTERS
A computer is an electronic machine that takes input from the user, processes the given input
and generates output in the form of useful information
A computer accepts input in different forms such as data, programs and user reply.
Programs refer to the set of instructions that can be executed by the computer in a
sequential or non-sequential manner.
Characteristics of Modern Computer:
Speed: Computer is a fast electronic device that can solve large and complex problems in
few seconds. The speed of a computer depends upon its hardware configuration.
Storage capacity: A computer can store huge amount of data in many different formats.
Computer storage is divided into 2 categories: Main memory and Secondary storage.
Accuracy: A computer carries out calculations with great accuracy. The accuracy achieved
by a computer depends upon its hardware configuration and the specified instructions.
Reliability: A computer produces results with no error. Most of the computer-generated
errors are in actuality human errors that are instigated by the user itself. Therefore,
computers are regarded as quite trustworthy machines.
Versatility: Computers are versatile machines. They can perform varied tasks and can be
used for many different purposes.
Diligence: Computers can perform repetitive calculations any number of times with the
same level of accuracy.
COMPONENTS OF COMPUTER
A computer is a system made of two major components: Hardware and Software.
The computer hardware is the physical equipment.
Computer software is the collection of programs that allows hardware to do its job.
Hardware component of the computer system consists of five components:
1. Input devices
2. Output devices
3. Central Processing Unit (CPU)
4. Primary storage devices
5. Auxiliary storage devices
Input device
It is a device where the programs and data
are entered into the computer.
Example: Keyboard, Mouse, Scanner.
Output device
It is a device to show output. The data
processed by the CPU, is made available to
the end user by the output devices.
Example: Monitor, Printer, Speaker.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download C programming unit 1 and more Study notes Programming Languages in PDF only on Docsity!

UNIT-I

INTRODUCTION TO COMPUTERS

A computer is an electronic machine that takes input from the user, processes the given input and generates output in the form of useful information  A computer accepts input in different forms such as data, programs and user reply.  Programs refer to the set of instructions that can be executed by the computer in a sequential or non-sequential manner. Characteristics of Modern Computer:Speed: Computer is a fast electronic device that can solve large and complex problems in few seconds. The speed of a computer depends upon its hardware configuration.  Storage capacity : A computer can store huge amount of data in many different formats. Computer storage is divided into 2 categories: Main memory and Secondary storage.  Accuracy : A computer carries out calculations with great accuracy. The accuracy achieved by a computer depends upon its hardware configuration and the specified instructions.  Reliability : A computer produces results with no error. Most of the computer-generated errors are in actuality human errors that are instigated by the user itself. Therefore, computers are regarded as quite trustworthy machines.  Versatility : Computers are versatile machines. They can perform varied tasks and can be used for many different purposes.  Diligence: Computers can perform repetitive calculations any number of times with the same level of accuracy. COMPONENTS OF COMPUTER A computer is a system made of two major components: Hardware and Software.  The computer hardware is the physical equipment.  Computer software is the collection of programs that allows hardware to do its job. Hardware component of the computer system consists of five components:

  1. Input devices
  2. Output devices
  3. Central Processing Unit (CPU)
  4. Primary storage devices
  5. Auxiliary storage devices Input device It is a device where the programs and data are entered into the computer. Example: Keyboard, Mouse, Scanner. Output device It is a device to show output. The data processed by the CPU, is made available to the end user by the output devices. Example: Monitor, Printer, Speaker.

Central Processing Unit (CPU) It is called as the brain of the computer. It is the processor of the computer that is responsible for controlling and executing instructions such as arithmetic calculations, comparisons among the data, and movement of data inside the system. Three components of CPU: Arithmetic and Logic Unit, Control Unit, Registers. Arithmetic and Logic Unit (ALU) ALU is a part of the CPU that performs arithmetic and logical operations on the data. It performs arithmetic operations such as addition, subtraction, multiplication, division etc. It performs logical operations such as greater than, less than, equal to, not equal to, left shift, right shift etc.  ALU takes input as an instruction that contains Opcode, Operands & Format code.  The output of ALU contains the result of the operation & the status of the result, whether it is final or not.  ALU stores the output in the CPU register. Control Unit (CU) Control Unit controls the flow of data and information.  It maintains the sequence of operations being performed by the CPU.  Control Unit guides the ALU about the operations that are to be performed.  Control unit suggests the I/O devices to which the data is to be communicated.  It uses a program counter register for retrieving the next instruction that is to be executed. Registers CPU Registers are high-speed memory locations used for holding instructions, data and intermediate results that are currently being processed. A processor can have different types of registers to hold different types of information.  Program Counter (PC) to keep track of the next instruction to be executed.  Accumulator (ACC) for storing the results produced by arithmetic and logic units.  Memory Data Register (MDR) for storing operands and data.  Memory Address Register (MAR) to hold the address of the next location in the memory to be accessed. Note: Buses are electrical pathways that transfer data and instructions among different parts of the computer. (i.e) transfers data among the processor to main memory devices and input/output devices connected to the computer. Memory Unit The memory unit of a computer is used to store data, instructions. The memory units of a computer are classified as primary memory and secondary memory. Main memory or Primary storage  It is a place where the programs and data are stored temporarily during processing.  The data in primary storage are erased when we turn off a personal computer or when we log off from a time-sharing system.  Examples: Random Access Memory (RAM), Read-Only Memory (ROM).

The purpose of an operating system is to provide an environment in which a user can execute programs in a convenient and efficient manner.  The operating system manages the resources such as CPU time, memory space, file-storage space, I/O devices and so on.  An operating system is a control program that manages the execution of user programs to prevent errors and improper use of the computer. Compilers  The process of converting high-level language code into machine language code is known as compilation.  A compiler is a software program that transforms high-level language source code into a low level object code (binary code) in machine language.  Machine language code is understood by the processor.  High level language code understood by user. COMPUTER LANGUAGES Computer Languages are categorized into 3 levels:

  1. Low level language or Machine language
  2. Symbolic language or Assembly language
  3. High level language or Procedural oriented language Machine Language  Computers only understands machine code or machine language.  Machine code consists of combination of 0's and 1's called binary data.  All instructions and data should be written using binary codes 1 and 0.  Machine language is difficult to understand and remember the various combinations of 1 ’s and 0 ’s representing numerous data and instructions.  Machine languages are usually referred to as the first generation languages. Assembly Language  The Assembly languages are introduced in 1950s.  They reduce programming complexity.  The assembly languages second-generation programming languages.  An assembly language program consists of a series of instructions and mnemonics that correspond to a stream of executable instructions.  Assembly instructions consists of an opcode followed by zero or more operands.  Opcode specifies the operation to be performed on the given arguments. Example: mov AL, 061h.  Opcode “mov” is used to move the hexadecimal value 61 into the processor register ‘AL’.  Note: During the execution, all the assembly language statements are converted to machine language statements by using Assembler program. High-Level Languages In High level language, the programs are written in human understandable languages like English. Example: C, C++, JAVA, PYTHON, FORTRAN, COBAL etc.

 Compilers are used to convert the high level language code to machine code. That means it converts alphabets and digits and other special characters to 0's and 1's.  Some programming languages uses interpreter to convert to machine code. Let the instruction: C=A+B  A, B, C are memory locations and the values of A and B are added and stored in C.  Programmers don’t need to manage memory addresses, CPU registers, or binary code directly.  Debugging is easy in high level languages. CREATING AND RUNNING PROGRAMS It is the job of a programmer to write and run the program. This process involves four steps.

  1. Creating and Editing program
  2. Compiling the program
  3. Linking the program
  4. Executing the program Creating a program  The program must be written into a file using "Text Editor" software.  The file name must ends with ".c" extension. Ex: welcome.c  A text editor helps us to write, modify and store character data.  After we complete the program, we save the file to disk. This file is called "Source File" (welcome.c).  The source file is given as an input to the compiler.

 Hence, there should be continuous interaction between the software development team and the end users.  Proper analysis of user requirements is quite essential for developing the software within a given timeframe.  It is used to control the software development cost.  It lead to faster and accurate development of a software. Feasibility Analysis Here, we will check whether the proposed software project is practical to implement or not.  In order to determine the feasibility of software development, the existing system of the user is analyzed properly.  Here we can understand requirements from user, collects background information, analyzing technical , economical, time schedule feasibility, and identify any risks or limitations in of the software project.  The analysis done in this step is documented as a report called feasibility report. Creating the Design This step involves developing a logical model or basic structure of the new software.  This step involves documenting the various specifications related to database and data structure design using UML diagrams.  The software design should be flexible and extensible.  Software should be modular in nature, so we can develop multiple module at the same time.  Software should run correctly in the existing system with an older version or with other software.  Software design must ensure secure and fault tolerant. Developing the Code According to the design specifications, the code for the different modules are developed.  The code can be written using programming languages such as C, C++ or Java.  The code must follow Proper naming conventions, proper indentation, and write proper comments,.  The code must be easily understood and logically correct.  The programmers use debugging tools to find errors in the code.  The programmers use compilers, interpreters to convert the code into machine language for its execution. Testing the Software Testing is used to detect any errors in the new software and rectify those errors.  verifi cation and validation activities will be performed during testing.  Verifi cation is the process of checking the software based on some predefined specifi cations.  Validation involves testing the product to ascertain whether it meets the user’s requirements so that we willg get correct output.

 We use various testing such as unit testing, integration testing, black box testing,white box testing etc. Deploying the Software In this step, we deliver the software to the end user.  That means software is installed in its target environment.  Software documentation is handed over to the users.  Some initial data are entered in the software to make it operational.  The users are also given training on the software’s interface and its other functions. Maintaining the Software It ensures that the software continues to run smoothly after its deployment.  During this phase, developers fix bugs reported by users, update the software to enhance security and improve performance, and add new features when required.  The outcome of this phase is an updated and improved version of the software that meets evolving user needs and technological requirements. COMPUTING ENVIRONMENTS There are several computing environment are built for different computing purposes:

  1. Personal computing
  2. Mobile computing
  3. Client server computing
  4. Distributed computing Personal computing It refers to the use of a single computer or workstation by an individual.  This setup is typically designed for personal tasks such as document creation, web browsing, gaming, and personal software development.  Example: Desktop PCs & laptops running operating systems like Windows, Mac, or Linux. Mobile Computing Mobile computing refers to computing on handheld smartphones and tablet computers.  Mobile devices are portable and lightweight.  As compared with desktop and laptop computers, mobile systems have small screen size, less memory capacity, less processing speed and also consumes less power consumption.  Operating systems like Apple iOS and Google Android are used in mobile devices.  Mobile systems are most widely used for taking photos & videos, e-mail and web browsing, Global positioning system (GPS), accelerometers and gyroscopes, etc. Client–Server Computing Client – Server computing is a Centralized system in which Server systems are used to service the requests generated by client systems. PC’s and mobile devices are connected to the Centralized system through internet connection. Example: A web browser (client) in a mobile devices requesting web pages to web server (Google server).

FLOW CHARTS

A Flow chart is a pictorial representation of an Algorithm.  The diagrammatic representation to solve the given problem is called Flow chart.  The flow of information is represented inside the flowchart in a step-by-step form.  Flowchart uses different symbols for depicting different activities. Name Representation Description Start and End (^) Starting and Ending of a process is represented by an oval or a rounded rectangle. Every flow chart must have start and end symbols. Input / Output It is represented by a parallelogram. All Input and output statements of process are written inside parallelogram. Action / process / calculations It is represented by a rectangle. All the statements related to calculations, actions, logics are written inside rectangle. Decision/ condition All the conditional statements are written inside a rhombus. Ex: if a>0. The result is either true or false. Each of these values takes the flow of the program to a different direction. Arrow Arrow represents the flow of process and the sequence of steps in a flowchart. Connector It is represented by a circle in a flowchart. It represents the continuation of the flow of steps when a flowchart continues to the next page. A character such as an alphabet (a-z or A-Z) can be placed in the circle at the position where the flow is broken and the same character is also placed in the circle at the position from where the flowchart continues. Rules for Designing a Flowchart  It must begin with a “Start” and end with a “Stop” symbol.  The standard process flow should be either from top to bottom or from left to right.  The instructions specified in the flowchart must be simple and concise.  The arrows must be aligned properly so as to clearly depict the flow of program control.  A process or action flowchart symbol must have only one input arrow and one output arrow.  Use of connectors should be generally avoided as they make the program more complex. Advantages of Flowcharts  Visual representations gives better understanding of problem statement.  It helps to understand the flow of program control in an easy way.  It is easy to develop program code using flow charts.  It helps in avoiding semantic errors.  The use of flowcharts works well for small program design.

Disadvantages of Flowcharts  For a large program, the flow chart might become very complex and confusing.  Modification of a flowchart is difficult and requires lot of rework.  It takes a lot of time to create a flowchart.  Excessive use of connectors in a flowchart may confuse the programmers. PSUDO CODE Pseudo code only focuses on the logic of the program.  Pseudo code does not have any specific syntax, we can write it in general English language.  Pseudo code helps the programmer understand the basic logic of the program.  Once we write a pseudo code, a programmer can select any programming language to write the source code of program. Rules for writing Pseudo code  Instruction or operation code in a pseudo code statement must be kept all capitalized;  Example: READ (specifying input operation), PRINT (specifying print command) etc.  The ending of looping and decision-making constructs must be labelled appropriately with corresponding “END” keywords.  The pseudo code instructions must be properly indented just like the statements are indented in a computer program.  Convoluted programming instructions must be avoided by writing simple and clear instructions. Advantages of Pseudo code  It is easy to understand as it uses English phrases for writing program instructions.  Developing program code using pseudo code is also easier in comparison to flowcharts.  Modifying the pseudo code instructions are easier.  The use of pseudo code works well for large program design. Disadvantages of Pseudo code  There is no standard format for developing a pseudo code. Therefore, it may become a challenge to use the same pseudo code by different programmers.  Since pseudo code does not use any kind of pictorial representations for program elements, sometimes, it may become difficult to understand the program logic.  Pseudo codes are at a disadvantage in comparison to flowcharts when it comes to understanding the flow of program control. Write an algorithm to find the average of marks obtained by a student in three subjects.

  1. Start
  2. Read the marks of three subjects from the user into marks1, marks2, marks3.
  3. Calculate average = (marks1 + marks2 + marks3)/
  4. Display average marks
  5. Stop

Draw a flowchart to find out whether a given number is prime or not.

NUMBER SYSTEMS

There are four types of Number systems are used by computers:

  1. Decimal system
  2. Binary system
  3. Octal system
  4. Hexadecimal system Decimal System  It is base 10 number system, represented as (X) 10.  There are total of 10 symbols (0-9) used 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.  Each digit’s position represents a power of 10.  The decimal system can be used to represent both the integer and floating point values.  Floating point values are represented by using a dot (.) called decimal point. Binary system

 The binary system is base 2, represented as (X) 2.

 There are total of 2 symbols used in Binary system 0 &1.  It is used by computers to represent data.  Each digit’s position represents a power of 2. Octal system  The Octal system is base 8, represented as (X) 8.  Total of 8 symbols (0-7) are used in octal system 0, 1, 2, 3, 4, 5, 6, 7.  Each digit’s position represents a power of 8. Hexa Decimal system

 The Hexa Decimal system is base 16, represented as (X) 16.

 Total of 16symbols used 0-9 and A-F. (i.e.) 0, 1, 2, 3, 4, 5, 6, 7,8,9,A,B,C,D,E,F.  Here A-10, B-11, C-12, D-13, E-14, F-15.  Each digit’s position represents a power of 16. NUMBER SYSTEM CONVERSION The following conversions can be done:

  1. Decimal to Binary
  2. Decimal to Octal
  3. Decimal to Hexa Decimal
  4. Binary to Decimal
  5. Binary to Octal
  6. Binary to Hexa Decimal
  7. Octal to Decimal
  8. Octal to Binary
  9. Octal to Hexa Decimal
  10. Hexa Decimal to Decimal
  11. Hexa Decimal to Binary
  12. Hexa Decimal to octal

Note: If the left most group does not have 3 bits then we prefix 0 ’s to the left over bits in that group so that it will become a group of 3. Example_2: Convert the binary number (1101101) 2 to Octal number. Group 3 bits from the right side: (1) (101)(101). The left most group has only one bit, hence we need to prefix 2 bits of 0 ’s, to make it a group of 3-bits. Therefore, (001) (101) (101) hence, 001 1, 101 5, 101  5 Hence (1101101) 2 = (155) 8 Binary to Hexa-Decimal conversion To convert a binary number to Hexa-Decimal number we need to group of 4 bits from the right side. The Hexa-Decimal values are ranging from 0-9 and A-F. Hence, a group of 4 bits ranges from 0000 to 1111 represents 0-F as shown below. 0 - 0000 4 - 0100 8- 1000 C- 1100 1 - 0001 5 - 0101 9- 1001 D- 1101 2 - 0010 6 - 0110 A- 1010 E- 1110 3 - 0011 7 - 0111 B- 1011 F- 1111 Example_1: 10110001 We can group 4 bits from right, hence we divide them into (1011) (0001). 1011 B and 0001 1. Hence , (10110001) 2 = (B1) 16 Note: If the left most group does not have 4 bits then we prefix 0 ’s to the left over bits in that group so that it will become a group of 4. Example_2: Convert the binary number (1101101) 2 to Hexa-Decimal number. Group 4 bits from the right side: (110)(1101). The left most group has only 3 bits, hence we need to prefix 1 bit of 0 ’s, to make it a group of 4-bits. Therefore, (0110)(1101) Hence 0110 6, 1101 D Hence (1101101) 2 = (6D) 16 Octal to Decimal Conversion To convert from octal to decimal system, we need to multiply each digit with powers of 8. Example: Let the octal number be (354) 8. =(38^2 ) + (58^1 ) + (48^0 ) =(364) + (540) + (41) =192 + 200 + 4 =396. Answer: (354) 8 = (396) 10 Octal to Binary conversion To convert from octal to binary, we need to write the 3-bit binary value for each octal digit. Example: Convert (354) 8 to binary value. (3) 8 – 011 (5) 8  101 (4) 8  100 Answer: (354) 8 = (011101100) 2

Hexa Decimal to Decimal conversion To convert from octal to decimal system, we need to multiply each digit with powers of 16. Example: Let the hexa decimal value be (45F) (^16) =(416^2 ) + (516^1 ) + (F16^0 ) =(4256) + (516) + (151) =1024 + 80 + 15 =1119. Answer: (45F) 16 =(1119) 10. Hexa Decimal to Binary conversion To convert from octal to binary, we need to write the 3-bit binary value for each octal digit. Example: Convert (35F) 16 to binary value. (3) 16  0011 (5) 16  0101 (F) 16  1111 Hence (35F) 16 = (001101011111) 2 Octal to Hexa Decimal conversion We need to follow two steps to convert from octal to hexa decimal:

  1. Convert Octal to Binary
  2. Convert Binary to Hexa decimal Example: Convert octal value (354) 8 to Hexa Decimal. Step_1: Convert (354) 8 to binary value. (3) 8  011 (5) 8  101 (4) 8  100 (354) 8 = (011101100) 2 Note: We can delete the leading 0 ’s, hence the answer is: (354) 8 = (11101100) 2 Step_2: Convert (1110 1100) 2 to Hexa decimal. Group 4 bits from the right side: (1110)(1100). Therefore, (0110)(1101) hence 1110  E & 1100  C Hence (11101100) 2 = (EC) 16 Final answer: (354) 8 = (EC) 16 Hexa Decimal to Octal conversion We need to follow two steps to convert from octal to hexa decimal:
  3. Convert Hexa decimal to Binary
  4. Convert Binary to octal decimal Example: Convert Hexa Decimal value (EC) 16 to Octal. Step_1: Convert (EC) 16 to binary value. (E) 16  1110 (C) 16  1100 (EC) 16 = (11101100) 2 Step_2: Convert (11101100) 2 to Octal. Group 3 bits from the right side: (11) (101) (100). Add a 0 bit to the leftmost group to make it group of 3 bits. Therefore, (011) (101) (100) 011- 3, 101-5, 100- 4 Hence (11101100) 2 = (354) 8 Final answer: (EC) 16 = (354) 8

main() section: Every C program must have one main() function. main() contains two parts, variable declaration part and executable part. Declaration part declares all the variables used in the executable part. Opening and the closing braces { } are used to define the scope of main( ). The program execution begins at the opening brace and ends at the closing brace. All statements in declaration and executable parts end with a semicolon (;).  Subprogram section contains all the user-defined functions that are called in the main(). Note: main( ) section is mandatory and all remaining sections are optional. CHARACTER SET The characters in C are grouped into the four categories:

  1. Letters: Upper case (A-Z) , Lower case (a-z)
  2. Digits: 0-9 (0,1,2,3,4,5,6,7,8,9)
  3. Special characters: &! @ # $ % ^ * ( ) + - / *. etc.
  4. White spaces: Blank space, horizontal tab, new line etc. C TOKENS In a C program the smallest individual units are known as tokens. C has six types of tokens:
  5. Keywords
  6. Identifiers
  7. Constants
  8. Strings
  9. Special symbols
  10. Operators KEYWORDS A keyword in C language have fixed meaning. There are 32 keywords in C language.  Keywords serve as basic building blocks for program statements.  All keywords must be written in lowercase.  We cannot use keywords for variable names. auto const double float int short struct unsigned break continue else for long signed switch void case default enum goto register sizeof typedef volatile char do extern if return static union while IDENTIFIERS Identifiers refer to the names of variables, functions and arrays. These are user-defined names and consist of a sequence of letters, digits, underscore symbol. Rules for defining an identifier:
  11. First character must be an alphabet (or underscore).
  12. Must consist of only letters, digits or underscore.
  13. Only first 31 characters are significant.
  14. We cannot use a keyword as identifier.
  15. Identifier must not contain white space.
  16. The underscore character is used to connect to words to make it a single name. Example: first_name, last_name. Date_of_birth, string_length() etc.

CONSTANTS

Constants in C refer to fixed values that do not change during the execution of a program.

  1. Integer constants
  2. Real constants
  3. Single character constants
  4. String constants Integer constants An integer constant refers to a sequence of digits. There are three types of integers constants: Decimal, Octal, Hexadecimal integer.  Decimal: 0,1,2,-15,17,-324,65535 etc.  Octal: An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0. Example: 037,0, 0435, 0551, etc.  Hexadecimal: A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. 0X2, 0x9F, 0Xbcd, 0x, etc. Real constants These are the numbers with a decimal point or in exponential form.  They are also called as Floating point constants.  Example: 0.0083, – 0.75, 435.36, +247.0 etc.  A real number can be expressed in exponential (or scientific) notation.  Example: 3.14, -1.05, 2.5e3 (which means 2.5 × 10 ³).  Note: In 2.5e3, 2.5 is called mantissa and 3 is called exponent. Single Character Constants It contains a single character enclosed within a pair of single quote marks.  Example: ‘ 5 ’ ‘X’ ‘;’ ‘ ’  Note: Anything that are enclosed with in a single quote becomes a character, hence '5' is not a number it is an ascii character.  Each character constant is associated with an ASCII value. String constants A string constant is a sequence of characters enclosed in double quotes.  The characters may be letters, numbers, special characters and blank space.  Example: “Hello!” “ 2025 ” “Good Morning” “What is your name?” “5+3” “X”  String constants may contain one more characters inside double quotes.  Hence "X" is a string constant, not single character constant. Backslash Character Constants Backslash character constants are used in output functions.  Backslash constants also called Escape sequences.  Each character is preceded by backslash( \ )  Example: '\n' : New line, '\t : horizontal tab, ‘\v’ : vertical tab, ‘\0’ : null etc.