programing in c language, Lecture notes of Computer Science

programming in c language for beginers

Typology: Lecture notes

2018/2019

Uploaded on 08/02/2019

siva-sai-krishna-patukuri
siva-sai-krishna-patukuri 🇮🇳

1 document

1 / 78

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PROGRAMMING IN C
Compiled By –
SIVA SAI KRISHNA.PATUKURI, M.C.A.
PH: 7396451713.
NAME :
……………………………………………….
SEMISTER :
……………………………………………….
SUBJECT :
……………………………………………….
CELL NO :
……………………………………………….
(Opp: Vignan High School, Jagadamba center, Rayachoty)
EMAIL:[email protected], Cell: 9676320007
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
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e

Partial preview of the text

Download programing in c language and more Lecture notes Computer Science in PDF only on Docsity!

PROGRAMMING IN C

Compiled By –

SIVA SAI KRISHNA.PATUKURI, M.C.A.

PH: 7396451713.

NAME :

SEMISTER :

SUBJECT :

CELL NO :

( Opp: Vignan High School, Jagadamba center, Rayachoty)

EMAIL:[email protected], Cell: 9676320007

PROGRAMMING IN C

II Semester Examinations - Model Question Paper Time: 3 Hours Max. Marks: 75

PART - A 5 × 5 marks = 25

  1. Define Type Conversion and Type Casting?
  2. What are the basic data types used in C?
  3. Describe the Function Declaration with example?
  4. Differentiate Break and Continue statements?
  5. How to Declare an Array? Give an example.
  6. Briefly describe few String Functions?
  7. What are the drawbacks of Pointers?
  8. Define Command Line arguments? PART - B 5 × 10 marks = 50
  9. i) Discuss about the Structure of C Program with neat diagram? ii) What are the rules followed in declaring a C Variable? Give an example. OR
  10. Write a Sample program in C which explains the usage of I/O Statements in C?
  11. i) List and explain all Storage Classes available in C? ii) Explain the Usage of Functions in C? OR
  12. Distinguish the Decision Control and Looping Statements with an example Program?
  13. i) Write a Program to find the Maximum of an array? ii) Describe and Differentiate String and Character functions? OR
  14. Write a Program for performing Matrix multiplication using two dimensional arrays?
  15. i) How to declare a Pointer Variable? Explain its usage with sample program? ii) Explain the Merits and Demerits of Enumerated Data Types? OR
  16. What are the key roles played by Structures and Unions in C programming?
  17. i) How to Read and Write data into Files? Explain it with sample program? ii) Explain the role of Command Line Arguments in C? OR
  18. i) Explain the Error Handling mechanisms during File Operations in C? ii) Write a sample Program which creates a Temporary File?

Introduction to Algorithms

What is an algorithm? Although there is no universally agreed-on wording to describe this notion, there is a general agreement about what the concept means:

SIVA SAI KRISHNA.PATUKURI
M.C.A^2

COMPILED BY:- SIVA SAI KRISHNA.PATUKURI (^) M.C.A

Αλλ οφ τηοσε μετηοδσ λογιχσ μαψ νοτ βε γοοδ, φορ γιϖεν προβλεμ; τηερε 0 0 B 5

0 0 B 5

0 0 αψ βε σο ανψ αλγοριτηB 5 σ νοτ οφ αλλ εθυαλιτψ. 0 0 Τηε φολλοωινγ αρε τηε πριB 5 αρψ 0 0 φαχτορσ τηατ αρε οφτεν υσεδ το ϕυδγε τηε θυαλιτψ οφ τηε αλγοριτηB 5 σ.

▲ Τι0 0B 5 ε

▲ Μεμορψ

▲ Αχχυραχψ

▲ Σεθυενχε

  1. Τι 0 0B 5 ε: Το εξεχυτε α προγρα0 0B 5 , τηε χο0 0B 5 πυτερ σψστε0 0B 5 τακεσ σο0 0B 5 ε α0 0B 5 ουντ 0 0 B 5

0 0 B 5

0 0 οφ τι ε. Τηε λεσσερ τηε τι ε ρεθυιρεδ, τηε βεττερ ισ τηε αλγοριτηB 5.

  1. Με 0 0B 5 ορψ: Το εξεχυτε α προγρα0 0B 5 , τηε χο0 0B 5 πυτερ σψστε0 0B 5 τακεσ σο0 0B 5 ε 0 0 B 5

0 0 B 5

0 0 B 5

0 0 B 5

0 0 α ουντ οφ ε ορψ 0 0 στοραγε. Τηε λεσσερ τηε εB 5 ορψ ρεθυιρεδ, τηε βεττερ ισ τηε αλγοριτηB 5.

  1. Αχχυραχψ: Μυλτιπλε αλγοριτη0 0B 5 σ 0 0B 5αψ προϖιδε συιταβλε ορ χορρεχτ 0 0 B 5

0 0 B 5

0 0 B 5

0 0 σολυτιονσ το α γιϖεν προβλε , σο ε οφ τηεσε αψ προϖιδε (^) B 5ορε αχχυρατε ρεσυλτ τηαν οτηερσ.

  1. Σεθυενχε: Τηε προχεδυρε οφ αν αλγοριτη0 0B 5 0 0B 5υστ φορ0 0B 5 ιν α σεθυενχε ανδ 0 0 B 5

0 0 B 5

0 0 B 5

0 0 σο ε (^) 0 0οφ τηε ινστρυχτιονσ οφ αν αλγοριτη αψ βε ρεπεατεδ ιν νυB 5 βερ B 5

0 0 οφ τι εσ ορ υντιλ α παρτιχυλαρ χονδιτιον ισ (^) B 5ετ.

0 0 B 5

0 0 Θ) Ωριτε αν αλγοριτη το φινδ ουτ τηε σι B 5 πλε ιντερεστ? 0 0 Σι B 5 πλε ιντερεστ (ΣΙ) =ΠΤΡ/

  1. Σταρτ
  2. Ρεαδ Π,Τ,Ρ ϖαλυεσ
  3. Χαλχυλατε ΣΙ=ΠΤΡ/
  4. Ωριτε ΣΙ
  5. Στοπ 0 0 B 5

0 0 Θ) Ωριτε αν αλγοριτη το φινδ ουτ τηε Βιγγεστ οφ τωο νυ B 5 βερσ?

  1. Σταρτ
  2. Ρεαδ Α,Β
  3. Ιφ Α> Β τηεν

Βιγ=Α

SIVA SAI KRISHNA.PATUKURI
M.C.A^4

COMPILED BY:- SIVA SAI KRISHNA.PATUKURI (^) M.C.A

Οτηερ ωισε

Βιγ=Β

  1. Ωριτε Βιγ
  2. Στοπ

Q) What is flow chart? Explain different flowchart symbols?

  • A flowchart is diagrammatic representation that illustrates the sequence of operations to be performed to arrive at the solution. The operating instructions are placed in boxes which are connected by arrows to include the order of execution. Flowchart is a pictorial representation of an algorithm, it is the layout ,visual representation of the plan to be followed of the corresponding algorithm.
  • Flowchart Symbols: The flowchart symbols as given below are as per connection followed by International Standard Organizational (ISO).

Symbol

Description

Flow-lines: These are the left to right or top to bottom lines connecting

symbols. These lines show the flow of control throw the program.

Terminal symbol: The oval symbol is used always begins and ends the

flowchart. It is the first and last symbol of the program logic of

flowchart.

Input/output symbol : The parallelogram is used for both input(read) and

output(write),such that this symbol is used to denote any function of an

I/O device in the program

Process Symbol : The rectangle symbol is used primarily for calculations

and initialization of memory locations. All the arithmetic operations are

performed by using this symbol.

Decision Symbol : The diamond shaped symbol is used in a flowchart to

indicate at a point at which a decision has to be made and a branch to one

of two or more alternative point is possible.

nn

Connectors: A connector symbol is represented by a circle and a letter or

digit is placed in the circle to specify the link.

Calculate total and average

Write name, class, total, average

Pseudo code:

Read name, class, m1, m2, m

Total=m1+m2+m

Average=total/

Write name, class, total, average

Q) Types and Classifica�on of programming languages:

A language is a communica�on tool, which is helps the user to interact with computer by wri�ng programs. There are three types of languages

  1. MACHINERY LANGUAGE
  2. ASSEMBLY LANGUAGE
  3. HIGH LEVEL LANGUAGE Machinery Language:

The one and only one language, which the computer itself can understand is the

machinery language. A machinery language is the set of instructions, which are coded

as a series of binary form (0’s and 1’s). Writing programs in machine language is very

difficult. Further the machine language is machine dependant and the instruction in

the machine language will differ from machine to machine.

Assembly Language:

Assembly language is the language over the machine language. It uses certain

predefined symbolic code called as mnemonic instead of binary code. Mnemonic

codes are very much condensed from of English words like Add for addition, Sub

for subtraction etc. It is easy to remember for writing program. In assembly language

easy and less time consuming as compared to machinery language. It is also

machinery dependent and various from computer to computer. In assembly language

an assembler is used to convert all the instructions into machinery language.

High Level Language:

The high level language is universal and user friendly, it is similar to English language and hence better understands and easier and less time consuming as compare to assembly language and machinery language.

It is machinery independent and can be used in different type of computers. Compiler or Interpreters are used to convert the source program into object program.

Q) Difference between POP and OOP?

  • Top down approach in program design. • Bottom up approach in program design.
  • (^) No access specifiers are used. Example: In structure all the members are public. - (^) Public,Private,protected Access specifiers are used. Example:In classData is Private and Functions are public.
  • Larger programs are divided into Functions. - Larger programs are divided into entites known as objects.
  • (^) Most of the Functions share global data means data move openly around the system from one Function to another Function. - (^) Data is private and Functions inside class can only access the private data.
  • Example of POP is C, FORTRAN.Example of OOP is C++.java
  • (^) In POP operator cannot be overloaded. • (^) In OOP operator can be overloaded by using the concept of operator overloading.
  • Objects are very difficult to add in POP.Objects ( i.e data and Functions ) are very easy to add at any time whenever necessary.
  • Procedural in nature.Non-Procedural in nature.

Features of Programming Languages

There are some conventional features which a programming language must

possess, these features are:

  • It must have syntactic rules for forming statements.
  • It must have a vocabulary that consists of letters of the alphabet.
  • It must have a language structure, which consists of keywords,

expressions and statements.

  • It may require a translator before it can be understood by a computer.
  • Programming languages are written and processed by the computer

for the purpose of communicating data between the human being and

the computer.

3.4 Methodologies or different category of high level languages?

Some programming methodologies are stated below:

  • Procedural Programming : A procedural program is a series of steps, each of

which performs a calculation, retrieves input, or produces output. Concepts like assignments, loops, sequences and conditional statements are the building blocks of procedural programming. Major procedural programming languages are COBOL, FORTRAN, C, AND C++.

  • Object-Oriented (OO) Programming : The OO program is a collection of objects

that interact with each other by passing messages that transform their state. The

SIVA SAI KRISHNA.PATUKURI
M.C.A^8

COMPILED BY:- SIVA SAI KRISHNA.PATUKURI (^) M.C.A

Object Program 0101 10101010 0100 00001101

  1. Third – Generation Language : These are written in English with symbols and digits. Then are known as High level language (HLL). common high level languages are c,c++, COBOL, BASIC, FORTRAN, PASCAL, etc.

For execution the program is translation into binary form by compiler or interpreter.

Source program

Compiler

Object Prog.

Linker

Executable Program

Run the program

  1. Fourth – Generation Language (4GL‟s) : is a programming language or

programming environment designed with a specific purpose in mind, such as the

development of commercial business software. In the history of computer science,

the 4GL followed the 3GL in an upward trend toward higher abstraction and

statement power. The 4GL was followed by efforts to define and use a 5GL.

History of C Programming Language

  1. C is a programming language which born at “AT & T’s Bell Laboratory” of USA in 1972.
  2. C was written by Dennis Ritchie, thats why he is also called as father of c programming language.
  3. C language was created for a specific purpose i.e designing the UNIX operating system (which is currently base of many UNIX based OS).
  4. From the beginning, C was intended to be useful to allow busy programmers to get things done because C is such a powerful, dominant and supple language
  5. Its use quickly spread beyond Bell Labs in the late 70’s because of its long list of strong features

C Programming Language Timeline :

Programming Language Development Year Developed by

SIVA SAI KRISHNA.PATUKURI
M.C.A^10

COMPILED BY:- SIVA SAI KRISHNA.PATUKURI (^) M.C.A

ALGOL 1960 International Group

BCPL 1967 Martin Richards

B 1970 Ken Thompson

Traditional C 1972 Dennis Ritchie

K&R C 1978 Brain Kernighan and Dennis Ritchie

ANSI C 1989 ANSI Committee

ANSI/ISO C 1990 ISO Committee

C99 1999 Standardization Committee

C Language Features

C Programming is widely used in Computer Technology, We can say that C is inspiration for development of other languages. We can use C for different purposes. Below are some of the Features of C Programming –

Features of C Programming Language:

1. Low Level Features:

C Programming provides low level features that are generally provided by the Lower

level languages. C is Closely Related to Lower level Language such as “Assembly

Language“.

It is easier to write assembly language codes in C programming.

2. Portability :

C Programs are portable i.e they can be run on any Compiler with Little or no

Modification

Compiler and Preprocessor make it Possible for C Program to run it on Different PC

3. Powerful

Provides Wide verity of ‘Data Types‘

Provides Wide verity of ‘Functions’

Provides useful Control & Loop Control Statements

4. Bit Manipulation

C Programs can be manipulated using bits. We can perform different operations at

bit level. We can manage memory representation at bit level. [Eg. We can use

Structure to manage Memory at Bit Level]

To write a C program, we first create functions and then put them together. A C

program may contain one or more sections. They are illustrated below.

  1. Documentation section : The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later.
  2. Link section : The link section provides instructions to the compiler to link functions from the system library.
  3. Definition section : The definition section defines all symbolic constants.
  4. 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.
  5. main () function section : Every C program must have one main function section. This section contains two parts; declaration part and executable part
  6. Declaration part : The declaration part declares all the variables used in the executable part.
  7. Executable part : There is at least one statement in the executable part. These two parts must appear between the opening and closing 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.
  1. (^) 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.

All section, except the main () function section may be absent when they are not

required.

Rules to write C program

  1. C program should be written in lower cased (small letters)
  2. C is case sensitive (‘a’ is different from ‘A’)
  3. (^) main() is compulsory in all the program
  4. stdio.h header file is compulsory in all the programs.
  5. All the variables in the program should be declared after the main() starts with their respective data type. Q) What are the files used in C Program? Every C Program has four kinds of files. They are source file, header file, object file, and executable file.

Source file: The source file contains the source code of the program. The file extension of any C source code file is ‘.c’. This file contains C source code that defines the main function and may be other functions. The main() is starting point of execution when you successfully compile and run the program. A C program may include other source code files with the file extension ‘.c’.

SIVA SAI KRISHNA.PATUKURI
M.C.A^14

COMPILED BY:- SIVA SAI KRISHNA.PATUKURI (^) M.C.A

C Character Set

A Character Set denotes any alphabet digit or special symbols used to

represent information. Following shows the valid alphabets, numbers and special

symbols allowed in C.

Alphabets A - Z a - z

Numbers 0 - 9

Special Symbols ~ `! @ # $ % ^ & * ( ) - _ = + [ ] { } \ | ; : ‘ " ,. < > /?

Tokens

Identifiers: The names of variables, functions and arrays are identifiers. These are

user-defined names and consist of a sequence of letters and digits. Such as,

my_num, _ton etc.

Rules for identifier:

  1. Must consist of only letters, digits and underscores.
  2. First character must be an alphabet or underscore.
  3. Only first 31 characters are significant.
  4. Cannot use a keyword.
  5. Must not contain white spaces.

C Keywords

  1. Keywords are the word whose meaning (defined) is already present in the Compiler. These are also called as reserved words it has meaning in the user environment.
  2. The keywords cannot be used as a variable name or function name because if we do so, we are trying to assign a new meaning to the keyword, which is not allowed by the compiler.
  3. There are totally 32 keywords in C.
  4. Keywords should be lowercased.
  5. Keywords are also called "Reserved words".
  6. An important aspect of C is that it has minimal number of keywords with the formal C syntax form the C programming language.
  7. (^) The 32 keywords are: auto doubleint struct

break else long switch

case enum register typedef

SIVA SAI KRISHNA.PATUKURI
M.C.A^16

COMPILED BY:- SIVA SAI KRISHNA.PATUKURI (^) M.C.A

char extern return union

const float short unsigned

continue for signed void

defaultgoto sizeof volatile

do if static while

Q) Variables in C?

A variable is a value. The value can be change at the time of program

execution.

Syntax: variable name;

Example: Int a=10;

Rules to name Variable

  1. A variable name should start with an alphabet or an underscore not number.
  2. Keyword should not be used as variable name.
  3. No comma or space allowed in between the variable name.
  4. No special characters allowed other than an underscore

Valid and Invalid Variable Name

Valid Invalid

rollno roll-no

roll_no roll no

sum 2a

b3 int

_b3 acc’ts

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

Constants can be defined as symbolic constant, because it is easy to refer the value whenever it becomes necessary Ex:

Valid examples are: 0 +47 17 -

(ii) Octal Integer Constants Octal integer constants can consist of any combinations of digits from 0 to 7. However, the first digit must be zero (0), in order to identify the constant as an octal number. For instance, decimal integer 8 will be written as 010 as octal integer.

Valid examples are: 0 047 017 0240

(iii) Hexadecimal Integer Constants Hexadecimal integer constants can consists of any combination of digits from 0 to 9 and letters from A to F (either uppercase or lowercase). A hexadecimal integer constant must begin with either 0x or 0X. For instance, decimal integer 12 will be written as 0XC as hexadecimal integer.

Valid examples are: 0x 0X2 0x7A 0xbcd

Real Constants Real constants are also called as floating point constants. Real constants are numbers having fractional parts. These may be written in one of the two forms :

5.i. Fractional form

(ii) Exponential form

(i) Fractional Form The fractional form consists of a series of digits representing the whole part followed by a decimal point and series of digits representing the floating part. The whole part or fractional part can be omitted but not both. The decimal point cannot be omitted.

Valid examples are :

(ii) Exponential Form The exponential (or scientific) form of representation of real constants is usually used if the value of the constant is either too small or too large. In exponential form of representation, the real constant is represented in two parts namely, mantissa and exponent. The syntax is :

For example, 3.5 can be written as 0.35 x 10 = 0.35E1 where the mantissa part is 0.5 (the part appearing before E) and the exponent part is 1 (the part appearing after E).

Single Character Constants A single character constant or a character constant consist of a single character encloses within apostrophes.

Valid examples are :

‘B’ ‘V’ ‘+’ ‘$’ ‘ ’

String Constants

A string constant is a sequence of characters encloses within double quotation marks. Technically, a string is an array of characters. The compiler automatically places the null character (\0) at the end of each such string, so program can conveniently find the end.

Valid examples are :

“CRRDC” “2002” “BABJAN SHAIK”
.” “B” “$120”

Q) Data types in C Language?

Data type means which types of data you want to store in to the variable is called as

data type. It is also called as memory object. By using the data type to allocate the memory to

the single variable.

C language is rich in its data types. ANSI supports three types of data types.

SIVA SAI KRISHNA.PATUKURI
M.C.A^20

COMPILED BY:- SIVA SAI KRISHNA.PATUKURI (^) M.C.A