SlideShare a Scribd company logo
Programming with C
Chapter 2
C programming
Example - c program to take input
from user using scanf
#include <stdio.h>
main()
{
int number;
printf("Enter an integern");
scanf("%d",&number);
printf("Integer entered by you is %dn", number);
return 0;
}
Character (char) variable in C
• #include <stdio.h>
• int main()
{
• char c; // char variable declaration
• c = 'A'; // defining a char variable
printf("value of c is %c", c);
• return 0;
}
Integer (int) variable in C
#include <stdio.h>
int main()
{
int i; // integer variable declaration
i = 123; // defining integer variable
printf("value of i is %d", i);
return 0;
}
Rules of Precedence
• Expressions contained operators +, -, *, / & % follow
rules of precedence (priority) to perform calculation.
• Multiplication, division and remainder operators are
applied first, while, addition and subtraction are
evaluated next.
• *, /, % are said to be on same level of precedence.
• Operator in expressions contained within pairs of
parenthesis are evaluated first.
• Evaluation proceed from left to right
Floating point (float) variable in C
#include <stdio.h>
int main()
{
float f; // floating point variable declaration
f = 12.001234; // defining float variable
printf("value of f is %f", f);
return 0;
}
Double precision(double) floating
point variable in C
#include <stdio.h>
int main()
{
double d; // double precision variable declaration
d = 12.001234; // defining double precision variable
printf("value of d is %e", d);
return 0;
}
Simple Calculator performing
Basic Athematic Operations
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integersn");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %dn",add);
printf("Difference = %dn",subtract);
printf("Multiplication = %dn",multiply);
printf("Division = %.2fn",divide);
return 0;
}
Operator Precedence
• Parenthesis have highest level of precedence.
• ( ( a + b ) + c )
Operator Operations Order of Evaluation (precedence)
( ) Parenthesis Evaluated first. If nested then inner most
evaluated first.
* Multiplication
Evaluated second. If several they are
evaluated form left to right
/ Division
% Remainder
+ Addition Evaluated last. If there are several, they
are evaluated left to right
- Subtraction Evaluated last. If there are several, they
are evaluated left to right
Sample Algebra & C Expressions
Algebra C Programming
m = a + b + c + d + e
5
m = (a + b + c + d + e ) / 5; end with a ; colon
Y = mx + b Y = m * x + b;
❸ ❷ ❷ Precedence
Z = pr%q + w/x - y Z = p * r % q + w / x – y;
❻ ❶ ❷ ❹ ❸ ❺ Precedence
Order precedence?
Y = a * x * x + b * x + c; Nested operators follow L-R Rule
Solve as per precedence?
Y = 2 * 5 * 5 + 3 * 5 + 7;
Decision Making
• Executable C statements either perform actions such as
calculations or make decisions.
• For example: Exam Grade is greater than or equal to 60
Marks, if statements is correct, program will print “Pass”
otherwise “Fail”.
• IF ( Marks = 60 ) then Pass Else Fail
• IF (Marks => 60 ) then Pass Else Fail
• If condition is met means condition is true, the statement is
executed
• If the condition is not met, the body statement is not
executed
Equality Operators & Relational
Operators
• Conditions in if statement are formed by using the equality
operators and relational operators.
• The relational operators have the same level of precedence
and they associate left to right
• The equality operators have a lower level of precedence than
the relational operators.
Equality & Relational Operators
Algebraic equality or
relational operator
C equality or
relational
operator
Example of
C
condition
Meaning of C condition
Equality operators
= == X == y x is equal to y
≠ != X != y x is not equal to y
Relational operators
> > x > y X is greater than y
< < X < y X is less than y
≥ >= X >= y X is greater than or equal to
y
≤ <= X <= y X is less than or equal to y
Program to check odd or even using modulus operator
#include <stdio.h>
int main()
{
int n;
printf("Enter an integern");
scanf("%d", &n);
if (n%2 == 0)
printf("Evenn");
else
printf("Oddn");
return 0;
}
C programming
Results
Selection statements
• C provides three types of selection structures
in the form of statements.
1) If statement
The if selection statement either performs
(selects) an action if a condition is true
• Or skips the action if the condition is false.
• Also called a single-selection statement
• if (grade >= 60)
• {
• printf(“Passedn”);
• } //end if
2) If…else statement
This selection statement performs an action if a
condition is true and performs a different action
if the condition is false (called else).
Also called a double-selection statement,
because it selects among many different actions.
• If student’s grade is greater than or equal to 60
• Print “passed”
• Else
• Print “failed”
Example:
if (grade >= 60)
{
printf(“Passedn”);
} //end if
else {
printf(“Failedn”); } // end else
• #include <stdio.h>
•
• main()
• {
• int x = 1;
•
• if ( x == 1 )
• printf("x is equal to one.n");
• else
• printf("For comparison use == as = is the assignment operator.n");
•
• return 0;
• }
3) The switch statement
The switch statement performs one of many
different actions depending on the value of an
expression.
Also called a multi-selection statement; because
it selects among many different actions.
Loop or repetition statement
• While loop
• While repetition statement specifies that an action is to be
repeated while a condition is true.
• When condition will become false.
• The repetition terminates.
• The first statement after the repetition statement executes.
• #include <stdio.h>
•
• main()
• {
• int value = 1;
•
• while(value<=3)
• {
• printf("Value is %dn", value);
• value++;
• }
•
• return 0;
• }
• Output:
Value is 1
Value is 2
Value is 3

More Related Content

PPTX
Basic c operators
PPT
6 operators-in-c
PPT
Types of c operators ppt
PPT
C operator and expression
PPT
Types of operators in C
PPTX
Expression and Operartor In C Programming
PDF
Operators in c programming
PPT
Operators in c language
Basic c operators
6 operators-in-c
Types of c operators ppt
C operator and expression
Types of operators in C
Expression and Operartor In C Programming
Operators in c programming
Operators in c language

What's hot (19)

PPTX
Control statements in c
PPTX
Logical and Conditional Operator In C language
PPTX
Operator in c programming
PPTX
Decision Control Structure If & Else
PPTX
Control Flow Statements
PPTX
C OPERATOR
PDF
Unit II chapter 4 Loops in C
PPTX
PPTX
Operators and expressions in c language
PPSX
Bsit1
PDF
Conditional operators
 
PPTX
C language operators
PPTX
Operator of C language
PPTX
operators and control statements in c language
PPT
Control structure
PPT
Basic c operators
PDF
Java basic operators
PPTX
Control structure of c
Control statements in c
Logical and Conditional Operator In C language
Operator in c programming
Decision Control Structure If & Else
Control Flow Statements
C OPERATOR
Unit II chapter 4 Loops in C
Operators and expressions in c language
Bsit1
Conditional operators
 
C language operators
Operator of C language
operators and control statements in c language
Control structure
Basic c operators
Java basic operators
Control structure of c
Ad

Similar to C programming (20)

PPTX
the refernce of programming C notes ppt.pptx
DOC
2. operator
PPTX
Programming in C
PPT
c-programming
PDF
learn basic to advance C Programming Notes
PPTX
Decision Making and Branching
PPTX
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
DOC
C fundamental
PPTX
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
PPT
Unit i intro-operators
PPTX
C Programming with oops Concept and Pointer
PDF
C language concept with code apna college.pdf
PPT
intro to c
PPT
Control statments in c
PDF
Programming for Problem Solving
DOCX
Programming Fundamentals lecture 7
PDF
Programming for Problem Solving
DOCX
C Programming
DOCX
C programming Lab 2
PPT
C_Language_PS&PC_Notes.ppt
the refernce of programming C notes ppt.pptx
2. operator
Programming in C
c-programming
learn basic to advance C Programming Notes
Decision Making and Branching
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
C fundamental
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
Unit i intro-operators
C Programming with oops Concept and Pointer
C language concept with code apna college.pdf
intro to c
Control statments in c
Programming for Problem Solving
Programming Fundamentals lecture 7
Programming for Problem Solving
C Programming
C programming Lab 2
C_Language_PS&PC_Notes.ppt
Ad

More from Xad Kuain (8)

PDF
Avl trees
PDF
avl insertion-rotation
PDF
History evaluation
PPT
Computer and programming language
PDF
How to build a lamp server-sample
PDF
Oracle 11g Database Administration
PDF
Computer Graphics & linear Algebra
PPTX
Quality assurance by Sadquain
Avl trees
avl insertion-rotation
History evaluation
Computer and programming language
How to build a lamp server-sample
Oracle 11g Database Administration
Computer Graphics & linear Algebra
Quality assurance by Sadquain

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PDF
Digital Strategies for Manufacturing Companies
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
AI in Product Development-omnex systems
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Essential Infomation Tech presentation.pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Transform Your Business with a Software ERP System
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
System and Network Administration Chapter 2
PPTX
L1 - Introduction to python Backend.pptx
Nekopoi APK 2025 free lastest update
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PTS Company Brochure 2025 (1).pdf.......
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Navsoft: AI-Powered Business Solutions & Custom Software Development
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Odoo POS Development Services by CandidRoot Solutions
AI in Product Development-omnex systems
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Essential Infomation Tech presentation.pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Transform Your Business with a Software ERP System
2025 Textile ERP Trends: SAP, Odoo & Oracle
Operating system designcfffgfgggggggvggggggggg
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
System and Network Administration Chapter 2
L1 - Introduction to python Backend.pptx

C programming

  • 3. Example - c program to take input from user using scanf #include <stdio.h> main() { int number; printf("Enter an integern"); scanf("%d",&number); printf("Integer entered by you is %dn", number); return 0; }
  • 4. Character (char) variable in C • #include <stdio.h> • int main() { • char c; // char variable declaration • c = 'A'; // defining a char variable printf("value of c is %c", c); • return 0; }
  • 5. Integer (int) variable in C #include <stdio.h> int main() { int i; // integer variable declaration i = 123; // defining integer variable printf("value of i is %d", i); return 0; }
  • 6. Rules of Precedence • Expressions contained operators +, -, *, / & % follow rules of precedence (priority) to perform calculation. • Multiplication, division and remainder operators are applied first, while, addition and subtraction are evaluated next. • *, /, % are said to be on same level of precedence. • Operator in expressions contained within pairs of parenthesis are evaluated first. • Evaluation proceed from left to right
  • 7. Floating point (float) variable in C #include <stdio.h> int main() { float f; // floating point variable declaration f = 12.001234; // defining float variable printf("value of f is %f", f); return 0; }
  • 8. Double precision(double) floating point variable in C #include <stdio.h> int main() { double d; // double precision variable declaration d = 12.001234; // defining double precision variable printf("value of d is %e", d); return 0; }
  • 9. Simple Calculator performing Basic Athematic Operations #include <stdio.h> int main() { int first, second, add, subtract, multiply; float divide; printf("Enter two integersn"); scanf("%d%d", &first, &second); add = first + second; subtract = first - second; multiply = first * second; divide = first / (float)second; //typecasting printf("Sum = %dn",add); printf("Difference = %dn",subtract); printf("Multiplication = %dn",multiply); printf("Division = %.2fn",divide); return 0; }
  • 10. Operator Precedence • Parenthesis have highest level of precedence. • ( ( a + b ) + c ) Operator Operations Order of Evaluation (precedence) ( ) Parenthesis Evaluated first. If nested then inner most evaluated first. * Multiplication Evaluated second. If several they are evaluated form left to right / Division % Remainder + Addition Evaluated last. If there are several, they are evaluated left to right - Subtraction Evaluated last. If there are several, they are evaluated left to right
  • 11. Sample Algebra & C Expressions Algebra C Programming m = a + b + c + d + e 5 m = (a + b + c + d + e ) / 5; end with a ; colon Y = mx + b Y = m * x + b; ❸ ❷ ❷ Precedence Z = pr%q + w/x - y Z = p * r % q + w / x – y; ❻ ❶ ❷ ❹ ❸ ❺ Precedence Order precedence? Y = a * x * x + b * x + c; Nested operators follow L-R Rule Solve as per precedence? Y = 2 * 5 * 5 + 3 * 5 + 7;
  • 12. Decision Making • Executable C statements either perform actions such as calculations or make decisions. • For example: Exam Grade is greater than or equal to 60 Marks, if statements is correct, program will print “Pass” otherwise “Fail”. • IF ( Marks = 60 ) then Pass Else Fail • IF (Marks => 60 ) then Pass Else Fail • If condition is met means condition is true, the statement is executed • If the condition is not met, the body statement is not executed
  • 13. Equality Operators & Relational Operators • Conditions in if statement are formed by using the equality operators and relational operators. • The relational operators have the same level of precedence and they associate left to right • The equality operators have a lower level of precedence than the relational operators.
  • 14. Equality & Relational Operators Algebraic equality or relational operator C equality or relational operator Example of C condition Meaning of C condition Equality operators = == X == y x is equal to y ≠ != X != y x is not equal to y Relational operators > > x > y X is greater than y < < X < y X is less than y ≥ >= X >= y X is greater than or equal to y ≤ <= X <= y X is less than or equal to y
  • 15. Program to check odd or even using modulus operator #include <stdio.h> int main() { int n; printf("Enter an integern"); scanf("%d", &n); if (n%2 == 0) printf("Evenn"); else printf("Oddn"); return 0; }
  • 18. Selection statements • C provides three types of selection structures in the form of statements. 1) If statement The if selection statement either performs (selects) an action if a condition is true • Or skips the action if the condition is false. • Also called a single-selection statement
  • 19. • if (grade >= 60) • { • printf(“Passedn”); • } //end if
  • 20. 2) If…else statement This selection statement performs an action if a condition is true and performs a different action if the condition is false (called else). Also called a double-selection statement, because it selects among many different actions.
  • 21. • If student’s grade is greater than or equal to 60 • Print “passed” • Else • Print “failed” Example: if (grade >= 60) { printf(“Passedn”); } //end if else { printf(“Failedn”); } // end else
  • 22. • #include <stdio.h> • • main() • { • int x = 1; • • if ( x == 1 ) • printf("x is equal to one.n"); • else • printf("For comparison use == as = is the assignment operator.n"); • • return 0; • }
  • 23. 3) The switch statement The switch statement performs one of many different actions depending on the value of an expression. Also called a multi-selection statement; because it selects among many different actions.
  • 24. Loop or repetition statement • While loop • While repetition statement specifies that an action is to be repeated while a condition is true. • When condition will become false. • The repetition terminates. • The first statement after the repetition statement executes.
  • 25. • #include <stdio.h> • • main() • { • int value = 1; • • while(value<=3) • { • printf("Value is %dn", value); • value++; • } • • return 0; • }
  • 26. • Output: Value is 1 Value is 2 Value is 3