Académique Documents
Professionnel Documents
Culture Documents
Last Lecture
Introduction to Computer
Representation of Data Programming Language
C Programming Language
Basic Structure
Simple C Program
Todays Lecture
Programming Attributes
Structure of C program C Character Set
Variables & Constants
Arithmetic Operators Relational Operators Logical Operators Assignment Operators Increment and Decrement Operators
int main(void) {
return(0);
}
Simple C Program
Standard Header File #include<stdio.h> #define KMS_PER_MILE 1.609 Preprocessor directive
constant
Reserved Words
int main (void) { float miles, kms; /* Read distance in miles */ printf( Enter distance in miles \n); scanf (%f, &miles); /*Convert the distance to kms*/ kms = KMS_PER_MILES * miles;
Comments
Standard Identifier
printf( Distance in kms = \t %f ,kms ) ; // Display the resultant distance in Kms return(0); }
C Character Set
Uppercase letters A to Z , lower case letters a to
Escape sequences
Its a name associated with a memory cell whose value can be changed. All variables must be declared before they can appear in executable statements It consists of a data type, followed by one or more variables. E.g. double miles, kms;
Variables
Invalid
5thplayer x Order-no double
count value
Remark
First char is number First char is not letter - is not allowed Double is a reserved word Blank is not permitted
Valid
player5 x Order_no doubletype
Count_value
Its a name associated with a memory cell whose value can not be changed. Declared by #define directive. E.g. #define KMS_PER_MILE 1.609
const
Data Types
It is a set of values and a set of operations on
those values.
Primary Data types Derived Data types (discussed later) User defined data types (discussed later)
double
Double precision
8 bytes
Assignment Statement
It stores a values or a computation result
in a variable It is used to perform arithmetic operations in a program e.g. kms = KMS_PER_MILES * miles;
Assignment operator
Multiply operator
printf() Function
Escape sequence function arguments placeholder
Function name
scanf() Function
Addressof operator
Function name
scanf ( %d
Format String
Variables
Sample Program
#include<stdio.h> /* program to display use of I/O and data types */ int main() Input { int marks; Enter your marks: float average; 70 Enter average marks: char grade; 50.5 printf(Enter your marks:\n); Enter your grade: scanf(%d, &marks); printf(Enter average marks:\n); A scanf(%f, &average); Output printf(Enter your grade:\n); Marks = 70 scanf(%c, &grade); Average = 50.5 printf(Marks = %d\n,marks); printf(Average = %f\n,average); Grade = A printf(Grade = %c\n,grade); return (0); }
Arithmetic Operators
Operator + * / Meaning Addition or unary plus Subtraction or unary minus Multiplication Division
Modulo Division
Arithmetic Expression
Let int x = 15; int y = 6;int z;
Example:
z z z z z
= = = = =
x+y x-y x*y x/y (decimal part truncated) x%y (remainder of the division)
Example 2 :Program to compute area and circumference of a circle #include<stdio.h> #define PI 3.14 int main(void) { int radius; float area,circum; printf("Enter radius of a circle\n"); scanf("%d", &radius); area = PI * radius * radius; circum = 2 * PI * radius; printf("Area = %f \t Circumference = %f ",area, circum); return(0); }
Precedence
1 2 3
Associativity
Left to Right Left to Right Left to Right
+, -
Left to Right
Assignment Operators
Simple Assignment
Example
Let
1.
int x, a, b, c;
2.
3.
Type Conversion
Implicit Type Conversion
Explicit Type Conversion
intermediate values to the proper data type Lower type is automatically converted to the higher type before operation proceeds
i * f
d;
(type-name) expression int female_no, male_no; float ratio; ratio = (float) female_number / male_number;
E.g
type of variable on the left of the assignment = before assigning result to it.
float to int truncation of fractional part. double to float rounding of digits long int to int dropping of excess higher order
bits
More Examples:
Write a equivalent C expression for
Relational Operators
Operator < <= > Is less than Is less than or equal to Is greater than Meaning
>=
== !=
Examples:
5 <= 7
TRUE 5 < -8 FALSE 11 < 8+4 TRUE x+y == a+b TRUE if value of x+y is equal to value of a+b -20 >= 0 FALSE
Logical Operators
Operator && || ! Meaning Logical AND Logical OR Logical NOT
Q. Are !(a>b) and a<=b same?? Q. Are !(a==b) and a!=b same??
Exercise
1. Write a program which takes marks of five
students as input and computes the average marks. Print the average mark of five students.
2. Write a program to compute roots of a quadratic
equation ax2 + bx + c = 0
Exercise
3 . Write a program to solve two linear equations 2x + 8y 10 = 0 5x + 6y 5 = 0
++i is equivalent to: i = i+1 --i is equivalent to: i = i-1 Is ++i and i++ are same??
In prefix, the variable is incremented (or decremented) first and then the expression is evaluated using the new value of the variable. Ex. m = --n; or m = ++n;
i = 10 j = 11
i=--j;
What is the values of i and j?
Conditional Operator
Also called Ternary operator
Syntax
Variable = expression1 ? expression2
: expression3
Example: a=5;
Output X = 10
b=10;
x =(a>b) ? a : b; What will be the value of x??
Bitwise Operators
Operator & | Meaning Bitwise AND Bitwise OR
^
<< >>
Bitwise exclusive OR
Shift left Shift right
Special Operators
The Sizeof Operator
Examples:
Comma Operator
Used to link the related expressions
together. Evaluation starts from left to right. And the value of the right most expression is the value of the combined expression. Example:
1.
Operators
Associativity
Rank
(),[] +, -, ++, --, !, ~, *, &, sizeof, (type) *, /, % Binary plus(+) and Binary minus (-) <<, >> <, <=, >, >= ==, != & ^ | && || ?: =, *=, /=, %=, +=, -=, &=, ^= ,
L to R R to L L to R L to R L to R L to R L to R L to R L to R L to R L to R L to R R to L R to L L to R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Algorithms
An Algorithm is just a detailed sequence of simple steps that are needed to solve a problem
Flow Charts
Flow Charts are maps or graphical representations of a process. Steps in a process are shown with symbolic shapes, and the flow of the process is indicated with arrows connecting the symbols
Symbols in Flowchart
START , END
INPUT
DECISION
CONNECTOR CONNECTOR
Marks = 50 Avg = 45
Flow chart to find whether the the marks of a student is above avg or below avg
END