Before - NCSU COE People

Download Report

Transcript Before - NCSU COE People

CSC 253
Lecture 3
Let’s look again at …
 the program from the end of the last
class.
 Here’s the start, and the power function:
#include <stdlib.h>
#include <stdio.h>
int pow(int base, int exp) {
int x = base;
int i;
for (i = 1; i < exp; i++) x = x*base;
return x;
}
}
Here’s the main program




int main() {
int i;
int x=-2;
long int xx;





printf("The
printf("The
x=-1;
printf("The
printf("The




for (i = 1; i < 34; i++) {
xx = pow(2, i);
printf("2^%d -- Unsigned: %u; signed: %i; hex: %x\n", i, xx, xx, xx);
}


xx = pow(2, 31);
printf("xx-1: %i; xx: %i\n", xx-1, xx);

return EXIT_SUCCESS;
int value of -2 in hex is %x\n",x);
int value of -2 in octal is %o\n",x);
int value of -1 in hex is %x\n",x);
int value of -1 in octal is %o\n",x);
How could we change it to …
 stop when the unsigned number was first
different from the signed number?
 print just the largest int & largest unsigned
int?
 print the numbers in nice columns?





i
signed value of 2i
unsigned value of 2i
octal value of 2i
hex value of 2i
Now let’s try character I/O
 Write a program to read a character,
then print
 the character just read, and
 its octal value.
Now string input …
 Write a program to read a hex string
and print
 its character equivalent, and
 the string just read
Now let’s write a program to…
 print out the sizes of
 short ints
 ints
 long ints
 floats
 doubles