Warm up questions

Warm up questions
1. What are the four steps in C compiling to create executable programs?
2. What the option –S does with gcc
command?
Lecture 3
1. C debugging with GDB
2. C program language basics a) C program structure
b) primary data types and variables
c) program constructs
Debugging example: compute factorial n!
# include <stdio.h>
int main() {
int i, num, j;
printf ("Enter an integer: ");
scanf ("%d", &num );
for (int i = 1; i < num; i++) {
j = j*i; }
printf("The factorial of %d is %d\n", num, j);
return 0;
}
Debugging C buy GDB
$gcc ‐g factorial.c
$gdb a.out
(gdb) l // list of the source
(gdb) break 10 // set a breaking point at line 10, or b 10
(gdb) run a.out // or r a.out
(gdb) print j // print current value of variable j
(gdb) c // continue
(gdb) s //progress one step or next
(gdb) quit
(gdb) help
GDB commonly used commands
b main ‐ Puts a breakpoint at the beginning of the program / or break
b ‐ Puts a breakpoint at the current line
b N ‐ Puts a breakpoint at line N
b +N ‐ Puts a breakpoint N lines down from the current line
b fn ‐ Puts a breakpoint at the beginning of function "fn"
d N ‐ Deletes breakpoint number N / or delete N
info break ‐ list breakpoints
r ‐ Runs the program until a breakpoint or error / run
c ‐ Continues running the program until the next breakpoint or error /continue
f ‐ Runs until the current function is finished s ‐ Runs the next line of the program / step
s N ‐ Runs the next N lines of the program
n ‐ Like s, but it does not step into functions / next
u N ‐ Runs until you get N lines in front of the current line
p var ‐ Prints the current value of the variable "var“ / print
bt ‐ Prints a stack trace u ‐ Goes up a level in the stack
d ‐ Goes down a level in the stack
q ‐ Quits gdb / quit
About C programming language
‐ C is a general‐purpose programming language initially developed by Dennis Ritchie between 1969‐1973 at AT&T Bell Labs
‐ American National Standards Institute (ANSI) approved first C standard (known as ANSI C or C89) in 1989
‐ International Organization for Standardization (ISO) proved a revised standard (known as C99) in 1999
‐ The current version of the standard (known as C11) was approved in 2011
‐ C is one of the most widely used programming languages. C compilers are available for majority of computer architectures and OS
C program structure
C source program
[pre‐processor statements]
[declare global variables and constants]
[declare function prototypes]
int main(argument ) {
[program statements] return 0;
}
[implementation of functions]
memory map
Data/variable types
1. Data type defines how data is stored in memory
– Primary data types, e.g. char, int, float, etc. (no boolean)
– Secondary data types, e.g., array, pointer, struct, union, enum, etc
2. Variable type ‐‐ in program, use variable to refer to a data of certain type, a C variable always has a type Primary data types (size is platform dependent)
4
‐‐2^31+1 to +2^31‐1
8
‐‐2^63+1 to +2^63‐1
4
‐0 to +2^32‐1
8
‐0 to +2^63‐1
16
The char type
• char type is stored as an integer using ASCII code. E.g. A is coded as 65, a as 97, + as 43. Variables
•
variables are names used to refer to some memory location that holds a value of certain data type
• A variable has to be declared (and initiated or assigned value) before it can be used properly
Declaration:
int a; int b;
char c; float f;
Initialization a = 2;
b = 3;
c = ‘a’;
f = 1.41;
Arithmetic operators
+ addition ‐ subtraction
* multiplication
/ division
% mod (remainder)
Two special operators: ++ and –
i++;  i = i + 1;
i‐‐;  i = i ‐1;
Division: int and float
Integer division is different from float division
int / int gives int, e.g 5/2 gives 2
float / float gives float, e.g. 1.0/2.0 gives 0.5 int a=5, b=2; float f1 = 1.5, f2 = 3.5; a = a/b; // a = 2 Conversion/cast :
(float) int results in float, e.g. f1 = (float) b; // f1 = 2.0 b = (int) f2; // b = 3
Precedence of operators in expression
A + B * C means ?
(A+B) * C
A + (B*C) The second meaning
* has priority over +
Partial list of priority
Operator
Description
Associativity
left-to-right
()
[]
.
->
++
*
--
/
+
%
-
&
Parentheses (grouping)
Brackets (array subscript)
Member selection via object name
Member selection via pointer
Postfix increment/decrement
Multiplication/division/modulus
Addition/subtraction
Bitwise AND
left-to-right
left-to-right
left-to-right
C program constructs
• Three program constructs : 1. Sequence, 2. Selection, 3. Repetition
• Sequence: linear order int a = 10;
char c = ‘a’;
• Selection structure: three types
1. if‐statement
float a = 3.41415;
if (a >= 0.0)
if (condition) group1;
printf(“the number %5.1lf is positive”, a);
2. if‐else statement
if (marks >= 50.0)
if (condition) printf(“You pass”);
group1; else
else printf(“You fail”);
group2;
3. switch statement
char Grade;
switch( Grade ) {
case 'A' : printf( "Excellent" );
break;
case 'B' : printf( "Good" );
break;
case ‘C’ : printf( “OK" );
break;
default : printf( "What is your grade anyway?" ); }
• Repetition (loop) construct: three types
1. For statement for (count = 1; count <=10; count++) printf(“%d”,count); 2. while loop
while (condition is true) {
group;
}
3. do‐while loop
do {
group;
} while (condition is true);
break statement comes out of a loop
char c; for(;;) { printf( "\nPress any key, Q to quit: " ); scanf("%c", &c); if (c == 'Q') break; } Example of prompt input
#include <stdio.h>
int main(){
char c;
do {
printf("Please enter a charater in lower case\n");
c = getchar();
printf("%c\n", c ‐ 32 );
if (c == 'Q')
return 0; // or break;
#include <stdio.h>
int main(){
char c;
do {
printf("Please enter a charater in lower case\n");
c = getchar();
printf("%c\n", c ‐ 32 );
do { if (getchar() == '\n') break;
} while (1);
do { // fflush(stdin); if ( getchar() == '\n') break;
} while (1);
} while( c !='Q' );
} while(1);
return 0;
return 0;
}
}
continue statement moves to the next iteration of a loop
/* output the integers between 100 and 200, not divisible by 3*/
#include <stdio.h>
int main(void) {
int n;
for (n = 100; n <= 200; n++)
{
if (n%3==0)
continue;
printf("%d ", n);
}
printf("\n");
return 0;
}
return vs exit() statements
• To terminate a program – return 0, exit the program where the statement is
– exit()  program is terminated immediately. Use with <stdlib.h>, or exit(status), status = 0, 1 • Don’t use exit() except
– error handling (exit returns a value)
– event‐driven programming.
How to kill the program if it falls into an infinite loop?