SlideServe
Menu
Préférences
© SlideServe · v2026

C Primitives - I

traugott-nigel avatar
34 diapositives · 453 Views · Nov 03, 2014

Discover the history, structure, and values of C programming, and explore machine language concepts towards coding efficiency. Learn the basics, syntax, and essentials of C programming in this comprehensive guide. Get insights into compilers, high-level vs. low-level languages, and the significance of C in various applications like home automation and electronics. Unveil the historical journey of C language evolution, its key milestones, and its importance in the tech world.

C Primitives - I - slide 1
01 / 34C Primitives - I
1 / 17

Télécharger la présentation

C Primitives - I
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. C Primitives - I Math 130Lecture # 0101/21/05

  2. Topics • History of C • Structure of a C program • Values and variables • Expressions • Function calls • Comments printf(“Hello World”);

  3. Machine Language 10100110 01110110 00100110 00000000 11111010 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc...

  4. From Algorithms to Programs • Both are sets of instructions on how to do a task • Algorithm: • talking to humans, easy to understand • in plain (English) language • Program: • talking to computer (compiler) • can be regarded as a “formal expression” of an algorithm

  5. 10100110 01110110 00100110 00000000 11111010 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc... High-Level Language #include <stdio.h> int main() { printf(“Hello World”); return 0; } Source code Executable code Compilers and linkers translate a high level program into executable machine code.

  6. Why C? • Structured language • Standard library exists, allowing portability • See Bronson, Appendix • Wide availability on a variety of computers • Home temperature control • VCR control • LCD watch • Low level activities possible • It can produce lean and efficient code • Widely used

  7. History of C • CPL Combined Programming Language (Barron et al., 1963) • BCPL Basic CPL (Richards, 1969) • B (Thompson, 1970) • C K&R C (Ritchie, 1972) • ANSI C American National Standards Institute C (X3J11, 1989) • C99 (JTC1/SC22/WG14, ISO/IEC 9899, 1999)

  8. Basic Structure of a C Program output “Hello World!” Example: Hello World Algorithm: C Program: #include <stdio.h> int main() { printf(“HelloWorld!”); return 0; }

  9. Basic Structure of a C Program (cont) Example: Hello world C Program: #include <stdio.h> int main() { printf(“Hello World!”); return 0; } Includes standard input/output library of procedures. Read: “Pound-include” or “hash-include”

  10. Basic Structure of a C Program Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; } Curly braces mark the beginning and end of a block of instructions.

  11. Basic Structure of a C Program Example: Hello World C Program: Instruction (function call) to output “Hello World” #include <stdio.h> int main() { printf(“Hello World”); return 0; }

  12. Basic Structure of a C Program “Statements” (lines of instructions) always end with a semi-colon (;) Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; }

  13. Print out numbers 0 to 9: set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 int main() { return 0; }

  14. Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 (cont) #include <stdio.h> int main() { return 0; }

  15. Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 (cont) #include <stdio.h> /* Print out numbers 0 to 9 */ int main() { return 0; } Comment

  16. Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 (cont) #include <stdio.h> /* Print out numbers 0 to 9*/ int main() { int count; return 0; } Variable declaration

  17. Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 (cont) #include <stdio.h> /* Print out numbers 0 to 9*/ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; }

  18. Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 (cont) #include <stdio.h> /* Print out numbers 0 to 9*/ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Assignment of a value (right expression) to a variable (left).

  19. Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 (cont) #include <stdio.h> /* Print out numbers 0 to 9*/ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } No semi-colon here!

  20. Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 (cont) #include <stdio.h> /* Print out numbers 0 to 9*/ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; }

  21. Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 (cont) #include <stdio.h> /* Print out numbers 0 to 9*/ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Format string

  22. Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Example -- Count to 10 (cont) #include <stdio.h> /* Print out numbers 0 to 9*/ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; }

  23. Example -- What’s your sign? #include <stdio.h> /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” }

  24. Example -- What’s your sign? (cont) #include <stdio.h> /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” }

  25. Example -- What’s your sign? (cont) #include <stdio.h> /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Find the sign of a number output “Enter a number” input num if (numis less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” }

  26. Example -- What’s your sign? (cont) #include <stdio.h> /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” }

  27. Example -- What’s your sign? (cont) #include <stdio.h> /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Find the sign of a number output “Enter a number” input num if (numis less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” }

  28. Topics • c • Structure of a C program • Values and variables • Expressions • Function calls • Comments

  29. Values and Variables • Basic Types: • Integers • Floating point numbers • Characters • Character Strings

  30. Basic Types: int and float • Integers (int) 0 1 1000 -1 -10 666 • Floating point numbers (float) 1.0 .1 1.0e-1 1e1

  31. Basic Types: char • Characters (char) ’a’ ’z’ ’A’ ’Z’ ’?’ ’@’ ’0’ ’9’ - Special Characters: preceded by\ ’\n’ ’\t’ ’\0’ ’\’’ ’\\’ etc.

  32. Basic Types: character string • Character Strings (a string of char-s) • Examples: • ”Hi there!” • ”Line 1\nLine 2\nLine 3” • ”” • ”\”\””

  33. Summary • History of C • Structure of a C program • Values and variables

  34. Next Lecture: More C Primitives Expressions Precedence Function calls Comments Sources Bronson, ch 2 Preparation