the c programming lecture 24. summary of previous lecture programming in real life. introduction ...
TRANSCRIPT
![Page 1: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/1.jpg)
The C Programming
Lecture 24
![Page 2: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/2.jpg)
Summary of Previous LectureProgramming in real life.
Introduction
What is Problem Solving? Problem Solving process.
Algorithm History Working Definition Examples
Algorithms to Programs
![Page 3: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/3.jpg)
Summary of Previous LectureComponents of Algorithms
Variables and values Instructions Sequences Procedures Selections Repetitions Documentation
Software Development ProcessTop Down Algorithm Design
![Page 4: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/4.jpg)
Today’s Lecture Algorithms and Programs
A C programming languageHistory of CC, A High level language
How to get started with C.Basic Structure of a C programData Storage and Data TypesVariables, Keywords, identifiers, Assignment
![Page 5: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/5.jpg)
Today’s Lectureconstant variable
printf() and scanf() functions and usage Precedence
int and float Unary operations Increment and decrement operations
![Page 6: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/6.jpg)
Today’s Lecture Comments Error and its Types Summary
![Page 7: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/7.jpg)
From Algorithms to Programs
Both are sets of instructions on how to do a task Algorithm:
talking to humans, easy to understand in plain (English) language
Program: talking to computer (compiler) can be regarded as a “formal expression” of an
algorithm
![Page 8: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/8.jpg)
A C Programming Language Flexible language:
Structured language Low level activities possible
It can produce lean and efficient code Wide availability on a variety of computers Widely used!
![Page 9: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/9.jpg)
History of C CPL Combined Programming Language (Barron et al., 1963)
BCPL Basic CPL (Richards, 1969)
B (Thompson, 1970)
C K&R C (Ritchie, 1972)
ANSI C American National Standards Institute C (X3J11, 1989)
C99 (JTC1/SC22/WG14, ISO/IEC 9899, 1999)
![Page 10: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/10.jpg)
A High-Level Language
Compilers and linkers translate a high level program into executable machine code.
#include <stdio.h>
int main(){ printf(“Hello World”);
return 0;}
Source code Executable code
10100110 0111011000100110 0000000011111010 1111101001001110 1010011011100110 1001011011001110 0010111010100110 0100111011111010 0110011001001110 10000110
etc...
![Page 11: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/11.jpg)
How to get started? Download Turbo C++ Version 3.0 a free
software.http://turbo-c.soft32.com/ Install it!
Follow the step by step guide
for your first program!
![Page 12: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/12.jpg)
Turbo C++ IDE Version 3
Click New to open a program window
![Page 13: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/13.jpg)
Open a new window for writing a program
Output Message window
![Page 14: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/14.jpg)
Set the Directories in Option Menu
![Page 15: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/15.jpg)
Set the output directory path
![Page 16: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/16.jpg)
Write your first program here
Write your program here!
![Page 17: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/17.jpg)
Compile to find errors
Compile to check errors
![Page 18: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/18.jpg)
No Errors Found
![Page 19: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/19.jpg)
Execute the program by Run option
RUN to execute
![Page 20: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/20.jpg)
See output by pressing Ctrl+F5
Output of your first program
![Page 21: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/21.jpg)
Basic Structure of a C Program
#include <stdio.h> int main(){
printf(“Hello World”);
return 0;}
C Program: Pre Processor Directive include <stdio.h> file in this program! stdio.h
contains declaration of printf used in the
program
Example: Hello World
![Page 22: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/22.jpg)
Basic Structure of a C Program
#include <stdio.h> int main(){
printf(“Hello World”);
return 0;}
C Program:
Program control is started from the main
function.
Example: Hello World
![Page 23: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/23.jpg)
Main Function = Main Gate
You can enter the premises of the building through main gate! You can enter the premises of the building through main gate! Similarly program control is entered through main function..!Similarly program control is entered through main function..!
![Page 24: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/24.jpg)
Basic Structure of a C Program
#include <stdio.h> int main(){
printf(“Hello World”);
return 0;}
C Program:
int indicates that only an integer value can come
out of this function
Example: Hello World
![Page 25: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/25.jpg)
Basic Structure of a C Program
#include <stdio.h> int main(){
printf(“Hello World”);
return 0;}
C Program:
Curly braces mark the beginning and end of a
block of instructions.
Example: Hello World
![Page 26: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/26.jpg)
Basic Structure of a C Program
#include <stdio.h> int main(){
printf(“Hello World”);
return 0;}
C Program:
Instruction (function call) to output “Hello World”.
This will print Hello World on the output screen
Example: Hello World
![Page 27: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/27.jpg)
Basic Structure of a C Program
#include <stdio.h> int main(){
printf(“Hello World”);
return 0;}
C Program:
“Statements” (lines of instructions) always end with a semi-colon (;)
Example: Hello World
![Page 28: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/28.jpg)
Where to store data? A data type is a representation of data that
defines a size and valid range for data. Built-in types: char, int, float Type modifiers: long, short, const User-defined types (arrays and records)
What about “strings”?Strings are arrays of char (discussed later)
![Page 29: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/29.jpg)
Character Representation
Characters are stored as a small integer Each character has a unique integer
equivalent specified by its position in the ASCII table (pronounced “as-key”) American Standard Code for Information
Interchange
![Page 30: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/30.jpg)
![Page 31: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/31.jpg)
Character Representation
The ASCII values range from 0 to 127value 0: special character ’\0’
(a.k.a. NUL character)value 127: special character <DEL>other special characters:
’\n’ ’\t’ ’\’’ ’\\’ etc.various “extended” sets from 128 to 255
![Page 32: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/32.jpg)
Remember: Variables
This jarcan contain
10 cookies
50 grams of sugar
3 slices of cake
etc.
ValuesVariable
• Are containers for values – places to store values
• Example:
![Page 33: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/33.jpg)
Variable
Is a logical name for a container (an actual piece of computer memory for
values) Has a type associated with it
tells the computer how to interpret the bits Must be declared before use:
int i; float result;
int i=0; char initial=’K’;
![Page 34: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/34.jpg)
Variable Declaration: Examples
int myID;
myID
Variable
![Page 35: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/35.jpg)
Variable Declaration: Examples
int myID;
char myInitial = ’J’;
Single “forward quotes” or apostrophe (’) rather than
“back quotes” (‘)
![Page 36: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/36.jpg)
Variable Declaration: Examples
int myID;
char myInitial = ’J’; 01001010myInitial
![Page 37: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/37.jpg)
Variable Declaration: Examples
int myID;
char myInitial = ’J’;
char myInitial = 74 ;
01001010myInitial
![Page 38: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/38.jpg)
Variable Declaration: Examples
float commission = 0.05;
short int myHeight = 183; /* cm */
long int mySalary = 100000000000000000000;
long float chanceOfADate = 3e-500;
double chanceOfA2ndDate = 1.5e-500;
![Page 39: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/39.jpg)
float commission = 0.05;
short int myHeight = 183; /* cm */
long int mySalary = 100000000000000000000;
long float chanceOfADate = 3e-500;
double chance_of_a_2nd_date = 1.5e-500;
Variable Declaration: Examples
“Keywords”
![Page 40: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/40.jpg)
Keyword
...has a special meaning in C ...is “case-sensitive” ...cannot be used as variable names Examples:
int, char, long, main, float, double, const, while, for, if, else, return, break, case, switch, default, typedef, struct, etc.
![Page 41: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/41.jpg)
Variable Declaration: Examples
float commission = 0.05;
short int myHeight = 183; /* cm */
long int mySalary = 100000000000000000000;
long float chanceOfADate = 3e-500;
double chanceOfA2ndDate = 1.5e-500;
“Identifiers”
![Page 42: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/42.jpg)
Identifier ...is a series of characters consisting of
letters, digits and underscores ( _) ...cannot begin with a digit ...must not be a keyword ...is “case-sensitive” Examples:
sUmoFA, x1, y2, _my_ID_, Main (careful!)
![Page 43: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/43.jpg)
Assignment Puts a specified value into a specified
variable Assignment operator: =
<variable name> = <expression> ;
not to be confused with ==
![Page 44: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/44.jpg)
Assignment: Examplesfloat x = 2.5 ;
char ch ;
int number ;
ch = ’\n’ ;
number = 4 + 5 ;
/* current value of number is 9. */
number = number * 2;
/* current value of number is now 18. */
![Page 45: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/45.jpg)
Assignment Value must have a type assignable to the
variable Value may be automatically converted to
fit the new container Example:
various.c
![Page 46: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/46.jpg)
#include <stdio.h>
/* Do various assignment statements */
int main(){ int integer;
char character;
float floatingPoint;
integer = 33;
character = 33;
floatingPoint = 33;
integer = 'A';
character = 'A';
floatingPoint = 'A';
integer = 33.33;
character = 33.33;
floatingPoint = 33.33;
integer = floatingPoint;
floatingPoint = integer;
return 0;
}
various.c
![Page 47: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/47.jpg)
Constant Variables ...are variables that don’t vary ...may not be assigned to. ...must be initialized
const float Pi = 3.14159;
const int classSize = 100;
![Page 48: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/48.jpg)
Constant Variables: Examplesconst int myID = 192;
myID = 666; /* Error! */
const int passMark = 80;
short char pAsSgRaDe = ’P’;
const float pi = 3.1415926; /* oops */
const double golden_ratio = 1.61803398874989;
![Page 49: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/49.jpg)
Converts an angle from degrees to radians
output “Enter angle in degrees”
input angleInDegrees
angleInRadians = / 180 * angleInDegrees
output angleInRadians
#include <stdio.h>
/* Converts an angle in degrees to radians. */
const float PI = 3.1415926;
int main(){ float angleInDegs; float angleInRads;
printf("Enter angle in degrees:"); scanf("%f", &angleInDegs);
angleInRads = PI/180*angleInDegs;
printf("%f\n", angleInRads); return 0;}
Example: Constants
![Page 50: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/50.jpg)
Example: Constants
“Global” constant variable
“Local” variables
more on this later...
#include <stdio.h>
/* Converts an angle in degrees to radians. */
const float PI = 3.1415926;
int main(){ float angleInDegs; float angleInRads;
printf("Enter angle in degrees: "); scanf("%f", &angleInDegs);
angleInRads = PI/180*angleInDegs;
printf("%f\n", angleInRads); return 0;}
![Page 51: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/51.jpg)
Example: Constants
Print text on the screen
scanf function gets values from
user
#include <stdio.h>
/* Converts an angle in degrees to radians. */
const float PI = 3.1415926;
int main(){ float angleInDegs; float angleInRads;
printf("Enter angle in degrees: "); scanf("%f", &angleInDegs);
angleInRads = PI/180*angleInDegs;
printf("%f\n", angleInRads); return 0;}
![Page 52: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/52.jpg)
Example: Constants
Result will be stored in
angleInRads
#include <stdio.h>
/* Converts an angle in degrees to radians. */
const float PI = 3.1415926;
int main(){ float angleInDegs; float angleInRads;
printf("Enter angle in degrees: "); scanf("%f", &angleInDegs);
angleInRads = PI/180*angleInDegs;
printf("%f\n", angleInRads); return 0;}
![Page 53: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/53.jpg)
Example: Constants
Will be printed on the screen
#include <stdio.h>
/* Converts an angle in degrees to radians. */
const float PI = 3.1415926;
int main(){ float angleInDegs; float angleInRads;
printf("Enter angle in degrees: "); scanf("%f", &angleInDegs);
angleInRads = PI/180*angleInDegs;
printf("%f\n", angleInRads); return 0;}
![Page 54: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/54.jpg)
printf() function In order to use printf(), function you need to include
<stdio.h> as #include<stdio.h>
To print an integer %d is used in printf
For example
This will print 9 on the output screen
![Page 55: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/55.jpg)
printf() function To print a string on the screen %s is used in printf or
simply type words with printf.
For example This will print Hello world! This
is 9
![Page 56: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/56.jpg)
printf() function To print a floating point number %f is used
For example
This will print 9.200000 on the
screen
![Page 57: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/57.jpg)
printf() function Floating point with 2 digit precision, use %0.2f
For example
This will print 9.20 on the screen
![Page 58: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/58.jpg)
scanf function Same include directive is used,
#include<stdio.h> Used to get a value from user at run time.
For example scanf(“%d”, &i);
Will get an integer value in the variable i. Can get character, floating or integer value
from the user.
![Page 59: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/59.jpg)
scanf example Ask user to enter a value
scans the value
Prints the user input value on
the screen
![Page 60: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/60.jpg)
Output of previous program
![Page 61: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/61.jpg)
We have covered Type Variables Keyword and Identifiers Assignments Constant Variables
![Page 62: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/62.jpg)
Precedence in Expressions
Defines the order in which an expression is evaluated
![Page 63: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/63.jpg)
Precedence in Expressions -- Example
1 + 2 * 3 - 4 / 5 =
B stands for brackets, O for Order (exponents), D for division, M for multiplication, A for addition, and S for subtraction.
B.O.D.M.A.S.1 + (2 * 3) - (4 / 5)
![Page 64: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/64.jpg)
More on precedence *, /, % are at the same level of precedence +, - are at the same level of precedence For operators at the same “level”, left-to-right
ordering is applied.2 + 3 – 1 = (2 + 3) – 1 = 42 – 3 + 1 = (2 – 3) + 1 = 0
2 * 3 / 4 = (2 * 3) / 4 = 6 / 42 / 3 * 4 = (2 / 3) * 4 = 0 * 4
![Page 65: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/65.jpg)
Precedence in Expressions – Example
6.2
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 66: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/66.jpg)
Precedence in Expressions –Example..
6.2
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 67: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/67.jpg)
Precedence in Expressions – Example..
Integer division results in integer quotient
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 68: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/68.jpg)
Precedence in Expressions – Example
= 0
D’oh
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 69: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/69.jpg)
Precedence in Expressions ..
7
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 70: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/70.jpg)
int and float
float is a “communicable” type Example:
1 + 2 * 3 - 4.0 / 5
= 1 + (2 * 3) - (4.0 / 5)
= 1 + 6 - 0.8
= 6.2All integers are in Black
![Page 71: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/71.jpg)
int and float – Example 2
(1 + 2) * (3 - 4) / 5
= ((1 + 2) * (3 - 4)) / 5
= (3 * -1) / 5
= -3 / 5
= 0
![Page 72: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/72.jpg)
int and float – Example 2
(1 + 2.0) * (3 - 4) / 5
= ((1 + 2.0) * (3 - 4)) / 5
= (3.0 * -1) / 5
= -3.0 / 5
= -0.6
![Page 73: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/73.jpg)
int and float – Example 3
(1 + 2.0) * ((3 - 4) / 5)
= (1 + 2.0) * (-1 / 5)
= 3.0 * 0
= 0.0
![Page 74: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/74.jpg)
Unary operators Called unary because they require one operand. Example
i = +1; /* + used as a unary operator */
j = -i; /* - used as a unary operator */
The unary + operator does nothing, just emphasis that a numeric constant is positive.
The unary – operator produces the negative of its operand.
i=+1 is equal to i=i+1
![Page 75: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/75.jpg)
Increment and decrement operators
++ is the increment operator i++;
is equivalent toi = i + 1;
-- is the decrement operatorj--;
is equivalent toj = j - 1;
![Page 76: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/76.jpg)
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
Example -- Simple Expressions
![Page 77: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/77.jpg)
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
Example -- Simple Expressions
![Page 78: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/78.jpg)
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
int main(){
return 0;}
Example -- Simple Expressions
![Page 79: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/79.jpg)
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
int main(){float result;
return 0;}
Example -- Simple Expressions
![Page 80: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/80.jpg)
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
int main(){float result;
result = 1 + 2 * 3 - 4 / 5;
return 0;}
Example -- Simple Expressions
![Page 81: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/81.jpg)
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
int main(){float result;
result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);
return 0;}
Example -- Simple Expressions
![Page 82: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/82.jpg)
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
Output: 7.000000
#include <stdio.h>
/* Evaluate an expression */
int main(){float result;
result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);
return 0;}
Example -- Simple Expressions
![Page 83: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/83.jpg)
Comments
Essential for documenting programs Run from a /* to the next */ Examples:
/* THIS IS A COMMENT */
/* So isthis */
/*** ...and this.***/
![Page 84: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/84.jpg)
Comments ..
Comments do not “nest”
/* Comments start with a “/*”and end with a “*/”but they don’t nest! */
![Page 85: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/85.jpg)
Errors in C Program Do not get Fear of an Error! All good programmers started with lot of
Errors Understand them and remove them!
![Page 86: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/86.jpg)
Types of Errors In c program you may get
Syntax errorLogical errorRun time errors
![Page 87: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/87.jpg)
Syntax Error These errors occur because of wrongly
typed statements, which are not according to the syntax or grammatical rules of the language. For example, in C, if you don’t place a semi-
colon after the statement (as shown below), it results in a syntax error.
printf(“Hello,world”)
![Page 88: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/88.jpg)
Logical Error These errors occur because of logically
incorrect instructions in the program. Let us assume that in a 1000 line program, if
there should be an instruction, which multiplies two numbers and is wrongly written to perform addition.
This logically incorrect instruction may produce wrong results. Detecting such errors are difficult!
![Page 89: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/89.jpg)
Run Time Errors These errors occur during the execution of the
programs though the program is free from syntax and logical errors.
Some of the most common reasons for these errors are When you instruct your computer to divide a number
by zero. When you instruct your computer to find logarithm
of a negative number. When you instruct your computer to find the square
root of a negative integer.
![Page 90: The C Programming Lecture 24. Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process](https://reader035.vdocument.in/reader035/viewer/2022070323/56649da65503460f94a92225/html5/thumbnails/90.jpg)
Summary High level language is converted into computer
understandable by a compiler. Compiler converts source code into computer
understandable code
C Programming is a way to program computers It has a syntax It has commands and structure
C can not be learnt! It can be understood by implementing it!