CS157: Intro to C, Part II

Spring 2018

Bases

See this page as a slide show

More on bases

And why are printf and scanf so complicated?

0xdeadbeef
0666
01010101001100001000101001
%d %i %f %u %lf %%%%!

Why oh Why?

I’m thinking of a number…

0644 in octal… okayyyyyy…

What is 0644 in binary?

Hardest way is convert to decimal, then convert to binary.

What is 0644 in octal?

What is 0644 in decimal?

What is 0644 in hexadecimal?

What is 0644 in hexadecimal?

Why doesn’t the easy way work for other conversions?

It’s all about the ratios:

FromToRatioDifficulty
HexadecimalBinary16 = 24Easy
HexadecimalOctal16 = 84/3Not so bad
HexadecimalDecimal16 = 101.20412Hard
DecimalOctal10 = 81.1073Hard
DecimalBinary10 = 23.3219Hard
OctalBinary 8 = 23Easy

Yow!

So, our number is:

110100100 in binary or… 644 in octal or… 420 in decimal or… 1a4 in hexadecimal…

Nothing? Nothing at ALL!?

Nope, not a thing. Try it:

printf("%d\n", 0644);
printf("%d\n", 420);
printf("%d\n", 0x1a4);
420
420
420

Why??

The computer doesn’t care!

How does it know?

What if it is isn’t an int?

MAKE THEM MATCH!!!!

Best case: Bad things happen.

printf("%d",3.14f);
c.c: In function 'main':
c.c:1: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double'
-1610301720

Worst Case: Crash!

So, whats the “&” and scanf?

scanf reads values IN

Lying to scanf

int foo;
scanf("%f",&foo);

What is in foo?

Why not & in printf?

Example

#include <stdio.h>
int main() {
    int fred = 12;
    printf("%d", fred);
    return 0;
}

Example2

#include <stdio.h>
int main() {
    int fred = 12;
    scanf("%d", fred);
    return 0;
}

Example2

#include <stdio.h>
int main() {
    int fred = 12;
    scanf("%d", fred);
    return 0;
}

Example2

#include <stdio.h>
int main() {
    int fred = 12;
    scanf("%d", &fred);
    return 0;
}

What about strings?

int main() {
    char name[6];
    strcpy(name, "Bubba");
    printf("%s\n", name);
    return 0;
}


     name[0]  name[1]  name[2]  name[3]  name[4]  name[5]
    ┌────────┬────────┬────────┬────────┬────────┬────────┐
    │   B    │   u    │   b    │   b    │   a    │   \0   │
    └────────┴────────┴────────┴────────┴────────┴────────┘

What about strings?

int main() {
    char name[6];
    strcpy(name, "Bubba");
    printf("%s\n", name);
    return 0;
}


     name[0]  name[1]  name[2]  name[3]  name[4]  name[5]
    ┌────────┬────────┬────────┬────────┬────────┬────────┐
    │   B    │   u    │   b    │   b    │   a    │   \0   │
    └────────┴────────┴────────┴────────┴────────┴────────┘

What about strings?

int main() {
    char name[6];
    strcpy(name, "Bubba");
    scanf("%s\n", name);
    return 0;
}


     name[0]  name[1]  name[2]  name[3]  name[4]  name[5]
    ┌────────┬────────┬────────┬────────┬────────┬────────┐
    │   B    │   u    │   b    │   b    │   a    │   \0   │
    └────────┴────────┴────────┴────────┴────────┴────────┘

It all sounds so delicate

Let me say that again

c11 -Wall

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2017-12-19T11:20

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building