Actually, C++ and Java are much the same as C. C was designed in - - PowerPoint PPT Presentation

actually c and java are much the
SMART_READER_LITE
LIVE PREVIEW

Actually, C++ and Java are much the same as C. C was designed in - - PowerPoint PPT Presentation

C is a high level language (HLL) Its syntax is much the same as Java and C++, but no objects. Actually, C++ and Java are much the same as C. C was designed in the 1970s. 1 Java 2 C 3 Why C ? 1. Millions of lines of code have been


slide-1
SLIDE 1

1

C is a high level language (HLL) Its syntax is much the same as Java and C++, but no objects. Actually, C++ and Java are much the same as C. C was designed in the 1970s.

slide-2
SLIDE 2

2

Java

slide-3
SLIDE 3

3

C

slide-4
SLIDE 4

4

Why C ?

  • 1. Millions of lines of code have been

written, so you are going to run into it.

One place you are likely to run into it are the UW-Madison 500-level CS systems classes.

  • 2. It gives a common language to

reference, so the remainder of 354 can base examples on C code (not Java, C++, Ruby, Python, etc.).

  • 3. Any C++ programmer also needs to

know C.

slide-5
SLIDE 5

5

Comments in C code

/* my comment */ comments may span lines

  • pening delimiter

closing delimiter

slide-6
SLIDE 6

6

/* hello world program */ #include <stdio.h> main() { printf("hello, world\n"); }

slide-7
SLIDE 7

7

C’s printf() is like: Java’s System.out.print()

  • r C++’s

cout

slide-8
SLIDE 8

8

Java source code compiler Java “byte codes” Java interpreter (JVM)

slide-9
SLIDE 9

9

high level language source code compiler assembly language source code assembler machine code linking and loading execution

slide-10
SLIDE 10

10

high level language source code C preprocessor C compiler assembly language source code

slide-11
SLIDE 11

11

the C preprocessor does

  • 1. #include
  • 2. macro substitution

for example: #define MAX 100

  • 3. conditional compilation
slide-12
SLIDE 12

12

1.

#include <stdio.h> #include "myheaders.h"

slide-13
SLIDE 13

13

#include <stdio.h>

#define MAX 100 main() { int x; x = 1; while (x <= MAX) { printf("%d\n", x); x++; } }

2.

slide-14
SLIDE 14

14

C's basic types

  • char – An ASCII character. Typically 1

byte.

  • int – A 2's complement integer.

Typically the size of a word on the architecture.

  • float – An IEEE single precision floating

point value. 32 bits.

  • double – An IEEE double precision

floating point value. 64 bits Not listed here: pointer types.

slide-15
SLIDE 15

15

Type qualifiers

If and when you believe you need to use these, look them up:

  • signed
  • unsigned – causes integer arithmetic
  • perations to be ones for unsigned

integers

  • long – relates to size
  • short – relates to size
  • const – do not use this qualifier to

define a constant. What happens when code tries to change a variable declared as const is implementation dependent.

slide-16
SLIDE 16

16

C string

An array of characters, which uses the null character to delimit the end of the string.

'\0' is the null character. All zeros bit

pattern in ASCII. "Hi." "12"

slide-17
SLIDE 17

17

I/O Concept and Implementation

Logical places where input comes from and

  • utput goes to:

stdin stdout stderr

slide-18
SLIDE 18

18

Output

int printf(char *format [, arg1] … ); return value: # characters printed

  • ptional,

as referenced by string

(in the stdio library)

function name string, called the format string

slide-19
SLIDE 19

19

printf("howdy"); howdy

A better way to do this (for security reasons) is puts("howdy");

slide-20
SLIDE 20

20

Within the format string, to reference variable (values): %d for an integer %u for unsigned %c for a character %s for a string Each %character references an argument

slide-21
SLIDE 21

21

int x, y; x = 3; y = 5; printf(" = %d\n", x, y, x + y); %d %d +

3 + 5= 8

^

newline character

slide-22
SLIDE 22

22

#include <stdio.h> main() { int x; x = 354; printf("[%d]\n", x); printf("[%1d]\n", x); printf("[%2d]\n", x); printf("[%3d]\n", x); printf("[%4d]\n", x); printf("[%-4d]\n", x); } OUTPUT [354] [354] [354] [354] [ 354] [354 ]

Optional field width

slide-23
SLIDE 23

23

#include <stdio.h> main() { float x; x = 1234.5678; printf("[%f]\n", x); printf("[%1f]\n", x); printf("[%2f]\n", x); printf("[%20f]\n", x); printf("[%-20f]\n", x); printf("[%1.2f]\n", x); printf("[%-2.3f]\n", x); printf("[%-20.3f]\n", x); } OUTPUT [1234.567749] [1234.567749] [1234.567749] [ 1234.567749] [1234.567749 ] [1234.57] [1234.568] [1234.568 ]