Unit-1: (Fundamentals)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Unit-1

(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.

Let us take a look at the various parts of the C program −

 The first line #include <stdio.h> is a preprocessor command, which tells a C


compiler to include stdio.h file before going to actual compilation.
 The next line int main() is the main function where the program execution
begins.
 The next line /*...*/ will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in the
program.
 The printf(...) is another function available in C which is used to print the
messages to be displayed on the screen.
 The line return 0; terminates the main() function and returns the value 0.

Character set of C
character:-

It denotes any alphabet, digit or special symbol used to represent information.


Use:-

These characters can be combined to form variables. C uses constants, variables,


operators, keywords and expressions as building blocks to form a basic C program.

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.

The characters in C are grouped into the following two categories:

1. Source character set


(a) Alphabets
(b) Digits
(c) Special Characters
(d) White Spaces
2. Execution character set
(a) Escape Sequence

SPECIAL CHARACTERS

~ tilde % percent sign | vertical bar @ at symbol + plus sign


^ caret < less than _ underscore - minus sign > greater than
& ampersand # number sign = equal to
* asterisk $ dollar sign / slash ( left ] right bracket
parenthesis
apostrophe \ back slash ) right ′ { open brace
parenthesis
; semicolon : colon [ left bracket " quotation } right flower
mark brace

, comma ! exclamation ? Question . dot operator


mark mark

WHITESPACE CHARACTERS

\b blank space \t horizontal tab \v vertical tab


\r carriage return \f form feed \n new line
\\ Back slash \’ Single quote \" Double quote
\? Question mark \0 Null \a Alarm (bell)
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 –

For example:

Here individual tokens are:

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).

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 –

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 types in C can be classified as follows −

S.No. Types & Description


Basic Types
1 They are arithmetic types and are further classified into: (a) integer types and
(b) floating-point types.
Enumerated types
2 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.
Derived types
4 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; 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 −

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 –

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 −

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 void Type


The void type specifies that no value is available. It is used in three kinds of situations

S.No. Types & Description


Function returns as void
1 There are various functions in C which do not return any 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);
Function arguments as void
2 There are various functions in C which do not accept any parameter. A
function with no parameter can accept a void. For example, int rand(void);
Pointers to void
3 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.
Variable
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 −

S.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.

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.

Some of the examples are listed here

Lvalues and Rvalues in C


There are two kinds of expressions in C −

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 −

 Using #define preprocessor.


 Using const keyword.

The #define Preprocessor


Given below is the form to use #define preprocessor to define a constant –

The following example explains it in detail −


The const Keyword
You can use const prefix to declare constants with a specific type as follows –

The following example explains it in detail −

Note that it is a good programming practice to define constants in CAPITALS.

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

Note: Search the details of these operators from the GOOGLE.


Operators form expressions by joining individual constants, variables, array elements.
C includes a large number of operators which fall into different categories. Arithmetic
operators, unary operators, relational and logical operators, assignment operators and
the conditional operators are used to form expressions. The data items on which
operators act upon are called operands. Some operators require two operands while
others require only one operand. Most operators allow the individual operands to be
expressions. A few operators permit only single variable as operand.

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.

Another important point to consider is the order in which consecutive operations


within the same precedence group are carried out. This is known as associativity.

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


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

********************** End of the unit-1 ********************

You might also like