Syntax What the Compiler needs to understand your program 1
Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other C code For example #include <stdio.h> replaces the #include line with the contents of the stdio.h file. 2
White Space Needed to separate tokens e.g. then break are two contiguous keywords, but thenbreak is a single identifier Otherwise, ignored then break is the same as then break is the same as then break Enables programmer to choose formatting preferences 4
Keywords asm auto break case char const continue default do double else enum extern float for fortran goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while 5
Keyword Classification Variable Declaration / Types char, const, double, enum, float, int, long, short, signed, struct, typedef, union, unsigned, void Control Flow break, case, continue, default, do, else, for, if, return, switch, while Advanced auto, extern, goto, sizeof, static Arcane asm, fortran, register, volatile 6
Identifiers Function, parameter, and variable names Must start with a letter or an underscore Underscores usually avoided May not contain white space After the first letter, can be any number, letter, or underscore Identifiers are case sensitive polyarea is different from PolyArea Choose names that are descriptive, and easy to type Be4aTgh9_fr37200aBy is probably not a good choice 7
Comments Anything starting with /* up to the next */ is a comment /* comments may span lines and continue on to the next line */ Comments do NOT nest /* x=3; /* reset x to 3 */ this was a mistake */ ^ syntax error this not declared // causes a comment to the end of the line (white space matters) Use comments to help reader understand what the code does! No need to comment the obvious: a=a+3; // add three to a Comments ignored by compiler 8
Block Comments /* ----------------------------------------------- I like this style of comment because I can add or remove lines from the comment without special comment reformat intervention. Besides, it looks clean. ------------------------------------------------*/ 9
C Statements A statement is a list of tokens that ends with a semi-colon a=a+3; int c=7; int cent_to_far(int c); A C statement may span more than one line in the file int MyLongFunctionNamedFunctionThatTakesLotsOfArguments( int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8); There may be more than one statement on one line in the file int a=5; a=a+6; int b; b=cent_to_far(a); 10
C Blocks A block of statements is a list of statements, surrounded by { and } { - Left curly brace } Right curly brace A block can be used anywhere a statement can be used Blocks of statements can be nested { statement1; } { statement2; statement3; } statement4; Inside Block Outside Block 11
Function Definition Function Signature, Followed by Embodiment Block of statements 12
C File Some stuff up front #include pre-processor directives Function Declarations Global Variable Declarations (more in the future) Function Definitions 13
Statements in Functions Variable Declarations Assignment/ Expression Statements (Lecture 05) Control Flow Statements (Lecture 06?) 14
C Variables A variable is a named piece of data Variables in C have A name (specified by the programmer) A value (may be unassigned/unknown) A location in memory (determined by the compiler) A type (size and interpretation) (more to come scope/ storage class/ etc.) Variables must be declared before they are used! 15
Variable Declaration Statement type name; type : One of the built-in types or a derived type name : Any valid identifier Examples: int age; char First_Initial; float gpa; 16
Variable Concept Age Memory???????????????????????????????????????????????????????????????????????? First_Initial gpa 17
Problem All computer data consists of strings of 0 s and 1 s How does the computer know what the string means? 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 12 12 11 10 9 8 7 6 5 4 3 2 1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Float: -2.75 Unsigned Int: 3,244,371,200 Char: abcdefg Int: -1,070,596,096 18
Solution Tag each piece of data / location in memory with a type Type tells compiler how many bits to use Type tells compiler how to interpret those bits Enables automatic conversion from one type to another Enables compiler to check to make sure data is used correctly 19
Built In Type Number Symbol void Integer Real Char Bit 8 bit 16 bit 32 bit 64 bit float double char char char unsigned char short unsigned short int unsigned int long unsigned long 20
Q: Why four flavors of Integers? A: Allows programmer to choose size Type # Bits 1 Min Max U Max char 8-128 127 255 short 16-32,768 32,767 65,535 int 32 ~-2.15 x 10 9 ~2.15 x 10 9 ~4.3 x 10 9 long 64 ~-9 x 10 18 ~9 x 10 18 ~18.5x10 18 1 Number of bits may be different on different machines! 21
Q: Why signed vs. unsigned? A: Unsigned goes from 0 to 2 x max A: Some data can never go negative e.g. width Wouldn t it be nice if compiler checked for you! Signed is the default (what you get if you ask for int ) Avoid unsigned data can get you into trouble! 22
Q: What is char? char is ambiguous it can be used for small integers, characters, or bit flags It s up to the programmer to determine how to use char data! 23
ASCII Character Coding ASCII American Standard Code for Information Interchange Mapping from numbers 0-255, to characters
Variable Declaration Statement (w/ init.) type name = const; type : One of the built-in types or a derived type name : Any valid identifier const : Constant specification (should be of type type) Examples: int age = 17; char First_Initial = a ; float gpa = 3.985; 25
Constant Number Symbol Integer Real Character String Decimal Octal Hexadecimal Binary 26
Specifying Constant Numbers Decimal: digits 0-9 or underscore; no leading zeroes! e.g. 4 23 0 93 16 934531 23_391 1_973_300_023 Octal : digits 0-7 or underscore; with leading zero! e.g. 0 04 027 0135 020 03441203 055_537 016_547_425_467 Hexadecimal : prefix 0x, digits 0-9 + ABCDEF and underscore e.g. 0x0 0x4 0x17 0x87 0x10 0xE4283 0x5b5f 0x759E_2B37 Binary : prefix 0b, digits 0 and 1 and underscore e.g. 0b0 0b0100 0b00010111 0b10000111 0b00010000 Real : digits 0-9 with decimal point and underscore and e e.g. 4.0 3.1414 6.22e23
Specifying Letters A character constant is a single letter enclosed in SINGLE quotes e.g. a A 3 % Θ A string is a list of letters enclosed in DOUBLE quotes e.g. Professor Bartenstein student id: There is no built-in string type in C strings are arrays of characters Strings don t always act the way we expect them to More later
Local Variables Variable Declaration within function (usually at top) Automatic storage class by default New variable each time function starts Initialized when function starts (if initializer specified) No longer available when function ends 29
Global Variables Very rarely used in this class! Declared outside of all functions usually at top of file Static storage class by default New variable ONLY when program starts Initialized ONLY when program starts (if initializer specified) Available until program ends i.e. available to all functions
Parameters: Special Variables Like Variables, have Name Type Value Location in Memory Created when a function is invoked Initialized with argument value (as specified in the invocation) Destroyed after function returns Implication: changing a parameter value DOES NOT change the argument! 31
Resources Programming in C, Chapter 3 WikiPedia: C Syntax (https://en.wikipedia.org/wiki/c_syntax) WikiPedia: C Data Types (https://en.wikipedia.org/wiki/c_data_types) WikiPedia: Type Theory (https://en.wikipedia.org/wiki/type_theory) 32