UNIT - 1
INTRODUCTION TO C
What is Programming and Languages?
A programming language is a notation for
writing programs, which are specifications of a
computation or algorithm
A programming language is a set of rules that provides
a way of telling a computer what operations to perform
A programming language is a set of rules for
communicating an algorithm
Medium of communication between computer and the
user containing words, symbols and syntax rules
Each language has its own vocabulary and rules.
Generation of Programming Languages
• There are Five Generations of Programming languages
1 Machine Language (1st Generation Language – 1945)
2 Assembly Language(2nd Generation Language – 1950s)
3 High Level Language (3rd Generation Language – 1960s)
4 Very High Level Language (4th Generation Language – 1970s)
5 Natural Language (5th Generation Language – 1980)
List of High Level Language
• FORTRAN - Formula Translation Language
• COBOL - Common Business Oriented Language
• ALGOL - Algorithmic Language
• RPG - Report Program Generator
• APL - A Programming Language
• BASIC - Beginners All Purpose Symbolic Instruction Code
• PL/I - Programming Language I
• PASCAL -Named after Blaise Pascal, a French Philosopher
• Ada - Named after Lady Lovelace Ada
• C - A General Purpose Programming Language
• C++ Object Oriented Programming Language
• JAVA -Object Oriented Programming Language
5
Some examples of the use of C Programming
Operating Systems.
Language Compilers.
Assemblers.
Text Editors.
Print Spoolers.
Network Drivers.
Modern Programs.
Data Bases.
Hardware
Internet Application
Memory Management
Firewalls
Virus Protection
Gaming S/W etc.,
History of C
• C is a middle-level programming language
developed by Dennis Ritchie during the early 1970s
while working at AT&T Bell Labs in the USA.
• Eventually, C was developed during 1971-73,
containing both high-level functionality and the
detailed features required to program an operating
system.
• C is a general-purpose programming language and
can efficiently work on enterprise applications,
games, graphics, and applications requiring
7
Why Should I learn C Programming
• C is a procedural programming language.
• The main features of C language include low-
level access to memory, simple set of
keywords, and clean style, these features
make C language suitable for system
programming like operating system or
compiler development.
8
Why Should I learn C Programming Cont..
1. There are some good reasons to learn to program in C. First,
The age has its advantages: C has been around for 30
years, and there is a ton of source code available in the field.
2. This means there's a lot to learn from, and a lot to use.
Moreover, many of the issues with the language have been
clearly elucidated
3. Plus, with C, you get lots of strong opinions mixed with
insights that you can understand.
9
Why Should I learn C Programming Cont..
• In fact, a lot of fun programming is done in C -- for instance,
system software and data managers.
• If you want to be able to do more than write a simple web
app, C is a great language.
• If you want to write a great, fast game, C is again a great
choice.
• You can write an entire Operating System Software in C.
10
Benefits or Importance of C Language
• As a middle-level language, C combines the features of both high-
level and low-level languages.
• C is a structured programming language which allows a complex
program to be broken into simpler programs called functions.
• Various features of C including direct access to machine level
hardware.
• C language an optimum choice for scripting applications and
drivers of embedded systems.
• C language is case-sensitive which means lowercase and
uppercase letters are treated differently.
11
Benefits or Importance of C Language
• C is highly portable and is used for scripting system applications
which form a major part of Windows, UNIX, and Linux operating
system.
• C is a general-purpose programming language and can
efficiently work on enterprise applications, games, graphics, and
applications requiring calculations, etc.
• C language has a rich library which provides a number of built-in
functions.
• C implements algorithms and data structures swiftly, facilitating
faster computations in programs. like MATLAB and Mathematica.
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Structure of C Program
• Every C program contains a number of building
blocks.
• These building blocks should be written in a
correct order and procedure, to execute without
any errors.
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
1. Documentation Section
• The general comments are included in this
section.
• The comments are not a part of executable
programs.
• The comments are placed between delimiters (/*
and */).
Example:
• //Factorial of a given number
• /* This program for
factorial of a given number */
2. Preprocessor Section
• It provides preprocessor statements which direct
the compiler to link functions from the system
library.
Example
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include <stdio.h>
printf(), scanf()
#include<conio.h>
clrscr(), getch()
#include <math.h>
pow(), sqrt()
#include<string.h>
strlen(),strcpy()
3. Definition Section
• This section defines all symbolic constants.
• It refers to assigning macros of a name to a
constant.
Example
#define PI 3.14
#define TRUE 1
#define FALSE 0
Global Declaration Section
• It contains variable declarations, which can be
accessed anywhere within the program.
• It should declare before the main() function.
• Global variable otherwise known as External
variable or Public variable.
Example
int a;
main()
{
------
------
}
main() function
• Each and every C program should have only
one main() function.
• Without main() function, the program cannot be
executed.
• The main function should be written in lowercase
only.
• It should not be terminated with semicolon.
Declaration Part
• Each and every variable should be declared
before going to use those variables in execution
part.
Execution part
• Here, the logic of the program can be
implemented.
• These statements are known as program
statements or building blocks of a program.
• They provide instructions to the computer to
perform a specific task.
Subprogram section
• The subprogram section contains all the user-
defined functions that are called in the main()
function.
• User-defined functions are generally placed
immediately after the main() function, although
they may appear in any order.
Example Program 1
#include<stdio.h>
void main()
{
printf(“SMVEC”);
}
Output:
SMVEC
Example Program 2
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the value of a=”);
scanf(“%d”,&a);
printf(“Enter the value of b=”);
scanf(“%d”,&b);
c=a+b;
printf(“Sum of a and b=%d”,c);
}
Example Program 3
Process of Compiling and running a C program
COMPILATION & LINKING PROCESSES
Execution is the process of running the program,
to execute a ‘c’ program, we need to follow the
steps given below.
1. Creating the program
2. Compiling the program
3. Linking the program with system library.
4. Executing the program.
Sample.C Sample.obj Sample.exe
Compiler Linker
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Turbo C++ IDE
First C Program
Save the file with .c extension
SAMPLE.C
Compile the file using Alt+F9
Run the file using Ctrl+F9
IDE(Integrated Development Environment )
• An integrated development environment (IDE)
is a software application that provides
comprehensive facilities to computer
programmers for software development.
An IDE normally consists of at least a source
code editor, build automation tools and a
debugger.
IDE FOR C/C++
• Visual Studio Code
• Eclipse
• NetBeans
• Sublime Text
• Atom
• Code::Blocks
• CodeLite
• CodeWarrior
Visual Studio code
• A visual studio code is a lightweight software application with a
powerful source code editor that runs on the desktop.
• It is a free source code editor developed by Microsoft for
Windows, Mac OS and Linux. It is a software editor that has a
rich extension of various languages like C++, C+, C, Java, Python
, PHP, Go, etc. and runtime language extensions such as .NET
and Unity.
• It is easy to edit, build, syntax highlighting, snippets, code
refactoring and debugging. In visual studio code, we can change
the application's background theme, keyboard shortcuts set on
our preferences, install an extension and add additional
functionality.
It is an open-source code editor developed by Microsoft for Windows, Linux
and Mac OS. Visual Studio Code is based on an Electron framework
Visual Studio code
• Key Benefits:
• Support for Debugging
• Syntax highlighting
• Intelligent Code completion, snippets and code refactoring
• EmbeddedGit Control
• Completely portable
• Easy customization
• Programming Languages Supported: C, C++, C#, CSS, Go,
HTML, Java, JavaScript, Python, PHP, TypeScript and much
more.
PROGRAM
A program consists of a series of instructions
that a computer processes to perform the
required operation.
Characteristics of a program
• Accuracy
• Clarity
• Modularity
• Portability
• Flexibility
• Efficiency
• Generality
• Documentation
Program Development Life Cycle
• Problem Definition
• Problem Analysis
• Algorithm Development
• Coding & Documentation
• Testing & Debugging
• Maintenance
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Programming languages
• Procedural programming (e.g. C, Fortran, Pascal)
• Object-oriented programming (e.g. C++, Java,
Python)
• Functional programming (e.g. Lisp, ML, Haskell)
• Logic programming (e.g. Prolog)
PROBLEM SOLVING TECHNIQUES
Problem solving technique is a set of techniques
and graphical tools that help in providing logic
for solving a problem.
Some of the problem solving tools are
1. Algorithm
2. Flowchart
3. Pseudo code
Algorithm
• Algorithm means the logic of a program.
• It is a step-by-step description of how to arrive
at a solution of a given problem.
Characteristics of an algorithm
• In algorithms each and every instruction should be
precise.
• In algorithms each and every instruction should be
unambiguous.
• The instructions in an algorithm should not be repeated
infinitely.
• Ensure that the algorithm will ultimately terminate.
• The algorithm should be written in sequence.
• It looks like normal English.
• The desired result should be obtained only after the
algorithm terminates.
Algorithm for sum of two numbers
Flowchart
• Flowchart is a pictorial representation of an
algorithm.
• It is often used by programmer as a program
planning tool for organizing a sequence of
step necessary to solve a problem by a
computer.
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Flowchart Symbols
Algorithm and Flowchart
Sum of Two numbers
Data Name (or) Variable
• In C, a variable is a data name used for storing a
data value.
• Its value may be changed during program
execution.
• The value of variables keeps on changing during
the execution of a program.
• In other words, a variable can be assigned
different values at different times during the
execution of a program.
Rules for naming the variables
• A variable name can be any combination of alphabets,
digits or underscore.
• But first character should be alphabets or an underscore
(_).
• The length of the variable cannot exceed up to 8
characters long.
• Some of the c compilers can be recognized up to 31
characters long.
• The variable should not be a C keyword.
• Both lowercase and uppercase are permitted.
• The special symbols are not permitted except underscore.
Syntax:
datatype v1,v2,v3,….,vn;
Description:
datatype - It is the type of data.
v1, v2, v3 ,…, vn - list of variables.
Example 1:
int regno;
float cgpa;
char name[10];
Example 2:
Declaration of multiple variables of the same data types
can be done in one statement.
int mark1;
int mark2;
int mark3;
int mark4;
can be written to become
int mark1, mark2, mark3, mark4;
Two types:
✓ Local variables
✓ Global variables
Local variables:
The variables which are defined inside a
function block or inside a compound statement of a
function sub- program are called local variables.
#include<stdio.h>
void main()
{
int i=10;
----
----
}
Global variables:
The variables that are declared before the
function main() are called the global/external
variables.
int a=5;
#include<stdio.h>
void main()
{
Int i=10;
----
----
}
Data Type
• Data type is the type of the data, that are going
to access within the program.
• C supports different data types, each data may
have predefined memory requirement and
storage representation.
User defined
data types
Empty data set
Primary data
types
Derived data
types
Data Types
char
int
double
float
typedef
enum
array
pointer
union
struct
void
1. Primary data types
C has the following basic built-in primary
datatypes.
• char
• int
• float
• double
char (Character)
Char (Character) data type is used to store single character, within
single quotes e.g. 'a', 'z', 'e' etc.
Syntax:
char variablename;
Example:
char chi='a', cha;
int (Integer)
Integer data type is used to store numeric values without any decimal
point e.g. 7, -101, 107, etc.
Syntax:
int variablename;
Example:
int roll, marks, age;
int marks=100;
float
Float data type is used to store numeric values with decimal
point. e.g. 3.14, 7.67 etc. e.g. percentage, price, pi, area etc.
Syntax:
float variable name;
Example:
float per, area;
per=95.7;
double
Double is used to define BIG floating point numbers. It
reserves twice the storage for the number. It contains 8 bytes.
Syntax:
double variablename;
Example:
double Atoms;
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Format Specifiers
• %d or %i – integer
• %c – character
• %f – float
• %s – string
• %e – double
• %o – octal
• %x - Hexadecimal
PROGRAM
//primary data types
#include<stdio.h>
#include<conio.h>
void main()
{
int rollno;
float fees;
char grade;
clrscr();
printf("tprimary data typesn");
printf("Enter rollno:n");
scanf("%d",&rollno);
printf("Enter fees:n");
scanf("%f",&fees);
printf("Enter grade:n");
scanf("%c",&grade);
printf("The details aren");
printf("Roll no=%dn",rollno);
printf("Fees=%.2fn",fees);
printf("Grade=%cn",grade);
getch();
}
OUTPUT
2. User defined data type
• User defined data type is used to create new
data types.
• Types:
– typedef
– enum
typedef:
• The 'typedef' allows the user to define new data-types that
are equivalent to existing data types.
Syntax:
typedef type new-type;
Type refers to an existing data type. New-type refers to the
new user-defined data type.
Example:
typedef int number;
Declare integer variables as: number roll, age, marks;
It is equivalent to: int roll, age, marks;
enum (Enumeration)
• Enumeration types provide the facility to specify the
possible values of a variable by meaningful symbolic
means.
Syntax:
enum enumname{member1,member2,...,membern};
Example:
enum colour{black,white,pink,red,green,yellow};
• An enumeration type is implemented by associating the
integer value with the enumeration constant.
• Thus, the value 0 is associated with black, 1 with white, 2
with pink, 3 with red, 4 with green, 5 with yellow.
3. Derived Data type
• Data types that are derived from fundamental data types are called
derived data types.
• Types – array, pointer, structure and union.
Array
• An array is a collection of variables of same type i.e. collection of
homogeneous data referred by a common name. In memory, array
elements are stored in a continuous location.
Syntax:
datatype arrayname[ ];
Example:
int a[10];
char name[20];
pointer
• A pointer is a special variable that holds a memory
address of another variable.
Syntax:
datatype *var_name;
Example:
int a,*b;
b=&a;
* - indirection operator
& - address of operator
variable 'b' stores the address of variable 'a'.
#include<stdio.h>
void main()
{
int a,*b;
a=10;
b=&a;
printf(“value of a=%d”,a);
printf(“address of a=%d”,&a);
printf(“value of b=%d”,b);
printf(“address of b=%d”,&b);
printf(“value of a through b=%d”,*b);
}
10
a
-12
-12
b
-14
Variable
Value
Address
output
Struct
• A struct is a user defined data type that stores multiple values of
same or different data types under a single name. In memory, the
entire structure variable is stored in sequence.
Syntax: Example:
struct <structure name>
{
member1;
member2;
-----
-----
};
Structure name is the name of structure. e.g. store details of a
student as - name, roll, marks.
struct student
{
char name [20];
int roll,
float marks;
};
union
• A union is a user defined data type that stores multiple
values of same or different data types under a single
name. In memory, union variables are stored in a
common memory location.
Syntax: Example:
union <tag name>
{
var1;
var2;
-----
----
};
Tag name is the name of union, e.g, store details of a
student as- name, roll, marks.
union student
{
char name[20];
int roll,
float marks;
};
Difference between Structure and Union
C Character Set
• The character set is the fundamental raw
material of any language and they are used to
represent information.
SOURCE CHARACTER SET
EXECUTION CHARACTER SET
Escape
Sequence
Name Meaning
a Alert Sounds a beep
b Backspace Backs up 1 character
f Formfeed Starts a new screen of page
n New line Moves to beginning of next line
r Carriage return Moves to beginning of current line
t Horizontal tab Moves to next tab position
v Vertical tab Moves down a fixed amount
 Back slash Prints a back slash
’ Single quotation Prints a single quotation
” Double quotation Prints a double quotation
? Question mark Prints a question mark
C TOKENS
1. Keywords
2. Identifiers
3. Constants
4. String
5. Operators
6. Special Symbols
C Tokens
Operators
Constants
Special Symbols
String
Identifiers
Keywords
1. Keywords
• Keywords are C tokens that have a strict meaning.
– They are explicitly reserved and cannot be redefined.
– All keywords must be written in lowercase.
– All keywords have fixed meanings and these meanings cannot be
changed.
• C has 32 key words.
– Some implementations such as Borland’s C or Microsoft’s C have
additional key words.
List of Keywords
auto do goto signed unsigned
break double if sizeof void
case else int static volatile
char enum long struct while
const extern register switch
continue float return typedef
default for short union
2. Identifiers
• It is used to refer the name of the variables, functions
and arrays.
• An identifier is a token:
– Composed of a sequence of letters, digits, and the
underscore character _
• Note: Variable names are identifiers
Rules
• An identifier must begin with a letter.
• Both upper case and lower cases are permitted.
• Keywords are not permitted.
• No special symbols are permitted except underscore
(_).
• Some C compiler accept under score may appear in
first letter.
• Maximum length of an identifier is 31.
• Blank spaces are not allowed in between an
identifier.
Example:
• Regno
• _regno
• Reg no
• Regno4
• 5regno
3. Constants
Numeric constants
• There are two types of numeric constants,
1. Integer constants
2. Real or floating-point constants
Integer constants
• An integer constant refers to a sequence of digits
without a decimal point.
Example: marks 90, per 75
Real Constants
• A Real constant is made up of a sequence of numeric
digits with presence of a decimal point.
Example: distance =126.0; Height = 5.6;
Character Constants
• There are two types, they are given below.
i) Single Character constants
ii) String constants
Single Character constants
• A single character constant is a single alphabet, a single
digit or a single special symbol enclosed within single
inverted commas.
Example: ‫ۥ‬
m =
‫ۥ‬ ‫ۥ‬ ‫ۥۥ‬A ‫ۥ‬
String constants
• A string constant is a sequence of characters enclosed in
double Quotes, the characters may be letters, numbers,
special characters and blank spaces etc.
Example: ”hi”, “325”.
Importance of Input and Output function in C
We feed information into the computer, tell the computer what do
with it, and then get a result back out.
• The information we put into a computer is called input and
• The information we receive from the computer is called output.
• Input can come from just about anywhere. Keystrokes on a
keyboard, data from an internet connection, or sound waves
converted to electrical signals are examples of input.
• Output can also take many forms such as video played on a
monitor, a string of text displayed in a terminal, or data we save
onto a hard drive.
• The collection of input and generation of output is known
under the general term, input/output, or I/O for short, and is a
core function of computers.
Input and Output functions in C
Interestingly, the C programming language doesn't
have I/O abilities built into it. It does, however, provide
us with an external library containing I/O functions
which we can compile and link into our programs.
We have already used an output library
function: printf().
You may recall this function resided in
the stdio.h library file.
As that file's name implies, stdio.h contains
standardized I/O functions for adding input and output
capability to our programs.
Input and Output functions in C – I/O Stream
• C has no built-in statements for input or output.
• All input and output is performed with streams.
• A "stream" is a sequence of characters
organized into lines.
• Each line consists of zero or more characters
and ends with the "newline" character.
I/O Stream
• All input and output is performed with streams,
which are sequences of bytes.
• In input operations, the bytes flow from a device
(e.g., a keyboard, a disk drive, a network connection)
to main memory.
• In output operations, bytes flow from main memory
to a device (e.g., a display screen, a printer, a disk
drive, a network connection, and so on).
I/O Stream
• Normally, the standard input stream is connected
to the keyboard and the standard output stream
is connected to the screen.
• Operating systems often allow these streams to
be redirected to other devices.
• A third stream, the standard error stream, is
connected to the screen.
• Error messages are output to the standard error
stream.
Types of Stream in C - Summary
• Standard input stream is called "stdin" and is
normally connected to the keyboard
• Standard output stream is called "stdout" and
is normally connected to the display screen.
• Standard error stream is called "stderr" and is
also normally connected to the screen.
Input / Output data flow and assign
• A stream is an abstraction that refers to
a flow of data.
standard
output
device
stdout variable a
printf(”%d”,a);
standard
input
device
stdin variable a
scanf(”%d”,&a);
Keyboard
Monitor
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
getc() and putc()
• getc() which reads the next available
character from the screen.
• putc() which prints the character.
Syntax:
x=getc();
putc();
Example:
char x;
x=getc();
putc(x);
Program:
#include <stdio.h>
void main( )
{
int c;
printf( "Enter a
value :");
c = getc( );
printf( "nYou
entered: "); putc( c );
}
Output:
Enter a value :
a
You entered: a
getchar() and putchar()
• getchar() which reads the next available
character from the screen.
• putchar() which prints the character.
Syntax:
x=getchar();
putchar(x);
Example:
char x;
x=getchar();
Program:
#include <stdio.h>
void main( )
{
int c;
printf( "Enter a
value :");
c = getchar( );
printf( "nYou
entered: ");
putchar( c );
Output:
Enter a value :
a
You entered: a
gets() and puts()
• gets() is used to read the string from the standard
input device (keyboard)
• puts() is used to display a single character to
standard output device.
Syntax:
gets(dataname);
puts(dataname);
Example:
char
name[10];
Program:
#include<stdio.h>
void main()
{
char name[100];
printf("Enter a
string");
gets( name );
printf(“You
entered:”)
puts( name );
Output:
Enter a string :
rahul
You entered:
rahul
scanf() and printf()
• The standard input-output header file, named stdio.h contains
the definition of the functions printf() and scanf(), which are used
to display output on screen and to take input from user
respectively.
Program:
#include<stdio.h>
void main()
{
int i;
printf("Please enter a
value:");
scanf("%d", &i);
printf( "nYou
entered: %d", i);
}
Output:
Please enter a
value: 10
You entered: 10
Scanf() with multiple input with Same data
type
void main()
{
int m1,m2,m3;
printf(“Enter 3 marks for m1,m2,m3:”);
scanf(“%d %d %d”,&m1,&m2,&m3);
printf(“nThe given Marks : %d %d
%d”,m1,m2,m3);
}
output:
Enter 3 marks for m1,m2,m3: 67 87
95
The given Marks : 67 87 95
void main()
{
int age
float per;
printf(“Enter your age and percentage:”);
scanf(“%d %f” ,&age,&per);
printf(“nMy age is %d and percentage is:
%f,age,per);
}
output:
Enter your age and percentage: 18 85.26
My age is 18 and percentage is: 85.26
Scanf() with multiple input with different data type
Operators and Operands
• Operators are symbols that instruct the computer to
perform a single, simple task, and most consist of just
one or two characters ( =, +, *, <=, or == -), but some
operators consist of a complete word (sizeof()).
• Operands are expressions or values on which an
operator operates or works (often constants or
variables but sub-expressions are also permitted).
Operand examples:
In each example, + is a operator while the symbols
appearing on either side of it are operands.
a. Operands may be constants
2 + 5
b. Operands may be variables
A + B
c. Operands may be more complex expressions
2*A + 5*B
Number of Operands
Operators are characterized by the number of operands that they require. Most
operators require one or two operands but C does have one operator that
requires three.
Unary Operators
Unary operators only require one operand. Most of the time the operand is
placed to the right of the operator but sometimes it is placed to the left.
The most common example is the unary minus: N, which changes the sign of
‑
the value stored in variable N.
Binary Operators
Binary operators require two operands. The arithmetic operators are the most
familiar examples of binary operators. The assignment operator is also common.
For example:
A = counter + 5;
"counter" and "5" are the "+" operator's operands, and "A" and the value of the
expression "counter + 5" are the "=" operator's operands. Note that the above
example ends with a semicolon, which also makes it an example of a statement
(specifically, an assignment statement).
Arithmetic Expressions: Design Issues
1-109
• Design issues for arithmetic expressions
– operator precedence rules
– operator associativity rules
– order of operand evaluation
– operand evaluation side effects
– mode mixing expressions
Arithmetic Expressions: Operators
1-110
• A unary operator has one operand
• A binary operator has two operands
• A ternary operator has three operands
Order of Operator Evaluation
• All operators have two characteristics that effect
the order in which they are evaluated:
precedence and associativity.
• The rules that establish these characteristics are
either based on similar properties from algebra or
follow the best practices that have evolved over
decades of programming language development.
Operator Precedence Rules
1-112
• The operator precedence rules for expression
evaluation define the order in which
“adjacent” operators of different precedence
levels are evaluated
113
Structure of Arithmetic Expressions
For example:
a + b - c * d / e % f
-a + b - c * d / e % f – (398 + g) * 5981 / 15 % h
((-a) + b - c) * d / e % f – ((398 + g) * 5981 / 15) %
h
114
Structure of Arithmetic Expressions
Putting a term in parentheses may change the value
of the expression, because a term inside
parentheses will be calculated first.
For example:
a + b * c is evaluated as
“multiply b by c, then add a,” but
(a + b) * c is evaluated as
“add a and b, then multiply by c”
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Operator Precedence
• What is the order of evaluation in the following
expressions?
a + b + c + d + e
4
3
2
a + b * c - d / e
3 2
4
1
a / (b + c) - d % e
2 3
4
1
a / (b * (c + (d - e)))
4 1
2
3
1
Assignment Revisited
• You can consider assignment as an operator, with
a lower precedence than the arithmetic operators
First the expression on the right hand
side of the = operator is evaluated
Then the result is stored in the
variable on the left hand side
answer = sum / 4 + MAX * lowest;
1
4 3 2
Assignment Revisited
• The right and left hand sides of an assignment
statement can contain the same variable
First, one is added to the
original value of count
Then the result is stored back into count
(overwriting the original value)
count = count + 1;
119
Precedence Order Examples
• 1 - 2 - 3 =
• 1 - (2 - 3) =
• 1 + 2 * 3 + 4 =
• 1 + 2) * 3 + 4 =
• 24 / 2 * 4 =
• 24 / (2 * 4) =
• 5 + 4 % 6 / 2 =
• 5 + 4 % (6 / 2) =
• 5 + 4) % (6 / 2) =
Precedence and Order of evaluation
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
ASCII
• ASCII, abbreviated from American Standard
Code for Information Interchange, is a character
encoding standard for electronic communication.
• ASCII codes represent text in computers,
telecommunications equipment, and other
device.
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Type Conversion (or) Type casting
• A type cast is basically a conversion from one
type to another.
• There are two types of type conversion:
• Implicit type conversion
– Done by the compiler on its own
• Explicit type conversion
– Done by the user
#include<stdio.h>
int main()
{
int x = 10;
char y = 'a';
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
}
Output:
x = 107, z = 108.000000
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Type Conversion
• Explicit type conversion
– Converted by programmer using cast operator
– (type name) expression;
x=(int)7.5
y=(int) (a+b)
m=(23*2)%(int)5.4
#include<stdio.h>
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);
return 0;
}
129
What is an Expression?
a + b - c * d / e % f – (398 + g) * 5981 / 15 % h
In programming, an expression is a combination
of:
• Operands
• Operators
• Parentheses: ( )
• Eg: C=a+b;
• C,a,b=operands
• =,+: operators
Operators are
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators
Arithmetic Operators
• An arithmetic operator performs mathematical operations
such as addition, subtraction, multiplication, division etc
on numerical values (constants and variables).
Types of operator Symbolic
Representation
Arithmetic operator +,-,*,/ and %
Relational operator >,<,==,>=,<= and !=
Logical operator &&,|| and !
Increment and
decrement operator
++ and --
Assignment operator =
Bitwise operator &,|,^,>>,<< and ~
Comma operator ,
Conditional Operator ?:
CO2. To gain knowledge about the
control structures in C
Arithmetic operators
Operator Meaning
+ Addition or Unary Plus
– Subtraction or Unary Minus
* Multiplication
/ Division
% Modulus Operator
CO2. To gain knowledge about the
control structures in C
Integer Arithmetic
• Let x = 27 and y = 5
z = x + y  32
z = x – y  22
z = x * y  115
z = x / y  5
z = x % y  2
Q:5
R:2
CO2. To gain knowledge about the
control structures in C
Floating point arithmetic
• Let x = 14.0 and y = 4.0 then
z = x + y  18.0
z = x – y  10.0
z = x * y  56.0
z = x / y  3.50
The operator % cannot be used with FLOAT
operands
Mixed mode Arithmetic
When one of the operand is real and the other is
integer then the result is also real and the
operation is called mixed mode arithmetic.
Eg: 15/10.0= 1.5
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Relational Operators
• Relational Operators: Compare two quantities and
depending on their relation taken certain decision.
<, <=, > >=, ==, != are the relational operators.
 Relational operators are used in decision making and
loops.
The expression
operand1 relational-operator operand2
takes a value of 1(int) if the relationship is true and
0(int) if relationship is false.
Example
int a = 25, b = 30, c, d;
c = a < b;
d = a > b;
value of c will be 1 and that of d will be 0.
CO2. To gain knowledge about the
control structures in C
Relational Expressions
6.5 <= 25 TRUE
-65 > 0 FALSE
10 < 7 + 5 TRUE
CO2. To gain knowledge about the
control structures in C
Relational Operators
Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Logical Operators
• Logical Operators: Used to test more than one condition and make
decisions. Logical operators are commonly used in decision making in C
programming.
– &&, || and ! are the three logical operators.
– expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero.
– expr1 || expr2 has a value 1 if expr1 and expr2 both are nonzero.
– !expr1 has a value 1 if expr1 is zero else 0.
Example
if ( marks >= 40 && attendance >= 75 ) grade = ‘P’
If ( marks < 40 || attendance < 75 ) grade = ‘F’
AND
0*0=0
0*1=0
1*0=0
1*1=1
OR
0+0=0
0+1=1
1+0=1
1+1=1
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
CO2. To gain knowledge about the
control structures in C
Find out the answer
a = 5
b = 4
x=10, y = 9
m=6
n = 3
a > b && x = = 10 True / 1
a < m || a < n True / 1
! (x >= y) False / 0
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Assignment Operators
• Assignment operators: Assigning the result of an expression to a
variable. An assignment operator is used for assigning a value to a
variable. The most common assignment operator is =.
The general form of an assignment operator is
v op= exp
Where v is a variable and op is a binary arithmetic operator. This
statement is equivalent to
v = v op (exp)
a = a + b can be written as a += b
a = a * b can be written as a *= b
a = a / b can be written as a /= b
a = a - b can be written as a -= b
a = a*(n+1) can be written as a*=n+1
CO2. To gain knowledge about the
control structures in C
Assignment Operators
Statement with simple
assignment operator
Statement with
shorthand operator
a = a + 1 a += 1
a = a – 1 a -= 1
a = a * (n+1) a *= (n+1)
a = a / (n+1) a /= (n+1)
a = a % b a %= b
CO2. To gain knowledge about the
control structures in C
Assignment Operators
Statement with simple
assignment operator
Statement with
shorthand operator
a = a + 1 a += 1
a = a – 1 a -= 1
a = a * (n+1) a *= (n+1)
a = a / (n+1) a /= (n+1)
a = a % b a %= b
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
• Unary Operators Unary operators act on single
operands. C language supports three unary operators.
They are unary minus, increment, and decrement
operators.
• Unary Minus (–) Unary minus operator negates the
value of its operand.
• For example, if a number is positive then it becomes
negative when preceded with a unary minus operator.
Similarly, if the number is negative, it becomes positive
after applying the unary minus operator.
• For example, int a, b = 10; a = –(b); The result of this
expression is a = –10, because variable b has a positive
value. After applying unary minus operator (–) on the
operand b, the value becomes –10, which indicates it
has a negative value.
C Increment and Decrement Operators
• C programming has two operators increment ++ and
decrement - - to change the value of an operand
(constant or variable) by 1.
Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1.
These two operators are unary operators, meaning they
only operate on a single operand.
A=10
A+b; binary
++a; unary 11
--a; unary 9
Increment and Decrement Operators
• Increment and Decrement Operators
– The operators ++ and –- are called increment and decrement operators.
– Post Increment: a++
– Pre increment: ++a
– a++ = a=a+1
– a-- = a=a-1
– Example
Let a = 10
a++ =10
++a= 11
(++b)+b+b = 33
b+(++b)+b = 33
b+b+(++b) = 31
b+b*(++b) = 132
Post decrement: a--
Pre decrement: --a
Rules for increment and decrement
• Increment and decrement operators are unary operators and thus
require variable as their operands.
• When postfix ++ or – is used with a variable in an expression , the
expression evaluated first using the original value of the variable
and then the variable is incremented (or decremented) by one.
• When prefix ++ or – is used in an expression , the variable is
incremented first then the expression is evaluated using the new
value of the variable.
• The precedence and associativity of ++ and – operators are the
same as those of unary + and unary -.
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Increment and Decrement Operators
m = 5;
y = ++m; (prefix)
After Execution:
y=6, m=6
m = 5;
y = m++; (post fix)
After Execution:
y=5, m=6
Conditional Operators
• Conditional Operator: The conditional operator contains a condition followed by
two statements or values. If the condition is true the first statement is executed ,
otherwise the second statement.
– A ternary operator pair” ? :“ is available in to construct conditional
expressions of the following form:
condition ? exp1: exp2
where exp1, exp2, exp3 are expressions.
Eg:
a=10; b=15;
x=(a>b) ? a : b;
if the condition is true a part will be executed else b part will be executed.
C Bitwise Operators
• During computation, mathematical operations like:
addition, subtraction, multiplication, division, etc are
converted to bit-level which makes processing faster and
saves power.
• Bitwise operators are used in C programming to perform
bit-level operations
Operators
• Bitwise Operator
– To manipulate data at bit level, used for testing the
bits, or shifting them right to left.
– They not be applied to float or double.
Operator meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
12 & 25=8
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Bitwise XOR Operator
Same = > zero
Diff => one
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
~n=-(n+1)
~35=-(35+1)=-36
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
CO2. To gain knowledge about the
control structures in C
Special Operators
• comma operator int a,b,c;
• size of operator sifeof(a);=>2
• pointer operators (& and *)
• member selection operators (. and ->).
• structure
CO2. To gain knowledge about the
control structures in C
The Comma Operator
value = (x = 10, y = 5, x + y);
In for loops:
for (n=1, m=10, n <=m; n++,m++)
In while loops
While (c=getchar(), c != ‘1’)
Exchanging values
t = x, x = y, y = t;
Comma Operator
• Comma operators are used to link related
expressions together. For example:
int a, c = 5, d;
The sizeof operator
• The sizeof is a unary operator that returns the size of
data (constants, variables, array, structure, etc).
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Pointer operators
&
Returns the address of
a variable.
&a; returns the actual
address of the variable.
* Pointer to a variable. *a;
dot (.) operator
The dot (.) operator is used for direct member
selection via object name. In other words, it is
used to access the child object. It is used in
Structure and Union.
Syntax:
object.member;
• An Arrow operator in C allows to access elements in Structures and
Unions. It is used with a pointer variable pointing to a structure or union.
• The arrow operator is formed by using a minus sign, followed by the
greater than symbol as shown below.
Syntax:
(pointer_name)->(variable_name)
Operation: The -> operator in C gives the value held by variable_name to
structure or union variable pointer_name.
Difference between Dot(.) and Arrow(->) operator
The Dot(.) operator is used to normally access
members of a structure or union.
The Arrow(->) operator exists to access the
members of the structure or the unions using
pointers.

More Related Content

PPTX
C session 1.pptx
PPTX
C_Programming_Notes_ICE
PPTX
Programming in C
PPTX
Programming in c
PPTX
Introduction to C programming
PPTX
Computer Programming In C.pptx
PDF
PPTX
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C session 1.pptx
C_Programming_Notes_ICE
Programming in C
Programming in c
Introduction to C programming
Computer Programming In C.pptx
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY

Similar to UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx (20)

PPTX
PPTX
PPTX
Lecture1.Introduction to Computer programming.pptx
PDF
Understanding C and its Applications.pdf
PPTX
Unit ii
PPTX
Master the Fundamentals of C Programming Language
PPTX
Programming in C & Decision Making Branching
PPTX
CSE_1201_Lecture_1_Introduction_to_Programming_0fd134f8149173dfa0821f1575f733...
PPTX
Introduction to c programming
PPTX
Programming in C and Decision Making Branching
PPTX
Introduction to C Programming
PPTX
Introduction_to_Programming.pptx
DOCX
Programming In C- (1)jhgjhgjhgjhghj.docx
PPTX
Fundamental programming Nota Topic 1.pptx
PDF
Lecture 1
PDF
X-CS-8.0 Programming in C Language 2022-2023.pdf
PPTX
PPTX
C programming
PPTX
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
PPTX
Embedded c c++ programming fundamentals master
Lecture1.Introduction to Computer programming.pptx
Understanding C and its Applications.pdf
Unit ii
Master the Fundamentals of C Programming Language
Programming in C & Decision Making Branching
CSE_1201_Lecture_1_Introduction_to_Programming_0fd134f8149173dfa0821f1575f733...
Introduction to c programming
Programming in C and Decision Making Branching
Introduction to C Programming
Introduction_to_Programming.pptx
Programming In C- (1)jhgjhgjhgjhghj.docx
Fundamental programming Nota Topic 1.pptx
Lecture 1
X-CS-8.0 Programming in C Language 2022-2023.pdf
C programming
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
Embedded c c++ programming fundamentals master
Ad

More from RoselinLourd (18)

PPT
function_v1fgdfdf5645ythyth6ythythgbg.ppt
PPTX
SCADAhthfhggff6776576765656756756767.pptx
PPTX
Introduction to C Programming fjhjhjh.pptx
PPTX
types of learningyjhjfhnjfnfnhnnnnn.pptx
PPTX
intelligent agentguggjhkhkhkhkhkhkhkj.pptx
PPTX
K means Clustering algorithmgfgbfgb.pptx
PPTX
PAIML-UNIT 5hgthgghrgergrgrgrgrgrgrg.pptx
PPTX
PAIML - UNIT 4dfvdfvdfvdfvdfvvfdvsd.pptx
PPTX
PAIML-UNIT 3dfgbgfdbdfgbdfbdfbdfbdfb.pptx
PPTX
PAIML - UNIT 2dfbdfbvdfvdvdfvdfvdfv.pptx
PPTX
PAIML - UNIT 1ascascascasasfhfgbfgb.pptx
PPT
1680242330325_Knowledge Representation1 (1).ppt
DOCX
1680242330325_Knowledge Representation1 (1).docx
DOCX
Unit IV Knowledge representation we.docx
PDF
1680242333702_name,type and measures of logic.pdf
PPTX
Unitii scm for engineering students knowledge
PPTX
ERP UNIT 1.PART IIghjgjhgjgggggggkhk.pptx
PPT
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
function_v1fgdfdf5645ythyth6ythythgbg.ppt
SCADAhthfhggff6776576765656756756767.pptx
Introduction to C Programming fjhjhjh.pptx
types of learningyjhjfhnjfnfnhnnnnn.pptx
intelligent agentguggjhkhkhkhkhkhkhkj.pptx
K means Clustering algorithmgfgbfgb.pptx
PAIML-UNIT 5hgthgghrgergrgrgrgrgrgrg.pptx
PAIML - UNIT 4dfvdfvdfvdfvdfvvfdvsd.pptx
PAIML-UNIT 3dfgbgfdbdfgbdfbdfbdfbdfb.pptx
PAIML - UNIT 2dfbdfbvdfvdvdfvdfvdfv.pptx
PAIML - UNIT 1ascascascasasfhfgbfgb.pptx
1680242330325_Knowledge Representation1 (1).ppt
1680242330325_Knowledge Representation1 (1).docx
Unit IV Knowledge representation we.docx
1680242333702_name,type and measures of logic.pdf
Unitii scm for engineering students knowledge
ERP UNIT 1.PART IIghjgjhgjgggggggkhk.pptx
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
Ad

Recently uploaded (20)

PDF
Chevening Scholarship Application and Interview Preparation Guide
PDF
CAT 2024 VARC One - Shot Revision Marathon by Shabana.pptx.pdf
PPTX
MMW-CHAPTER-1-final.pptx major Elementary Education
PPTX
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
PPT
hsl powerpoint resource goyloveh feb 07.ppt
PDF
Kalaari-SaaS-Founder-Playbook-2024-Edition-.pdf
PDF
Diabetes Mellitus , types , clinical picture, investigation and managment
PPTX
4. Diagnosis and treatment planning in RPD.pptx
PPTX
Theoretical for class.pptxgshdhddhdhdhgd
PDF
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
PPTX
2025 High Blood Pressure Guideline Slide Set.pptx
DOCX
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
PDF
0520_Scheme_of_Work_(for_examination_from_2021).pdf
PDF
WHAT NURSES SAY_ COMMUNICATION BEHAVIORS ASSOCIATED WITH THE COMP.pdf
PDF
Disorder of Endocrine system (1).pdfyyhyyyy
PPTX
Neurology of Systemic disease all systems
PPTX
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
PPT
hemostasis and its significance, physiology
PPTX
Power Point PR B.Inggris 12 Ed. 2019.pptx
PPTX
Designing Adaptive Learning Paths in Virtual Learning Environments
Chevening Scholarship Application and Interview Preparation Guide
CAT 2024 VARC One - Shot Revision Marathon by Shabana.pptx.pdf
MMW-CHAPTER-1-final.pptx major Elementary Education
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
hsl powerpoint resource goyloveh feb 07.ppt
Kalaari-SaaS-Founder-Playbook-2024-Edition-.pdf
Diabetes Mellitus , types , clinical picture, investigation and managment
4. Diagnosis and treatment planning in RPD.pptx
Theoretical for class.pptxgshdhddhdhdhgd
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
2025 High Blood Pressure Guideline Slide Set.pptx
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
0520_Scheme_of_Work_(for_examination_from_2021).pdf
WHAT NURSES SAY_ COMMUNICATION BEHAVIORS ASSOCIATED WITH THE COMP.pdf
Disorder of Endocrine system (1).pdfyyhyyyy
Neurology of Systemic disease all systems
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
hemostasis and its significance, physiology
Power Point PR B.Inggris 12 Ed. 2019.pptx
Designing Adaptive Learning Paths in Virtual Learning Environments

UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx

  • 2. What is Programming and Languages? A programming language is a notation for writing programs, which are specifications of a computation or algorithm A programming language is a set of rules that provides a way of telling a computer what operations to perform A programming language is a set of rules for communicating an algorithm Medium of communication between computer and the user containing words, symbols and syntax rules Each language has its own vocabulary and rules.
  • 3. Generation of Programming Languages • There are Five Generations of Programming languages 1 Machine Language (1st Generation Language – 1945) 2 Assembly Language(2nd Generation Language – 1950s) 3 High Level Language (3rd Generation Language – 1960s) 4 Very High Level Language (4th Generation Language – 1970s) 5 Natural Language (5th Generation Language – 1980)
  • 4. List of High Level Language • FORTRAN - Formula Translation Language • COBOL - Common Business Oriented Language • ALGOL - Algorithmic Language • RPG - Report Program Generator • APL - A Programming Language • BASIC - Beginners All Purpose Symbolic Instruction Code • PL/I - Programming Language I • PASCAL -Named after Blaise Pascal, a French Philosopher • Ada - Named after Lady Lovelace Ada • C - A General Purpose Programming Language • C++ Object Oriented Programming Language • JAVA -Object Oriented Programming Language
  • 5. 5 Some examples of the use of C Programming Operating Systems. Language Compilers. Assemblers. Text Editors. Print Spoolers. Network Drivers. Modern Programs. Data Bases. Hardware Internet Application Memory Management Firewalls Virus Protection Gaming S/W etc.,
  • 6. History of C • C is a middle-level programming language developed by Dennis Ritchie during the early 1970s while working at AT&T Bell Labs in the USA. • Eventually, C was developed during 1971-73, containing both high-level functionality and the detailed features required to program an operating system. • C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring
  • 7. 7 Why Should I learn C Programming • C is a procedural programming language. • The main features of C language include low- level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.
  • 8. 8 Why Should I learn C Programming Cont.. 1. There are some good reasons to learn to program in C. First, The age has its advantages: C has been around for 30 years, and there is a ton of source code available in the field. 2. This means there's a lot to learn from, and a lot to use. Moreover, many of the issues with the language have been clearly elucidated 3. Plus, with C, you get lots of strong opinions mixed with insights that you can understand.
  • 9. 9 Why Should I learn C Programming Cont.. • In fact, a lot of fun programming is done in C -- for instance, system software and data managers. • If you want to be able to do more than write a simple web app, C is a great language. • If you want to write a great, fast game, C is again a great choice. • You can write an entire Operating System Software in C.
  • 10. 10 Benefits or Importance of C Language • As a middle-level language, C combines the features of both high- level and low-level languages. • C is a structured programming language which allows a complex program to be broken into simpler programs called functions. • Various features of C including direct access to machine level hardware. • C language an optimum choice for scripting applications and drivers of embedded systems. • C language is case-sensitive which means lowercase and uppercase letters are treated differently.
  • 11. 11 Benefits or Importance of C Language • C is highly portable and is used for scripting system applications which form a major part of Windows, UNIX, and Linux operating system. • C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc. • C language has a rich library which provides a number of built-in functions. • C implements algorithms and data structures swiftly, facilitating faster computations in programs. like MATLAB and Mathematica.
  • 13. Structure of C Program • Every C program contains a number of building blocks. • These building blocks should be written in a correct order and procedure, to execute without any errors.
  • 15. 1. Documentation Section • The general comments are included in this section. • The comments are not a part of executable programs. • The comments are placed between delimiters (/* and */). Example: • //Factorial of a given number • /* This program for factorial of a given number */
  • 16. 2. Preprocessor Section • It provides preprocessor statements which direct the compiler to link functions from the system library. Example #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h>
  • 17. #include <stdio.h> printf(), scanf() #include<conio.h> clrscr(), getch() #include <math.h> pow(), sqrt() #include<string.h> strlen(),strcpy()
  • 18. 3. Definition Section • This section defines all symbolic constants. • It refers to assigning macros of a name to a constant. Example #define PI 3.14 #define TRUE 1 #define FALSE 0
  • 19. Global Declaration Section • It contains variable declarations, which can be accessed anywhere within the program. • It should declare before the main() function. • Global variable otherwise known as External variable or Public variable. Example int a; main() { ------ ------ }
  • 20. main() function • Each and every C program should have only one main() function. • Without main() function, the program cannot be executed. • The main function should be written in lowercase only. • It should not be terminated with semicolon.
  • 21. Declaration Part • Each and every variable should be declared before going to use those variables in execution part. Execution part • Here, the logic of the program can be implemented. • These statements are known as program statements or building blocks of a program. • They provide instructions to the computer to perform a specific task.
  • 22. Subprogram section • The subprogram section contains all the user- defined functions that are called in the main() function. • User-defined functions are generally placed immediately after the main() function, although they may appear in any order.
  • 23. Example Program 1 #include<stdio.h> void main() { printf(“SMVEC”); } Output: SMVEC
  • 24. Example Program 2 #include<stdio.h> void main() { int a,b,c; printf(“Enter the value of a=”); scanf(“%d”,&a); printf(“Enter the value of b=”); scanf(“%d”,&b); c=a+b; printf(“Sum of a and b=%d”,c); }
  • 26. Process of Compiling and running a C program
  • 27. COMPILATION & LINKING PROCESSES Execution is the process of running the program, to execute a ‘c’ program, we need to follow the steps given below. 1. Creating the program 2. Compiling the program 3. Linking the program with system library. 4. Executing the program.
  • 32. Save the file with .c extension
  • 34. Compile the file using Alt+F9
  • 35. Run the file using Ctrl+F9
  • 36. IDE(Integrated Development Environment ) • An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build automation tools and a debugger.
  • 37. IDE FOR C/C++ • Visual Studio Code • Eclipse • NetBeans • Sublime Text • Atom • Code::Blocks • CodeLite • CodeWarrior
  • 38. Visual Studio code • A visual studio code is a lightweight software application with a powerful source code editor that runs on the desktop. • It is a free source code editor developed by Microsoft for Windows, Mac OS and Linux. It is a software editor that has a rich extension of various languages like C++, C+, C, Java, Python , PHP, Go, etc. and runtime language extensions such as .NET and Unity. • It is easy to edit, build, syntax highlighting, snippets, code refactoring and debugging. In visual studio code, we can change the application's background theme, keyboard shortcuts set on our preferences, install an extension and add additional functionality.
  • 39. It is an open-source code editor developed by Microsoft for Windows, Linux and Mac OS. Visual Studio Code is based on an Electron framework
  • 40. Visual Studio code • Key Benefits: • Support for Debugging • Syntax highlighting • Intelligent Code completion, snippets and code refactoring • EmbeddedGit Control • Completely portable • Easy customization • Programming Languages Supported: C, C++, C#, CSS, Go, HTML, Java, JavaScript, Python, PHP, TypeScript and much more.
  • 41. PROGRAM A program consists of a series of instructions that a computer processes to perform the required operation.
  • 42. Characteristics of a program • Accuracy • Clarity • Modularity • Portability • Flexibility • Efficiency • Generality • Documentation
  • 43. Program Development Life Cycle • Problem Definition • Problem Analysis • Algorithm Development • Coding & Documentation • Testing & Debugging • Maintenance
  • 45. Programming languages • Procedural programming (e.g. C, Fortran, Pascal) • Object-oriented programming (e.g. C++, Java, Python) • Functional programming (e.g. Lisp, ML, Haskell) • Logic programming (e.g. Prolog)
  • 46. PROBLEM SOLVING TECHNIQUES Problem solving technique is a set of techniques and graphical tools that help in providing logic for solving a problem. Some of the problem solving tools are 1. Algorithm 2. Flowchart 3. Pseudo code
  • 47. Algorithm • Algorithm means the logic of a program. • It is a step-by-step description of how to arrive at a solution of a given problem.
  • 48. Characteristics of an algorithm • In algorithms each and every instruction should be precise. • In algorithms each and every instruction should be unambiguous. • The instructions in an algorithm should not be repeated infinitely. • Ensure that the algorithm will ultimately terminate. • The algorithm should be written in sequence. • It looks like normal English. • The desired result should be obtained only after the algorithm terminates.
  • 49. Algorithm for sum of two numbers
  • 50. Flowchart • Flowchart is a pictorial representation of an algorithm. • It is often used by programmer as a program planning tool for organizing a sequence of step necessary to solve a problem by a computer.
  • 53. Algorithm and Flowchart Sum of Two numbers
  • 54. Data Name (or) Variable • In C, a variable is a data name used for storing a data value. • Its value may be changed during program execution. • The value of variables keeps on changing during the execution of a program. • In other words, a variable can be assigned different values at different times during the execution of a program.
  • 55. Rules for naming the variables • A variable name can be any combination of alphabets, digits or underscore. • But first character should be alphabets or an underscore (_). • The length of the variable cannot exceed up to 8 characters long. • Some of the c compilers can be recognized up to 31 characters long. • The variable should not be a C keyword. • Both lowercase and uppercase are permitted. • The special symbols are not permitted except underscore.
  • 56. Syntax: datatype v1,v2,v3,….,vn; Description: datatype - It is the type of data. v1, v2, v3 ,…, vn - list of variables. Example 1: int regno; float cgpa; char name[10];
  • 57. Example 2: Declaration of multiple variables of the same data types can be done in one statement. int mark1; int mark2; int mark3; int mark4; can be written to become int mark1, mark2, mark3, mark4;
  • 58. Two types: ✓ Local variables ✓ Global variables
  • 59. Local variables: The variables which are defined inside a function block or inside a compound statement of a function sub- program are called local variables. #include<stdio.h> void main() { int i=10; ---- ---- }
  • 60. Global variables: The variables that are declared before the function main() are called the global/external variables. int a=5; #include<stdio.h> void main() { Int i=10; ---- ---- }
  • 61. Data Type • Data type is the type of the data, that are going to access within the program. • C supports different data types, each data may have predefined memory requirement and storage representation.
  • 62. User defined data types Empty data set Primary data types Derived data types Data Types char int double float typedef enum array pointer union struct void
  • 63. 1. Primary data types C has the following basic built-in primary datatypes. • char • int • float • double
  • 64. char (Character) Char (Character) data type is used to store single character, within single quotes e.g. 'a', 'z', 'e' etc. Syntax: char variablename; Example: char chi='a', cha; int (Integer) Integer data type is used to store numeric values without any decimal point e.g. 7, -101, 107, etc. Syntax: int variablename; Example: int roll, marks, age; int marks=100;
  • 65. float Float data type is used to store numeric values with decimal point. e.g. 3.14, 7.67 etc. e.g. percentage, price, pi, area etc. Syntax: float variable name; Example: float per, area; per=95.7; double Double is used to define BIG floating point numbers. It reserves twice the storage for the number. It contains 8 bytes. Syntax: double variablename; Example: double Atoms;
  • 67. Format Specifiers • %d or %i – integer • %c – character • %f – float • %s – string • %e – double • %o – octal • %x - Hexadecimal
  • 68. PROGRAM //primary data types #include<stdio.h> #include<conio.h> void main() { int rollno; float fees; char grade; clrscr(); printf("tprimary data typesn"); printf("Enter rollno:n"); scanf("%d",&rollno); printf("Enter fees:n"); scanf("%f",&fees); printf("Enter grade:n"); scanf("%c",&grade); printf("The details aren"); printf("Roll no=%dn",rollno); printf("Fees=%.2fn",fees); printf("Grade=%cn",grade); getch(); }
  • 70. 2. User defined data type • User defined data type is used to create new data types. • Types: – typedef – enum
  • 71. typedef: • The 'typedef' allows the user to define new data-types that are equivalent to existing data types. Syntax: typedef type new-type; Type refers to an existing data type. New-type refers to the new user-defined data type. Example: typedef int number; Declare integer variables as: number roll, age, marks; It is equivalent to: int roll, age, marks;
  • 72. enum (Enumeration) • Enumeration types provide the facility to specify the possible values of a variable by meaningful symbolic means. Syntax: enum enumname{member1,member2,...,membern}; Example: enum colour{black,white,pink,red,green,yellow}; • An enumeration type is implemented by associating the integer value with the enumeration constant. • Thus, the value 0 is associated with black, 1 with white, 2 with pink, 3 with red, 4 with green, 5 with yellow.
  • 73. 3. Derived Data type • Data types that are derived from fundamental data types are called derived data types. • Types – array, pointer, structure and union. Array • An array is a collection of variables of same type i.e. collection of homogeneous data referred by a common name. In memory, array elements are stored in a continuous location. Syntax: datatype arrayname[ ]; Example: int a[10]; char name[20];
  • 74. pointer • A pointer is a special variable that holds a memory address of another variable. Syntax: datatype *var_name; Example: int a,*b; b=&a; * - indirection operator & - address of operator variable 'b' stores the address of variable 'a'.
  • 75. #include<stdio.h> void main() { int a,*b; a=10; b=&a; printf(“value of a=%d”,a); printf(“address of a=%d”,&a); printf(“value of b=%d”,b); printf(“address of b=%d”,&b); printf(“value of a through b=%d”,*b); } 10 a -12 -12 b -14 Variable Value Address
  • 77. Struct • A struct is a user defined data type that stores multiple values of same or different data types under a single name. In memory, the entire structure variable is stored in sequence. Syntax: Example: struct <structure name> { member1; member2; ----- ----- }; Structure name is the name of structure. e.g. store details of a student as - name, roll, marks. struct student { char name [20]; int roll, float marks; };
  • 78. union • A union is a user defined data type that stores multiple values of same or different data types under a single name. In memory, union variables are stored in a common memory location. Syntax: Example: union <tag name> { var1; var2; ----- ---- }; Tag name is the name of union, e.g, store details of a student as- name, roll, marks. union student { char name[20]; int roll, float marks; };
  • 80. C Character Set • The character set is the fundamental raw material of any language and they are used to represent information.
  • 82. EXECUTION CHARACTER SET Escape Sequence Name Meaning a Alert Sounds a beep b Backspace Backs up 1 character f Formfeed Starts a new screen of page n New line Moves to beginning of next line r Carriage return Moves to beginning of current line t Horizontal tab Moves to next tab position v Vertical tab Moves down a fixed amount Back slash Prints a back slash ’ Single quotation Prints a single quotation ” Double quotation Prints a double quotation ? Question mark Prints a question mark
  • 83. C TOKENS 1. Keywords 2. Identifiers 3. Constants 4. String 5. Operators 6. Special Symbols C Tokens Operators Constants Special Symbols String Identifiers Keywords
  • 84. 1. Keywords • Keywords are C tokens that have a strict meaning. – They are explicitly reserved and cannot be redefined. – All keywords must be written in lowercase. – All keywords have fixed meanings and these meanings cannot be changed. • C has 32 key words. – Some implementations such as Borland’s C or Microsoft’s C have additional key words.
  • 85. List of Keywords auto do goto signed unsigned break double if sizeof void case else int static volatile char enum long struct while const extern register switch continue float return typedef default for short union
  • 86. 2. Identifiers • It is used to refer the name of the variables, functions and arrays. • An identifier is a token: – Composed of a sequence of letters, digits, and the underscore character _ • Note: Variable names are identifiers
  • 87. Rules • An identifier must begin with a letter. • Both upper case and lower cases are permitted. • Keywords are not permitted. • No special symbols are permitted except underscore (_). • Some C compiler accept under score may appear in first letter. • Maximum length of an identifier is 31. • Blank spaces are not allowed in between an identifier.
  • 88. Example: • Regno • _regno • Reg no • Regno4 • 5regno
  • 90. Numeric constants • There are two types of numeric constants, 1. Integer constants 2. Real or floating-point constants Integer constants • An integer constant refers to a sequence of digits without a decimal point. Example: marks 90, per 75 Real Constants • A Real constant is made up of a sequence of numeric digits with presence of a decimal point. Example: distance =126.0; Height = 5.6;
  • 91. Character Constants • There are two types, they are given below. i) Single Character constants ii) String constants Single Character constants • A single character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Example: ‫ۥ‬ m = ‫ۥ‬ ‫ۥ‬ ‫ۥۥ‬A ‫ۥ‬ String constants • A string constant is a sequence of characters enclosed in double Quotes, the characters may be letters, numbers, special characters and blank spaces etc. Example: ”hi”, “325”.
  • 92. Importance of Input and Output function in C We feed information into the computer, tell the computer what do with it, and then get a result back out. • The information we put into a computer is called input and • The information we receive from the computer is called output. • Input can come from just about anywhere. Keystrokes on a keyboard, data from an internet connection, or sound waves converted to electrical signals are examples of input. • Output can also take many forms such as video played on a monitor, a string of text displayed in a terminal, or data we save onto a hard drive. • The collection of input and generation of output is known under the general term, input/output, or I/O for short, and is a core function of computers.
  • 93. Input and Output functions in C Interestingly, the C programming language doesn't have I/O abilities built into it. It does, however, provide us with an external library containing I/O functions which we can compile and link into our programs. We have already used an output library function: printf(). You may recall this function resided in the stdio.h library file. As that file's name implies, stdio.h contains standardized I/O functions for adding input and output capability to our programs.
  • 94. Input and Output functions in C – I/O Stream • C has no built-in statements for input or output. • All input and output is performed with streams. • A "stream" is a sequence of characters organized into lines. • Each line consists of zero or more characters and ends with the "newline" character.
  • 95. I/O Stream • All input and output is performed with streams, which are sequences of bytes. • In input operations, the bytes flow from a device (e.g., a keyboard, a disk drive, a network connection) to main memory. • In output operations, bytes flow from main memory to a device (e.g., a display screen, a printer, a disk drive, a network connection, and so on).
  • 96. I/O Stream • Normally, the standard input stream is connected to the keyboard and the standard output stream is connected to the screen. • Operating systems often allow these streams to be redirected to other devices. • A third stream, the standard error stream, is connected to the screen. • Error messages are output to the standard error stream.
  • 97. Types of Stream in C - Summary • Standard input stream is called "stdin" and is normally connected to the keyboard • Standard output stream is called "stdout" and is normally connected to the display screen. • Standard error stream is called "stderr" and is also normally connected to the screen.
  • 98. Input / Output data flow and assign • A stream is an abstraction that refers to a flow of data. standard output device stdout variable a printf(”%d”,a); standard input device stdin variable a scanf(”%d”,&a); Keyboard Monitor
  • 100. getc() and putc() • getc() which reads the next available character from the screen. • putc() which prints the character. Syntax: x=getc(); putc(); Example: char x; x=getc(); putc(x); Program: #include <stdio.h> void main( ) { int c; printf( "Enter a value :"); c = getc( ); printf( "nYou entered: "); putc( c ); } Output: Enter a value : a You entered: a
  • 101. getchar() and putchar() • getchar() which reads the next available character from the screen. • putchar() which prints the character. Syntax: x=getchar(); putchar(x); Example: char x; x=getchar(); Program: #include <stdio.h> void main( ) { int c; printf( "Enter a value :"); c = getchar( ); printf( "nYou entered: "); putchar( c ); Output: Enter a value : a You entered: a
  • 102. gets() and puts() • gets() is used to read the string from the standard input device (keyboard) • puts() is used to display a single character to standard output device. Syntax: gets(dataname); puts(dataname); Example: char name[10]; Program: #include<stdio.h> void main() { char name[100]; printf("Enter a string"); gets( name ); printf(“You entered:”) puts( name ); Output: Enter a string : rahul You entered: rahul
  • 103. scanf() and printf() • The standard input-output header file, named stdio.h contains the definition of the functions printf() and scanf(), which are used to display output on screen and to take input from user respectively. Program: #include<stdio.h> void main() { int i; printf("Please enter a value:"); scanf("%d", &i); printf( "nYou entered: %d", i); } Output: Please enter a value: 10 You entered: 10
  • 104. Scanf() with multiple input with Same data type void main() { int m1,m2,m3; printf(“Enter 3 marks for m1,m2,m3:”); scanf(“%d %d %d”,&m1,&m2,&m3); printf(“nThe given Marks : %d %d %d”,m1,m2,m3); } output: Enter 3 marks for m1,m2,m3: 67 87 95 The given Marks : 67 87 95
  • 105. void main() { int age float per; printf(“Enter your age and percentage:”); scanf(“%d %f” ,&age,&per); printf(“nMy age is %d and percentage is: %f,age,per); } output: Enter your age and percentage: 18 85.26 My age is 18 and percentage is: 85.26 Scanf() with multiple input with different data type
  • 106. Operators and Operands • Operators are symbols that instruct the computer to perform a single, simple task, and most consist of just one or two characters ( =, +, *, <=, or == -), but some operators consist of a complete word (sizeof()). • Operands are expressions or values on which an operator operates or works (often constants or variables but sub-expressions are also permitted).
  • 107. Operand examples: In each example, + is a operator while the symbols appearing on either side of it are operands. a. Operands may be constants 2 + 5 b. Operands may be variables A + B c. Operands may be more complex expressions 2*A + 5*B
  • 108. Number of Operands Operators are characterized by the number of operands that they require. Most operators require one or two operands but C does have one operator that requires three. Unary Operators Unary operators only require one operand. Most of the time the operand is placed to the right of the operator but sometimes it is placed to the left. The most common example is the unary minus: N, which changes the sign of ‑ the value stored in variable N. Binary Operators Binary operators require two operands. The arithmetic operators are the most familiar examples of binary operators. The assignment operator is also common. For example: A = counter + 5; "counter" and "5" are the "+" operator's operands, and "A" and the value of the expression "counter + 5" are the "=" operator's operands. Note that the above example ends with a semicolon, which also makes it an example of a statement (specifically, an assignment statement).
  • 109. Arithmetic Expressions: Design Issues 1-109 • Design issues for arithmetic expressions – operator precedence rules – operator associativity rules – order of operand evaluation – operand evaluation side effects – mode mixing expressions
  • 110. Arithmetic Expressions: Operators 1-110 • A unary operator has one operand • A binary operator has two operands • A ternary operator has three operands
  • 111. Order of Operator Evaluation • All operators have two characteristics that effect the order in which they are evaluated: precedence and associativity. • The rules that establish these characteristics are either based on similar properties from algebra or follow the best practices that have evolved over decades of programming language development.
  • 112. Operator Precedence Rules 1-112 • The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated
  • 113. 113 Structure of Arithmetic Expressions For example: a + b - c * d / e % f -a + b - c * d / e % f – (398 + g) * 5981 / 15 % h ((-a) + b - c) * d / e % f – ((398 + g) * 5981 / 15) % h
  • 114. 114 Structure of Arithmetic Expressions Putting a term in parentheses may change the value of the expression, because a term inside parentheses will be calculated first. For example: a + b * c is evaluated as “multiply b by c, then add a,” but (a + b) * c is evaluated as “add a and b, then multiply by c”
  • 116. Operator Precedence • What is the order of evaluation in the following expressions? a + b + c + d + e 4 3 2 a + b * c - d / e 3 2 4 1 a / (b + c) - d % e 2 3 4 1 a / (b * (c + (d - e))) 4 1 2 3 1
  • 117. Assignment Revisited • You can consider assignment as an operator, with a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 1 4 3 2
  • 118. Assignment Revisited • The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count Then the result is stored back into count (overwriting the original value) count = count + 1;
  • 119. 119 Precedence Order Examples • 1 - 2 - 3 = • 1 - (2 - 3) = • 1 + 2 * 3 + 4 = • 1 + 2) * 3 + 4 = • 24 / 2 * 4 = • 24 / (2 * 4) = • 5 + 4 % 6 / 2 = • 5 + 4 % (6 / 2) = • 5 + 4) % (6 / 2) =
  • 120. Precedence and Order of evaluation
  • 122. ASCII • ASCII, abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. • ASCII codes represent text in computers, telecommunications equipment, and other device.
  • 124. Type Conversion (or) Type casting • A type cast is basically a conversion from one type to another. • There are two types of type conversion: • Implicit type conversion – Done by the compiler on its own • Explicit type conversion – Done by the user
  • 125. #include<stdio.h> int main() { int x = 10; char y = 'a'; // y implicitly converted to int. ASCII // value of 'a' is 97 x = x + y; // x is implicitly converted to float float z = x + 1.0; printf("x = %d, z = %f", x, z); return 0; } Output: x = 107, z = 108.000000
  • 127. Type Conversion • Explicit type conversion – Converted by programmer using cast operator – (type name) expression; x=(int)7.5 y=(int) (a+b) m=(23*2)%(int)5.4
  • 128. #include<stdio.h> int main() { double x = 1.2; // Explicit conversion from double to int int sum = (int)x + 1; printf("sum = %d", sum); return 0; }
  • 129. 129 What is an Expression? a + b - c * d / e % f – (398 + g) * 5981 / 15 % h In programming, an expression is a combination of: • Operands • Operators • Parentheses: ( ) • Eg: C=a+b; • C,a,b=operands • =,+: operators
  • 130. Operators are Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special operators
  • 131. Arithmetic Operators • An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
  • 132. Types of operator Symbolic Representation Arithmetic operator +,-,*,/ and % Relational operator >,<,==,>=,<= and != Logical operator &&,|| and ! Increment and decrement operator ++ and -- Assignment operator = Bitwise operator &,|,^,>>,<< and ~ Comma operator , Conditional Operator ?:
  • 133. CO2. To gain knowledge about the control structures in C Arithmetic operators Operator Meaning + Addition or Unary Plus – Subtraction or Unary Minus * Multiplication / Division % Modulus Operator
  • 134. CO2. To gain knowledge about the control structures in C Integer Arithmetic • Let x = 27 and y = 5 z = x + y  32 z = x – y  22 z = x * y  115 z = x / y  5 z = x % y  2 Q:5 R:2
  • 135. CO2. To gain knowledge about the control structures in C Floating point arithmetic • Let x = 14.0 and y = 4.0 then z = x + y  18.0 z = x – y  10.0 z = x * y  56.0 z = x / y  3.50 The operator % cannot be used with FLOAT operands
  • 136. Mixed mode Arithmetic When one of the operand is real and the other is integer then the result is also real and the operation is called mixed mode arithmetic. Eg: 15/10.0= 1.5
  • 138. Relational Operators • Relational Operators: Compare two quantities and depending on their relation taken certain decision. <, <=, > >=, ==, != are the relational operators.  Relational operators are used in decision making and loops. The expression operand1 relational-operator operand2 takes a value of 1(int) if the relationship is true and 0(int) if relationship is false. Example int a = 25, b = 30, c, d; c = a < b; d = a > b; value of c will be 1 and that of d will be 0.
  • 139. CO2. To gain knowledge about the control structures in C Relational Expressions 6.5 <= 25 TRUE -65 > 0 FALSE 10 < 7 + 5 TRUE
  • 140. CO2. To gain knowledge about the control structures in C Relational Operators Operator Meaning < is less than <= is less than or equal to > is greater than >= is greater than or equal to == is equal to != is not equal to
  • 142. Logical Operators • Logical Operators: Used to test more than one condition and make decisions. Logical operators are commonly used in decision making in C programming. – &&, || and ! are the three logical operators. – expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero. – expr1 || expr2 has a value 1 if expr1 and expr2 both are nonzero. – !expr1 has a value 1 if expr1 is zero else 0. Example if ( marks >= 40 && attendance >= 75 ) grade = ‘P’ If ( marks < 40 || attendance < 75 ) grade = ‘F’ AND 0*0=0 0*1=0 1*0=0 1*1=1 OR 0+0=0 0+1=1 1+0=1 1+1=1
  • 144. CO2. To gain knowledge about the control structures in C Find out the answer a = 5 b = 4 x=10, y = 9 m=6 n = 3 a > b && x = = 10 True / 1 a < m || a < n True / 1 ! (x >= y) False / 0
  • 146. Assignment Operators • Assignment operators: Assigning the result of an expression to a variable. An assignment operator is used for assigning a value to a variable. The most common assignment operator is =. The general form of an assignment operator is v op= exp Where v is a variable and op is a binary arithmetic operator. This statement is equivalent to v = v op (exp) a = a + b can be written as a += b a = a * b can be written as a *= b a = a / b can be written as a /= b a = a - b can be written as a -= b a = a*(n+1) can be written as a*=n+1
  • 147. CO2. To gain knowledge about the control structures in C Assignment Operators Statement with simple assignment operator Statement with shorthand operator a = a + 1 a += 1 a = a – 1 a -= 1 a = a * (n+1) a *= (n+1) a = a / (n+1) a /= (n+1) a = a % b a %= b
  • 148. CO2. To gain knowledge about the control structures in C Assignment Operators Statement with simple assignment operator Statement with shorthand operator a = a + 1 a += 1 a = a – 1 a -= 1 a = a * (n+1) a *= (n+1) a = a / (n+1) a /= (n+1) a = a % b a %= b
  • 150. • Unary Operators Unary operators act on single operands. C language supports three unary operators. They are unary minus, increment, and decrement operators. • Unary Minus (–) Unary minus operator negates the value of its operand. • For example, if a number is positive then it becomes negative when preceded with a unary minus operator. Similarly, if the number is negative, it becomes positive after applying the unary minus operator. • For example, int a, b = 10; a = –(b); The result of this expression is a = –10, because variable b has a positive value. After applying unary minus operator (–) on the operand b, the value becomes –10, which indicates it has a negative value.
  • 151. C Increment and Decrement Operators • C programming has two operators increment ++ and decrement - - to change the value of an operand (constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand. A=10 A+b; binary ++a; unary 11 --a; unary 9
  • 152. Increment and Decrement Operators • Increment and Decrement Operators – The operators ++ and –- are called increment and decrement operators. – Post Increment: a++ – Pre increment: ++a – a++ = a=a+1 – a-- = a=a-1 – Example Let a = 10 a++ =10 ++a= 11 (++b)+b+b = 33 b+(++b)+b = 33 b+b+(++b) = 31 b+b*(++b) = 132 Post decrement: a-- Pre decrement: --a
  • 153. Rules for increment and decrement • Increment and decrement operators are unary operators and thus require variable as their operands. • When postfix ++ or – is used with a variable in an expression , the expression evaluated first using the original value of the variable and then the variable is incremented (or decremented) by one. • When prefix ++ or – is used in an expression , the variable is incremented first then the expression is evaluated using the new value of the variable. • The precedence and associativity of ++ and – operators are the same as those of unary + and unary -.
  • 155. Increment and Decrement Operators m = 5; y = ++m; (prefix) After Execution: y=6, m=6 m = 5; y = m++; (post fix) After Execution: y=5, m=6
  • 156. Conditional Operators • Conditional Operator: The conditional operator contains a condition followed by two statements or values. If the condition is true the first statement is executed , otherwise the second statement. – A ternary operator pair” ? :“ is available in to construct conditional expressions of the following form: condition ? exp1: exp2 where exp1, exp2, exp3 are expressions. Eg: a=10; b=15; x=(a>b) ? a : b; if the condition is true a part will be executed else b part will be executed.
  • 157. C Bitwise Operators • During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power. • Bitwise operators are used in C programming to perform bit-level operations
  • 158. Operators • Bitwise Operator – To manipulate data at bit level, used for testing the bits, or shifting them right to left. – They not be applied to float or double. Operator meaning & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR << Shift left >> Shift right
  • 163. Bitwise XOR Operator Same = > zero Diff => one
  • 170. CO2. To gain knowledge about the control structures in C Special Operators • comma operator int a,b,c; • size of operator sifeof(a);=>2 • pointer operators (& and *) • member selection operators (. and ->). • structure
  • 171. CO2. To gain knowledge about the control structures in C The Comma Operator value = (x = 10, y = 5, x + y); In for loops: for (n=1, m=10, n <=m; n++,m++) In while loops While (c=getchar(), c != ‘1’) Exchanging values t = x, x = y, y = t;
  • 172. Comma Operator • Comma operators are used to link related expressions together. For example: int a, c = 5, d;
  • 173. The sizeof operator • The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).
  • 175. Pointer operators & Returns the address of a variable. &a; returns the actual address of the variable. * Pointer to a variable. *a;
  • 176. dot (.) operator The dot (.) operator is used for direct member selection via object name. In other words, it is used to access the child object. It is used in Structure and Union. Syntax: object.member;
  • 177. • An Arrow operator in C allows to access elements in Structures and Unions. It is used with a pointer variable pointing to a structure or union. • The arrow operator is formed by using a minus sign, followed by the greater than symbol as shown below. Syntax: (pointer_name)->(variable_name) Operation: The -> operator in C gives the value held by variable_name to structure or union variable pointer_name.
  • 178. Difference between Dot(.) and Arrow(->) operator The Dot(.) operator is used to normally access members of a structure or union. The Arrow(->) operator exists to access the members of the structure or the unions using pointers.