Docsity

C Programming: Functions, Types, and String Manipulation, Exams of Programming Languages

Toggle sidebar
bg1
PROGRAMMING FOR PROBLEM SOLVING
DIGITAL NOTES
B.TECH
(I YEAR – I SEM)
(2018-19)
DEPARTMENT OF CSE & IT
(R18A0501) PROGRAMMING FOR PROBLEM SOLVING SYLLABUS
Course Objectives
• To understand the various steps in Program development.
• To understand the basic concepts in C Programming Language.
• To learn how to write modular and readable C Programs
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
pf4f
pf50
pf51
pf52

Partial preview of the text

Download C Programming: Functions, Types, and String Manipulation and more Exams Programming Languages in PDF only on Docsity!

PROGRAMMING FOR PROBLEM SOLVING

DIGITAL NOTES

B.TECH

(I YEAR – I SEM)

DEPARTMENT OF CSE & IT

(R18A0501) PROGRAMMING FOR PROBLEM SOLVING SYLLABUS

Course Objectives

  • To understand the various steps in Program development.
  • To understand the basic concepts in C Programming Language.
  • To learn how to write modular and readable C Programs
  • To learn to write programs (using structured programming approach) in C to solve problems. UNIT ‐ I Introduction to Computing – Computer Systems-Hardware and Software, Computer Languages, Algorithm, Flowchart, Representation of Algorithm and Flowchart with examples. Introduction to C – History of C, Features of C, Structure of C Program, Character Set, C Tokens-Keywords, Identifiers, Constants, Variables, Data types, Operators. UNIT ‐ II Statements ‐Selection statements (Decision Making)- if and switch statements with examples, Repetition statements (loops)- while, for, do while‐ statements with examples, Unconditional statements- break, continue, goto statements with examples. UNIT – III Functions ‐Designing Structured Programs, Types of Functions-User defined functions, Standard functions, Categories of functions, Parameter Passing techniques, Storage classes, Recursion. UNIT ‐ IV Arrays- Declaration and Initialization, One dimensional Arrays, Two dimensional Arrays. Strings- Declaration and Initialization, String Input / Output functions, String manipulation functions. UNIT-V Pointers- Introduction, Definition and Declaration of pointers, address operator, Pointer variables, Pointers with Arrays. Structures- Introduction, Declaration and Initialization, Array of Structures, Unions. TEXT BOOKS:
  1. Computer Programming with C, Special Edition MRCET, Mc Graw Hill Publishers 2017.‐
  2. Computer Science: A Structured Programming Approach Using C, B.A.Forouzan and R.F. Gilberg, Third Edition, Cengage Learning. REFERENCE BOOKS:
  3. The C Programming Language, B.W. Kernighan and Dennis M.Ritchie, PHI.
  4. Computer Programming, E.Balagurusamy, First Edition, TMH.
  5. C and Data structures – P. Padmanabham, Third Edition, B.S. Publications.
  6. Programming in C, Ashok Kamthane. Pearson Education India.

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 a mouse, 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 among data, and movement of data inside the system. Today’s computers may have one ,two, or more CPUs. Primary storage ,also known as main memory, 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. The output device is usually a monitor or a printer to show output. If the output 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. Hardware Software Computer Sys tem

Auxiliary storage , also known as secondary storage , is used for both input and output. It is the place where the programs and data are stored permanently. 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: system software and application software .System software manages the computer resources .It provides the interface between the hardware and the users. Application software, on the other hand is directly responsible for helping users solve their problems. Fig: Types of software System Software: System software consists of programs that manage the hardware resources of a computer and perform required information processing tasks. These programs are divided into three classes: the operating system, system support, and system development. The operating system provides services such as a user interface, file and database access, and interfaces to communication systems such as Internet protocols. The primary purpose of this software is to keep the system operating in an efficient manner while allowing the users access to the system. System support software provides system utilities and other operating services. Examples of system utilities are sort programs and disk format programs. Operating services consists of programs that provide performance statistics for the operational staff and security monitors to protect the system and data. System suppor t System Developme n General Purpose Application Specific Syste m softw are Application software Softwa re Operati ng Systems

To write a program for a computer, we must use a computer language. Over the years computer languages have evolved from machine languages 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 languages available 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 the internal 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 would convert programs into machine language. The early programming languages simply mirror to the machine languages using symbols of mnemonics to represent the various machine language instructions because they used symbols, these languages were known as symbolic languages. Computer does not understand symbolic language it must be translated to the machine language. A special program called assembler translates symbolic code into machine language. Because symbolic languages had to be assembled into machine language they soon became known as assembly languages. Symbolic language uses symbols or mnemonics to represent the various ,machine language instructions. High Level Languages: Symbolic languages greatly improved programming effificiency; they still 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. High level languages are portable to many different computers, allowing the programmer to concentrate on the application problem at hand rather than the intricacies of the computer. High-level languages are designed to relieve the programmer from the details of the assembly language. High level languages share one thing with symbolic languages, They must be converted into machine language. The process of converting them is known as compilation. The first widely used high-level languages, FORTRAN (FORmula TRANslation)was created by John Backus and an IBM team in 1957;it is still widely used today in scientific and engineering applications. After FORTRAN was COBOL(Common Business-Oriented Language). Admiral Hopper was played a key role in the development of the COBOL Business language. C is a high-level language used for system software and new application code. ALGORITHM: Algorithms was developed by an Arab mathematician. It is chalked out step-by-step approach to solve a given problem. It is represented in an English like language and has some mathematical symbols like ->, >, <, = etc. To solve a given problem or to write a program you approach towards solution of the problem in a systematic, disciplined, non-adhoc, step-by-step way is called Algorithmic approach. Algorithm is a penned strategy(to write) to find a solution. Example: Algorithm/pseudo code to add two numbers Step 1: Start Step 2:Read the two numbers in to a,b Step 3: c=a+b Step 4: write/print c Step 5: Stop. FLOW CHART : A Flow chart is a Graphical representation of an Algorithm or a portion of an Algorithm. Flow charts are drawn using certain special purpose symbols such as Rectangles, Diamonds, Ovals and small circles. These symbols are connected by arrows called flow lines. (or) The diagrammatic representation of way to solve the given problem is called flow chart.

Arrow Flow Joins two symbols and also represents flow of execution. 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 1972 at 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 still retaining 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 the first 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. Machine-specific features are isolated in a few modules within the UNIX kernel, which makes it easy for you to modify them when you are porting to a different hardware architecture. C was first designed by Dennis Ritchie for use with UNIX on DEC PDP-11 computers. The language evolved from Martin Richard's BCPL, and one of 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 and Ritchie. 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 dated 1999 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 sections is to be followed.

  1. Documentation section
  2. Linking section
  3. Definition section
  4. Global declaration section
  5. Main function section { Declaration section Executable section }
  6. Sub program or function section 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.

E.g. +, -, * E.g. [ ], { } E.g. “asifia” E.g. -15.4, ‘a’, 200 E.g. rate,no_of_hours E.g. int, printf A C program can be divided into these tokens. A C program contains minimum 3 c tokens no matter what the size of the program is. KEYWORDS : There are certain words, called keywords (reserved words) that have a predefined meaning in ‘C’ language. These keywords are only to be used for their intended purpose and not as identifiers. The following table shows the standard ‘C’ keywords Auto Break Case Char Const Continue Default Do Double Else Enum Extern Float For Goto If Int Long Register Return Short Signed Sizeof Static Struct Switch Typedef Union Unsigned void OPERATORS SPECIAL SYMBOLS STRINGS CONSTANTS IDENTIFIERS C TOKENS KEY WORDS

Volatile While 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).

  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 it is. The format of declaring a variable is Data-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 no commas are required. eg float a, b, c; int e, f, grand total; char present_or_absent; ASSIGNING VALUES : When we name and declare variables we need to assign value to the variable. In some cases we assign value to the variable directly like a=10; in our program.
  1. The advantage of declaring an unsigned constant is to increase the range of storage.
  2. Floating point constants contain a decimal point or an exponent or both. For Eg : 123.4,1e- 2,1.4E-4,etc.The suffixes f or F indicate a float constant while the absence of f or F indicate the double, l or L indicate long double. CHARACTER CONSTANTS: A character constant is written as one character with in single quotes such as ‘a’. The value of a character constant is the numerical value of the character in the machines character set. certain character constants can be represented by escape sequences like ‘\n’. These sequences look like two characters but represent only one. The following are the some of the examples of escape sequences: Escape sequence Description \a Alert \b Backspace \f Form feed \n New Line \r Carriage return \t Horizontal Tab \v Vertical Tab String constants or string literal is a sequence of zero or more characters surrounded by a double quote. Example , “ I am a little boy”. quotes are not a part of the string. To distinguish between a character constant and a string that contains a single character ex: ‘a’ is not same as “a”. ‘a’ is an integer used to produce the numeric value of letter a in the machine character set, while “a” is an array of characters containing one character and a ‘\0’ as a string in C is an array of characters terminated by NULL.

There is one another kind of constant i.e Enumeration constant , it is a list of constant integer values. Ex.: enum color { RED, Green, BLUE } The first name in the enum has the value 0 and the next 1 and so on unless explicit values are specified. If not all values specified , unspecified values continue the progression from the last specified value. For example Enum months { JAN=1, FEB,MAR, …, DEC - Where the value of FEB is 2 and MAR is 3 and so on. Enumerations provide a convenient way to associate constant values with names. 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 are calculating a sum of two numbers we will name the variable that will hold the value of sum of two numbers as 'sum'. 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 the type 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 on basic data types. E.g.: int a,b;

USER DEFINED DATATYPES:

C Provides a facility called typedef for creating new data type names defined 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 of num1,num2as int variables. “typedef” ia more useful with structures and pointers. POINTER DATA TYPES: Pointer data type is necessary to store the address of a variable. INPUT AND OUTPUT STATEMENTS : The simplest of input operator is getchar to read a single character from the input device. varname=getchar(); you need to declare varname. The simplest of output operator is putchar to output a single character on the 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, for that 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 is stored. 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() { 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); } OPERATORS : An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulations. They form expressions.