FOC Unit 1B slides
FOC Unit 1B slides
Faculty:
Mrs.Darshana A Naik
Mrs.Shilpa H
Mrs Sunitha R S
◼ Source Program
◼ printable/Readable Program file
◼ Object Program
◼ nonprintable machine readable file
◼ Executable Program
◼ nonprintable executable code
◼ Syntax errors
◼ reported by the compiler
◼ Linker errors
◼ reported by the linker
◼ Execution/Run-time errors
◼ reported by the operating system
◼ Compiler
◼ Converts source program to object program
◼ Linker
◼ Converts object program to executable program
return 0; statement
16
… DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
31
Memory Snapshot
2.0 r
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 21
Symbolic Constants
◼ What if you want to use a better estimate of ?
For example, you want 3.141593 instead of 3.14.
◼ You need to replace all by hand
◼ Better solution, define as a symbolic constant, e.g.
#define PI 3.141593
…
area = PI * r * r;
circumference = 2 * PI * r;
◼ Defined with a preprocessor directive
◼ Compiler replaces each occurrence of the directive identifier with the
constant value in all statements that follow the directive
◼ Example 4
y=z;
y=5;
long double, double, float, long integer, integer, short integer, char
→ Data may be lost. Be careful!
No data loss
x 3 x 5 x 5
y 5 y 5 y 5
x x x x 5
3 3 5
y y y y 3
5 5 5
temp temp temp temp 3
? 3 3
Output:
Angle = 45.50 degrees
Identifier
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 29
Conversion Specifiers for Output
Statements
Frequently Used
30
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Standard Output
Output of 157.8926
Specifier Value Printed
%f 157.892600
%6.2f 157.89
%7.3f 157.893
%7.4f 157.8926
%7.5f 157.89260
%e 1.578926e+02
%.3E 1.579E+02
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 31
Exercise
int sum = 65;
double average = 12.368; char ch = ‘b’;
Show the output line (or lines) generated by the following statements.
65 12.4
Character is b; Sum is A
Character is 98; Sum is 65
control string
◼ Example:
int distance; char unit_length;
scanf("%lf %c", &distance, &unit_length);
It is very important to use a specifier that is appropriate for the data
type of the variable
Frequently Used
12 45
12 23.2
12.1 10
12
1
a + b a + c c +b c − d
res = +
a−b a +c
39
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Math Functions
#include <math.h>
fabs(x) Absolute value of x.
velocity = sqrt(vo*vo+2*a*(x-xo));
velocity = sqrt(pow(vo,2)+2*a*(x-xo));
putchar(‘a’);
C= getchar();
44
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Exercise
What is the output of the following program
int main(void)
{
char ch1='a', ch2; char
ch3='X', ch4;
char ch5='8';
ch2 = toupper(ch1);
printf("%c %c \n",ch1,ch2); aA
ch4 = tolower(ch3); Xx
printf("%c %c \n",ch3,ch4); 2048
printf("%d\n",isdigit(ch5)); 512
printf("%d\n",islower(ch1)); 0
printf("%d\n",isalpha(ch5));
return(0);
46
} DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Summary
◼ Computing systems need a specific plan to design a solution for a
problem.
◼ The steps to engineering problem solving methodology are:
◼ Problem statement.
◼ Describe the input and output.
◼ Work the solution by hand with simple set of data.
◼ Design a solution and convert it into a program.
◼ Test the solution.
◼ Internal organization of a computer.
◼ Linking and loading process.