Professional Documents
Culture Documents
Slides 2
Slides 2
#include <stdio.h>
/* My first C program */
main() {
printf("Hello World!\n");
}
First C program
#include <stdio.h>
/* My first C program */
main() {
printf("Hello World!\n");
}
#include <stdio.h>
/* My first C program */
main() {
printf("Hello World!\n");
}
First C program
#include <stdio.h>
/* My first C program */
main() {
printf("Hello World!\n");
}
#include <stdio.h>
/* My first C program */
main() {
printf("Hello World!\n");
}
#include <stdio.h>
/* sum 2 integers */
main() {
int x;
int y;
int z;
z = x+y;
printf("%d\n", z);
}
Sum of 2 numbers
#include <stdio.h>
z = x+y;
printf("%d\n", z);
}
Sum of 2 numbers
#include <stdio.h>
z = x+y;
printf("%d\n", z);
}
Sum of 2 numbers
#include <stdio.h>
#include <stdio.h>
#include <stdio.h>
/* sum 2 integers */
• int : defines that x, y , z are
main() { of type integers.
int x = 98; • z = x+y : evaluates x+y
int y = 99; and stores it in z.
int z; • What will be output if we
print z?
z = x+y;
printf("%d\n", z);
}
Sum of 2 numbers – with initialization
#include <stdio.h>
/* sum 2 integers */
• int : defines that x, y , z are
main() { of type integers.
int x = 98; • z = x+y : evaluates x+y
int y = 99; and stores it in z.
int z; • What will be output if we
print z?
z = x+y;
printf("%d\n", z);
}
Weighted sum of 2 numbers
#include <stdio.h>
main() {
int mathMarks = 98;
int phyMarks = 99;
int total;
#include <stdio.h>
main() {
int mathMarks = 98;
int phyMarks = 99;
int total;
#include <stdio.h>
main() {
int mathMarks = 98;
int phyMarks = 99;
int total;
#include <stdio.h>
main() {
int mathMarks = 98;
int phyMarks = 99;
float total; /* float variable */
#include <stdio.h>
main() {
int mathMarks = 98;
int phyMarks = 99;
float total; /* float variable */
#include <stdio.h>
main() {
int mathMarks = 98;
int phyMarks = 99;
float total; /* float variable */
#include <stdio.h>
main() {
int mathMarks = 98;
int phyMarks = 99;
float total;
#include <stdio.h>
main() {
int mathMarks = 98;
int phyMarks = 99;
float total;
#include <stdio.h>
main() {
int mathMarks = 98;
int phyMarks = 99;
float total;
#include<stdio.h>
main() {
int x, y;
printf("Enter x:");
scanf("%d", &x);
printf("Enter y:");
scanf("%d", &y);
• Arithmetic operators: +, -, *, /
Basic operators in C
• Arithmetic operators: +, -, *, /
• Modulus operator: %
x % y : remainder when x is divided by y .
Basic operators in C
• Arithmetic operators: +, -, *, /
• Modulus operator: %
x % y : remainder when x is divided by y .
• Assignment operator: =
Basic operators in C
• Arithmetic operators: +, -, *, /
• Modulus operator: %
x % y : remainder when x is divided by y .
• Assignment operator: =
Operator precedence:
• first: parenthesized sub-expression; inner-most to outer-most.
• second: *, /, % ; left to right.
• third: +, - ; left to right.
Basic operators in C
• Arithmetic operators: +, -, *, /
• Modulus operator: %
x % y : remainder when x is divided by y .
• Assignment operator: =
Operator precedence:
• first: parenthesized sub-expression; inner-most to outer-most.
• second: *, /, % ; left to right.
• third: +, - ; left to right.
• total = 30 / 100 * mathMarks + 70 / 100 * phyMarks;
Basic operators in C
• Arithmetic operators: +, -, *, /
• Modulus operator: %
x % y : remainder when x is divided by y .
• Assignment operator: =
Operator precedence:
• first: parenthesized sub-expression; inner-most to outer-most.
• second: *, /, % ; left to right.
• third: +, - ; left to right.
• total = 30 / 100 * mathMarks + 70 / 100 * phyMarks;
total = ((30 / 100) * mathMarks) + ((70 / 100) * phyMarks);
Basic operators in C
• Arithmetic operators: +, -, *, /
• Modulus operator: %
x % y : remainder when x is divided by y .
• Assignment operator: =
Operator precedence:
• first: parenthesized sub-expression; inner-most to outer-most.
• second: *, /, % ; left to right.
• third: +, - ; left to right.
• total = 30 / 100 * mathMarks + 70 / 100 * phyMarks;
total = ((30 / 100) * mathMarks) + ((70 / 100) * phyMarks);
• z=a+b*c*d%e-f/g
Basic operators in C
• Arithmetic operators: +, -, *, /
• Modulus operator: %
x % y : remainder when x is divided by y .
• Assignment operator: =
Operator precedence:
• first: parenthesized sub-expression; inner-most to outer-most.
• second: *, /, % ; left to right.
• third: +, - ; left to right.
• total = 30 / 100 * mathMarks + 70 / 100 * phyMarks;
total = ((30 / 100) * mathMarks) + ((70 / 100) * phyMarks);
• z=a+b*c*d%e-f/g
z = a + (((b * c) * d) % e) - (f / g)
Increment / decrement operators
• ++, –
• prefix and post-fix only to a variable.
Increment / decrement operators
• ++, –
• prefix and post-fix only to a variable.
#include<stdio.h>
main() {
int x, y;
int n = 10;
x = n++;
y = ++n;
printf(" x = %d, y = %d\n", x, y);
}
Assignment operator =
Write a program that reads an integer from the input and prints 0
if the integer is even and 1 if the integer is odd.
main() {
printf ("%d\n", ’A’);
}
char
main() {
printf ("%d\n", ’A’);
}
• Output a character: printf(”%c”, x);
• Input a character: scanf(”%c”, &x);
unsigned int
Sign Magn.
0 0 0 0
0 0 1 +1
0 1 0 +2
0 1 1 +3
1 0 0 0
1 0 1 -1
1 1 0 -2
1 1 1 -3
Representing negative numbers
Sign Magnitude notation
• Use one bit for sign, others for magnitude of the number.
Sign Magn.
0 0 0 0
0 0 1 +1
0 1 0 +2
0 1 1 +3
1 0 0 0
1 0 1 -1
1 1 0 -2
1 1 1 -3
• 2 + (−3)
Representing negative numbers
• 2 + (−3)
• 3 + (−2)
float
Fixed point
• Position of radix point is fixed and is same for all numbers.
• Lets say we have 3 digits after radix point.
Fixed point vs floating point representation
Fixed point
• Position of radix point is fixed and is same for all numbers.
• Lets say we have 3 digits after radix point.
• (0.120 × 0.120)10 = (0.014)10
• A digit is lost.
Floating point
Fixed point vs floating point representation
Fixed point
• Position of radix point is fixed and is same for all numbers.
• Lets say we have 3 digits after radix point.
• (0.120 × 0.120)10 = (0.014)10
• A digit is lost.
Floating point
• 1.20 × (10)−1 × 1.20 × (10)−1 = 1.44 × (10)−2
• Wider range of numbers can be represented.
• IEEE standard: 32 bits are split as follows:
• First bit for sign.
• Next 8 bits for exponent.
• Next 23 bits for mantissa.
Fixed point vs floating point representation
Fixed point
• Position of radix point is fixed and is same for all numbers.
• Lets say we have 3 digits after radix point.
• (0.120 × 0.120)10 = (0.014)10
• A digit is lost.
Floating point
• 1.20 × (10)−1 × 1.20 × (10)−1 = 1.44 × (10)−2
• Wider range of numbers can be represented.
• IEEE standard: 32 bits are split as follows:
• First bit for sign.
• Next 8 bits for exponent.
• Next 23 bits for mantissa.
• (−39.9)10 = (−100111.11100)2 = (−1.0011111100)2 × 25 .
Output floats in C
#include<stdio.h>
main() {
float x = 2.00123;
printf ("x = %5.4f\n", x);
printf ("x = %8.7f\n", x);
}
Circumference of circle
#include<stdio.h>
main() {
float radius;
float circum;
#include<stdio.h>
main() {
float radius;
float circum;
#include<stdio.h>
main() {
float radius;
float circum;
printf(“%w.pC”, x);
Formatted output
printf(“%w.pC”, x);
printf(“%w.pC”, x);
ax 2 + bx + c
Square-roots of a Quadratic Equation
ax 2 + bx + c
• Get inputs a, b, c.
• Compute the discriminant.
• Compute the 2 roots.
• Output the 2 roots. (Assume real roots).