Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Slide 62Slide 63Slide 64Slide 65Slide 66Slide 67Slide 68Slide 69Slide 70Slide 71Slide 72Slide 73Slide 74Slide 75Slide 76Slide 77Slide 78Slide 79Slide 80Slide 812. C FUNDAMENTALSExample: Printing a Message/* Illustrates comments, strings, and the printf function */#include <stdio.h>int main(void){ printf("To C, or not to C: "); printf("that is the question.\n"); return 0;}Variables• Variables must be declared before they are used.• Variables can be declared one at a time:int height;int length;int width;int volume;or several can be declared at the same time:int height, length, width, volume;Variables• Always initialize variables before using them.Wrong: int height; printf("The height is %d\n", height);Right: int height; height = 5; printf("The height is %d\n", height);• An variable can initialized in its declaration: int height = 5;• Variable names must be legal identifiers.Identifiers• Identifiers may contain letters, digits, and underscores. An identifier must begin with a letter or underscore.• Examples of legal identifiers:times10get_next_char_doneIt’s usually best to avoid identifiers that begin with an underscore.Identifiers• Examples of illegal identifiers:10timesget-next-char• C is case-sensitive: it distinguishes between upper- and lower-case letters in identifiers. For example, the following identifiers are all different:eof eoF eOf eOF Eof EoF EOf EOFKeywordsautobreakcasecharconstcontinuedefaultdodoubleelseenumexternfloatforgotoifinline*intlongregisterrestrict*returnshortsignedsizeofstaticstructswitchtypedefunionunsignedvoidvolatilewhile_Bool*_Complex*_Imaginary**C99 only• The following keywords may not be used as identifiers:Keywords• Keywords (with the exception of _Bool, _Complex, and _Imaginary) and names of library functions (e.g., printf) must be written using only lowercase letters.The General Form of a Simple Program• The simplest C programs have the following form: int main(void) { declarations statements }The word int may be omitted (although it’s good practice to include it). The use of void is also optional.Each declaration and each statement (except for compound statements) must end with a semicolon.Defining Constants• A macro definition has the form#define identifier replacement-list• Macros often represent constants: values that won’t change during program execution:#define N 100• Macro definitions are handled by the C preprocessor. Wherever the identifier appears later in the source file, the preprocessor replaces it by the replacement list.3. FORMATTED INPUT/OUTPUTThe printf Function•The first argument in a call of printf is a string, which may contain both ordinary characters and conversion specifications, which begin with the % symbol.•Ordinary characters are printed as they appear in the string; conversion specifications are matched one-by-one with the remaining arguments in the call of printf:int i, j;float x, y;printf("i = %d, j = %d, x = %f, y = %f\n", i, j, x, y);Conversion SpecificationsA conversion specification has the form %-m.pX• m (optional) specifies the minimum field width.• - (optional) indicates left justification within the field; the default is right justification.• .p (optional) specifies:–Number of digits after the decimal point (for a floating point number)–Minimum number of digits to print (for an integer)Conversion Specifications•X is one of the following letters:d - decimal signed integere - exponential floating pointf - fixed decimal floating pointg - whichever of e or f produces a shorter string•This is not a complete description of conversion specifications.The scanf Function• scanf requires a format string, followed by variables into which input is to be stored. In most cases, each variable must be preceded by the symbol &.• Warning: Forgetting to put the & symbol in front of a variable will have unpredictable — and possibly disastrous — results.How scanf Worksscanf ignores white-space characters (blanks, tabs, and new-line characters) when searching for an input item. If scanf is used in the following way:scanf("%d%d%f%f", &i, &j, &x, &y);the input could be split over several lines:1-20 .3-4.0e3or put on a single line:1 -20 .3 -4.0e3How scanf Works• When scanf encounters a character that can’t be part of the current item, this character is read again during the scanning of the next input item or during the next call of scanf. Using the same call of scanf as above:scanf("%d%d%f%f", &i, &j, &x, &y);If the input is 1-20.3-4.0e3zThen i is assigned the value 1, j is –20, x is .3, and y is –4.0e3.The scanf Format String• A scanf format string may contain ordinary characters in addition to conversion specifications.• A non-white-space character in a format string must match the next input character or scanf terminates without reading further. (The non-matching character can be read by a later call of scanf.) The callscanf("%d/%d/%d", &month, &day, &year);will read5/ 28/ 2002but not5 / 28 / 20024. EXPRESSIONSArithmetic Operators•The arithmetic operators are: + unary plus - unary minus + addition - subtraction * multiplication / division % remainderOperator Precedence•When an expression contains more than one operator, the meaning of the expression may not be immediately clear:Does i + j * k mean (i + j) * k or i + (j * k) ?•In C, this potential ambiguity is resolved by operator precedence.•Precedence of the arithmetic operators:Highest: + - (unary)* / %Lowest: + - (binary)Associativity•Operator precedence rules alone aren’t enough when an expression contains two or more operators at the same level of precedence. The associativity of the operators now comes into play.•The binary arithmetic operators are all left associative (they group from left to right); the unary operators are right associative.•Examples of associativity:i - j - k means (i - j) - ki * j / k means (i * j) / ki - j * i + k


View Full Document

UF CGS 3460 - C FUNDAMENTALS

Download C FUNDAMENTALS
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C FUNDAMENTALS and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C FUNDAMENTALS 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?