Professional Documents
Culture Documents
Date 170807
Date 170807
Date 170807
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 1
Programming & Data Structure: CS11001/19001
Section - 9/C
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 2
Download
http://www.facweb.iitkgp.ernet.in/∼goutam
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 3
Types of Errors
• Compilation error.
• Linker error.
• Run-time error.
• Logical error.
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 4
Compilation Error
$ cc -Wall err.c
err.c: In function ‘main’:
err.c:6: parse error before "printf"
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 5
Compilation Error
int main()
{
int n ;
n = 10
printf("n = %d\n", n) ;
return 0 ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 6
Compilation Error
$ cc -Wall err.c
err.c: In function ‘main’:
err.c:6: called object is not a function
#include <stdio.h>
int main() {
int n ;
n = 10 ;
printf("%d\n",n(n-1)) ;
return 0 ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 7
Compilation Error
$ cc -Wall err.c
err.c: In function ‘main’:
err.c:12: parse error at end of input
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 8
Compilation Error
1 #include <stdio.h>
2 int main() {
3 int n, m, p ;
4 scanf("%d", &n) ;
5 if(n > 0) {
6 m = 2*n - 5 ;
7 if(m < 0) {
8 p = m + 10 ; n++ ;
9 }
10 printf("%d", m * n) ;
11 }
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 9
Linker Error
$ cc -Wall err.c
err.c: In function ‘main’:
err.c:9: warning: implicit declaration .. ‘facterial’
/tmp/cc4wwrDz.o: In function ‘main’:
/tmp/cc4wwrDz.o(.text+0x53): undefined reference to ‘facter
collect2: ld returned 1 exit status
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 10
Linker Error
#include <stdio.h>
int factorial(int n) {
if(n == 0) return 0 ;
else return n*factorial(n-1) ;
}
int main() {
int n ;
n = 10 ;
printf("fact(%d)=%d\n",
n,facterial(n));
return 0 ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 11
Linker Error
$ cc -Wall err.c
err.c:5: warning: implicit declaration of function ‘sqrt’
/tmp/ccqeQ265.o: In function ‘main’:
/tmp/ccqeQ265.o(.text+0x28): undefined reference to ‘sqrt’
collect2: ld returned 1 exit status
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 12
Linker Error
#include <stdio.h>
int main() {
float n ;
n = 10.0 ;
printf("sqrt(%f)=%f\n",
n,sqrt(n));
return 0 ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 13
Run-time Error
$ cc -Wall err.c
err.c: In function ‘main’:
err.c:5: warning: format argument is not a pointer (arg 2)
err.c:6: warning: control reaches end of non-void function
$ a.out
5.0
Segmentation fault
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 14
Run-time Error
#include <stdio.h>
int main() {
float n ;
scanf("%f", n) ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 15
Run-time Error
$ a.out
1
Floating point exception
• Funny message, actually divide-by zero -
printf(”%d”,n/(n-1)) ;
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 16
Logical Error
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 17
Logical Error
#include <stdio.h>
#include <math.h>
int main() {
float n ;
scanf("%f", &n) ;
printf("%f\n", pow(n, 1/3)) ;
return 0 ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 18
Logical Error
$ a.out
8.0
1.000000
$
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 19
Pythagorean Triple
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 20
Assignment IV
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 21
hcf()
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 22
Steps
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 23
Report
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 24
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 25
61 1
=2+ 1
27 3+ 1
1+
6
This may be represented in a compact form as
2; 3, 1, 6, where the first 2 indicates the integral
part of the improper fraction.
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 26
Assignment V
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 27
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 28
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 29
ASCII to Integer
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 30
ASCII to Integer
#include <stdio.h>
int main(){
char a ;
scanf("%c", &a) ;
printf("’%c’: %d\n", a, a - ’0’) ;
return 0 ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 31
ASCII to Integer
$ a.out
6
’6’: 6
$
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 32
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 33
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 34
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 35
Practice Problem IV
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 36
Practice Problem V
211 + 210 + 28 + 26 + 25 + 23 + 20
= 2048 + 1024 + 256 + 64 + 32 + 8 + 1
= 3433
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 37
& %