CS/COE 1520 pitt.edu/~ach54/cs1520 C C Developed by Dennis - - PowerPoint PPT Presentation

cs coe 1520
SMART_READER_LITE
LIVE PREVIEW

CS/COE 1520 pitt.edu/~ach54/cs1520 C C Developed by Dennis - - PowerPoint PPT Presentation

CS/COE 1520 pitt.edu/~ach54/cs1520 C C Developed by Dennis Ritchie as a language to build utilities for Unix Grew out of Ritchies work to improve the B programming language The first C Compiler was included with Version 2


slide-1
SLIDE 1

CS/COE 1520

pitt.edu/~ach54/cs1520

C

slide-2
SLIDE 2
  • Developed by Dennis Ritchie as a

language to build utilities for Unix

  • Grew out of Ritchie’s work to improve

the B programming language

  • The first C Compiler was included

with Version 2 Unix in 1972

  • By Version 4 Unix, the kernel was

reimplemented in C

  • Current version is C18

C

2

slide-3
SLIDE 3

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

Hello World

3

slide-4
SLIDE 4

#include <stdio.h> #include <stdlib.h> #include <time.h> int main(int argc, char **argv) { char str[25]; srand((unsigned) time(NULL)); unsigned int r = rand() % 100; while(r < 85) { if(r > 75) { printf("%u: so close!\n", r); } else if(r > 45) { printf("%u: Getting there...\n", r); } else { sprintf(str, "%u: Still so far away!\n", r); printf("%s", str); } r = rand() % 100; } printf("OUT!\n"); }

Basic Syntax

4

slide-5
SLIDE 5
  • Unlike JavaScript and Python, C is statically typed
  • However types can be cast from one type to another

Typing

5

slide-6
SLIDE 6
  • char

○ Can be signed or unsigned

  • int

○ Can be short or long ○ Can be signed or unsigned

  • float
  • double

○ Can be long

Basic Types

6

slide-7
SLIDE 7
  • +
  • *
  • /
  • %
  • ++
  • Numerical Operators

7

slide-8
SLIDE 8
  • _Bool data type added in C99
  • 0 is false
  • 1 is true
  • Any other value placed in the variable is stored as 1

Boolean Operators

8

slide-9
SLIDE 9
  • Arrays are a collection of elements of the same type
  • The size of the array is set when it is defined and it cannot be

a variable

○ float grades[50] ○ char hello[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’} ○ int coords[10][10]

  • To find the size of an array, you take the sizeof() the array

divided by the sizeof the data type

○ int helloSize = sizeof(hello)/sizeof(char)

Arrays

9

slide-10
SLIDE 10
  • There is no string data type
  • Strings are stored as char[]

○ char hello[12];

  • Strings literals can be defined using “

○ strcpy(hello, “Hello World!”);

  • String manipulation and examination is done with functions

○ strcpy ○ strcat ○ strlen ○ strcmp ○ ...

Strings

10

slide-11
SLIDE 11
  • Nope

Structures

11

slide-12
SLIDE 12
  • Very nope

Pointers

12

slide-13
SLIDE 13
  • // Single line comment
  • /*

Multiline comment */

Comments

13

slide-14
SLIDE 14
  • <
  • >
  • <=
  • >=
  • ==
  • !=

Comparison Operators

14

slide-15
SLIDE 15
  • &&
  • ||
  • !

Logical Operators

15

slide-16
SLIDE 16
  • =
  • +=
  • =
  • *=
  • /=
  • %=

Assignment Operators

16

slide-17
SLIDE 17

if (r > 75) { //true statements } else if(r > 45) { //else if true } else { //false statements }

Selection Structures

17

switch(x) { case val1: case val2: break; default: }

slide-18
SLIDE 18
  • while (condition) {

}

  • do {

} while (condition)

  • for(counter; condition; update counter) {

}

Loops

18

slide-19
SLIDE 19

int max(int a, int b) { if (a > b) { return a; } return b; } max(2,6);

Functions

19

slide-20
SLIDE 20
  • Prints a formatted string to stdout
  • printf(formatted string, var1, var2…)

○ Formatted string is a string literal with tags that start with % ■ Eg.

  • %s for strings
  • %d or %i for signed integers
  • %u for unsigned integers

○ printf("%u: Getting there...\n", r);

  • Sprintf is like printf except the resulting string is placed in a

variable

○ sprintf(str, "%u: Still so far away!\n", r);

Printf & sprintf

20

slide-21
SLIDE 21
  • There is a lot more to C

○ Structures ○ Pointers ○ Enums ○ Unions ○ Function pointers ○ Inline functions ○ Bitwise operators ○ Inline assembly ○ ...

This was a brief introduction

21