Programming with C
Chapter 2
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;
}
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

C programming

  • 1.
  • 3.
    Example - cprogram 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) variablein 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) variablein 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 pointvariable 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 BasicAthematic 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 • Parenthesishave 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 • ExecutableC 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 & RelationalOperators 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 checkodd 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; }
  • 17.
  • 18.
    Selection statements • Cprovides 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 Thisselection 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’sgrade 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 switchstatement 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 repetitionstatement • 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 is1 Value is 2 Value is 3