0% found this document useful (0 votes)
99 views16 pages

Sample 3471

Alhamdulillah sab theek hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend and let you know after discussion in the family and see if you don't mind me asking if you don't mind

Uploaded by

Shabana Yasmeen
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)
99 views16 pages

Sample 3471

Alhamdulillah sab theek hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend hai na aaj weekend and let you know after discussion in the family and see if you don't mind me asking if you don't mind

Uploaded by

Shabana Yasmeen
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

Programming in C

Programming in C

By

P. Rizwan Ahmed
MCA, [Link]., M.A., [Link],(Ph.D)
Head of the Department
Department of Information System Management
Mazharul Uloom College, Ambur, Vellore
Tamil Nadu

University Science Press


(An Imprint of Laxmi Publications Pvt. Ltd.)
Bangalore  Chennai  Cochin  Guwahati  Hyderabad
Jalandhar  Kolkata  lucknow  mumbai  Ranchi
New Delhi  BOSTON, USA
Copyright © 2014 by Laxmi Publications Pvt. Ltd. All rights reserved with the publishers.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted
in any form or by any means, electronic, mechanical, photocopying, recording or otherwise
without the prior written permission of the publisher.

Published by :
University Science Press
(An Imprint of Laxmi Publications Pvt. Ltd.)
113, Golden House, Darya Ganj,
New Delhi-110 002
Phone : 011-43 53 25 00
Fax : 011-43 53 25 28
[Link]
info@[Link]

Price : ` 125.00 Only. First Edition : 2014

Offices

  Bangalore 080-26 75 69 30   Chennai 044-24 34 47 26


  Cochin 0484-237 70 04, 405 13 03   Guwahati 0361-251 38 81
  Hyderabad 040-24 65 23 33   Jalandhar 0181-222 12 72
  Kolkata 033-22 27 43 84   Lucknow 0522-220 99 16
  Mumbai 022-24 91 54 15   Ranchi 0651-220 44 64

UPC-9710-125-PROGRAMMING IN c-ahm  C—


Typeset at : Sukuvisa Enterprises, New Delhi.  Printed at :
Dedicated to
My Parents
Mr Rasheed Ahmed
Mrs Farida Rasheed
and
My Wife
Mrs Sumira Rizwan
Preface
T his book was developed specifically for freshmen students taking up their first course in
programming, but people who are interested in learning C are also welcome to study it. Its
aim is to supplement classroom lectures by focusing on C programming. Topics are arranged
based on the order of class discussion.
I tried my level best to maintain a very simple language throughout the book to enable the
readers to understand the concepts very easily.
May you learn a lot from the study of this book, and may the knowledge that you have
gained be used for the common good of all people.
This book is written for the first course on Programming in C. It is very useful to [Link].
Computer Science, [Link]., Software Engineering, [Link]. Information System Management, [Link].
Software Computer Science, [Link]. Electronics, [Link]. Mathematics, [Link] Computer Applications,
BCA, [Link]. Information Technology, MCA, MBA, B.E./[Link] and AMIE students. Review
questions are added at the end of each chapter for practice purpose. This book contains the five
chapters are summarized here:
Chapter 1 covers the Fundamentals of C Programming, Programming Elements, Operators,
Expressions, Evaluation of Expressions and Library Functions. Chapter 2 contains Input and
Output Statements, Decision-making Statements, and Looping Statements. Chapter 3 contains
the functions, categories of functions, various storage classes, and recursion. Chapter 4 covers
the Arrays, Structure and Union. Chapter 5 covers the pointers, operations on pointers, files and
operation on files.
—Author

vii
Acknowledgments
W ith its blessings of almighty it gives me a great pleasure to acknowledge our revered
President of Ambur Muslim Educational Society (AMES) AliJanab Alhaj N. Md. Zackriah
Sahib, General Secretary, Ali Janab Alhaj Nathersa Md Sayeed Sahib, Correspondent and Secretary
Alijanab Alhaj Madekar Nazar Mohammed Sahib, Former Principal, Dr. D. Nisar Ahmed,
Principal, Dr. P. M. Aadil Ahmed, [Link], [Link]., Ph.D, for their inspiration and encouragement
in bringing out this book successfully.
I acknowledge our respected Prof. S. Joseph Gabriel, MCA, [Link]., Associate Professor and
Head of Computer Science and Prof. M. Mohammed Ismail, [Link].(IT)., [Link].(Phy.), [Link].,
PGDCA, Associate Professor in Computer Science and Chairman, Board of Studies in Computer
Science and Computer Applications, Member, Academic Council, Thiruvalluvar University,
Serkadu, Vellore, Prof. A. Herani Sahib, [Link]., [Link]., Associate Professor and Head of
Mathematics, Chairman, Board of Examiners in Mathematics, Thiruvalluvar University, Vellore
and Prof. A. Zakiuddin Ahmed, [Link]., [Link]., M.S, PGDCSM, Associate Professor in Computer
Science, and Mr. A. Aqueel Ahmed, [Link]., B.A., [Link]., Mazharul Uloom College, Ambur for their
constant support in bringing out this book.
I also thank my sister Shariha Sahifa and brother Mohammed Ramil, V.I Arshad Azeez for
their patience and support extended to me all the times.
—Author

viii
Contents
Preface (vii)
Acknowledgments (viii)
1. Fundamentals of C Programming 1—24
1.1 History in C 1
1.2 Why use C? 1
1.3 Why Learn C? 1
1.4 Features of C Language 2
1.5 Programming Elements 2
1.5.1 Character Set 2
1.5.2 Keywords 3
1.5.3 Identifiers 3
1.5.4 Constants 4
1.5.5 Data Types 4
1.6 Variables 5
1.6.1 Rules for Naming Variables 5
1.6.2 Declaration of Variable 5
1.6.3 Initialization of Variables/Assigning Values to Variables 6
1.7 Structure of C-Program 7
1.8 Operators 8
1.8.1 Arithmetic Operators 8
1.8.2 Relational Operators 9
1.8.3 Logical Operators 10
1.8.4 Assignment Operators 11
1.8.5 Increment and Decrement Operators 12
1.8.6 Bitwise Operators 13
1.8.7 Conditional Operator 13
1.8.8 Special Operators 14
1.9 Operator Precedence and Associativity 14
1.10 Creation and Execution of a C-Program 15
1.11 Expressions 16
1.12 Evaluation of Expressions 17
1.12.1 Precedence of Arithmetic Operators 17
1.13 Library Functions 18
1.13.1 String Functions (string.h) or String Handling Functions 18
1.13.2 Math Function(math.h) 20

ix
x

1.14 The C Preprocessor 21


Let Us Summarise 23
Review Questions 23
Exercises 24
2. I/O and Control Statements 25—52
2.1 I/O Statements 25
2.1.1 Single Character I/O 25
2.2 Formatted I/O 26
2.2.1 Formatted Input: scanf( ) 26
2.2.2 Formatted Output :printf() 26
2.3 String I/O Functions 27
2.3.1 String Input: gets() 27
2.3.2 String Output: puts() 27
2.4 Sample C Programs 28
2.5 Control Statements/Control Flow/Programming Flow Control 29
2.5.1 Decision–Making Statement 29
2.5.2. Looping Statement 37
2.6 Comma Operator 44
2.7 Sample C Programs 44
Let Us Summarise 51
Review Questions 51
Exercises 52
3. Functions and Storage Classes 53—59
3.1 Functions 53
3.1.1 Need for User Defined Functions 53
3.2 Return Statement 54
3.3 Function Prototype 55
3.4 Calling a Function 55
3.5 Formal and Actual Arguments 56
3.5.1 Formal Arguments 56
3.5.2 Passing Arguments 56
3.6 Category of Functions/Types of Function 56
3.6.1 Function with no Arguments and no Return Value 57
3.6.2 Function with Arguments and no Return Value 57
3.6.3 Function with no Arguments and Return Value 57
3.6.4 Function with Arguments and Return Value 57
3.7 Recursion 57
3.8 Storage Classes 58
xi

Let Us Summarise 59
Review Questions 59
Exercises 59
4. Arrays and Structures and Union 60—74
4.1 Arrays 60
4.1.1 One Dimensional Array 60
4.1.2 Two Dimensional Arrays 61
4.1.3 Multidimensional Arrays 62
4.1.4 Sample C Programs 62
4.2 Structure 65
4.2.1 Defining Structure 65
4.2.2 Structure Declaration 66
4.2.3 Giving Values to Structure Members 66
4.2.4 Structure Initialization 67
4.2.5 Difference Between Arrays and Structure 67
4.3 Structures within Structures 67
4.4 Pointers to Structures 68
4.5 Self-referential Structure 68
4.6 Union 70
4.6.1 Declaration of Union 70
4.7 Difference Between Structure and Union 71
4.8 Bitwise Operations 72
4.9 User Defined Data Type 73
Let Us Summarise 73
Review Questions 74
Exercises 74
5. Pointers and Files 75—87
5.1 Pointers 75
5.1.1 Accessing the Address of the Variable 75
5.1.2 Declaring and Initializing Pointers 76
5.1.3 Accessing a Variable Through its Pointer 76
5.1.4 Pointer Operators 76
5.2 Operations on Pointers 76
5.3 Arrays of Pointers 77
5.4 Pointers to Functions 78
5.5 Pointers and Arrays 79
5.6 Pointers and Structures 80
xii

5.7 Pointers and Function 80


5.7.1 Call by Value 80
5.7.2 Call by Reference 81
5.8 Dynamic Memory Allocation 82
5.9 Command Line Input or Arguments 83
5.10 Files 83
5.10.1 Creating a File 84
5.10.2 Reading a File 84
5.10.3 Writing a File 84
5.10.4 Opening a File 85
5.10.5 Closing a File 85
5.11 Operations on Files 86
Let Us Summarise 86
Review Questions 86
Exercises 87
Appendix I 88
Appendix II 98
Index 107
CHAPTER 1
FUNDAMENTALS OF C PROGRAMMING

1.1 HIsTORY In C
Introduction
C is a general-purpose computer programming language developed in 1972 by Dennis Ritchie
at the Bell Telephone Laboratories for use with the Unix operating system. C is a structured
programming language, which means that it allows you to develop programs using well-defined
control structures (you will learn about control structures in the articles to come), and provides
modularity (breaking the task into multiple sub tasks that are simple enough to understand and
to reuse). C is often called a middle-level language because it combines the best elements of low-
level or machine language with high-level languages.

1.2 wHY Use C?


C (and its object oriented version, C++) is one of the most widely used third generation
programming languages. Its power and flexibility ensure it is still the leading choice for almost
all areas of application, especially in the software development environment.
Many applications are written in C or C++, including the compilers for other programming
languages. It is the language many operating systems are written in including UNIX, DOS and
Windows. It continues to adapt to new uses, the latest being Java, which is used for programming
Internet applications.
C has much strength, it is flexible and portable, it can produce fast, compact code, it provides
the programmer with objects to create and manipulate complex structures (e.g. classes in C++)
and low level routines to control hardware (e.g. input and output ports and operating system
interrupts).

1.3 wHY LeARn C?


• Compact, fast, and powerful
• “Mid-level” Language
1
2 Programming in C

• Standard for program development (wide acceptance)


• It is everywhere! (Portable)
• Supports modular programming style
• Useful for all applications
• C is the native language of UNIX
• Easy to interface with system devices/assembly routines.

1.4 FeATURes OF C LAnGUAGe


• It is a flexible high-level structured programming language.
• It includes the features of low-level language like assembly language.
• It is portable. A program written for one type of computer can be used in any other type.
• It is much ability and efficient.
• It has an ability to extend itself.
• It has a number of built-in functions, which makes the programming
• C is modular, as it supports functions to divide the program in to sub-program.
• C is efficient on most machines, because certain constructs are machine dependant.
• C language is well suited for structured programing, thus requiring the user to think of
a problem in terms of function modules or blocks.

1.5 PROGRAMMInG eLeMenTs

1.5.1 Character set


Characters are used in a language to form words, numbers and expression. Characters used in C
language can be grouped in to four types:
• Letters
• Digits
• Special Characters
• White Spaces
C Character Set
Letters Digits
Uppercase A – Z 0 to 9
Lowercase a – z
Special characters
, comma + plus
; semicolon - minus
? question mark < less than
$ dollar symbol > greater than
# number sign ! exclamation mark
~ tild % percentage
* asterisk
Fundamentals of C Programming 3

White Spaces
Blank Space
Horizontal Tab
Carriage Return
New Line
Form Feed

1.5.2 Keywords
Keywords also referred as reserved words. Keywords have standard predefined specific meaning.
The user has no right to change its meaning. Keywords should be written in lower case. They
should be written in lower-case letters. The following keywords are reserved for C:
C Keywords
Switch Boolean Break Auto
For Case Const Register
Char Sizeof Void Static
Default Do Double Struct
While Continue Float Union
Include Else Go to Short
Nested For Char Signed
Int If Long Volatile

1.5.3 Identifiers
Identifiers refer to the names of variables, functions and arrays. These are user-defined names and
consist of a sequence of letters and digits with the letter as a first character. Both uppercase and
lowercase letters are permitted although lowercase letters are commonly used. The underscore is
used as a link between two words in long identifiers.

Rules for naming an identifiers


• Identifiers are formed with alphabets, digits and a special character underscore ( _ ).
• The first character must be an alphabet.
• No special characters are allowed other than underscore.
• They are case sensitive. That it Sum is different from SUM.

For Example
Valid Identifiers Invalid Identifiers
ROLL25 25ROLL
Register_No Register No
CT100 100CT
Programming In C By P. Rizwan Ahmed

40%
OFF

Publisher : Laxmi Publications ISBN : 9789383828326 Author : P. Rizwan Ahmed

Type the URL : [Link] 71

Get this eBook

You might also like