Amazon

Friday, June 24, 2016

C language Programming: Simple, Short, and Straightforward Way of Learning C Programming

C language Programming: Simple, Short, and Straightforward Way of Learning C Programming


Copyright 2016 - KDP Edition



Sherwyn Allibang




Edition License Notes
This book is authorized for your own personal use. This book may not be re-sold or offered away to other individuals. Much thanks to you for regarding the diligent work of the author.

Introduction
This book is for absolute beginners with or without prior knowledge in programming, as this book uses Simple words, Short sentences, and Straightforward paragraphs. The triple S way of learning C language programming. The topics covered in this book includes brief introduction to C language, variables, data types, control structures, functions, pointers, and input and output stream to external files. This book starts its discussion from short history to installation of the needed software resource and a step by step screen shots of how to write C language code, compile and execute C programs. It presents graphical representation of algorithms for simpler learning. This book is packed with working and running C program samples and after reading this book, the reader would be able to develop and create C language programs based particularly from problems given in computer science courses, hence, adopting to other programming language will be a lot easier. This book is your first step in your programming career.

TO GOD BE ALL THE GLORY!

The resources used in this book are:
> Personal Computer running Windows 7 Operating System     
> Dev-C++ available at http://bloodshed.net
Other Windows OS versions can also be used as long as it supports the above mentioned software. Also, you may ought to use other C compilers or IDE.


NOTE: All links provided in this book are updated occasionally. If a link becomes unavailable, email me at zherwyndbest@facebook.com


This chapter presents a brief overview of C language. It presents the things needed in starting C language programming.

Short history of C Language

C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Equipment Corporation PDP-11 computer in 1972. The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons:
1. Easy to learn.
2. Structured language.
3. Produces efficient programs.
4. It can handle low-level activities.
5. It can be compiled on a variety of computers.
6. It has a low-level language capability with the convenience of high-level programming.

C language was invented to write an operating system called UNIX. C is a successor of B language which was introduced around 1970. The language was formalized in 1988 by the American National Standard Institute (ANSI). By 1973 UNIX OS almost totally written in C. Today C is the most widely used System Programming Language. Most of the state of the art software have been implemented using C language.

Reasons to use C Language

To start with, I highly recommend C language for any beginning programmers to start their programming career because most of the high-level and other popular programming languages today is based from C language. Once you've learned the concepts behind programming language with C, adopting or migrating to other language will be easy as most of the things needed are pretty similar.

C was initially used for system development work, in particular the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:
1. Operating Systems
2. Language Compilers
3. Assemblers
4. Text Editors
4. Print Spoolers
5. Network Drivers
6. Modern Programs
7. Data Bases
8. Language Interpreters
9. Utilities
10. C Program File

Beginning to Program in C Language

All the C programs are written into text files with extension ".c" for example "hello.c", where ".c" is extension file name. These text files are called source files. The source files contains the C source codes and will later be transformed into a running executable file. The program development cycle of C language are:
1. Write the source codes into a text editor (ex: notepad) to create the source file.
2. Compile the source file to create the object file.
3. The object file is link into an executable (ex: .exe for windows) file.
4. Run the program and see the result.

Based from the program development cycle of C, notice that there are several things needed which includes a text editor, a compiler, and a linker. The good news is that, there is a software that comprises the three. It is called Dev-C++ which is available at http://bloodshed.net and for the download direct link at http://bloodshed-dev-c.en.softonic.com/download.
Dev-C++ is a C source code editor, a compiler, and a linker. All the things needed in creating a C program.
Note: You may ought to use other C compilers.

Installing the Dev-C++

Download the installer at the link provided above and install. Figure 1.1 presents the first screen upon running the installer.


Definition of programming terms

The following terms are defined conceptually and operationally in order to have a common understanding of the terms used in this book.
Argument. Arguments are actual value being pass to a function.
Bug. A bug is a general term used to describe any unexpected problem with the program during execution or when the programming is already running.
Block. A block is composed of two or more C statements. Also called compound statements. A block is enclosed with a pair of { } braces.
CamelCase. CamelCase is the naming convention for compound word which uses capital letters to delimit the word parts. For example, "studName", "studAge", and "intNum".
Clause. A section in the statement.
Code. It is the term used for statements written that make up a C program.
Condition. A statement that evaluates to either true (non zero) or false (zero).
Data Type. It the type of data being stored to a storage location like variable.
Debug. This refers to the process of examining and removing errors from a program's source code.
Expression. It is a statement that result to a numeric value.
Function. Function perform a specific task.
Label. Label is used a give details to a certain line of code in C language, hence this labels are ignored by the compiler, thus it does not affect the output of the program. Usually used for internal documentation.
Null. In computer programming, null is the term used for an uninitialized, undefined, empty, or meaningless value.
Parameter. Parameters are argument declaration of a function.
Statement. A statement is a complete direction instructing the computer to carry out some task. In C, statements are usually written one per line, although some statements span multiple lines. This term can be interchangeably use with code.
Syntax. Syntax is the grammar, structure, or order of the elements in a C language or any other programming language statement. (Semantics is the meaning of these elements.)
Variable. Variables are storage location for data in C programs.

The components of a C program

Lets discuss the components of a C programs through a sample program. This program prints the word "Hello World!" into the screen.  

Code listings of figure 2.1
#include"stdio.h"
main()
  {
  /*The line below prints a string*/
  printf("Hello World!");
  getch();
  }

Line 1. #include"stdio.h"
This line includes a C library for the use of the program. The inclusion of library in C program usually comes first.

Syntax #include directive:
#include"<lib-name>"

The <lib-name> in the syntax should be replaced with the library name. The other term for library is "include file" because literally, a library is a separate disk file that contains information needed by the program or the compiler.
The #include directive instructs the C compiler to add the contents of library or include file (sometimes called "Header files") into the program during compilation. The library files usually comes with the compiler, hence in Dev-C++, several libraries comes together with its default installation. These library files should have a ".h" file extension.
In line 1, the library included is the "stdio.h" which stands for standard input/output. This library is used in dealing with programs that accepts input and provides output, hence this is the most used library. Other libraries are discussed in the succeeding chapters of this book.

Line 2. main()
This line defines the main body of the program. The only component that is required in every C program is the main() function. In its simplest form, the main() function consists of the name main followed by a pair of empty parentheses ( ) and a pair of braces { } presented in lines 3 and 7. Within the braces are statements that make up the main body of the program. Under normal circumstances, program execution starts at the first statement in the main() function and terminates at the last statement in main().

Lines 3 and 7. { }
The curly braces { } are used to enclose the program lines that make up every C function including the main() function. A group of one or more statements enclosed within braces is called a block. Line 3 signifies the start of the content of the main function and line 7 signifies its end.

Line 4. /*The line below prints a string*/
The /*   */ (slash asterisk asterisk slash), are called comments. Anything enclose within the /* and */ becomes a comment. Comments are text details usually used by programmers to reference a line code statement in the source file. The comments are ignored by the compiler, hence does not affect the output of the program. The program will execute and run properly with or without comments. In line 4, the comments are used to give a reference or details that the next line will print a string on-screen.

Syntax for single-line comment:
/* <string-comment> */

The <string-comment> in the syntax should be replaced with a text entries that represents the comment of the programmer in the code. The comment can be on a single line or multi-line. Line 4 is an example of single line comment. Other compilers allows // as single line comment, which anything that follows // in the same line is considered comment. DevC++ allows the use of // as a single line comment.

Syntax for multi-line comment:
/* <string-comment-string-comment
-string-comment>  */

Line 5. printf("Hello World!");
The printf() statement is a library function under "stdio.h" library that displays information on-screen. The printf() statement can display a simple text message or the value of one or more program variables.

Syntax of printf() in printing literal text:
printf("<string-to-print>");

In the syntax, anything inside the " " double quotes is considered a literal text and will be printed on-screen. The <string-to-print> should be replaced with the text entries to be printed on-screen. The printf function must end with a ; semi-colon.

Line 5. getch();
The getch() "get character", is library function that accepts a character from the keyboard. The entered character however is not displayed on-screen. If the getch() is omitted from the code, the program will run, but upon printing the word "Hello World!", the program will immediately terminate. The program output closes immediately and may not be seen by the user. Hence the getch() is used as the trigger that when the output is presented on-screen, the user will have to press a key in the keyboard before the program terminates. The getch() is used as a terminator for the rest of the sample programs in this book. The getch() function must end with a ; semi-colon.
Note: The getch() and getche() functions are not a standard C functions as they are part of the conio library, but most C compilers supports this function including Dev-C++ and Turbo C. If you are using other compiler (especially for Linux OS) and the program does not run, you may ought to use getchar() instead of getch().

Writing, compiling, and running our first program
In writing the source file, open the Dev-C++ and create a new source file as presented in figure 2.2.


Enjoyed the sample? You may download the complete book at AMAZON.COM

Topics covered are:
Short history of C Language
Reasons to use C Language
Beginning to Program in C Language
Installing the Dev-C++
Self-assessment questions
Definition of programming terms
The components of a C program
Writing, compiling, and running our first program
Correcting errors
Statements
Null Statements
White spaces
Self-assessment questions
Variable
Variable Declaration and definition
Scope of variables
Constants
Keywords
Conversion specifiers of data types
Self-assessment questions
Displaying text on-screen
puts() function
printf() function
Literal text
An escape sequence
Accepting user input
getch() function
getche()  and getchar() functions
scanf() function
gets() function
Self-assessment questions
What is an Array?
Single-Dimensional
Multi-Dimensional Array
Strings
Declaring and defining a string
Defining a string using input functions
Strings' pre-defined functions
Length of a String
Concatenation of Strings
Compare Two Strings
Copy Strings
Reversing the Order of a String
Converting Uppercase Strings to Lowercase Strings
Converting Lowercase Strings to Uppercase Strings
Self-assessment questions
Expressions
Operators
Assignment Operator
Mathematical Operators
Binary
Unary
Mathematical operators precedence level and parentheses
Relational Operators
Logical Operators
Type Casting
Pre-defined Mathematical Functions
acos()
asin()
atan()
atan2()
cos()
cosh()
sin()
sinh()
tan()
tanh()
exp()
log()
log10()
pow()
sqrt()
ceil()
fabs()
floor()
fmod()
Self-assessment questions
The if() statement
Single-alternative
Dual-alternative
Multiple-alternative
Nested if() statement
The switch() statement
Things to consider in conditional statement
Self-assessment questions
What is a Loop?
Counter-controlled loop
for() loop statement
Nested for() loop statement
Condition-controlled loop
Pre-test loop
Post-test loop
The Infinite Loop
Self-assessment questions
User-defined function
User-defined function prototype
User-defined function definition
Calling a user-defined function
Things to consider in functions:
Self-assessment questions
Structures
Declaring a structure
Defining a structure
Accessing Structure Members
Compound declaration and definition of structure
Self-assessment questions
Pointers in C language
Pointer Declaration and definition
How pointers works?
Pointer Arithmetic
Self-assessment questions
File management in C
Defining and opening a file
Closing a File
Reading and writing a file
putc() and getc() functions
fprintf() and fscanf() functions
Deleting a File
Renaming a File
Self-assessment questions



1 comment: