1 / 18

C Language

C Language. Basic control statements and data types. Reuse a lot of what you already know from Java. Java was designed to “feel familiar” to an experienced C/C++ developer Most of the control statements and primitive numeric types are very similar

tena
Download Presentation

C Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Language Basic control statementsand data types CS-1030 Dr. Mark L. Hornick

  2. Reuse a lot of what you already know from Java • Java was designed to “feel familiar” to an experienced C/C++ developer • Most of the control statements and primitive numeric types are very similar • “Improvements” over the C++ language were introduced as warranted CS-1030 Dr. Mark L. Hornick

  3. C iteration statements • The C for() statement • Identical to Java! • The C while() statement • Identical to Java! • The C do() statement • Identical to Java! CS-1030 Dr. Mark L. Hornick

  4. C selection statements • The C if() statement • Identical to Java! • The C switch() statement • Identical to Java! CS-1030 Dr. Mark L. Hornick

  5. Wait… • I lied a little bit • switch(x) can use long datatype for x • for() expressions can be more complex than those of Java • Java: for(int i=0; i<5; i++ )… • C: for(int i=0, int j=0; i<5; i++,j++ )… CS-1030 Dr. Mark L. Hornick

  6. C language compatibility CS-280 Dr. Mark L. Hornick

  7. How to get an “F” • Use the C/C++ “goto” statement in your programs • It’s required usage is so rare that you may never encounter it • Most cases of usage can be restructured to use switch or nested if CS-1030 Dr. Mark L. Hornick

  8. Numeric Variables represent numeric values int x; // a integer variable named ‘x’ int y; // and one named ‘y’ Or int x1, y1; // multiple variables created//within one statement; each variable //separated by a comma CS-1030 Dr. Mark L. Hornick

  9. Java Numeric data types CS-1030 Dr. Mark L. Hornick

  10. CS-1030 Dr. Mark L. Hornick

  11. Compiler-specific datatypes(standard datatypes for C99) int8_t signed 8-bit integer uint8_t unsigned 8-bit integer int16_t signed 16-bit integer uint16_t unsigned 16-bit integer int32_t signed 32-bit integer uint32_t unsigned 32-bit integer CS-1030 Dr. Mark L. Hornick

  12. Initialization of Numeric Variables • When a declaration, such as int x, y; is made, memory locations to store data values for xand y are allocated. • But the variables are NOT initialized to any value • UNLESS, at the time the variable is declared, it is also explicitly initialized: int count = 10, height = 34; • Exception: Some debuggers assign a specific value to variables not explicitly initialized CS-1030 Dr. Mark L. Hornick

  13. Other C99/C++ primitive data types • bool • Represents logical true or false • In Java, the equivalent type is boolean • char • holds a single character • In Java, chars are Unicode (multi-byte) • In C++, chars are single-byte • Must use other types for wide characters in C++; i.e. wchar CS-1030 Dr. Mark L. Hornick

  14. No string class; only char arrays • The string class only exists for C++, not for C (there were no classes in C) • In C, character strings can only be handled as arrays of characters, e.g. char someText[] = “Hello”; • This is referred to as a “C-style string” CS-1030 Dr. Mark L. Hornick

  15. Precedence rules for operators • Same in C/C++ as in Java CS-1030 Dr. Mark L. Hornick

  16. Arithmetic Promotions • Same in both languages CS-1030 Dr. Mark L. Hornick

  17. Type modifiers • extern – specifies that the variable/function is defined in another compilation unit • static – retains value between function calls • register – ask the compiler to use a register to hold the value • auto – let the compiler decide whether to use • The default CS-280 Dr. Mark L. Hornick

  18. Pre-processor directives • C compilers always have a pre-processor that makes a first pass through the source file • Pre-processor directives are similar to assembler directives in spirit • #define – equates symbols with values • #if, #ifdef, #else, #endif – conditional compilation CS-280 Dr. Mark L. Hornick

More Related