cs coe 1520
play

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


  1. CS/COE 1520 pitt.edu/~ach54/cs1520 C

  2. C ● 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 2

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

  4. Basic Syntax #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"); } 4

  5. Typing ● Unlike JavaScript and Python, C is statically typed ● However types can be cast from one type to another 5

  6. Basic Types ● char ○ Can be signed or unsigned ● int ○ Can be short or long ○ Can be signed or unsigned ● float ● double ○ Can be long 6

  7. Numerical Operators ● + ● - ● * ● / ● % ● ++ ● -- 7

  8. Boolean Operators ● _Bool data type added in C99 ● 0 is false ● 1 is true ● Any other value placed in the variable is stored as 1 8

  9. Arrays ● 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) 9

  10. Strings ● 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 ○ ... 10

  11. Structures ● Nope 11

  12. Pointers ● Very nope 12

  13. Comments ● // Single line comment ● /* Multiline comment */ 13

  14. Comparison Operators ● < ● > ● <= ● >= ● == ● != 14

  15. Logical Operators ● && ● || ● ! 15

  16. Assignment Operators ● = ● += ● -= ● *= ● /= ● %= 16

  17. Selection Structures if (r > 75) { switch(x) { //true statements case val1: } else if(r > 45) { case val2: //else if true break; } else { default: //false statements } } 17

  18. Loops ● while (condition) { } ● do { } while (condition) ● for(counter; condition; update counter) { } 18

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

  20. Printf & sprintf ● 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); 20

  21. This was a brief introduction ● There is a lot more to C ○ Structures ○ Pointers ○ Enums ○ Unions ○ Function pointers ○ Inline functions ○ Bitwise operators ○ Inline assembly ○ ... 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend