C
programming Language
C is a
general-purpose, procedural, imperative computer programming language developed
in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the
UNIX operating system. C is the most widely used computer language. It keeps
fluctuating at number one scale of popularity along with Java programming
language, which is also equally popular and most widely used among modern
software programmers.
C Language – Overview
C is a
general-purpose, high-level language that was originally developed by Dennis M.
Ritchie to develop the UNIX operating system at Bell Labs. C was originally
first implemented on the DEC PDP-11 computer in 1972.
In 1978,
Brian Kernighan and Dennis Ritchie produced the first publicly available
description of C, now known as the K&R standard.
The UNIX
operating system, the C compiler, and essentially all UNIX application programs
have been written in C. C has now become a widely used professional language
for various reasons
- Easy to learn
- Structured
language
- It produces
efficient programs
- It can handle
low-level activities
- It can be
compiled on a variety of computer platforms
Why use C?
C was initially used for system development work, particularly 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 the
code written in assembly language. Some examples of the use of C might be −
- Operating
Systems
- Language Compilers
- Assemblers
- Text Editors
- Print Spoolers
- Network Drivers
- Modern Programs
- Databases
- Language
Interpreters
- Utilities
C Programs
·
A C program can vary from 3 lines to millions of lines and it
should be written into one or more text files with extension ".c";
for example, hello.c. You can use "vi", "vim" or
any other text editor to write your C program into a file.
·
This tutorial assumes that you know how to edit a text file and
how to write source code inside a program file.
C - Program Structure
Hello World Example
A C
program basically consists of the following parts −
- Preprocessor
Commands
- Functions
- Variables
- Statements &
Expressions
- Comments
Let us
look at a simple code that would print the words "Hello World" –
#include <stdio.h>
int main() {
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
Compile and Execute C Program
Let us
see how to save the source code in a file, and how to compile and run it.
Following are the simple steps −
·
Open a text editor and add the above-mentioned code.
·
Save the file as hello.c
·
Open a command prompt and go to the directory where you have saved
the file.
·
Type gcc hello.c and press enter to compile your
code.
·
If there are no errors in your code, the command prompt will take
you to the next line and would generate a.out executable file.
·
Now, type a.out to execute your program.
·
You will see the output "Hello World" printed
on the screen.
$ gcc hello.c
$ ./a.out
Hello, World!
C - Data Types
Data
types in c refer to an extensive system used for declaring variables or
functions of different types. The type of a variable determines how much space
it occupies in storage and how the bit pattern stored is interpreted.
The types
in C can be classified as follows –
No.
|
Types & Description
|
1
|
Basic Types
They are arithmetic
types and are further classified into: (a) integer types and (b)
floating-point types.
|
2
|
Enumerated types
They are again
arithmetic types and they are used to define variables that can only assign
certain discrete integer values throughout the program.
|
3
|
The type void
The type
specifier void indicates that no value is available.
|
4
|
Derived types
They include (a)
Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e)
Function types.
|
The array
types and structure types are referred collectively as the aggregate types. The
type of a function specifies the type of the function's return value. We will
see the basic types in the following section, where as other types will be
covered in the upcoming chapters.
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
C - Variables
A
variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in C has a specific type, which determines the size
and layout of the variable's memory; the range of values that can be stored
within that memory; and the set of operations that can be applied to the
variable.
The name
of a variable can be composed of letters, digits, and the underscore character.
It must begin with either a letter or an underscore. Upper and lowercase
letters are distinct because C is case-sensitive. Based on the basic types
explained in the previous chapter, there will be the following basic variable
types –
Sr.No.
|
Type & Description
|
1
|
char
Typically a single
octet(one byte). This is an integer type.
|
2
|
int
The most natural size
of integer for the machine.
|
3
|
float
A single-precision
floating point value.
|
4
|
double
A double-precision
floating point value.
|
5
|
void
Represents the
absence of type.
|
C
programming language also allows to define various other types of variables,
which we will cover in subsequent chapters like Enumeration, Pointer, Array,
Structure, Union, etc. For this chapter, let us study only basic variable
types.
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;
C - Operators
An
operator is a symbol that tells the compiler to perform specific mathematical
or logical functions. C language is rich in built-in operators and provides the
following types of operators −
- Arithmetic
Operators
- Relational
Operators
- Logical
Operators
- Bitwise
Operators
- Assignment
Operators
- Misc Operators
Arithmetic Operators
·
The following table shows all the arithmetic operators supported
by the C language. Assume variable A holds 10 and variable B holds
20 then −
Operator
|
Description
|
Example
|
+
|
Adds two
operands.
|
A + B = 30
|
−
|
Subtracts
second operand from the first.
|
A − B = -10
|
*
|
Multiplies
both operands.
|
A * B = 200
|
/
|
Divides
numerator by de-numerator.
|
B / A = 2
|
%
|
Modulus
Operator and remainder of after an integer division.
|
B % A = 0
|
++
|
Increment
operator increases the integer value by one.
|
A++ = 11
|
--
|
Decrement
operator decreases the integer value by one.
|
A-- = 9
|
ogical Operators
Following
table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0,
then −
Operator
|
Description
|
Example
|
&&
|
Called
Logical AND operator. If both the operands are non-zero, then the condition
becomes true.
|
(A
&& B) is false.
|
||
|
Called
Logical OR Operator. If any of the two operands is non-zero, then the
condition becomes true.
|
(A ||
B) is true.
|
!
|
Called
Logical NOT Operator. It is used to reverse the logical state of its operand.
If a condition is true, then Logical NOT operator will make it false.
|
!(A
&& B) is true.
|
Bitwise Operators
Bitwise
operator works on bits and perform bit-by-bit operation. The truth tables for
&, |, and ^ is as follows –
p
|
q
|
p & q
|
p | q
|
p ^ q
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
1
|
1
|
1
|
1
|
1
|
0
|
1
|
0
|
0
|
1
|
1
|
Assignment Operators
The
following table lists the assignment operators supported by the C language
Operator
|
Description
|
Example
|
=
|
Simple
assignment operator. Assigns values from right side operands to left side
operand
|
C = A
+ B will assign the value of A + B to C
|
+=
|
Add
AND assignment operator. It adds the right operand to the left operand and
assign the result to the left operand.
|
C +=
A is equivalent to C = C + A
|
-=
|
Subtract
AND assignment operator. It subtracts the right operand from the left operand
and assigns the result to the left operand.
|
C -=
A is equivalent to C = C - A
|
*=
|
Multiply
AND assignment operator. It multiplies the right operand with the left
operand and assigns the result to the left operand.
|
C *=
A is equivalent to C = C * A
|
/=
|
Divide
AND assignment operator. It divides the left operand with the right operand
and assigns the result to the left operand.
|
C /=
A is equivalent to C = C / A
|
%=
|
Modulus
AND assignment operator. It takes modulus using two operands and assigns the
result to the left operand.
|
C %=
A is equivalent to C = C % A
|
<<=
|
Left
shift AND assignment operator.
|
C
<<= 2 is same as C = C << 2
|
>>=
|
Right
shift AND assignment operator.
|
C
>>= 2 is same as C = C >> 2
|
&=
|
Bitwise
AND assignment operator.
|
C
&= 2 is same as C = C & 2
|
^=
|
Bitwise
exclusive OR and assignment operator.
|
C ^=
2 is same as C = C ^ 2
|
|=
|
Bitwise
inclusive OR and assignment operator.
|
C |=
2 is same as C = C | 2
|
Operators Precedence in C
Operator
precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others;
for example, the multiplication operator has a higher precedence than the
addition operator.
For
example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a
higher precedence than +, so it first gets multiplied with 3*2 and then adds
into 7.
Here,
operators with the highest precedence appear at the top of the table, those with
the lowest appear at the bottom. Within an expression, higher precedence
operators will be evaluated first.
Category
|
Operator
|
Associativity
|
Postfix
|
() [] -> . ++ - -
|
Left to right
|
Unary
|
+ - ! ~ ++ - - (type)* & sizeof
|
Right to left
|
Multiplicative
|
* / %
|
Left to right
|
Additive
|
+ -
|
Left to right
|
Shift
|
<< >>
|
Left to right
|
Relational
|
< <= > >=
|
Left to right
|
Equality
|
== !=
|
Left to right
|
Bitwise AND
|
&
|
Left to right
|
Bitwise XOR
|
^
|
Left to right
|
Bitwise OR
|
|
|
Left to right
|
Logical AND
|
&&
|
Left to right
|
Logical OR
|
||
|
Left to right
|
Conditional
|
?:
|
Right to left
|
Assignment
|
= += -= *= /= %=>>= <<= &= ^= |=
|
Right to left
|
Comma
|
,
|
Left to right
|
No comments:
Post a Comment