210 likes | 298 Views
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
E N D
C Language Basic control statementsand data types CS-1030 Dr. Mark L. Hornick
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
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
C selection statements • The C if() statement • Identical to Java! • The C switch() statement • Identical to Java! CS-1030 Dr. Mark L. Hornick
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
C language compatibility CS-280 Dr. Mark L. Hornick
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
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
Java Numeric data types CS-1030 Dr. Mark L. Hornick
CS-1030 Dr. Mark L. Hornick
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
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
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
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
Precedence rules for operators • Same in C/C++ as in Java CS-1030 Dr. Mark L. Hornick
Arithmetic Promotions • Same in both languages CS-1030 Dr. Mark L. Hornick
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
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