Chapter 3
Chapter 3
What is C?
History of C
Features of C
It is a robust language with rich set of built-in functions and operators that can be used to
write any complex program.
The C compiler combines the capabilities of an assembly language with features of a
high-level language.
Programs Written in C are efficient and fast. This is due to its variety of data type and
powerful operators.
It is many time faster than BASIC.
C is highly portable this means that programs once written can be run on another
machines with little or no modification.
Another important feature of C program, is its ability to extend itself.
A C program is basically a collection of functions that are supported by C library. We can
also create our own function and add it to C library.
C language is the most widely used language in operating systems and embedded system
development today.
Documentation Section
This section consists of comment lines which include the name of programmer, the author and
other details like time and date of writing the program. Documentation section helps anyone to
get an overview of the program.
Link Section
The link section consists of the header files of the functions that are used in the program. It
provides instructions to the compiler to link functions from the system library.
Definition Section
All the symbolic constants are written in definition section. Macros are known as symbolic
constants.
Subprogram Section
The subprogram section contains all the user defined functions that are used to perform a specific
task. These user defined functions are called in the main() function.
2. Digits
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
3. Specials Character
4. White Characters
Data Types
The data type in C defines the amount of storage allocated to variables, the values that they can
accept, and the operation that can be performed on those variables.
C is rich in data types. The verity of data type allow the programmer to select appropriate data type
to satisfy the need of application as well as the needs of different machine.
There are following type of data types supported by c programming
Primary Data Type
Derived Data Type
User Defined Data Type
Integer Type
Integers are the whole numbers, i.e. non-fractional numbers. They are defined in c by keyword
int. Generally integer require 16 bit of storage. Integers can be of following types:
Character Type
A single character can be defined as a character type data. Characters are stored in 8 bits. It is
represented by a keyword char. Its conversion character is %c.
struct st1{
int a;
float b;
char c;} Here 'struct' is a derived data types 'structure'. It consists of integer, float and character
variables. Structure, unions and enumerations are the derived data types used in C.
The C preprocessor is a collection of special statements, called directives that are executed at the
beginning of compilation process. Preprocessors directives usually appear at the beginning of a
program. A preprocessor directive may appear anywhere within a program. Preprocessor
directives follow special syntax rules that are different from the normal C syntax. They all begin
with the symbol # in column one and do not require a semicolon at the end. We have already
used the directives #define and #include to a limited extent. A set of commonly used
preprocessor directives and their functions are listed below:
Directives Functions
#define Defines a macro substitution
#undef Undefines a macro
#include Species the files to be included
#ifdef Test for a macro definition
#endif Specifies the end of #if
#ifndef Tests whether a macro is not defined
#if Tests a compile-time condition
#else Specifies alternatives when #if test fails.
Symbolic Constants
A symbolic constant is name that substitute for a sequence of character that cannot be changed.
The character may represent a numeric constant, a character constant, or a string. When the
program is compiled, each occurrence of a symbolic constant is replaced by its corresponding
character sequence. They are usually defined at the beginning of the program. The symbolic
constants may then appear later in the program in place of the numeric constants, character
constants, etc., that the symbolic constants represent.
For example:
A constant is a value or an identifier whose value cannot be altered in a program. For example: 1,
2.5, "C programming is easy", etc.
1. Integer constants
An integer constant is a numeric constant (associated with number) without any fractional or
exponential part. There are three types of integer constants in C programming:
decimal constant(base 10)
octal constant(base 8)
hexadecimal constant(base 16)
For example:
Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
In C programming, octal constant starts with a 0 and hexadecimal constant starts with a 0x.
2. Floating-point constants
A floating point constant is a numeric constant that has either a fractional form or an exponent
form. For example:
-2.0
0.0000234
-0.22E-5
Note: E-5 = 10-5
3. Character constants
A character constant is a constant which uses single quotation around characters. For example:
'a', 'l', 'm', 'F'
Variables:
A variable is an identifier that is used to represent some specified type of information within a
designated portion of the program. In its simplest form, a variable is an identifier that is used to
represent a single data item, i.e., a numerical quantity or a character constant. The data item must
be assigned to the variable at some point in the program. A given variable can be assigned
different data items at various places within the program. Thus, the information represented by
the variable can change during the execution of the program. However, the data type associated
with the variable are not change.
C supports a rich set if build in operators. An operator is a symbol that tells the computer to perform
mathematical or logical manipulations. Operators are used in programs to manipulate data and
variables. They usually form a part of mathematical or logical expressions.
C operators can be classified into a number of categories. They include:
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and Decrement operators
Conditional operators
Bitwise operators
Special operators
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.
&& Logial AND. True only if all If c = 5 and d = 2 then, expression ((c==5)
operands are true . &&(d>5)) equals to 0.
2. Bitwise OR operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C
Programming, bitwise OR operator is denoted by |.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)
x = (a > b)? a: b ;
In this example, x will be assigned the value of b. This can be achieved by using the if else statement
as follows:
if (a<b)
x = a;
else
x = b;
The data type and the value of an expression depends on the data types of the operands and the
order of evaluation of operators which is determined by the precedence and associativity of
operators. Let us first consider the order of evaluation. When expressions contain more than one
operator, the order in which the operators are evaluated depends on their precedence levels. A
higher precedence operator is evaluated before a lower precedence operator. If the precedence
levels of operators are the same, then the order of evaluation depends on their associativity (or,
grouping). In Chapter we briefly discussed the precedence and associativity of arithmetic
operators.
Summary of operators their precedence and associativity.
Type Conversion
When variables and constants of different types are combined in an expression then they are
converted to same data type. The process of converting one predefined type into another is called
type conversion.
Type conversion in c can be classified into the following two types:
2. Explicit Type Conversion The type conversion performed by the programmer by posing the
data type of the expression of specific type is known as explicit type conversion. The explicit
type conversion is also known as type casting.
Type casting in c is done in the following form:
(data type)expression;
where, data type is any valid c data type, and expression may be constant, variable or expression.
int main()
{
double x = 1.2;
return 0;
}
Output:
sum = 2