Unit-1: (Fundamentals)
Unit-1: (Fundamentals)
Unit-1: (Fundamentals)
(Fundamentals)
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
C is the most widely used computer language. It keeps fluctuating at number one
scale of popularity along with Java and Python programming languages, which is also
equally popular and most widely used among modern software programmers.
Facts about C
C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around the early 1970s.
The language was formalized in 1988 by the American National Standard
Institute (ANSI).
The UNIX OS was totally written in C.
Today C is the most widely used and popular System Programming Language.
Most of the state-of-the-art software have been implemented using C.
Today's most popular Linux OS and RDBMS MySQL have been written in C.
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
The C Compiler
The source code written in source file is the human readable source for your program.
It needs to be "compiled", into machine language so that your CPU can actually
execute the program as per the instructions given.
The compiler compiles the source codes into final executable programs. The most
frequently used and free available compiler is the GNU C/C++ compiler, otherwise you
can have compilers either from HP or Solaris if you have the respective operating
systems.
The following section explains how to install GNU C/C++ compiler on various OS. We
keep mentioning C/C++ together because GNU gcc compiler works for both C and C++
programming languages.
Character set of C
character:-
Character set:-
The character set is the fundamental raw material of any language and they are used
to represent information. Like natural languages, computer language will also have
well defined character set, which is useful to build the programs.
SPECIAL CHARACTERS
WHITESPACE CHARACTERS
For example:
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 as
shown in the previous example.
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 –
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).
Keywords
The following list shows the reserved words in C. These reserved words may not be
used as constants or variables or any other identifier names.
Whitespace in C
A line containing only whitespace, possibly with a comment, is known as a blank line,
and a C compiler totally ignores it.
Whitespace is the term used in C to describe blanks, tabs, newline characters and
comments. Whitespace separates one part of a statement from another and enables
the compiler to identify where one element in a statement, such as int, ends and the
next element begins. Therefore, in the following statement –
There must be at least one whitespace character (usually a space) between int and age
for the compiler to be able to distinguish them. On the other hand, in the following
statement –
No whitespace characters are necessary between Area and =, or between = and Length,
although you are free to include some if you wish to increase readability.
Data Type
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 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; where as other types will be covered in the upcoming units.
Integer Types
The following table provides the details of standard integer types with their storage
sizes and value ranges −
When you compile and execute the above program, it returns size of the integer 4 byte
on Linux.
Floating-Point Types
The following table provides the details of standard floating-point types with storage
sizes and value ranges and their precision −
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 −
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.
lvalue − Expressions that refer to a memory location are called "lvalue" expressions.
An lvalue may appear as either the left-hand or right-hand side of an assignment.
rvalue − The term rvalue refers to a data value that is stored at some address in
memory. An rvalue is an expression that cannot have a value assigned to it which
means an rvalue may appear on the right-hand side but not on the left-hand side of
an assignment.
Variables are lvalues and so they may appear on the left-hand side of an assignment.
Numeric literals are rvalues and so they may not be assigned and cannot appear on
the left-hand side. Take a look at the following valid and invalid statements –
Defining Constants
There are two simple ways in C to define constants −
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
Type Conversion
Operands that differ in type may undergo type conversion before the expression takes
on its final value. The final result has highest precision possible, consistent with data
types of the operands. The following rules apply when neither operand is unsigned.
If one operand is a floating point type and the other is a char or an int, the
char/int will be converted to the floating point type of the other operand and the
result will be expressed as such. So, an operation between an int and a double will
result in double.
If both operands are floating-point types with different precisions, the lower-
precision operand will be converted to the precision of the other operand and the
result will be expressed in this higher precision.
Float & double ----> double
Float & long double ----> long double
Double & long double ---->long double
If neither operand is a floating point type or a long int, then both operands will be
converted to int & the result will be int.
If neither operand is a floating point type but one is a long int, the other will be
converted to long int & the result will be long int.
The value of an expression can be converted to a different data type if desired. The ‘C’
programmer also has the choice of explicitly specifying how the values are to be
converted in a mixed mode expression. This feature is known in ‘C’ as coercion and
may be accomplished using what is called the cast operator . The name of data type
to which the conversion is to be made is enclosed in parentheses and placed directly
to the left of the value to be converted; the word “cast” never is actually used. The
example of type casting is as follows:
Remember due to type casting, the data type associated with the expression itself is
not changed, but it is the value of the expression that undergoes type conversion
wherever the cast appears. This is particularly relevant when the expression consists
of only a single variable.
Precedence
The operator within C are grouped hierarchically according to their order of evaluation
known as precedence. Obviously operations with a higher precedence are carried out
before operations having a lower precedence. The natural order can be altered by
making use of parentheses.
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.