In respectful memory of Dennis Ritchie, the true giant whose shoulders we have been standing on, for decades.
Summarizing chapters as bullet points from the epic "C Programming Language" book. Good for anyone who wants to do a refresher on the C language, they dearly knew very well. Or to do a quick interview prep.
Chapter 1-Introduction
- %[n]d – prints the integer in a space of 'n' characters wide,; if 'n' is not given, just prints the integer in as much space as needed
- %[[a][.b]]f-prints the floating point in space of width 'a' and with 'b' digits after decimal point
- %.0f suppresses the printing of the decimal point and the fraction part, which is zero.
- In C,a text stream is a sequence of characters divided into lines; each line consists of zero or more characters followed by a newline character.
- printf("End:%d",EOF); will print “End:-1”
- '\n' stands for the value of the newline character, which is 10 in ASCII
- white space characters are blank, tab, newline
- When the name of an array is used as an argument, the value passed to the function is the location or address of the beginning of the array - there is no copying of array elements
- Each local variable in a function comes into existence only when the function is called, and disappears when the function is exited. such variables are usually known as automatic variables
- An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it; this states the type of the variable. (the term external variable refers to a global variable)
- The declaration may be an explicit extern statement or may be implicit from context.If the definition of the external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function.Otherwise it is needed.
- If the program is in several source files, and a variable is defined in file1 and used in file2 and file3, then extern declarations are needed in file2 and file3 to connect the occurrences of the variable. The usual practice is to collect extern declarations of variables and functions in a separate file, historically called a header
- For function names and external variables, the number of characters in the name may be less than 31, because external names may be used by assemblers and loaders over which the language has no control
Chapter2:Types,Operators and expressions
- Each compiler is free to choose appropriate sizes for its own hardware, subject only to
int>=16bits
long>=32bits
short<=int
int<=long
- Whether plain chars are signed or unsigned is machine-dependent, but printable characters are always positive.
- A long constant is written with a terminal l (ell) or L.Unsigned constants are written with a terminal u or U, and the suffix ul or UL indicates unsigned long.
- Octal and hexadecimal constants may also be followed by L to make them long and U to make them unsigned: 0XFUL is an unsigned long constant with value 15 decimal.
- an arbitrary byte-sized bit pattern can be specified by '\ooo' where ooo is one to three octal digits (0...7) or by '\xhh' where hh is one or more hexadecimal digits (0...9, a...f, A...F).
- The complete set of escape sequences
a-alert (bell) character
\\-backslash
\b-backspace
\?-question mark
\f-formfeed
\'-single quote
\n-newline
\"-double quote
\r-carriage return
\ooo-octal number
\t-horizontal tab
\xhh-hexadecimal number
\v-vertical tab
- String constants can be concatenated at compile time:
"hello, " "world" is equivalent to "hello, world"
- The standard library function strlen(s) returns the length of its character string argument s, excluding the terminal '\0'
- Even character constants can be assigned to the names in a enum declaration because char constants are internally only numeric values
- Enumerations provide a convenient way to associate constant values with names, an alternative to #define with the advantage that the values can be generated for you
- If the variable in question is not automatic, the initialization is done once only, conceptionally before the program starts executing, and the initializer must be a constant expression.
- External and static variables are initialized to zero by default. Automatic variables for which is no explicit initializer have undefined (i.e., garbage) values.
- The % operator cannot be applied to a float or double.
- , the only automatic conversions are those that convert a ``narrower'' operand into a ``wider'' one without losing information, such as converting an integer into floating point
- atoi() , when provided with a string containing non-digit characters, returns the number represented by the input until the first non-digit character was encountered. Eg:atoi(12d3r) -> 12
- floats in an expression are not automatically converted to double;
- -1L > 1UL because -1L is promoted to unsigned long and thus appears to be a large positive number.
- -1L < 1U, because 1U, which is an unsigned int, is promoted to a signed long.
- Longer integers are converted to shorter ones or to chars by dropping the excess high-order bits. Eg 512 converted to char becomes 0 since the lower byte in 512 is 0
- the cast produces the value of variable in the proper type; variable itself is not altered
- If arguments are declared by a function prototype, as the normally should be, the declaration causes automatic coercion of any arguments when the function is called. Otherwise explicit casting is needed
- the expression ++n increments n before its value is used, while n++ increments n after its value has been used
- The increment and decrement operators can only be applied to variables; applying to an expression is illegal.
- Right shifting an unsigned quantity always fits the vacated bits with zero. Right shifting a signed quantity will fill with bit signs (``arithmetic shift'') on some machines and with 0-bits (``logical shift'') on others.
- Declaring a variable as unsigned makes it work with integers as well as chars , when performing bit wise operations
- For a number c, c&c-1 deletes the rightmost 1 bit. Hence the number of bits set in c can be calculated using (c=c&c-1) != 0
- & is used to mask or set some bits to 0; | is used to set some bits to 1
- | using 0's and & using 1's will yield the same number . Eg: c | 0 = c&1=c; c may be a sequence of 'n' bits
- To extract a 'n' bit field starting from 'pos' from 'c'. Right shift 'c' by 'pos' and & with a mask having n rightmost bits as 1 and rest of the bits 0
- Right Rotation of 'c' (represented using 'b' bits) by 'n' places can be done as follows
- x=c>>n
- y= Number having n rightmost digits of 'c' as its 'n' leftmost digits in the same order
- New c=x|y
- A terniary expression can return any type value as one of its results. The returned result values may also be of different typesOperatorAssociativity() [] -> .left to right! ~ ++ -- + - * (type) sizeofright to left* / %left to right+ -left to right<< >>left to right< <= > >=left to right== !=left to right&left to right^left to right|left to right&&left to right||left to right?:right to left= += -= *= /= %= &= ^= |= <<= >>=right to left,left to right
- Function calls, nested assignment statements, and increment and decrement operators cause ``side effects'' - some variable is changed as a by-product of the evaluation of an expression
- the order in which function arguments and the operands of operators are evaluated is not specified. So varyng results under various compilers
- In unix, while evaluating 'f() <op> g()' , f() is evaluated before g(). arguments are evaluated from right to left.Eg: g(++i,i++) call is resolved into g(i+2,i)To be continued ....
Comments
Post a Comment