Begin & Master Programming with C Fundamentals
By Mallam Awal
()
About this ebook
With this book you get sound fundamentals in both traditional C and the variants like Embedded C and C graphics. This book hopes to walk you through beginnings and advance you to become proficient at C language. I must confess it is hugely biased toward the self-taught learner; it is hands-on. You read a section and attempt all the code examples and exercises to test yourself and build skills.
With over 500 examples and exercises, the book walks you through the fundamentals of C language from the basics of variables and constants to expressions, doing maths with C, blocks and functions, control structures, strings and composite data like arrays, strucs, bitfields, unions etc. and management of memory and files.
Unlike traditional books on C, the book raises your level from novice by exposing you to the bigger issues of programming in C like error handling and management, data structures, defensive programming and best practices as encoded in the several acronyms (such as KISS, DRY, YAGNI etc.) covered in the book. The book also exposes you to the different forms of C programming with an introduction to embedded C programming and C graphics.
The next C book in this series advances you to higher levels of competence. Happy coding.
Chapter 1: C-Language & Programming Fundamentals
Chapter 2: Anatomy of a C Program
Chapter 3: Doing Mathematics with C
Chapter 4: Control Flow –Branching & Looping
Chapter 5: Data Structures & Algorithms I
Chapter 6: Data Structures II: Programmer defined types
Chapter 7: Functions
Chapter 8: File Processing
Chapter 9: Memory Management
Chapter 10: Errors & Error Handling in C
Chapter 11: Introduction to Graphics in C
Chapter 12: Embedded C Programming
Chapter 13: C-Standard Library, Preprocessors etc
Chapter 14: Principles & Standards: Best Practices in Programming
(KISS, DRY, YAGNI, STUPID code, SOLID code)
Chapter 15: Defensive Programming
Mallam Awal
Mallam Awal (birthname, Mohammed Awal M.) is a life-long student who loves to devour everything readable, and most of all loves to share ideas and knowledge. I am hot for ideas and always tunnelling for new insights about this complex world of ours. I have wide ranging interest and qualifications in disciplines ranging from mechanical engineering, project management, accounting, education, computing, programming, writing and poetry.When I am not teaching or consulting, you will find me researching things, answering questions on Quora, composing poems or enjoying entertainment from those cool cats at WWE.
Read more from Mallam Awal
Programming Essentials Rating: 5 out of 5 stars5/5Be an "A" Grade Student or Pass Better Trying Rating: 0 out of 5 stars0 ratings
Related to Begin & Master Programming with C Fundamentals
Related ebooks
C Programming Language Rating: 4 out of 5 stars4/5C in 30 Pages Rating: 5 out of 5 stars5/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5Programming Microcontrollers in C Rating: 5 out of 5 stars5/5C Programming Concepts Rating: 0 out of 5 stars0 ratingsC Programming Language, A Step By Step Beginner's Guide To Learn C Programming In 7 Days. Rating: 4 out of 5 stars4/5Programming Concepts in C++ Rating: 0 out of 5 stars0 ratingsC language Programming: Simple, Short, and Straightforward Way of Learning C Programming Rating: 3 out of 5 stars3/5C Programming: C Programming Language for beginners, teaching you how to learn to code in C fast! Rating: 0 out of 5 stars0 ratingsProgramming Language Concepts: Improving your Software Development Skills Rating: 0 out of 5 stars0 ratingsC Programming Language The Beginner’s Guide Rating: 0 out of 5 stars0 ratingsLearn C Programming in 24 Hours Rating: 0 out of 5 stars0 ratingsAssembly Language Coding in Color: ARM and NEON Rating: 0 out of 5 stars0 ratingsMicrosoft Visual C++ Windows Applications by Example Rating: 4 out of 5 stars4/5Introduction to Programming Languages Rating: 4 out of 5 stars4/5Advanced C Concepts and Programming: First Edition Rating: 3 out of 5 stars3/5Computer Programming Using C Rating: 0 out of 5 stars0 ratingsARM Microcontrollers Programming for Embedded Systems Rating: 5 out of 5 stars5/5Embedded C Programming: Techniques and Applications of C and PIC MCUS Rating: 3 out of 5 stars3/5Assembly Language:Simple, Short, And Straightforward Way Of Learning Assembly Programming Rating: 2 out of 5 stars2/5C Programming for the Pc the Mac and the Arduino Microcontroller System Rating: 0 out of 5 stars0 ratingsEmbedded Systems Complete Self-Assessment Guide Rating: 0 out of 5 stars0 ratingsC Language Programming Codes Rating: 0 out of 5 stars0 ratingsC Programmin Language Rating: 0 out of 5 stars0 ratingsFast and Effective Embedded Systems Design: Applying the ARM mbed Rating: 5 out of 5 stars5/5C++ Application Development with Code::Blocks Rating: 0 out of 5 stars0 ratingsARM-based Microcontroller Projects Using mbed Rating: 5 out of 5 stars5/5Embedded RTOS Design: Insights and Implementation Rating: 0 out of 5 stars0 ratings
Programming For You
Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5101 Amazing Nintendo NES Facts: Includes facts about the Famicom Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Teach Yourself C++ Rating: 4 out of 5 stars4/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratings
Reviews for Begin & Master Programming with C Fundamentals
0 ratings0 reviews
Book preview
Begin & Master Programming with C Fundamentals - Mallam Awal
Begin & Master Programming
with C Language fundamentals
By Mallam Awal
Begin & Master Programming Series. Book 2
Smashwords Edition, License Notes
This eBook is licensed for your personal enjoyment only. This eBook may not be re-sold or given away to other people. If you would like to share this book with another person, please purchase an additional copy for each recipient. If you’re reading this book and did not purchase it, or it was not purchased for your use only, then please return to your favorite eBook retailer and purchase your own copy.
Thank you for respecting the hard work of this author.
Copyright © Mallam Awal 2020
Dedication
This book is dedicated to Dennis McAllistair Ritchie (dmr)
who birthed the C language, the man whom the world has chosen to ignore.
His brainchild C, has served mankind profoundly giving almost
all of the major software infrastructure of modern computing
(Oses, drivers, dbs, compilers and more), and
spawned many modern programming languages or
served as the gang plank from which they launched themselves.
To the fond memory of this great man…
Table of Contents
Preface
Chapter 1: C-Language & Programming Fundamentals
Your first program: Coding on the command line
Download & use your first IDE
Chapter 2: Anatomy of a C Program
Programming language basics
Variables, data types keywords, operators etc.
Analysis of a program
Structuring programs
Programming paradigms
Chapter 3: Doing Mathematics with C
Doing maths with operators
Operator precedence/associativity
Typecasting
Common Errors in numerical operations
Doing maths with functions
Chapter 4: Control Flow –Branching & Looping
If statement
Using Tenary statement
Switch case
Looping
Break, Return,Continue etc.
Chapter 5: Data Structures & Algorithms I
Types of data structures
Considerations in choosing data structure
Algorithms: attirbutes & measures
Big O
Classification of algorithms
Chapter 6: Data Structures II: Programmer defined types
Arrays
Pointers
C-Structures
Modularising structure
Typedef
Bitfields
Unions
Enumerations
Character Arrays & strings
Chapter 7: Functions
Function prototype
Passing Arguments
Recursion
Best practices in creating functions
The Standard library functions
Randomization
Chapter 8: File Processing
File pointers
Working with text files
Working with Binary files
Chapter 9: Memory Management
Types of memory management in C
Different memory areas
Standard memory allocation functions
Dynamic Memory Management Issues & Challenges
Chapter 10: Errors & Error Handling in C
Compile Errors
Runtime Errors
Logical Errors
Error prevention, detection & recovery
Strategies for error handling in C
Chapter 11: Introduction to Graphics in C
Basics of doing graphics in C
Chapter 12: Embedded C Programming
Pre-requisites for programming embedded systems
Microcontrollers & hardware
Embedded C Basics
Structure & sample of embedded C programs
Chapter 13: C-Standard Library, Preprocessors etc
C Standard library
Preprocessor
Storage Class of a Variable
Localisation
Chapter 14: Principles & Standards: Best Practices in Programming
KISS
DRY
YAGNI
STUPID code
SOLID code
Chapter 15: Defensive Programming
Tools & techniques of defensive programming
Principles of defensive programming
Bibliography
Appendix 1, 2 & 3
About the Author
Connect with Mallam Awal
Other tittles from Mallam Awal
Preface
This book was born out of my own learning of C, augmented later by my teaching of this beautiful and elegant, clean code language. I had taught Basic as a first programming language in the 1990s to students who wanted personal tutorial in programming. Then I got fascinated by C and began to learn it. When I had to teach C as an Intro to programming course at Scholastic InstituteNG, I gathered up and updated notes I built up for my personal learning. These of course had to be augmented from further readings, and experience teaching and coding.
I must confess the book still is hugely tailored to a self-taught learner like me. You study a section and get exercises to practice to consolidate the new skills. As with all things practical, learning C or any programming language for that matter requires you work at it. That is think and write code of your own. You do not learn programming by reading only. So get into the habit of attempting all the exercises; over 500 of them given in the book. Of course, you should also get more from online tutorials and textbooks plus think up some projects of your own.
Programming is fascinating and deeply enjoyable; you write what seems like gibberish for the uninitiated, and something happens. You create, and by that you make the world a better place in your small way with programming. Programming solves problems, making work a tad bit less tedious and stressful. Consider how backward the world would have been without programs that solve some of the complex problems of space research or stock exchange transactions or even basic engineering computations or simulations.
Welcome to the world of programming. You are fortunate to be studying C in this book. If this is your first course in programming, you are even more blessed. C is the foundation of modern programming. Once you learn C, you are highly equipped to learn other languages easily. In addition, unlike some of the more abstracted modern languages, C teaches you some of the machine-intimate details of programming under the hood rivalled only by assembly language. That way, you learn discipline and meticulous coding. For, C does not protect you against your lousiness. C lets you fail and see the repercussions of your lousy coding that you may be more astute!
Enough of sermons, welcome to the world of C. Let’s get on with it bro./sis.
CHAPTER ONE
C-LANGUAGE & PROGRAMMING FUNDAMENTALS
1.0 THE LANGUAGE
C is an imperative programming language developed by Dennis Ritchie while working at Bell Labs between 1969 and 1972. It was first announced in 1972 and has since then grown in popularity and clout among programming languages. C was designed to support structured programming, remove the trouble of coding in assembly language while at the same maintaining closeness of the code to machine language as well as providing a high enough language close to English to be comfortable to work with.
C has found and continues to find uses in various applications like development of operating systems (UNIX and Windows for example), peripheral, embedded device and network drivers, assemblers, language interpreters and compilers, text editors, databases, print spoolers and many utilities, games etc. This is in addition to being the foundation or inspiration for many modern programming languages.
1.1 FEATURES OF C PROGRAMMING LANGUAGE
Programmers that came in the era of newer languages like Java and Python do snicker at C language considering it ancient and moribund. Yes, C is one of the oldest but the most foundational of modern programming languages that is still up and kicking stronger. It seems to defy death, and instead finds newer applications due to its unique features. These are:
i. Simplicity:
C language is light weight with small in-built vocabulary that is easy to learn. For beginners this may seem otherwise because the language appears cryptic at first. However, C is one of the least verbose languages where you can express ideas clearly in few constructs. The so-called higher
level languages tend to get verbose, and involve a lot of grammar to learn. C has syntax that is less difficult to follow and more compact.
ii. Portability:
C’s code translates easily and closely to machine language. Because of this, a C program can be developed on one platform and run on another without needing significant modifications. Well written C code done on Windows can easily run on other platforms like Linux. This closeness of C’s code to machine language makes it very efficient in developing especially, programs that talk
to the machine such as operating systems, drivers and compilers. This confers on it ability to create code that can run on many platforms.
iii. Power:
C’s portability arising from ability to generate code that maps to machine language efficiently, enables C to do with a higher level language that is easier to comprehend, what Assembly language does. C has code that speaks directly to hardware through pointers and such facilities like the malloc function, giving it power to control memory, disks and networks directly.
iv. Speed:
Since C’s code is close to machine language and translates easily when compiled, C programs are faster than those for most other languages. The ubiquity of C compilers and speed of C code is why other languages such as PHP, C++, Java and Python start out by interpreting their code to C before compiling. As they mature, they begin to compile direct to ASM (assembly language).
v. Structured Programming/Modular code:
C syntax supports arranging a program as self-contained blocks that can be run and debugged separately. This is achieved in C using blocks and functions to build self-contained code modules. Modular programming allows the programmer to avoid repetition of same routines in the different parts of the program. You write a function once and use it in every part of the program it is needed. This makes not only for efficient code but less development effort and time for the programmer.
vi. Use of Libraries (Extensibility)
C’s compactness with very small, in-built vocabulary limits its innate abilities. To augment the language, the designer made it capable of accepting extensions to its functions and capabilities by using add-on or plug-in
code packaged as header files called Libraries. C provides its own main headers grouped in the C Standard library. You add the header files using the directive
#include headername.h
For example to include the standard input-output (i/o) header file write, #include stdio.h..
Using libraries, gives C flexibility and scalability. You can create your own libraries to add code and functions that provide your required capabilities such as the Glib, a GNU provided open source library.
Exercise
Since almost all C programs need to do I/O you must include the standard i/o header (stdio.h) in your programs as the first line of code. So get familiar with it. Now write the following preprocessor directive (yes, that’s what it is called) twice:
#include
Note that there is no space between # and include but you leave one space between include and
vii. Case sensitivity:
C language is case sensitive. It sees a name written in lower case and upper case as different. For example, a variable name speed is deemed different from Speed, SPEED or speeD from the point of view of C. This is good as it makes for uniform style in naming tokens. Generally, the convention in C is: variables are named using lowercase while Constants use upper case. C is flexible though to allow breaking of this convention. However, you must maintain consistency.
Exercise:
Which of the following variable and constant names conform to C convention?
Variables: (a) Age (b) age (c) AGE (d) length (e) LENGTH
Constants: (f) const PI = 3.142 (g) const pi = 3.142 (h) const dollar = 300 (i) const DOLLAR = 300
1.1 HISTORY OF C LANGUAGE:
The language C was developed by Dennis McAlistair Ritchie (fondly called by friends dmr) while working on developing the UNIX operating system at AT & T Bell labs at the dawn of the 1970 decade. UNIX was originally written in Assembler by Dennis Ritchie, Ken Thompson and others. Dennis wanted a more flexible and comprehensible language for re-writing the UNIX kernel and utilities running on it. He sought to make improvements on an earlier language called B that Ken Thompson developed to improve on another earlier language called BCPL. Incorporating ideas from Fortran, Algol 68, PL.1 and Assembler, his improvements on B yielded a new, better language he called C. C was officially anounced in 1972.
C soon proved very popular and grew in clout and use in the 1980s becoming the main language used for major software that were run on UNIX and UNIX-like operating systems which then were in the majority. With the coming of the pc revolution, C was the main language in which Windows and many application software were first written. This is the reason why C is ubiquitous.
C has remained a dominant force in programming. It is a foundation language from which many languages evolved or drew their inspiration. Its simple syntax is also pervasive among most programming languages. That is why learning C as your first programming language is a key investment in your programming skills. It provides you the core syntax and other language infrastructure to learn the newer languages often based on it.
C has influenced and is the foundation for development of languages like C++, Java, Visual C, Objective C, D, C#, PHP, Perl, Go and JavaScript, Python, Rust, Limbo, Awk etc. Others use C’s facilities or convert to compile as C. C is behind most operating systems, device drivers and software in embedded devices plus back end networking/web programs.
1.2 VERSIONS OF C
From its first appearance about 1972 C’s first version was maintained by Dennis Ritchie. In 1978, he and his friend, Ken Thompson published a book tittled The C Programming Language
which provided the first standardisation of the language. The book is often referred to informally among C enthusiasts as "K & R C" (Ken & Ritchie C).
By 1989, the American Standards Institute, ANSI began to take charge of standardising C. Subsequent C standards and versions from 1990 have been handled by a committee of the International Standards Organisation (ISO). The versions are often named after the year of publication e.g. the ANSI 1989 Standard is called C89. See the table below for the standards to date of publication of this book.
Year Standard Version Name
1978 n/a K&R C
1989 ANSI X3.159-1989 C89
1990 ISO/IEC 9899:1990 C90
1995 ISO/IEC 9899/AMD1:1995 C95
1999 ISO/IEC 9899:1999 C99
2011 ISO/IEC 9899:2011 C11
2018 ISO/IEC 9899:2018 C18
1.3 PROGRAMMING BASICS
Programming is planning and creating instructions that tell the computer what to do, how, when and where to do it. A program directs the computer in a suitable language to act in a certain way and/or decide in a certain way as required in a particular context. A program consists of a list of instructions couched in suitable terms that a computer can understand and act on.
Developing a program involves three major phases: conception, production and deployment/ operation.
i. Conception: This involves seeing a need or problem that can be solved by use of computer, clarifying what needs to be done and how, then developing a set of steps (called algorithm) for effecting a programming solution to the problem.
ii. Production: This involves translating the solution concept developed for the perceived programmable solution (algorithm) into a language that a computer can understand and act on. Major tasks here are:
a. choose a language
b. write the code that performs the algorithm (or steps) required to solve the problem
c. test and debug the code till it works perfectly then
d. package the final code for shipment to the client or user.
iii. Deployment/Operation: This involves installing and running the newly created software by test users and the final user.
The core areas of interest of the programmer is conceiving (planning) and producing code. It is in this sense that the word programming is popularly used. The whole gamut of operations mentioned above are often referred to as software development. The set of code for a program saved as a file is called the source code.
1.4 SOFTWARE FOR PROGRAMMING
To produce programs you need what is loosely called a programming environment
in which to work. The programming environment comprise of software that you use to create, run/test and perfect your program. Looked at broadly, it also includes the operating system and other system facilities helping to host and support your programming. However, most often it is the narrower view of programming environment that is implied when the word is used i.e. software for creating, running, testing and perfecting a program.
There are broadly two kinds of programming environments (software):
i. Stand-alone Software for programming
ii. Integrated Development Environments
1.4.1 STAND-ALONE PROGRAMMING SOFTWARE:
In stand-alone environment, the software required for individual programming tasks are separate. The programmer has to manually call up each and get it to work by issuing a command from the command line. These software include: Translators (interpreter/compiler), linker, editor, debugger etc. These are installed and operated separately, and called individually as required during the programming.
a. Translators (Interpreters/Compilers)
A translator is a software that helps translate high-level programming code (source code) into machine code for subsequent execution by the computer. They are of two kinds:
i. Interpreter: An interpreter translates your source code into machine language one line at a time and executes, interactively. Languages like Python and Java use interpreters, which makes them slower.
ii. Compiler: This is a translator that converts all source code into a machine language file at a go before it is run. The file produced by a compiler is called object code file. Languages like C and C++ use compilers. The most popular compilers for C include: the open source, free GNU compiler collection (simply called gcc - MinGW/Cygwin), msvc, Clang, LLVM, Xcode for MacOSX, Digital Mars C/C++ compiler, Failsafe C etc.
b. Editor: This is a program in which you type the language code (source code). Popular editors programmers use for typing code are MS Notepad in Windows, NotePad++, DOS editor, Visual Studio (VS) Code, Sublime Text, Atom, Bluefish etc.
c. Debugger: A debugger is a software used for checking and correcting code. It lets the programmer step
through the code one line at a time to see how it is executing and possibly find what is wrong at any given line. Popular debuggers for C include gdb that works with gcc compiler, dbx that works with Sun CC, kdevelop for Linux, Valgrind is especially good for detecting memory leaks. Others include Totalview, DDD and cgdb.
d. Linker: this is a program that combines separate object files and libraries into a single executable file that can then be loaded into memory and run by the OS’s loader.
Compiler toolchain
Nowadays linker, compiler, assembler and debugger may come bundled together in what is called a compiler toolchain. When you install the compiler, they are all installed along.
Stand-alone compiling is often CUI
Using stand-alone software requires entering commands on the command line to call and use each of the software when necessary. It can be tedious and steep learning for a beginner.
1.4.2 CODING ON THE COMMAND LINE
This section is for those that may want to work from the command prompt (cmd) in Windows.
a. Check that you have a gnu gcc compiler installed:
- On Windows search bar, type cmd and press Enter on the keyboard.
- When the black command window shows up, type:
gcc -v
- Some text will appear giving information about your gcc compiler including the version if it is installed.
b. Install gcc compiler
- If gcc is not installed download any variant of it (TDM gcc or Cygwin), from the gnu website: https://gcc.gnu.org/ It’s free. Ensure to download 32bit or 64bit according to your Windows version. It is recommended you download the 64 bit, mingw–w64. It works also with 32bit Windows anyway.
Download the latest version (as at time of this writing, it is v7.0.)
- Double click the installer file downloaded to your computer and follow the prompts. Ensure to disable the checkbox for Search for Update
when about to install.
- Check that it installed afterward by typing gcc –v on cmd as done previously in 1.4.2 (a) above.
c. Type & Compile program
With a gcc compiler installed now we are ready to write and compile our first program from the command line.
- Open Notepad or any code editor installed on your computer. Type the following program and save it as welcome.c:
//my first program in C
#include
int main()
{
char name[20];
printf(Hi, I am C, what’s your name?
);
scanf(%s
, name);
printf(%s, you are welcome
, name);
return 0;
}
Save the file to My Documents or a folder of your own creation. Don’t worry if the program doesn’t make sense to you. You will understand it after completing the next chapter. Here we only want to learn to write and compile from cmd.
- To compile the program, go back to cmd. Change to the directory where the welcome.c file is. Suppose it is in Documents. Use cd to change directory. Type:
cd Documents
Press Enter.
The command line changes (depending on the name you assigned to your computer) to: c:\users\yourComputerName\Documents.
for example: c:\users\bigbob\Documents
then to compile, type: gcc welcome.c (general format is: gcc filename.c)
- To run (execute) the file after compiling, type: filename.exe
For this example we type: welcome.c. Press Enter.
You get an output on the cmd to enter your name as the program requires. Type the name and press Enter.
If you have problems getting the file to compile, check for typing errors. Follow the use of punctuation and spacing etc. strictly. Plus ensure you save the file in Notepad with the .c extension. Use Save As
option on Notepad and take care to change the Save As type
setting from *.txt to All files before saving your file with the .c extension.
If problems in compiling persist, seek the help of your instructor or on-line by typing a concise description of the issue or contact me on Quora or my author page. See the page Contact Mallam Awal
at the end of this book.
1.4.3 INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)
This is a single application that integrates all the programming tools in one place. An IDE often provides a gui window in which you can type your code, debug, run/test, compile and link all in one application. IDEs make programming easy and more fun by providing gui facilities like mouse commanding, and easy to use menus from which you do all your programming at one location.
Popular IDEs for C include: DevC++, CodeBlocks, Eclipse, Visual Studio Express, NetBeans etc. Most of these are free. IDEs are recommended if you are a beginning programmer. It takes some of the complexity of programming away, making you focus on actual coding. I recommend you start with an IDE. Later you can learn command line programming with Stand-alones.
For C programming you must install one of the above IDEs. For a total beginner, my recommendation is to download and install DevC++ with a TDM GCC compiler. DevC++ has less installation and configuration hassles. Its interface is also easier to learn and use. You can download it for free from www.bloodshed.net/devcpp.html When you are more comfortable, you could install the more sophisticated IDE called CodeBlocks. You can download it for free at: www.codeblocks.org
1.5 Download & Use Your first IDE
DevC++ is a simple IDE easy to download and use for a beginner. In this section, we shall download and use it to compile and run the example program welcome.c
. You can download one from the Bloodshed.net, the developer’s website. However, sourceforge has one that is pre-bundled with gcc. Download this if you have not already installed gcc compiler. We go for this.
1.5.1 To download, go to https://www.sourceforge.net/projects/orwelldev.cpp. Click download.
1.5.2 To install, double click the icon of the installer downloaded to your computer. Allow the default language selection English
or change as appropriate for you. Follow and answer other prompts. Wait for it to install.
1.5.3 To write, compile and run your first program:
- Start DevC++ from Start menu (in Windows). On File menu, click New
> Source File
. A blank page opens in the editor.
- Type or copy your program into the editor. It is recommended you type the code to get a feel of the editor. Notice code highlighting and code completion assistance as you type.
- Click Save on File menu or the shortcut bar below menu bar. In the Save As
dialog box, type the name of your file with the .c extension i.e. welcome.c for our example. Click Save
- On the Execute menu, click Compile
or press F9 on the keyboard. There is some flourish in the Compile Log panel below the code window after which it shows compile success report (Errors 0, Warnings 0, …) or any errors detected.
- To run the compiled program: On Execute menu, click Run
or press F10 on the keyboard.
- Voila! A small console window shows up with your program executing!
When you are done with the program, press any key to exit.
1.6 THE WORKINGS OF A PROGRAM
A computer program is a file containing lines of code (LoC) that perform a task or a set of related tasks. Developing a program involves thinking up a series of steps (algorithm) to perform the tasks to solve a programmable problem, and translating this into a process understandable by a computer using a programming language.
A program can be viewed and planned as an input-processing-output (IPO) process. A program needs to:
i. Accept (take) input (data) from the user.
ii. Perform some transformation on the data to convert it.
iii. Emit some output (as information or action) desired by the user.
A programming language at the minimum has to provide:
i. syntax i.e. vocabulary (word sets) and rules of their sequencing and combination. The syntax should provide words/punctuation and rules for the programmer to direct receiving input, performing transformations and issuing output by the program.
ii. semantics: language rules that specify correct usage of the syntax and what is meaningful.
Learning to program in a language requires mastering the syntax and semantics of the language in addition to designing algorithms and running its operating software.
1.7 NATURE OF INPUTS, PROCESSING & OUTPUTS OF A PROGRAM
As we saw in section 1.2, a program takes input transforms it and provides an output to the user by giving directions to the hardware (input devices, memory/RAM, output devices (consoles)).
a. Inputting:
The inputs to a program could come in the form of keystrokes from keyboard, mouse clicks, a signal over a network or from device or sensor, or some action on the part of a user (like gesture) or some action from a mechanism (such as a bimetal expanding while hot and triggering a signal).
b. Processing (or transformations)
Transformation by a program manipulates and converts the inputted data into more desirable and meaningful form for the user. Some major processings a program does include:
i. Basic i/o: Receive input and display output