[go: up one dir, main page]

0% found this document useful (0 votes)
35 views12 pages

C Unit-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views12 pages

C Unit-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming with C &C++: INDRAKANTI SHEKAR, V .V .R Raman , V.

N Battu - Himalaya publications

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

 Rules for defining variables.


 Scope and life of a variable

Data types:

 Type Conversion
 Formatted input and output operations

Operators:

 Introduction-Arithmetic-Relational- logical-Assignment-conditional-special -bitwise -


increment and decrement operators.
Types of languages:
 To write a computer program, a standard programming language is used.
 A programming language is composed of a set of instructions in a language understandable
to the programmer and recognizable by a computer.
 Programming languages can be classified as high-level, middle-level, and low-level.
 High-level languages such as BASIC, COBOL (Common Business Oriented Programming
Language), and FORTRAN (Formula Translation Language) are used to write application
programs.
 A middle-level language such as C is used for writing application and system programs.
 A low-level language such as the assembly language is mostly used to write system
programs.
 Low-level programming languages were the first category of programming languages to
evolve.
 Gradually, high-level and middle-level programming languages were developed and put to
use.

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
}

Sub program section


Function-1
Function-2
-
-
Function-n

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

 A keyword in C is a reserved word which has a specific meaning.

 Keywords in C cannot be used as identifiers.

 Keywords serve as the basic building blocks for program statements.

 Keywords in C are always in lowercase.

 ANSI C supports 32 keywords which are listed below:


Identifiers:
 Identifiers refer to the names of variables, functions and arrays.
 These are user-defined names and consist of sequence of letters and digits, with a
letter as a first character.
 Both uppercase and lowercase letters can be used, although lowercase letters are
generally used.
 The underscore character is also permitted in identifiers.
 There certain rules while writing identifiers. They are as follows:
• First character must be an alphabet or underscore.
• Must consist of only letters, digits or underscore.
• Only first 31 characters are significant.
• Cannot use a keyword.
• Must not contain white space.

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:

 An integer constant refers to a sequence of digits without decimal places.

 Decimal integers consist of umbers from 0 to 9, preceded by an optional + or – sign.

Ex: 133, +45, -15, 0, 342332 etc.

Real Constants:

 Real constants are represented by numbers containing fractional parts like 25.234.
Such numbers are called as real or floating-point constants.

ex: 0.067, -12.5, +4.67, .87, 121. Etc.


Single character constants:

 A single character constant or character constant contains a single character


enclosed in between single quotes.

Ex: ‘f ’, ‘A’, ‘/’, ‘4’

 Every character constant will have an associated integer value known as ASCII code.

String Constants:

 A sequence of characters that are enclosed between double quotes is known as a


string literal or string constant.

ex : “hi”, “Welcome” , “1-5-65/a/1” , “A” , ”9989567865”

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.

 So, for such special cases, C provides escape sequences.


Constant Meaning
\a Alert
\b Backspace
\f Form feed
\n New line
\r Carriage Return
\t Horizontal tab
\v Vertical tab
\’ Single quote
\” Double quote
\? Question mark
\\ Backslash
\0 Null

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.

Syntax: data-type variable-name;


Ex: int total;
float average;
char grade;

Data types in ‘C’:


 Data types are used to define the type of data that a variable can store to perform a
specific operation.
 All ‘C’ compilers support 5 primary data types.

1. Primary Data Types


2. Derived Data Type
3. User Defined Data Types

Primary data types:


 Character type (char) : A single character can be defined as a character (char) type
data.
 Characters are usually stored in 8 bits (1 byte) of internal storage.
 The qualifier signed or unsigned may be explicitly applied to char.
 While unsigned chars have values between 0 to 255, signed chars have values from -
128 to + 127
Integer type (int) :
 Integers are whole numbers with a range of values supported by a particular
machine.
 Generally, integers occupy one word of storage.
 In order to provide some control over the range of numbers and storage space, C has
3 classes of integer storage namely short int, and long int in both signed and
unsigned forms.
short
int
int
long int

Floating point type (float) :


 floating point numbers are defined in ‘C’ by the keyword float. When the accuracy is
provided by the floating point is not sufficient, the type double can be used to define
the number.
 Floating point numbers are stored in 32 bits with 6 digits of precision.
 A double data type number uses 64 bits giving a precision of 14 digits.
 To extend the precision further we may use long double which uses 80 bits.

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.

Operator Meaning Example


+ Addition or unary plus a+b
– Subtraction or unary minus a-b
* Multiplication a*b
/ Division (Quotient) a/b
% Modulo (Remainder) a%b

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

 The relational operators in C are as shown below:

Operator Meaning Example

< Less than a<b

<= Less than or equal to a<=b

> Greater than a>b

>= Greater than or equal to a>=b

== Equal to a==b

!= Not 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.

Operator Meaning Example

&& Logical AND (a>b)&&(a>c)

|| Logical OR (a>b)||(a>c)

! Logical NOT !(a>b)


4. Assignment Operators

 The assignment operators are used to assign value of an expression to a variable.


 The general assignment operator is = (equal).
 In C, there are some special assignment operators known as shorthand operators.
 The syntax of shorthand operators is as shown below:

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)

5. Increment and Decrement Operators:

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

Operator Meaning Post expression Pre expression

++ Increment by 1 a++ ++a

— Decrement by 1 a-- --a

6. Conditional Operator

 These are also called as Ternary operators.

 These operators operate 3 operands.

exp1 ? exp2 : exp3

 If exp1 is true then exp2 is executed.

 If exp1 is false then exp3 is executed.


7. Bitwise Operators
 C supports a set of operators which operate at bit-level.
 These operators are known as bitwise operators.
 The bitwise operators are used for testing a bit, setting a bit, complementing a bit or for
shifting the bits to left or right.

Operator Meaning Example

& Bitwise AND a&b

| Bitwise OR a|b

^ Bitwise Exclusive OR a^b

~ Bitwise NOT ~a

<< Left Shift a<<1

>> Right Shift a>>1

8. Special Operators
 C supports some special operators such as
comma operator “ , “
sizeof operator sizeof ()
address operator “ & “
pointer operator “ * “

You might also like