📄 Page
1
(This page has no text content)
📄 Page
2
(This page has no text content)
📄 Page
3
Learn C Programming from Scratch A step-by-step methodology with problem solving approach Mohammad Saleem Mir www.bpbonline.com
📄 Page
4
Copyright © 2024 BPB Online All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor BPB Online or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. BPB Online has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, BPB Online cannot guarantee the accuracy of this information. First published: 2024 Published by BPB Online WeWork 119 Marylebone Road London NW1 5PU UK | UAE | INDIA | SINGAPORE ISBN 978-93-55516-060 www.bpbonline.com
📄 Page
5
Dedicated to In recognition of your unwavering support, boundless encouragement, and enduring belief in my dreams, this book on C programming is dedicated to the pillars of my life – my beloved parents and cherished family members. Your love and guidance have been the foundation upon which I've built my journey in the world of programming. With heartfelt gratitude and endless love, Mohammad Saleem Mir
📄 Page
6
About the Author Mohammad Saleem Mir is currently working as Senior Assistant Professor at Higher Education Department, J&K, India. He has more than 15 years of teaching experience. During the course of his journey as a Teacher, he has taught at Post graduate as well as under-graduate levels. He has taught in general all the subjects associated to computer Sciences but took lead when it came to guiding students through Programming subjects, with the aim of building strong analytical skills and inculcate logical and reasoning skills into the students. The author holds bachelor’s degree in Sciences, Master’s Degree in Computer Applications/ Sciences from Kashmir University besides being a Doctorate student. He is currently pursuing Ph. D in the fields of Machine Learning/ Artificial Intelligence/ Medical Sciences. He has published a number of research papers in national as well as international Journals, besides presenting his research in various conference. The author has delivered inspirational talks in various conferences/ Refresher courses and various Training Programmes conducted by various Universities and colleges. The author remains actively involved in technical seminars and workshops.
📄 Page
7
About the Reviewer Srividya Richard, Sri is a passionate and a dedicated educator with a Masters of Engineering in Computer Science from Anna University. She considers herself as a self- confident and an enthusiastic person with a flair to logically conclude any given task with complete precision and accuracy. She is extremely goal oriented and an excellent team player. She has a decade long experience in teaching Engineering graduates at a reputed institution. Her research interests lie in the area of Machine learning, Data Analytics, Artificial Intelligence. She has published many research articles in her areas of interests in refereed journals and conferences. Sri’s love for technology and the English language has contributed immensely in reviewing and proofreading several articles that ranges from research thesis to books. During her academic career, Sri had the opportunity to mentor and execute several real time IT projects. She has also conducted various trainings and workshops and served as a committee member in organizing conferences and research seminars. The exposure to interdisciplinary projects and industry interactions has had a significant impact in honing her skills. As an AI enthusiast, Sri is delighted to embark on the path of equipping professionals with AI skills. She looks forward to leveraging the expertise gained over the years to empower individuals for both personal and professional development.
📄 Page
8
Acknowledgement This book is my first experience to pen down something for my esteemed readers. I sincerely hope they would appreciate it and let me know an honest review of the book. I would like to acknowledge with gratitude, the support and love of my family. They all kept me going, and conceiving this book would not have been possible without their support and encouragement. I am also grateful to BPB Publications for their guidance and expertise in bringing this book to fruition. It was a long journey of revising this book, with valuable participation and collaboration of reviewers, technical experts, and editors. Finally, I would like to thank all the readers who have taken an interest in my book and for their support in making it a reality. Your encouragement has been invaluable.
📄 Page
9
Preface I feel grateful to share my knowledge, analyses, and conclusions to raise the level of curiosity with regards to programming among the students of computer sciences, at the end of the day, we all will be beneficiaries. When one chooses computer sciences as a career, it may be noted that programing is an indispensable part of computer sciences and one cannot be a good computer scientist unless (s)he is good (if not exceptional) programmer. Well begun is half done, as they famously say; beginning on a good note, knowing fundamentals of programming clearly and building a solid base as far taking up more advanced form of programming, later in your career, is concerned is important. For this very, reason, this book focuses on giving an insight into the fundamentals of programming beforehand, and a sincere effort has been made to cover major concepts in detail so that the readers are kept interested. C is a foundational programming language and ought to be learned systematically. Getting measure of the features of C programming is very important. This book covers all the fundamental concepts, rest assured the readers would have a great time going through the book contents and would benefit from this book. The book is written in unpretentious manner. The target audience being programming beginners, who would be able to understand the concepts explained in the book quite easily. Each concept in the book is synchronously supplemented by coding examples to enhance clarity. The book has the following 10 chapters:
📄 Page
10
Chapter 1: Programming Methodology – In this Introduces you to the world of Programming Methodology, Problem Solving, Program Design etc. Chapter 2: C Programming Fundamentals – This chapter would acquaint you to the basic structure of the C Programming language, coding environment, and the syntax and references to write C Programs. Chapter 3: Control Statements – This chapter throws light on various control statements used in C programming. Chapter 4: Functions – This chapter gives an account of perhaps the most important topic in programming languages that is, functions. Chapter 5: Arrays – In this chapter, we will introduce arrays, multi-dimensional arrays, strings and more how we can work with them. Chapter 6: Pointers – In this chapter, we will learn about pointers, a concept that is found difficult by the programming novices, but not anymore. Chapter 7: Structures and Unions – In this chapter, we will learn to work with user defined data types like Structures and Unions. Chapter 8: File Handling – In this chapter, we will learn about how to work with hard disk files and different associated operations with programming examples. Chapter 9: C Preprocessors – In this chapter we will learn how to define and use Preprocessors in C and their importance. Chapter 10: C Graphics – In this chapter we will see the other side of the C Output, first one being the Text mode. Graphics programming in C used to drawing various geometrical shapes using inbuilt C functions.
📄 Page
11
Code Bundle and Coloured Images Please follow the link to download the Code Bundle and the Coloured Images of the book: https://rebrand.ly/9m5lv1u The code bundle for the book is also hosted on GitHub at https://github.com/bpbpublications/Learn-C- Programming-from-Scratch. In case there's an update to the code, it will be updated on the existing GitHub repository. We have code bundles from our rich catalogue of books and videos available at https://github.com/bpbpublications. Check them out! Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors, if any, that may have occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at : errata@bpbonline.com
📄 Page
12
Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family. Did you know that BPB offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.bpbonline.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at : business@bpbonline.com for more details. At www.bpbonline.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on BPB books and eBooks. Piracy If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at business@bpbonline.com with a link to the material. If you are interested in becoming an author If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit www.bpbonline.com. We have worked with thousands of developers and tech professionals, just like you, to help them share their insights with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Reviews Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions. We at BPB can understand what you think about our products, and our authors can see your feedback on their book. Thank you! For more information about BPB, please visit www.bpbonline.com. Join our book’s Discord space Join the book’s Discord Workspace for Latest updates, Offers, Tech happenings around the world, New Release and Sessions with the Authors:
📄 Page
13
https://discord.bpbonline.com
📄 Page
14
Table of Contents 1. Programming Methodology Introduction Structure Objectives Programming methodology Problem-solving methodology and techniques Features of a good program Computer as a problem-solving tool Computers and computing systems Computer system Hardware Software Compiler Assembler Debugger Interpreter Language translators Programming styles Procedural programming Modular programming Top-down modular programming Bottom-up modular programming Structured programming
📄 Page
15
Object Oriented Programming Algorithms Features of an algorithm Writing an algorithm Algorithm efficiency Importance of algorithmic efficiency Algorithm time complexity cases Implementation challenges Flowcharts History of flowcharts Flowchart symbols Meaning of a flowchart Flowchart examples Conclusion Exercise 2. C Programming Fundamentals Introduction Structure Objectives Facts about C Uses of C programming language Life cycle of a C program Integrated Development Environment GCC Visual Studio Code Turbo C++ for Windows 10+ Eclipse Dev-C++ Creating a source code
📄 Page
16
Save file Compile code Execute/ Run code C program components Tokens in C Semicolons Comments Keywords Whitespace in C Operators Arithmetic operators Relational operators Logical operators Bitwise operators Assignment operators Operator precedence in C Data types Variables Variable declaration Guidelines for declaring variables Initialization Identifiers Difference between variables and identifiers Constants Integer data type Integer expressions Precedence of operators Input-output in C Output formatting printf () Control characters
📄 Page
17
Read data supplied by a user Read data into a float or double variable putchar () fgtes () and fputs () Print an integer using a field width Floating point types Print double and float variables Floating-point expressions Assigning double/float to int Char data type void type Lvalues and Rvalues in C Literals Integer literals Floating-point literals Character constants String literals Defining constants Differences between constants and literals Storage class Auto Register Extern Static Extern storage class Writing basic programs in C Sequential logic Conclusion Exercise 3. Control Statements
📄 Page
18
Introduction Structure Objectives Control statements Decision making statements if statement if...else statement The if-else-if ladder Nested if statements Nested conditions Switch statements Loop statements Constituents of a loop Different loops used in C While loop do...while loop For loop Loop control statements Break Continue Example: Goto Infinite loop Nested loop Conclusion Exercise 4. Functions Introduction Structure
📄 Page
19
Objectives Functions Key features of functions in C Types of functions in C Defining a function Calling a function Formal arguments Return statement Various ‘return’ scenarios in C Function signature Mismatch Function variable scope Local variables Function parameters or variables Global variables Initializing local variable and global variables Callback function Function as an argument Recursion Memory usage in recursion Conclusion Exercise 5. Arrays Introduction Structure Objectives Arrays: One-dimensional array Declaring and initializing arrays Declaring arrays
📄 Page
20
Initializing arrays Bounds and array size Base address Array manipulation Accessing array elements Modifying array elements Passing arrays to functions Some array-based programs Character arrays - strings Accessing characters in a string Iterate through characters of a string String input and output Read a line of text String manipulation String concatenation Substring extraction Searching and replacing Parsing and tokenization Case conversion String formatting String length and manipulation functions 2-D strings Declaration and initialization of 2-D strings Reading input into a 2D string Accessing characters in a 2-D string Matrices Declaring 2-D array (Matrix) Initializing two-dimensional arrays 2-dimensional array elements access