C Coding Ultimate Step-By-Step Guide To Learning C programming fast Robert Anderson Copyright 2017 by Robert Anderson - All rights reserved. If you would like to share this book with another person, please purchase an additional copy for each recipient. Thank you for respecting the hard work of this author. Otherwise, the transmission, duplication or reproduction of any of the following work including specific information will be considered an illegal act irrespective of if it is done electronically or in print. This extends to creating a secondary or tertiary copy of the work or a recorded copy and is only allowed with express written consent from the Publisher. All additional right reserved.
TABLE OF CONTENTS Chapter History of C Running C on Window s Running C on Linu x Running C on Ma c Running C onlin e Prerequisite s Chapter Variables and Type s Arithmetic operation s Conditional s Iteratio n Function s Recursio n Array s User inpu t Quiz chapter Chapter 3 Pointer s Pointer Arithmeti c Function pointer s Storage Classification s File I/ O Exercis e Recursion Continue d Exercises chapter Chapter Structure s TypeDe f Enum s Exercis e Union s Variable argument list s Exercis e Exercises chapter Chapter 5 Header file s Pre-Processor Directive s Error Handlin g Type castin g Memory managemen t Exercis e Exercises chapter
Chapter 1
Introduction and Installation
History of C
C is a general-purposed computer programming language, that first appeared as a concept in 1972. The lead developer of C was Dennis Ritchie. The origin of C is closely related to the creation of the Unix OS. In this tutorial, we will go through the very basics of C right up to the intermediate level sections. The tutorials is designed to appeal to the first-time learners of a programming language.
Running C on Windows
Running C on Windows can be done with Visual Studio 2015.
Visual Studio can be downloaded directly from Microsoft, through the installation process C++ needs to be selected as language. The menu should look like this: After you select Visual C++ fully install the program. You should loads Visual Studio up and go to FILE -> New Project, youll be met by a Window like so: Select Win32 Console Application and click Ok A few menus will now appear, follow them through with the highlighted sections. (The highlighted tix-boxes need to be unchecked) : When the program template loads up you should notice you have not been provided with a C environment but a C++ one, you need to convert it to C: Go to the Solution explorer and it will open the Solution Explorer Menu Right click on the .cpp file and click Properties A menu will appear, expand the C/C++ menu, go to Advanced , loop for the Compile As section and select from the drop down menu Compile as C Code (/TC) The code is now ready to run! When the code you want to compile is reader click the green arrow on the top menu to run your console application.
Running C on Linux
Developing basic code on Linux is relatively easy first youll need the gcc compiler, if you dont have it installed run: apt-get install gcc-5 The next stage is to create your program, open up a text pad program (Leafpad in this case) use this basic program for testing purposes: #import int main() { printf("Hello, World!\n"); return 0; } Save this file with the .c extension. To compile and use the program youll use gcc like so: gcc -o Where; OutputProgtamName is the name of the executable you want C_FileName is the name of your C file To run the program just type the executables name followed by ./ ./
Running C on Mac
Running C on a Mac is relatively easy, open up the terminal and run clang --version Clang is a compiler built by Apple and with deal with all the aspects of translating code into machine code (1s and 0s) Start your notepad, write down your C code use this to start: #import int main() { printf("Hello, World!\n"); return 0; } Save this code with the .c extension.
Open up a terminal in the folder where you saved you .c file, and type make .c Where filename is the name of the file you just saved. To run the code just run it like so: ./.c
Running C online
One of my favourite online IDE is https://www.codechef.com/ide it provides a clear clean place to produce code.
Prerequisites
Throughout this tutorial you will see code snippets dotted around. Theres a few points that need explaining so you can get the most out of this tutorial: Comments Comments in code are signified by a // and the green colour, these are ignored by the computer and commenting is an important part of keeping computer code readable and easy to maintain. Starting coders tend to discard commenting and regret it later, dont be that programmer.
Variables and Types
The basis of a program is data, in the form of numbers and characters.
Variables and Types
The basis of a program is data, in the form of numbers and characters.
The storing, manipulation and output of this data gives functionality. Variables hold this data, think of it as a box used to store a single item. C is a heavily typed language where each variable must have a defined type. A type is an identifying keyword that defines what the variable can hold. The first type we will come across is the integer, this can hold real numbers (numbers without a decimal place), an integer is defined below: int integerName = 3; - int is the defined type keyword, we will learn about the difference possibilities later. - integerName is the ID for the variable, this can be anything you want to call it, this is used to allow a variable to have a meaningful name, the variable could be defined as int pineapple = 3; , but its good practise to make them relevant.
However, there are a few exception to this as a variable cannot be a single digit i.e. 4 or cannot contain special characters (!$%^&*) etc. - = 3; Is the assignment section, where the values of 3 is placed in the integer box for use later . This also ends with a semi colon, this is used to signify the end of a line. This variable can now be used in valid areas of the program, like so: int newInteger = integerName The value of intergerName defined earlier (3) will now be placed in the newInteger variable and they will now both have the value of 3. The value of integerName doesnt change as it is just being copied and placed into newInteger .
Next page