C Unit-1
C Unit-1
UNIT-1
INTRODUCTION TO ‘C’ LANGUAGE, VARIABLES, DATA TYPES AND OPERATORS
Introduction:
Types of languages
History of ‘c’ language
Basic structure
Programming Rules
Flow charts
Algorithms
Commonly used library functions
Executing the ‘C’ program pre-processors in ‘C’
Keywords and identifiers
Constants
Variables
Data types:
Type Conversion
Formatted input and output operations
Operators:
History of ‘C’:
‘C’ language is one of the most popular computer languages.
It is a structured, high-level, machine independent language.
The root of all modern languages is ALGOL, introduced in the early 1960.
In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming
Language)
In 1970, Ken Thompson created a language using many features of BCPL and called it as
simply ‘B’.
‘C’ was evolved from ALGOL, BCPL and B by Dennis Ritchie at Bell Laboratories in 1972.
‘C’ uses many concepts from these languages and added the concept of data types and other
powerful features.
Basic structure of C Program:
Documentation section
Link section
Definition section
Global declaration section
main () function section
{
Declaration part
Executable part
}
The documentation section consists of set of comment lines giving name of the program, the
author and other details.
The link section provides instructions to the compiler to link functions from the system
library.
The definition section defines all symbolic constants.
There are some variables that are used in more than one function. Such variables are called
global variables and are declared in global declaration section that is outside of all the
functions. This section also declares all user-defined functions.
Every ‘C’ program must have a main function section. This section consists two parts,
declaration part and executable part. The declaration part declares all the variables used in
the executable part.
There is at least one statement one statement at executable part. These parts must appear
between opening and closing braces.
The program execution begins at the opening brace and ends at the closing brace.
All statements in the declaration and executable parts end with a semi-colon (; ).
The sub-program section contains all the user-defined functions that are called in the main
function.
Algorithm and flowchart
In computer programming terms, an algorithm is a set of well-defined instructions to solve a
particular problem.
Algorithm is a step – by – step solution for a particular problem.
It takes a set of input and produces a desired output.
Example
Algorithm for finding the average of three numbers is as follows −
1. Start
2. Read 3 numbers a,b,c
3. Compute sum = a+b+c
4. Compute average = sum/3
5. Print average value
6. Stop
FLOW CHART
A flowchart is a pictorial (graphical) representation of an algorithm.
A flowchart is drawn using different kinds of symbols.
A symbol is used for a specific purpose.
Each symbol has name.
C Tokens:
in a paragraph of text, individual words and punctuation marks are considered as tokens.
Likewise in a C program, the smallest individual units are known as C tokens. C has six types
of tokens as shown below. C programs are written using these tokens and the syntax of the
language.
Keywords
Every word used in a C program is classified as either a keyword or as an identifier.
Constants:
Constants are fixed values, which do not change during the execution of a program.
C supports several types of constants, which are as shown below:
Integer constants:
Real Constants:
Real constants are represented by numbers containing fractional parts like 25.234.
Such numbers are called as real or floating-point constants.
Every character constant will have an associated integer value known as ASCII code.
String Constants:
Escape Sequences
C supports some special backslash character constants that are used in output
functions like printf().
For example, to move the cursor from the current line to next line, there is no
standard way to achieve it.
Variables in ’C’:
A variable is a data name that may be used to store a data value.
A variable may take different values at different times during execution of the
program.
A variable name can be chosen by the programmer in a meaningful way so as to reflect
its function or nature in the program.
Ex: Average, height, Total, Counter_1, class_strength
A variable name may consist of letters, digits and underscore ( _ ) character.
Rules to follow while creating variables:
• They must begin with a letter. Some systems permit underscore as first character.
• Length should not be exceeded more than 8 characters.
• Upper case and lowercase are different. That is variable Total is not the same as total.
• It should not be a keyword.
• White space is not allowed.
Declaration of variables:
After designing suitable variable names, we must declare them to the compiler.
It tells the compiler what the variable name is:
What type data the variable will hold.
float
double
Long double
void type: The void type has no values. This is usually used to specify the type of function. The
type of function is said to be void when it does not return any values to the calling function.
Operators in ‘C’
An operator is a symbol that operates on a value or a variable.
For example: + is an operator to perform addition.
In C, based on the functionality, operators are classified into 8 categories. They are:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
1. C Arithmetic Operators
The arithmetic operators can operate on any built-in data type in C.
The unary minus operator multiplies its single operand with -1.
Integer division truncates any fractional part.
The modulo division operator (%) produces the remainder of an integer division.
The modulo operator cannot be used on floating point values.
2. Relational Operators
In C, whenever there is a need to compare two values and make a decision based on the
outcome of the comparison, we use relational operators.
The relational operators are generally used in decision making statements like if, else if and
in looping statements like for, while, do while etc. Relational operators always evaluate to 0
(false) or 1 (true).
== Equal to a==b
3. Logical Operators
The relational operators are used to compare at most two values i.e. testing one condition.
To test more than one condition, we use logical operators along with relational operators.
The logical operators always evaluate to either 0 or 1 like relational operators.
|| Logical OR (a>b)||(a>c)
Operator Example
= a = 10
+= a+=10 (a=a+10)
-= a-=10 (a=a-10)
*= a*=10 (a=a*10)
/= a/=10 (a=a/10)
%= a%=10 (a=a%10)
The increment and decrement operators provided by C are used to increment or decrement
the operand by a value of one.
Both the increment and decrement operators are unary operators.
These operators are used extensively inside for loop and while loop.
There are two variations in how the increment and decrement operators can be used.
6. Conditional Operator
| Bitwise OR a|b
~ Bitwise NOT ~a
8. Special Operators
C supports some special operators such as
comma operator “ , “
sizeof operator sizeof ()
address operator “ & “
pointer operator “ * “