Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
EECS2031 A
Software Tools
F 2019
1
9/14/2019
gcc c compiler
• -o exectuableName
%gcc hello.c -o hello
%gcc -o hello hello.c
•3 combine
%gcc -ansi -Wall hello.c -o hello
• Definition – describe arguments and return value, and gives the code
2
9/14/2019
/* Contains declaration
functions (prototype) of printf() */
#include <stdio.h>
/* function definition */
float div (float i, float j) Defined before (first) call
{
return i / j;
}
main()
{
float x = 2.1, y = 3.2;
float su = div(x , y);
printf( "%f / %f = %f\n", x,y, su);
}
/* Contains declaration
functions (prototype) of printf() */
#include <stdio.h>
/* function declaration */
float div (float, float); /* float div (float divd, float divor)
Not Defined or declared
preferred for readability*/
before (first) call
main()
{
float x =2.1, y=3.2;
float su = div(x,y); Little luckier if return int…
printf( "%f / %f = %f\n", x,y, su);
}
/* function definition */
float div (float i, float j){ Defined after (first) call
return i / j;
}
6
3
9/14/2019
/* Contains declaration
functions (prototype) of printf() */
#include <stdio.h>
/* function declaration */
float div (float, float); /* float div (float divd, float divor)
preferred for readability*/
/* function definition */
float div (float i, float j){ Defined after (first) call
return i / j;
}
7
• int putchar(int c)
▪ Write the character c on standard output (screen or > outputFile)
4
9/14/2019
/* conversion
format string specification */
#include <stdio.h>
main()
{
float a, b;
printf("Enter two floats separated by <><>: " );
5
9/14/2019
6
9/14/2019
13
char 'a' 'b' compared directly. Strings not "a"== "b" Will elaborate today.
C basics
Compile and running Comments
Basic syntax
Comments
Variables
Functions
Basic IO functions
Expression
Statements
Preprocessing: #include, #define
14
7
9/14/2019
Statements
• Program to execute
▪ Ended with a ;
15
Expression
• Formed by combining operands (variable, constants and
function calls) using operators (+ - * % > < == != )
8
9/14/2019
Statements
hello.c hello.o
preprocessor compiler linker
a.out
- creates an executable
program e.g., a.out
18
- remove hello.o
9
9/14/2019
Declarations/ prototypes
int printf (..)
#include <stdio.h> int scanf(..)
main()
{ int getchar()
int i = 4; int putchar(int)
printf("this is %d\n",i);
char* gets(char *)
int sprintf (..)
}
#define directive
• Syntax #define name value
▪ Name called symbolic constant, conventionally written in
upper case
▪ Value can be any sequence of characters
#define N 100
main() { main() {
int i = 10 + N; int i = 10 + 100;
} }
▪ Textual replacement
o try yourself by cpp file.c or gcc –E file.c
cpp file.c > outputFile
Discuss later
10
9/14/2019
Summary
• Course introduction. C basics
▪ Variables:
o names don’t start with digit, _ , keyword Same in Java
▪ Functions: declaration vs definition
▪ Basic IO functions
o scanf & printf,
o getchar putchar
▪ Expression and statements
▪ C-preprocessing
• Today’s lecture:
▪ C data, type, operators (Ch 2)
21 ▪ C flow controls (Ch 3) self-study
EECS2031-Software Tools
11
9/14/2019
Outline
• Expressions
▪ Basic operators
▪ Type promotion and conversion
▪ Other operators
▪ Precedence of operators
23
24
12
9/14/2019
Text book:
4 basic types: char, int, float, double
C Types and sizes
3 qualifiers: short, long, unsigned
• Variables and values have types
• There are two basic types in ANSI-C: integer, and floating point
▪ Integer type
o char - character, 1 byte (8 bits)
o short (int) - short integer, usually 2 bytes (16 bits)
o int - integer, usually 2 or 4 bytes (16 or 32 bits)
o long (int) - long integer, usually 4 or 8 bytes (32 or 64 bits)
▪ Floating point
o float - single-precision, usually 4 bytes (32 bits)
o double - double-precision, usually 8 bytes (64 bits)
o long double - extended-precision
25
• Bit/byte/K/M/G/T
• int x;
1 bit
1 byte (8 bits)
4 byte
(32 bits)
26
13
9/14/2019
= 1,073,741,824 bytes
• int x;
1 bit
1 byte (8 bits)
4 byte
(32 bits)
27
= 1,073,741,824 bytes
1 bit
1 byte (8 bits)
• Be careful;
28
For your information
14
9/14/2019
29 = 26
Binary representations
… 25 24 23 22 21 20
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 Binary
… 26 2 5 2 4 2 3 22 21 20
30
1*22 + 0*21 + 1*20 =
4 +0 +1 =5
15
9/14/2019
31
1 + 1 is 0 with a carry
16
9/14/2019
▪ (signed) char
▪ (signed) int
▪ (signed) short int
▪ (signed) long int
• Range?
▪ signed -2n-1 ~ 2n-1-1 2n values
▪ unsigned 0 ~ 2n-1 2n values
33
▪ (signed) char
▪ (signed) int
▪ (signed) short int
▪ (signed) long int
17
9/14/2019
▪ 11111110’s decimal?
……
o - (flip +1)
o - (00000001+1) = -(00000010) = -2
35 0~~2n-1 -2n-1 ~~ 2n-1-1
2n=256 values 2n=256 values
18
9/14/2019
▪ signed char
▪ (signed) int
▪ (signed) short (int) short
▪ (signed) long (int) long
▪ unsigned char
▪ unsigned (int) unsigned
▪ unsigned short (int) unsigned short
▪ unsigned long (int) unsigned long
scanf ("%hd") for short int, ("%ld") for long int, ("%lld") for long long (C99)
•printf
d long
("%hd)int” ==int,“long”
for short ("%ld") for long int, ("%lld") for long long (C99)
▪ “unsigned long int” == “unsigned long”
37 For your information
• scanf ("%f") for float, ("%lf") for double, ("%Lf") for long double
•38 printf ("%f") for float, double, ("%Lf") for long double
19
9/14/2019
Summary
• Integer types:
▪ char
signed char unsigned char
▪ (signed) short unsigned short
▪ (signed) int unsigned int
▪ (signed) long unsigned long
• C99 added:
▪ (signed) long long int
39
▪ unsigned long long intbool, complex
Size of Types
• Relations of sizes:
▪ short ≤ int ≤ long
▪ float ≤ double ≤ long double
40
20
9/14/2019
Size of Types
• To get exact size of a type in a machine, use sizeof operator
▪ sizeof (int) or int a; sizeof a; or sizeof (a)
▪ in bytesallocation in byte
Memory
▪ Let us see our lab…..
41 ……
Size of Types
• To get exact size of a type in a machine, use sizeof operator
▪ sizeof (int) or int a; sizeof a ; or sizeof (a)
▪ Let us see our lab…..
Different on
different machines
(except char)
42 // c99
// c99
21
9/14/2019
22
9/14/2019
// 0 ~ 2 16-1 -1
// -2 32-1 -1 ~ 2 32-1 -1
// 0 ~ 2 32 -1
Outline
• Types and sizes
▪ Types: char, short, int, long, unsigned short, unsigned int, float, double …..
▪ Constant values (literals)
o char
o int
o float
• Expressions
▪ Basic operators
▪ Type promotion and conversion
▪ Other operators
▪ Precedence of operators
46
23
9/14/2019
Character Constants
Same in Java
47
Special Characters
char c = '\t';
char c2 = '\n'; Same in Java
24
9/14/2019
int i = 43;
char a = 'A';
49
25
9/14/2019
51
▪ 8 bits is enough
26
9/14/2019
53
Characters
• chars are treated in C as small integers, char
variables and constants are identical to int in
arithmetic expressions:
▪ char c is converted to its encoding (index in
the character set table)
same in Java
• Same for other expressions. In relational expression, characters can be
compared directly, comparing indexes/encodings
aChar == EOF // index == -1? ➔ expr with value 0 (false)
aChar < 'H' // 69 < 72? Earlier in table? ➔ expr with 1 (true)
54
27
9/14/2019
System.out.println(aCh+0); // 51
System.out.println(aCh+4); // 55
Characters
• Since chars are just small integers, char
variables and constants are identical to int in
arithmetic expressions:
▪ char c is converted to its encoding (index in
the character set table)
28
9/14/2019
Characters
• Since chars are just small integers, char
variables and constants are identical to int in
arithmetic expressions. Some programming idioms
that take advantage of this:
Characters
• Since chars are just small integers, char
variables and constants are identical to int in
arithmetic expressions. Some programming idioms
that take advantage of this:
29
9/14/2019
#include<stdio.h>
main(){
char le = 'J'; // 74
while (le <= 'Q') {
printf ("%d %c %cack %c\n", le,le,le, le+1);
le++;
}
}
74 J Jack K
75 K Kack L
76 L Lack M
77 M Mack N same in Java
78 N Nack O
79 O Oack P
80 P Pack Q
6081 Q Qack R
30
9/14/2019
Outline
• Types and sizes
▪ Types
▪ Constant values (literals)
o char treated as small int
o int different bases
o float
• Expressions
▪ Basic operators
▪ Type promotion and conversion
▪ Other operators
▪ Precedence of operators
61
2. Octal [base 8]
▪ Start with zero 0
same in Java
▪ int x = 037 (31 in decimal)
• Ways
Octalfor
and hex are
people useful
to write for representing bytes
numbers.
No▪ effect
Can on
be how
easilythe
converted
numbers to binary Java also has the 4th way: binary
are representations
62 int x = 0b00011111
o Binary representations are invalid in C programs
stored –- always binary.
31
9/14/2019
= 851
63 = 46687
Others To decimal
• 3 2 8
3*102 + 2*101 + 8*100 = Decimal 328
102 101 100 300 + 20 +8 = 328
• 1 0 1
1*22 + 0*21 + 1*20 =
22 21 20 4 +0 +1 =5 Binary
32
9/14/2019
• 01 0 0 1 1 0 0
int a = 0114 Octal
1 1 4
Hex 0x34F
• 01 0 0 1 1 0 0 int a = 0X4C Hex
4 C = 0x4c
33
9/14/2019
Try it!
`
Also a writeup
“Number
system.pdf” on the
course website
67
• E.g.
5 as an “(signed) (decimal) int” 5
5U as an “unsigned (decimal) int” 5
5L as a “(signed) long (int)” 5
5UL or 5ul as an “unsigned long (int)” 5
037 as an “(signed) int (oct)” decimal: 31
0x32dUL as an “unsigned long (int) in hex 32d”
059 as an ?
68 0x39G2 as an ?
34
9/14/2019
Outline
• Types and sizes
▪ Types
▪ Constant values (literals)
o char
o int
o float
• Expressions
▪ Basic operators
▪ Type promotion and conversion
▪ Other operators
▪ Precedence of operators
69
70 same in Java
35
9/14/2019
Outline
• Types and sizes
▪ Types
▪ Constant values (literals)
o char
o int
o float
• Expressions
▪ Basic operators
▪ Type promotion and conversion
▪ Other operators
▪ Precedence of operators
71
Arrays
36
9/14/2019
Declaring Arrays
• Declare and initialize (how to do in Java?)
Accessing Arrays
• In C, you can only assign to array members
▪ This means you cannot assign to an whole array:
j 0 0 0 0
for (i=0; i<4; i++) i=0; k
k[i] = j[i]; while(i<4)
{
k[i] = j[i];
i++;
74 }
37
9/14/2019
#include <stdio.h>
#define N 10
int main () {
int c, i;
int digit[N];
return 0;
}
75
'H' 'e' 'l' 'l' 'o' '\0' '\0' added for you
is equivalent to
char s[]= {'H', 'e', 'l', 'l', 'o', '\0'}
76
38
9/14/2019
main() { H e l l o \0
char s1[]= "Hello";
sizeof s1: 6 strlen(s1): 5
char s2[8];
printf("s1: %s\n",s1); // s1: hello
int i=0;
while (s1[i] != '\0')
{ H e l l o \0
s2[i] = s1[i];
sizeof s2: 8 strlen(s2): 5
i++;
}
s2[i]='\0'; /*finally add \0 manually*/
39
9/14/2019
main() { H e l l o \0
char s1[]= "Hello";
sizeof s1: 6 strlen(s1): 5
char s2[8];
printf("s1: %s\n",s1); // s1: hello
int i=0;
while (1){
s2[i] = s1[i]; H e l l o \0
if(s2[i] == '\0')
sizeof s2: 8 strlen(s2): 5
break;
i++;
}
40
9/14/2019
i++;
}
}
Note: array name as a
main() { function parameter is
char s1[]= "Hello!"; a pointer ( will study
char s2[8]; later). Therefore, the
stringcopy2(s2, s1); size of the array need
printf("s2 is %s\n",s2); not to be specified in
the function
return 0;
parameter
}81
main() {
char my_strg[100];
int a;
41
9/14/2019
Outline
• Types and sizes
▪ Types
▪ Constant values (literals)
o char
o int
o float
• Expressions
▪ Basic operators (arithmetic, relational and logical)
▪ Type promotion and conversion
▪ Other operators (bitwise, bit shifting , compound assignment,
conditional)
▪ Precedence of operators
83
Expressions
• Expressions are made up of operands (things we operate upon)
and operators (things that do the operations: + - * % > <)
▪ x+y/2, i>=0, x==y, i++,…
42
9/14/2019
Expressions
85
Arithmetic (unary)
Increment/Decrement Operators
• ++ increment
• -- decrement same in Java
43
9/14/2019
Arithmetic (unary)
Increment/Decrement Operators
• ++ increment
• -- decrement same in Java
x = 2; x = 2;
y = x++; // increment after y = ++x; // increment before
assignment assignment
printf("%d %d",x, y); printf("%d %d",x, y);
87
x:3 y:2 x: 3 y:3
Arithmetic (unary)
Increment/Decrement Operators
• ++ increment
• -- decrement same in Java
x = 2; x = 2;
y = x--; // decrement after y = --x; // decrement before
assignment assignment
printf("%d %d",x, y); printf("%d %d",x, y);
88
x:1 y:2 x: 1 y:1
44
9/14/2019
Arithmetic (unary)
Increment/Decrement Operators
• The prefix/postfix effect can be subtle
int x = 3, y, z;
y= x++; // post-increment. y=x; x=x+1;
z= ++x; // pre-increment. x=x+1; z=x;
printf("x:%d y:%d z:%d",x, y,z);
same in Java
• What are the output?
x:5 y:3 z:5
89
A common use of ++ --
/*initialize to 0 */
#include <stdio.h>
#define N 10
int main () {
int i=0;
int digit[N];
// succinct code
while (i< N) while ( i< N)
{ {
digit[i]=0; digit[i++]=0;
i++; //post-increment
} }
90
45
9/14/2019
▪ Expressions
o Basic operators (arithmetic, relational and logical)
✓ y=x++; y=++x;
✓ if (x = 2)
o Type conversion and promotion
o Other operators (bitwise, bit shifting , compound assignment,
conditional)
✓ Bit: |, &, ~. ^, << >>
✓ Compound: x + = 10; x >>= 10; x += y + 3
o Precedence of operators
91
• Functions and Program Structure (Chapter 4)
46