C Style Guide Flashcards
Keywords 1: Variable Definition
a quantity that may be changed according to the mathematical problem
int x;
Keywords 2: Variable Declaration
x = 10;
Keywords 3. Expressions
a combination of operands and operators
A+B;
A*B;
A==B:
Keywords 4. Operators
+, -, *, /, %, ++, –,
Relational Operators: ==, !=, >, > >=, <=,
Logical Operators: &&, ||, !(A &&B)
https://www.tutorialspoint.com/cprogramming/c_operators.htm
Keywords 5. Precedence table
https://www.cs.uic.edu/~i109/Notes/COperatorPrecedenceTable.pdf
operators will be performed in a different order of precedence, according to the precedence table.
Keywords 6. Sequence points
https://learn.microsoft.com/en-us/cpp/c-language/c-sequence-points?view=msvc-170
A point where all of the previous expressions must have been completed before continueing with the following expressions
Keywords 7. bits/bytes/wchar
wide char, meant to store UTF standard characters
bit, 1 and 0.
byte, 8 bits.
Keywords 8. Binary/octal/hexadecimal/decimal
don’t make me do it
Keywords 9. ASCII table
https://www.asciitable.com/
American Standard Code for Information Interchange,
Keywords 10. UTF
Unicode Transformation Format.
UTF-8: 9bit unicode conversion format.
UTF 65 = A
UTF 66 = B
etc
Keywords 11. Escape characters
backslash followed by uh, something.
https://www.ibm.com/docs/en/rdfi/9.6.0?topic=set-escape-sequences
\n creates a new line
\a creates an alert(?)
etc.
Expressions:
; - separator
int a; variable definition
void foo(int); function declaration
if(a > b){/* . . . /}; flow control statement, (a>b is an expression)
return, continue, break, for, do and while; flow control statements
void bar(void){/ . . . /}; function definition
struct definition: X{/ . . . */}
Alias Definition: typedef struct X X_t
Parts of Expressions
Type: int*, char, const, long, etc. including whether it is an l-value or non l-value
(l-value: expression representing a persistent memory location. can be accessed after the expression has been evaluated. & operator can only be used on l-value expressions)
Value: value of the expression
Side effect: something that can happen because of going through the expression at run time, but not needed in calculating the result.
Evaluation process: NOT a side effect.
Examples of Expressions
85
integer literal, type: non l-value int, value: 85
‘a’
character literal. Type: non l-value int
“abc”
String literal. Type: l-value array of 4 characters. value: when converted to a char*, value is the address of the first character
x
Variable, Type: l-value expression of the type x, value: value that x was initialized or assigned to.
-Evaluation process:
Sequence Points
Sequence Point: “A point in the code that guarentees the order of side effects”
Sequence Points: &&, ||, ? and ,
Sequence Point: ;
function call has two sequence points: one before it is called, and one after it returns. (must complete all side-effects preceding the parameter input, and return value.)
Exercise 1: Given the following code snippet, fill in the following:
int a = 3;
int foo();
a. a
a
Type: int(l-value), Value: 3,
Q1 Why are coding conventions important
Q2 What is the difference between a declaration and a definition?
Q3 Where will you find declarations? Definitions?
Q4 What is the difference between unspecified and undefined?
Q5 What does the following code do?
int main()
{
int x = 20, y = 35;
x = y++ + x++;
y = ++y + ++x;
printf(“%d%d”, x, y);
}
Q6 What does the following code do?
a[i] = i++;
Q7 Which operators create expressions with side effects?
Q8 Why is this important?
Q9 Where would you use the comma operator?
Q10 Why are the operators arranged in that specific order in the precedence table?
Q12 Will the following code compile? Will it run? Why?
Q13 Find / create 5 confusing expressions, and confuse your colleagues and mentors.
Q14 For each of these code snippets, is the bahaviour defined? Can you predict the order? Why?
15 Count all the expresisons and sub-expressions in every example.
a-for each expression and sub-expression, identify its type, value, any side effects, evaluation process, and what could go wrong.
b. Compile and run the code snippers. Do they do what you expected?
c- Investigate implicit conversions and promotion rules for the last two snippers: