History Of C Language:
In computing, C is a general-purpose programming language initially developed by Dennis Ritchie between 1969 and 1973 at Bell Labs. Its design provides constructs that map efficiently to typical machine instructions, and therefore it found lasting use in applications that had formerly been coded in assembly language, most notably system software like the Unix computer operating system.C is one of the most widely used programming languages of all time, and there are very few computer architectures for which a C compiler does not exist. Many later languages have borrowed directly or indirectly from C, including C#, D, Go, Java, JavaScript, Limbo, LPC, Perl, PHP, Python, and Unix's C shell. The most pervasive influence on these languages has been syntactical, and they tend to combine the recognizable expression and statement syntax of C with underlying type systems, data models, and semantics that can be radically different. C++ started as a preprocessor for C and is currently nearly a superset of C. Before there was an official standard for C, many users and implementers relied on an informal specification contained in a book by Ritchie and Brian Kernighan; that version is generally referred to as "K&R" C. In 1989 the American National Standards Institute published a standard for C (generally called "ANSI C" or "C89"). The next year, the same specification was approved by the International Organization for Standardization as an international standard (generally called "C90"). ISO later released an extension to the internationalization support of the standard in 1995, and a revised standard (known as "C99") in 1999. The current version of the standard (now known as "C11") was approved in December of 2011.
Features and characteristics of c language
- General Purpose Programming
Language: it is general
purpose programming language. It is usually called “system programming
language” but equally suited to writing a variety of applications.
- Middle Level: As a middle
level language it bridges elements of high level language with the
functionality of assembly language.
- Structured Programming: C is very suited
for structured programming. The programmers can easily divide a problem
into a number of modules or functions.
- Simplicity: C is simple to
use because of its structured approach. It has a wide collection of
inbuilt functions, keywords, operators and data types.
- Portability: This refers to
the ability of a program to run in different environments. With the
availability of compilers for almost all operating systems and hardware
platforms, it is easy to write code on one system which can be easily
ported to another.
- Wide Acceptability: C is widely
popular in the software industry. Its importance is not entirely derived
from its use as primary development language but also because of its use
as an interface language to some of the visual languages.
- Flexibility: C Language
combines the convenience and portable nature of a high level language with
the flexibility of a low level language. It can be interfaced readily to
other programming languages.
- Efficient Compilation and
Execution: The process of
compilation and execution of programs is quite fast in C Language as
compared to other languages like BASIC (Beginner’s All purpose Symbolic Instruction Code) and FORTRAN
(FORmula TRANslator).
- Modularity: C Language
programs can be divided into small modules with the help of functions
which help in increasing understanding of the programs.
- Clarity: The features
like keywords, in-built functions and structures help to improve the
clarity and hence understanding of the program.
- High Availability: The software of
C Language is readily available in market and can be easily installed on
the computer.
- Easy Debugging: The syntax
errors can be easily detected by the C compiler. The error is displayed
with line number of the code and the error message.
- Memory Management: Various memory
management in-built functions are available in C language which helps to
save memory and hence improve efficiency of the program. e.g.
malloc(),alloc() and calloc().
- Recursion: Recursion is a
technique in which the function calls itself again and again until a
condition is achieved.
- Rich set of Library Functions: C has a rich set
of library functions. These are the functions that provide readymade
functionality to the users. It also supports graphic programming.
C Compiler
C Compiler |
A program that translates source code into object code.
The compiler derives its name from the way it works, looking at the entire
piece of source code and collecting and reorganizing the instructions.
Thus, a compiler differs from an interpreter,
which analyzes and executes each line of source code in
succession, without looking at the entire program. The advantage of
interpreters is that they can execute a program immediately. Compilers require
some time before an executable program emerges. However, programs produced by
compilers run much faster than the same programs
executed by an interpreter.
Every high-level programming
language (except
strictly interpretive languages) comes with a compiler. In effect, the compiler
is the language,
because it defines which instructions are acceptable. Because compilers
translate source code into object code, which is unique for each type of computer, many
compilers are available for the same language. For example, there is a FORTRAN compiler for PCs and another for Apple Macintosh computers.
In addition, the compiler industry is quite competitive, so there are actually
many compilers for each language on each type of computer. More than a dozen
companies develop and sell C compilers for the PC.
Keywords
"Keywords"
are words that have special meaning to the C compiler. In translation phases 7
and 8, an identifier cannot have the same spelling and case as a C keyword. The
C language uses the following keywords:
auto
|
double
|
int
|
struct
|
break
|
else
|
long
|
switch
|
case
|
enum
|
register
|
typedef
|
char
|
extern
|
return
|
union
|
const
|
float
|
short
|
unsigned
|
continue
|
for
|
signed
|
void
|
default
|
goto
|
sizeof
|
volatile
|
do
|
if
|
static
|
while
|
You cannot redefine
keywords. However, you can specify text to be substituted for keywords before
compilation by using C preprocessor directives.
Identifier
An Identifier is a name for a variable, type, type member, template, class, function,
namespace etc and is
usually limited to letters, digits and underscores. Certain words are reserved
and cannot be used as identifiers such as new.
Identifier can be freely named, the following restrictions.
·
Alphanumeric characters ( a ~ z , A~Z , 0~9 ) and half underscore ( _ ) can
only be used.
·
The first character of the first contain letters ( a ~ z , A~Z ) or half underscore ( _ ) can only
be used.
be used.
·
Case is distinguishable. That is, word and WORD is recognized as
a separate identifier.
·
Reserved words are not allowed. However, part of an identifier
reserved words can be
included.
included.
Here
are the rules you need to know:
1.
Identifier name must be a sequence of letter and digits, and must begin with a
letter.
2.
The underscore character (‘_’) is considered as letter.
3.
Names shouldn't be a keyword (such as int , float, if ,break, for etc)
4.
Both upper-case letter and lower-case letter characters are allowed. However,
they're not interchangeable.
5.
No identifier may be keyword.
6.
No special characters, such as semicolon, period, blank space, slash or comma
are permitted.
Examples
of legal and illegal identifiers follow, first some legal identifiers:
float
_number;
float
a;
int
this_is_a_very_detailed_name_for_an_identifier;
The
following are illegal:
float
:e;
float
for;
float
9PI;
float
.3.14;
float
7g;Algorithms and Flowchart:-
Algorithms
- A sequential solution of any
program that written in human language, called algorithm.
- Algorithm is first step of the
solution process, after the analysis of problem, programmer writes the
algorithm of that problem.
- Example of Algorithms:
Q. Write a algorithm to
find out number is odd or even?
Ans.
step 1 : start
step 2 : input number
step 3 : rem=number mod
2
step 4 : if rem=0 then
print "number even"
else
print "number odd"
endif
step 5 : stop
Flowchart
1. Graphical representation of any program is called flowchart.
2. There are some standard graphics that are used in flowchart as following:
Q. Make a flowchart to
input temperature, if temperature is less than 32 then print "below
freezing" otherwise print "above freezing"?
Integer Types
The "integral" types
in C form a family of integer types. They all behave like integers and
can be mixed together and used
in similar ways. The differences are due to the different
number of bits
("widths") used to implement each type -- the wider types can store a
greater ranges of values.
·
char
ASCII character -- at least 8 bits. Pronounced "car".As
apractical matter
char is basically always a byte which is 8 bits which is enough to store
asingle ASCII character. 8 bits provides a signed range of -128..127 or
an
unsigned rangeis0..255. char is also required to be the "smallest
addressable
unit" for the machine -- each byte in memory has its own address.
·
short
Small integer -- at least 16 bits which provides a signed range of
-32768..32767. Typical size is 16 bits. Not used
so much.
·
int
Default integer -- at least 16
bits, with 32 bits being typical. Defined to be the "most
comfortable" size for the computer. If you do not really care about
the range for an
integer variable, declare it int since that is likely to be an
appropriate size (16 or 32
bit) which works well for that machine.
·
long
Large integer -- at least 32 bits. Typical size is 32 bits which gives a
signed range
of about -2 billion ..+2 billion. Some compilers support "long
long" for 64 bit ints.
DataTypes
·
Standard –
int, float, char,double
·
User defined data types -
–arrays,
–structures,pointers,
–enumerateddatatypeetc.
C-
Constants
ð A constant is a quantity that does
not change during the the program execution. This quantity can be stored at a
location in the memory of computer.
ð Types of C- Constants
1. Primary Constants
· Integer Constant
· Real Constant
· Character Constant
2. Secondary Constants
· Array
· Pointer
· Structure
· Union
· Enum , etc.
# Rules For Constructing Integer Constants
· An integer constant must have at
least one digit.
· It must not have a decimal point.
· It can be either positive or
negative.
· If no sign precedes an integer
constant , it is assumed to be positive.
· No commas or blanks are allowed
within an integer constant.
· The allowable range of an integer
constants is -32768 to 37767.
· For Example - 426 , +782 , -8000 ,
-7605
# Rules For Constructing Real Constants
· A real constant must have at least
one digit .
· It must have a decimal point .
· It could be either positive or
negative.
· Default sign is positive.
· No commas or blanks are allowed
within a real constant.
· For Example - +325.34 , 426.0 ,
-32.76 , -48.5792
# Rules For Constructing Character Constants
· A character constant is a single
alphabet , a single alphabet , a single digit or a single special symbol
enclosed within single inverted commas. Both the inverted commas should point
to the left .
For Example : - ’A’ is a valid character constant
whereas ‘A’ is not.
· The maximum length of a character
constant can be 1 character
For Example :- ’A’ , ’I’ , ’5’
· There are special case char constants, such as '\t'
for tab, for characters which
are not convenient to type on a keyboard.
'A' uppercase
'A' character
'\n' newline character
'\t' tab character
'\0' the "null"
character -- integer value 0 (different from the char digit '0')
'\012' the character with value 12
in octal, which is decimal 10
Variables
ð A quantity that may vary during
program execution is called as variables.
ð Variables names are the names given
to the location in the memory of computer where different constants are stored
. These location can contain integer , real and characters.
ð The maximum length of a variable
would be 8 characters.
#
Rules For Constructing Variable Names
· A variable name is any combination of
1 to 31 alphabets , digits or underscores . Some compilers allow variable name
whose length could be upt o 247 characters. Still , it would be safer to stick
to the rule of 31 characters . Do not create unnecessarily long variables as it
adds to your typing effort.
· The first Character in the variable
name must be an alphabet or underscore.
· No commas or blanks are allowed
within a variable name.
· No special symbol other than an
underscore (as in gross_sal) can be used in
a variable name
· For Example :- si_int , m_hra ,
pop_e_89
Format
Specifies Meaning
1. %c - Read a single character.
2. %d - Read a decimal integer.
3. %e - Read a floating point value.
4. %f - Read a floating point value.
5. %g - Read a floating point value.
6. %h - Read a short integer.
7. %i - Read a decimal, hexadecimal, octadecimal
8. %o - Read an octal integer.
9. %s - Read a string.
10.%u - Read an unsigned decimal integer.
11.%x - Read a hexadecimal integer.
12.%l - Read a double floating value.
13.%L - Read a long double floating value.
Declaration
Form of Declaration:
typelist of variables;
/* each separated by , and finally
terminatedby ; */
Examples:
• intx, y, z;
Array
• charname[20]
·
charch = ‘A’;
/* character is enclosed within‘’*/
How
Do We Add Comments In C
· Comments in C are enclosed by slash/star pairs: /* .. comments .. */ which
may cross multiple lines. C++ introduced a form of comment started by two
slashes and extending to the end of the line: // comment until the line end
· The // comment form is so handy that
many C compilers now also support it, although it is not technically part of
the C language.
· Along with well-chosen function names,
comments are an important part of wellwrittencode. Comments should not just
repeat what the code says. Comments should describe what the code accomplishes
which is much more interesting than .a translation of what each statement does. Comments should
also narrate what is .tricky or non-obvious about a section
of code.
Note
: -
1. int – 2 bytes ( 16 bits )
2. char – 1 byte
3. float – 4 bytes
4. double – 8 bytes
Arithmetic
Expression
An expressionisacombinationof variables, constantsandoperatorswrittenaccordingtothe
syntax of C language.
· Every expression
evaluates to a value of a certain type that can be assigned to a variable.
Precedence
in Arithmetic Operators
·
· Anarithmeticexpressionwithoutparenthesiswill
beevaluatedfromlefttorightusingtherulesof precedence of operators.
·
· Therearetwodistinctprioritylevelsofarithmetic operators in C.
High priority * / %
Low
priority + -
Rules
for evaluation of an expression
• When Parenthesisare used, the expressions
within parenthesis assume highest priority.
• Parenthesized sub expression left to right are
evaluated.
• If parenthesis
are nested, the evaluation begins with the innermost sub expression.
• The precedence
rule is applied in determining
theorderofapplicationofoperatorsinevaluating sub expressions.
• The
associability rule is applied when two or more operators of the same precedence level appear in the sub expression.
Operator
precedence and associativity
• Each operator
in C has a precedence associatedwith it.
• The precedence
is used
to determine how
anexpression involvingmore than
one operatoris evaluated.
• There are
distinct levels of precedence and an operatormaybelongtoone ofthese levels.
• The operators
of higher
precedence are evaluatedfirst.
• The operators
of same precedence
are evaluatedfrom right
to left
or from
left to rightdepending onthe
level.
• This is known as
associativity property of
an operator
Examples
x + y * z / 2 + p
x + (y * z)/ 2 + p
x + ((y * z) /
2)+ p
(x + ((y * z) / 2))+ p
((x + ((y * z) / 2)) + p)
x + y - z / 2 * p
(x + y)- z / 2 *
p
(x + y) –
(z / 2)* p
(x + y) – ((z / 2) * p)
((x + y) – ((z /
2) * p))
Arithmetic
Expression
// the
valueof xisaddedwiththe valueof i
after
incrementing it by 1 then
i is incremented by 1
x +(++i); x + (i++);
after decreasing it by 1 then
i is decreased by 1.
x +(--i ); x = x +(i-- );
Operators
1. Arithmetic Operator.
2. Assignment Operator.
3. Logical Operator.
4. Bitwise Operator.
5. Relational Operator.
6. Increment / Decrement Operator.
7. Conditional / Ternary Operator.
8. Special / Comma Operator.
#
Arithmetic Operator
It includes the following operators
:-
§ + à Addition
§ - à Subtraction
§ * à Multiplication
§ / à Division
§ % à Remainder
(Mod)
#
Bitwise Operator
It includes the following operator :-
· & à bitwise
AND
· | à bitwise
inclusive OR
· ^ à bitwise
exclusive OR
· ~ à One’s
Compliment
· << à left shift
· >> à right shift
Do not confuse the Bitwise operators with the logical
operators. The
bitwise connectives
are one character wide (&, |) while the boolean
connectives are two characters wide (&&,||).
The bitwise operators
have higher precedence than the boolean operators. The
compiler will
never help you out with a type error if you use &
when you meant &&.
As far as the type checker is concerned, they are
identical-- they both
take and produce integers since there is no distinct
boolean type
# Logical Operator
It includes the following operator :-
· && à AND
· || à
OR
· ! à
NOT
#
Unary Increment & Decrement Operator
Increment Operator include ‘ ++ ’
Decrement Operator include ‘ -- ’
The
unary ++ and -- operators increment or decrement the value in a
variable.
There are"pre" and "post" variants for both operators which
do
slightly
different things (explainedbelow)
var++
|
increment
|
"post" variant
|
++var
|
increment
|
"pre"
variant
|
Pre
and Post Variations
The Pre/Post variation has to do with nesting a
variable with the increment or
decrementoperator inside an expression -- should the
entire expression represent
the value of the variable before or after the
change? I never use the operators in
this way (see below), but
an example looks like...
int i = 42;
int j;
j = (i++ + 10);
// i is now 43
// j is now 52 (NOT 53)
j = (++i + 10)
// i is now 44
// j is now 54
# Relational
Operator
It includes the following operator :-
· < à LESS
THAN
· <= à LESS
THAN EQUAL TO
· >= à GREATER
THAN EQUAL TO
· != à
NOT EQUAL TO
· == à
Equality
#
Ternary or Conditional Operator
Ternary Operator is ‘ ?:’
#
Assignment or Compound Assignment Operator
It includes the following operator :-
· += à Increment
by RHS
· -= à decrement
by RHS
· *= à Multiply
by RHS
· /= à divide
by RHS
· %= à Mod
by RHS
· &= à Bitwise
and RHS
· |= à Bitwise
OR by RHS
· ^= à BitwiseXOR
by RHS
· <<= à Bitwise
left shift RHS (multiply by power of 2)
· >>= à Bitwise
right shift by RHS (divide by power of 2)
#
Comma Operator or Special Operator
Comma Operator includes ‘ , ’
TYPE CONVERSIONS
by Aakriti(3rd CSE)
Data Types in C:
Integer
Types
Floating-Point
Types
The void
Type
TYPE CONVERSIONS
by Aakriti(3rd CSE)
C
permits mixing of constants and variables of different types in a expressions.
C automatically converts any intermediate values to the proper type so that the
expressions can be evaluated without losing any significance. The automatic
conversion is known as IMPLICIT TYPE CONVERSION.
For example, consider the
following assignment statements.
int i;
float b;
i=3.5;
b=30;
Here in the first assignment
statement though the expression’s value is a float(3.5) it cannot be stored in “i”
since it is an int. In such a case the float is denoted to an
int and then its value is stored .Hence what
gets stored in i is 3.
Exactly opposite happens in the next statement. Here
30 is promoted to 30.000000 and then
stored in b,since b being a float variable cannot hold anything except a float
value.
EXAMPLE:
float a,b,c;
int s;
S=a*b*c/100+32/4-3*1.1;
Here, in the assignment statement some operands are ints whereas others are floats. As we know, during evaluation of the expression the ints would be promoted to floats and the result of the expressions would be a float. But when this float value is assign to s it is again denoted to an int and then stored in s.
Observe the results of the arithmetic statements shown in below.It has been assumed that k is an integer variable and a is a real variable.
ARITHMETIC INSTRUCTIONS.
|
RESULT
|
ARITHMETIC INSTRUCTIONS
|
RESULT
|
K=2/9
|
0
|
a =2/9
|
0 0
|
K=2.0/9
|
0
|
a =2.0/9
|
0 2222
|
K=2/9.0
|
0
|
a =2/9.0
|
0 2222
|
K=2.0/9.0
|
0
|
a =2.0/9.0
|
0 2222
|
K=9/2
|
4
|
a =9/2
|
4 5
|
K=9.0/2
|
4
|
a=9.0/2
|
4 5
|
K=9/2.0
|
4
|
a =9/2.0
|
4 5
|
Note that though the followings statements gives the same results,0, the results are obtained differently.
K=2/9;
K=2.0/9;
In the first statements, since both 2 and 9 are integers,the results is an integer,i.e.0. The 0 is then assigned to k and so on…Data Types in C:
In the C programming
language, data types 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:
S.N.
|
Types and Description
|
1
|
Basic Types:
They are arithmetic types and consists of the two types: (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 be assigned 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 to collectively as the aggregate types. The type of a
function specifies the type of the function's return value. We will see basic
types in the following section, whereas, other types will be covered in the
upcoming chapters.
Integer
Types
Following table gives you
details about standard integer types with its 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. Following
is an example to get the size of int type on any machine:
#include
#include
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
Following table gives you
details about 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. Following example will
print storage space taken by a float type and its range values:
#include
#include
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:
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6
The void
Type
The void type specifies that
no value is available. It is used in three kinds of situations:
S.N.
|
Types and Description
|
1
|
Function returns as void
There are various functions in C which do not return value or you can say they return void. A function with no return value has the return type as void. For example void exit (int status); |
2
|
Function arguments as void
There are various functions in C which do not accept any parameter. A function with no parameter can accept as a void. For example, int rand(void); |
3
|
Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type. |
The void type may not be
understood to you at this point, so let us proceed and we will cover these
concepts in the upcoming chapters.
No comments:
Post a Comment