Monday, May 8, 2017
Saturday, May 6, 2017
Sunday, April 30, 2017
Thursday, April 6, 2017
Friday, March 24, 2017
Proper Guide Book in C Programming Language (You can be a Programmer)
Proper Guide Book in C Programming
Language
You
can be a Programmer
|
|||
|
Name: DM. Mehedi
Hasan Abid
Email:dmabid2050@gmail.com
CEO at Binary Pathshala
Learn Today Lead Tomorrow.
You can be a Programmer.
Email:dmabid2050@gmail.com
CEO at Binary Pathshala
Learn Today Lead Tomorrow.
You can be a Programmer.
Abstract
Hi! My name is DM. Mehedi Hasan
Abid, I am student of Daffodil International University. I have make a proper
guide of C Programming Language. There are more than 100 examples and 100+
question in this book, this is the speciality of the book.If anyone practice
all the example and solve all the question in the question which I have given
in the last. He can be a good programmer. At last best wishes to everyone and
happy coding.
C Programming Language
History
C is a powerful general-purpose
programming language. It is fast, portable and available in all platforms. If
you are new to programming, C is a good choice to start your programming journey.
This is a comprehensive guide on how to get started in C programming language,
why you should learn it and how you can learn it.
Then let’s lean about the history of C Programming Language.C was
developed at Bell Laboratories in 1972 by Dennis Richie. Many of its
principles and ideas were taken from the earlier language B and B's earlier
ancestors B CPL and CPL. CPL (Combined Programming Language) was developed with
the purpose of creating a language that was capable of both high level, machine
independent programming and would still allow the programmer to control the
behavior of individual bits of information. The one major drawback of CPL was
that it was too large for use in many applications. In 1967, B CPL (Basic CPL)
was created as a scaled down version of CPL while still retaining its basic
features. In 1970, Ken Thompson, while working at Bell Labs, took this process
further by developing the B language. B was a scaled down version of BCPL
written specifically for use in systems programming. Finally in 1972, a
co-worker of Ken Thompson, Dennis Ritchie, returned some of the generality
found in BCPL to the B language in the process of developing the language we
now know as C.
In 1983, the American National Standards Institute (ANSI) formed a
committee to establish a standard definition of C which became known as ANSI
Standard C. Today C is in widespread use with a rich standard library of
functions.
What will you gain if you learn C?
If you don’t know C, you don’t know what you are doing as a
programmer. Sure, your application works fine and all.
1. You will understand how a computer
works.
If you know C, you will not only know how your program works but, you will be able to create a mental model on how a computer works (including memory management and allocation). You will learn to appreciate the freedom that C provides unlike Python and Java.
Understanding C allows you to write programs that you never thought were possible before (or at the very least, you will have a broader understanding of computer architecture and programming as a whole).
If you know C, you will not only know how your program works but, you will be able to create a mental model on how a computer works (including memory management and allocation). You will learn to appreciate the freedom that C provides unlike Python and Java.
Understanding C allows you to write programs that you never thought were possible before (or at the very least, you will have a broader understanding of computer architecture and programming as a whole).
2. C is the lingua franca of
programming.
Almost all high-level programming languages like Java, Python, and JavaScript etc. can interface with C programming. Also, it’s a good language to express common ideas in programming. Doesn’t matter if the person you are talking with doesn’t know C, you can still convey your idea in a way they can understand.
Almost all high-level programming languages like Java, Python, and JavaScript etc. can interface with C programming. Also, it’s a good language to express common ideas in programming. Doesn’t matter if the person you are talking with doesn’t know C, you can still convey your idea in a way they can understand.
3. Opportunity to work on open source
projects that impact millions of people.
At first, you may overlook the fact that C is an important language. If you need to develop a mobile app, you need Java (for Android), Swift and Objective C (for iOS). And there are dozens of languages like C#, PHP, ASP.net, Ruby, Python for building web application. Then, where is C programming?
Python is used for making wide range for applications. And, C is used for making Python. If you want to contribute to Python, you need to know C programming to work on Python interpreter that impacts millions of Python programmers. This is just one example. A large number of software’s that you use today is powered by C.
Some of the larger open source projects where C programming is used are Linux Kernel, Python Interpreter, and SQLite Database.
Another language that’s commonly used for large open source project is C++. If you know C and C++, you can contribute to large open source projects that impacts hundreds of millions of people.
At first, you may overlook the fact that C is an important language. If you need to develop a mobile app, you need Java (for Android), Swift and Objective C (for iOS). And there are dozens of languages like C#, PHP, ASP.net, Ruby, Python for building web application. Then, where is C programming?
Python is used for making wide range for applications. And, C is used for making Python. If you want to contribute to Python, you need to know C programming to work on Python interpreter that impacts millions of Python programmers. This is just one example. A large number of software’s that you use today is powered by C.
Some of the larger open source projects where C programming is used are Linux Kernel, Python Interpreter, and SQLite Database.
Another language that’s commonly used for large open source project is C++. If you know C and C++, you can contribute to large open source projects that impacts hundreds of millions of people.
4. You will write better programs.
To be honest, this statement may not be true all the time. However, knowing how computer works and manage memory gives your insight on how to write efficient code in other programming languages.
To be honest, this statement may not be true all the time. However, knowing how computer works and manage memory gives your insight on how to write efficient code in other programming languages.
5. You will find it much easier to
learn other programming languages.
A lot of popular programming languages are based on C (and C++, considered superset of C programming with OOP features). If you know C, you will get a head start learning C++.
Languages like C# and Java are related to C and C++. Also, the syntax of JavaScript and PHP is similar to C.
If you know C and C++ programming, you will not have any problem switching to another language.
A lot of popular programming languages are based on C (and C++, considered superset of C programming with OOP features). If you know C, you will get a head start learning C++.
Languages like C# and Java are related to C and C++. Also, the syntax of JavaScript and PHP is similar to C.
If you know C and C++ programming, you will not have any problem switching to another language.
Compile and run C programming on your OS
To run C Programming in
Windows we can use different types of software or with the compiler we can run
C program from command prompt (cmd), there are different types of software’s
(Code blocks, DevC++, Visual studio). The most popular software for C
Programming Language is Code blocks so let’s see how to download a software
called Code::Blocks. Then, write C code, save the file with .c extension and
execute the code.
To make this
procedure even easier, follow this step by step guide.
- Go to the binary
release download page of Code:Blocks official site.
3.Open the Code::Blocks Setup file and
follow the instructions (Next > I agree > Next > Install);
you don’t need to change anything. This installs the Code::Blocks with gnu
gcc compiler, which is the best compiler to start with for beginners.
Now, open Code::Blocks and go to File > New > Empty file (Shortcut: Ctrl
+ Shift + N)
5.Write the C code and save the file
with .c extension. To save the file, go to File
> Save (Shortcut: Ctrl + S).
Important: The filename should end with a .c extension,
like: hello.c, your-program-name.c
6.To run the program, go to Build
> Build and Run (Shortcut: F9). This will build
the executable file and run it you will see the output like the below
Image.
If your program
doesn’t run and if you see error message like the image below "can't find
compiler executable in your search path (GNU GCC compiler)"
Then go to Settings
> Compiler > Toolchain executables and click Auto-detect.
This should solve the issue in most cases.
.The Fun Begins from here
so let’s start
You will learn to write a
“Hello, World!” program in this section.
Why “Hello, World!” program?
“Hello, World!” is a simple
program that displays “Hello, World!” on the screen. Since, it’s a very simple
program, it is used to illustrate the basic syntax of any programming language.
This program is often used to
introduce programming language to a beginner. So, let’s get started.
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
How “Hello, World!” program
works?
Include
stdio.h header file in your program.
C programming is small and
cannot do much by itself. You need to use libraries that are necessary to run
the program. The
stdio.h
is a header file and C compiler knows
the location of that file. To use the file, you need to include it in your
program using #include
preprocessor.
Why do
you need stdio.h file in this program?
In this program, we have used
printf()
function
which displays the text inside the quotation mark. Since printf()
is
defined in stdio.h
,
you need to include stdio.h
.
The
main() function
In C programming, the code
execution begins from the start of main() function (doesn’t matter if
main()
isn’t
located at the beginning).
The code inside the curly
braces { } is the body of
main()
function. The main()
function
is mandatory in every C program.int main() {
}
This program doesn’t do
anything but, it’s a valid C program.
The
printf() function
The
printf()
is
a library function that sends formatted output to the screen (displays the
string inside the quotation mark). Notice the semicolon at the end of the
statement.
In our program, it displays Hello, World! on the screen.
Remember, you need to include
stdio.h
file
in your program for this to work.
The
return statement
The return statement
return 0;
inside
the main()
function ends the program. This
statement isn’t mandatory. However, it’s considered good programming practice
to use it.
Key
notes to take away
- All C program starts from the
main()
function and it’s mandatory. - You can use the required header file
that’s necessary in the program. For example: To use
sqrt()
function to calculate square root andpow()
function to find power of a number, you need to includemath.h
header file in your program. - C is case-sensitive; the use of
uppercase letter and lowercase letter have different meanings.
- The C program ends when the program
encounters the return statement inside the
main()
function. However, return statement inside the main function is not mandatory. - The statement in a C program ends
with a semicolon.
C Keywords
Keywords are predefined,
reserved words used in programming that have a special meaning. Keywords are
part of the syntax and they cannot be used as an identifier. For example:
int money;
Here, int is a keyword that indicates 'money' is a variable
of type integer.
As C is a case sensitive
language, all keywords must be written in lowercase. Here is a list of all
keywords allowed in ANSI C. There are 32 keywords in C Programming Language.
Keywords in C Language
|
|||
auto
|
double
|
int
|
struct
|
break
|
else
|
long
|
switch
|
case
|
enum
|
register
|
typedef
|
char
|
extern
|
return
|
union
|
continue
|
for
|
signed
|
void
|
do
|
if
|
static
|
while
|
default
|
goto
|
sizeof
|
volatile
|
const
|
float
|
short
|
unsigned
|
Now may be you
are thinking what is variable? Ok then let’s see what is variable?
Basic about Variables
In programming, a variable is a
container (storage area) to hold data.
To indicate the storage area,
each variable should be given a unique name (identifier). Variable names are
just the symbolic representation of a memory location. For example:
int playerScore = 95;
Here, playerScore is a variable of integer type. The
variable is assigned value: 95.
The value of a variable can be
changed, hence the name 'variable'.
In C programming, you have to
declare a variable before you can use it.
Rules
for naming a variable in C
- A variable name can have letters
(both uppercase and lowercase letters), digits and underscore only.
- The first letter of a variable should
be either a letter or an underscore. However, it is discouraged to start
variable name with an underscore. It is because variable name that starts
with an underscore can conflict with system name and may cause error.
- There is no rule on how long a
variable can be. However, only the first 31 characters of a variable are
checked by the compiler. So, the first 31 letters of two variables in a
program should be different.
C is a strongly typed language.
What this means it that, the type of a variable cannot be changed.
Tokens
in C
A C program consists of various tokens and a token is either a
keyword, an identifier, a constant, a string literal, or a symbol. For example,
the following C statement consists of five tokens −
printf("Hello, World! \n");
The individual tokens are −
printf
(
"Hello, World! \n"
)
;
Semicolons
In a C program, the semicolon is a statement terminator. That is,
each individual statement must be ended with a semicolon. It indicates the end
of one logical entity.
Given below are two different statements −
printf("Hello, World! \n");
return 0;
Comments
Comments are like helping text in your C program and they are
ignored by the compiler. They start with /* and terminate with the characters
*/ as shown below −
/* my first program in C,
This is for multiline comment*/
Single line comments is identified by // as shown below −
// my first program in C,
This is single line comment.
You cannot have comments within comments and they do not occur
within a string or character literals.
Identifiers
A C identifier is a name used to identify a variable, function, or
any other user-defined item. An identifier starts with a letter A to Z, a to z,
or an underscore '_' followed by zero or more letters, underscores, and digits
(0 to 9).
C does not allow punctuation characters such as @, $, and % within
identifiers. C is a case-sensitive programming language. Thus, Manpower and manpower are two different identifiers in C.
Here are some examples of acceptable identifiers −
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
C Programming Data Types
![C Programming Data Types](file:///C:/Users/MHABID~1/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg)
In C programming, variables or
memory locations should be declared before it can be used. Similarly, a
function also needs to be declared before use.
Data types simply refers to the
type and size of data associated with variables and functions
Data types in C
- Fundamental Data Types
- Integer types
- Floating type (float, double)
- Character type
- Derived Data Types
·
Arrays
·
Pointers
·
Structures
·
Enumeration
This tutorial will focus on
fundamental data types. To learn about derived data types, visit the
corresponding tutorial.
Integer Types
The following table provides the details of standard integer types
with their storage sizes and value ranges −
Type
|
Storage size
|
Value range
|
char
|
1 byte
|
-128 to 127 or 0 to 255
|
unsigned char
|
1 byte
|
0 to 255
|
signed char
|
1 byte
|
-128 to 127
|
int
|
2 or 4 bytes
|
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
|
unsigned int
|
2 or 4 bytes
|
0 to 65,535 or 0 to 4,294,967,295
|
short
|
2 bytes
|
-32,768 to 32,767
|
unsigned short
|
2 bytes
|
0 to 65,535
|
long
|
4 bytes
|
-2,147,483,648 to 2,147,483,647
|
unsigned long
|
4 bytes
|
0 to 4,294,967,295
|
To get the exact size of a type or a variable on a particular
platform, you can use the sizeof operator. The expressions sizeof (type) yields the storage size of the object
or type in bytes. Given below is an example to get the size of int type on any
machine −
#include <stdio.h>
#include <limits.h>
int main() {
printf("Storage size for int : %d \n", sizeof(int));
return 0;
}
When you compile and execute the above program, it produces the
following result on Linux −
Storage size for int : 4
Floating-Point
Types
The following table provide the details of standard floating-point
types with storage sizes and value ranges and their precision −
Type
|
Storage size
|
Value range
|
Precision
|
float
|
4 byte
|
1.2E-38 to 3.4E+38
|
6 decimal places
|
double
|
8 byte
|
2.3E-308 to 1.7E+308
|
15 decimal places
|
long double
|
10 byte
|
3.4E-4932 to 1.1E+4932
|
19 decimal places
|
The header file float.h defines macros that allow you to use these
values and other details about the binary representation of real numbers in
your programs. The following example prints the storage space taken by a float
type and its range values −
#include <stdio.h>
#include <float.h>
int main() {
printf("Storage size for float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );
return 0;
}
When you compile and execute the above program, it produces the
following result on Linux (Don’t worry about what is linux it’s just another
operating system like windows) −
Storage size for float: 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6
Difference between float and double
The size of float (single precision float data type)
is 4 bytes. And the size of double(double
precision float data type) is 8 bytes. Floating point variables has a precision
of 6 digits whereas the precision of double is 14 digits.
char - Character types
Keyword char is used for declaring character
type variables. For example:
char test = 'h'
Here, test is a character variable. The value of test is 'h'.
The size of character variable is 1
byte.
Again Variable Definition in C
A variable definition tells the compiler where and how much
storage to create for the variable. A variable definition specifies a data type
and contains a list of one or more variables of that type as follows −
type variable_list;
Here, type must be a valid C data type including
char, w_char, int, float, double, bool, or any user-defined object; and variable_list may consist of one or more identifier
names separated by commas. Some valid declarations are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i,
j, k; declares and defines
the variables i, j, and k; which instruct the compiler to create variables
named i, j and k of type int.
Variables can be initialized (assigned an initial value) in their
declaration. The initializer consists of an equal sign followed by a constant
expression as follows −
type variable_name = value;
Some examples are −
extern int d = 3, f = 5; // declaration of d and f.
int d = 3, f = 5; // definition and initializing d and f.
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value 'x'.
For definition without an initializer: variables with static
storage duration are implicitly initialized with NULL (all bytes have the value
0); the initial value of all other variables are undefined.
Variable
Declaration in C
A variable declaration provides assurance to the compiler that
there exists a variable with the given type and name so that the compiler can
proceed for further compilation without requiring the complete detail about the
variable. A variable definition has its meaning at the time of compilation
only, the compiler needs actual variable definition at the time of linking the
program.
A variable declaration is useful when you are using multiple files
and you define your variable in one of the files which will be available at the
time of linking of the program. You will use the keyword extern to declare a variable at any place.
Though you can declare a variable multiple times in your C program, it can be
defined only once in a file, a function, or a block of code.
Example
Try the following example, where variables have been declared at
the top, but they have been defined and initialized inside the main function −
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () {
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
When the above code is compiled and executed, it produces the
following result −
value of c : 30
value of f : 23.333334
Defining Constants
There are two
simple ways in C to define constants −
·
Using #define preprocessor.
·
Using const keyword.
The #define
Preprocessor
Given below is the
form to use #define preprocessor to define a constant −
#define
identifier value
The following
example explains it in detail −
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main() {
int area;
area = LENGTH * WIDTH;
printf("value of
area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above code
is compiled and executed, it produces the following result −
value of area :
50
The const
Keyword
You can use const prefix
to declare constants with a specific type as follows −
const type
variable = value;
The following
example explains it in detail −
#include <stdio.h>
int main() {
const int
LENGTH = 10;
const int
WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of
area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above code
is compiled and executed, it produces the following result −
Value of area: 50
Note that it is a
good programming practice to define constants in CAPITALS.
Escape Sequences/
Character Constants
Sometimes, it is necessary to use
characters which cannot be typed or has special meaning in C programming. For
example: newline (enter), tab, question mark etc. In order to use these
characters, escape sequence is used.
For example: \n is used for newline. The
backslash ( \ ) causes
"escape" from the normal way the characters are interpreted by the
compiler.
Escape Sequences/ Character Constants
|
|
Escape Sequences
|
Character
|
\b
|
Backspace
|
\f
|
Form feed
|
\n
|
Newline
|
\r
|
Return
|
\t
|
Horizontal tab
|
\v
|
Vertical tab
|
\\
|
Backslash
|
\'
|
Single quotation mark
|
\"
|
Double quotation mark
|
\?
|
Question mark
|
\0
|
Null character
|
String constants
String literals or
constants are enclosed in double quotes "". A string contains
characters that are similar to character literals: plain characters, escape
sequences, and universal characters.
You can break a
long line into multiple lines using string literals and separating those using
white spaces.
Here are some
examples of string literals. All the three forms are identical strings.
"hello,
dear"
"hello, \
dear"
"hello,
" "d" "ear"