Some important points in C Programming
Here I am writing some points about C programming which I acquired during my learning about programming. These points may help you to get more from the C language, and increase the curiosity to explore it wider.
1) You can not use return keyword in ternary operators(?,:)
2) You can't left the bracket empty of while and for loops
like while() and for();
3) if i=5; then i=i+(6,7,8,2,9); will assign i=5+9;Because
of ',' operator association.
4) You can only apply the register specifier to local
variables and to the formal parameters in a function.
5) If You declared too many register variables then
compiler automatically transforms register variables
into non register variables when the limit is reached.
6) You can't obtain the address of a register variable
by using the & operator.
7) Register variable only for int and char type.It may
support other types(not in Dev-C++5.3.0.4) but significant
effect can be obtain only with integer and character types.
8) (L,U) for int,(L,F) for real are suffixes for constants
in C. You can use lowercase also (l,u,f) in place of
uppercase.
9) (0x,0X,0) for (hex-lower,hex-upper,octa) are prefixes
for constants in C.
10) '-' unary operator: expression -a is same as (-1)*a.
11) Operators on the same level of precedence are evaluated
by the compiler from left to right.
12) Bitwise operation only work with the standard char and
int data types and variants.
13) Using sizeof operator with type You must have to use
parentheses while with variable it is not necessary.
int a; sizeof a;//Valid sizeof int;//Not Valid
14) sizeof is evaluated at compile time and the value
returned by sizeof is of type size_t. size_t corresponds
loosely to an unsigned integer.
15) Comma(,) Operator let a statement a=(ex1,ex2,ex3);
Evaluation of this statement will be like following.
a=ex1;
a=ex2;
a=ex3;
finally value of ex3 will asign to a.
16) All operators, except the unary operators and '?'
associate from left to right.
17) Casts (type) are technically unary operators.
18) Redundant or additional parentheses do not cause errors
or slow down the execution of an expression.
19) The rand( ) function requires the header <stdlib.h>
20) C89 specifies that at least 15 levels of nesting must
be supported by the compiler. C99 raises this limit to 127.
21) 0?a=5:b=7; give error lvalue reqired but 0?a=5:(b=7);
does not give any error.
22) C89 specifies that a switch can have at most 257 case
statements. C99 supports at most 1,023 case statements.
23) switch only work with int and char. The cases must not
be redundent and variable, they must be unique constants.
24) In switch block the statement just after
'{' and before a 'case' never will execute.
25) Label of goto is any valid label either before
or after goto.
26) kbhit() is a function defined in <stdio.h>
which return 0 until a key pressed after pressing a
key it returns non-zero.
27) void exit(int return_code) defined in <stdlib.h>
28) C performs no bounds checking on arrays.
29) statement int* a,b,c; only declare a as integer pointer
not all b and c, it is like int *a,b,c;
30) "%p" in printf() print the address of pointer taken.
explicit cast is required to convert to or from a
31) A void * pointer is called a generic pointer.No
void * pointer.(In C++ an explicit type cast is needed.)
32) Pointer operations are performed relative to the base
type of the pointer. While it is technically permissible
for a pointer to point to some other type of object,
the pointer will still "think" that it is pointing to
an object of its base type.
33) pointer operations are governed by the type of the
pointer, not the type of the object being pointed to.
34) You can only compare and substract operation
on pointers.
35) Pointer arithmetic can be faster than the standard
array-indexing notation.
36) In a function prototype declaration type *a and
type a[] are same.
37) Multiple indirection known as pointers to pointers.
38) Global and static local pointers are automatically
initialized to null.if you want to initialize a pointer
to null declare like this int *p=0;
39) Null is used because C guarantees that no object will
exist at the null address.If you try to operate or access
the value at null address it will give segmentation fault.
40) char *s="Hello"; works while int *a={3,4,5}; not.
C compiler handle string constants by creating a string
table and storing them in to that table. In which a
character pointer can directly point a string constant.
41) char *s="Hello"; then printf(s);
is same as printf("%s",s);
42) Function pointer: A function has a physical location
in memory that can be assigned to a pointer.You obtain
the address of a function by using the function's name
without any parentheses or arguments like arrays.
43) Declare a function: int (*fun)(int a,int b); For the
declartion (*fun) style is necessary while calling the
function in either style
(*fun)() or fun() but (*fun) style is prefrred.
44) It is necessary to initialize a pointer before going
to use it otherwise it may cause a very serious problem.
45) You cannot use goto to jump into the middle of another
function.You cannot define a function within a function.
This is why C is not technically a block-structured
language.
46) Each command line argument must be separated by
a space or a tab. Commas, semicolons, and the like are
not considered separators.
47) Recursion is the process of defining something
in terms of itself, and is sometimes called circular
definition.
48) The only function that does not require a prototype
is main( ) because it is the first function called
when your program begins.
49) In C89 you can use prototype int fun();
instead of int fun(type1,type2,.....);
50) For variable number of arguments you must end the
declaration of its parameters using three periods like
int fun(int a,..);
Any function that uses a variable number of parameters
must have at least one actual parameter.
51) inline is only a request to the compiler, and can be
ignored.
52) The C language gives you five ways to create a custom
data type structure,union,bit-field, enumeration
and typedef.
53) A structure { struct tag{int a;} struct-variables; }
Where either tag or structure-variables may be omitted,
but not both.
54) When we initialize a block data structure and the
given initialize values are less than totla fields then
remaining fields initialize withe zero automatically.
55) To pass a structure to a function using call by
reference is efficient and recommended because using
pointer, only address need to push in stack while passing
vlaue of structre is big overhead to push the whole
structure in stack.
56) unsigned a=5; signed b=3;is valid and considerd as int.
57) A bit-field must be a member of a structure or union.
The type of a bitfield must be char,int, signed,
or unsigned.
58) You cannot take the address of a bit-field. Bit-fields
cannot be arrayed.
59) You do not have to name each bit-field. This makes it
easy to reach the bit you want, bypassing unused ones.
unsigned : 4; is a valid statement.
60) In enum you can specify the value of one or more of the
symbols by using an initializer,by following the symbol
with an equal sign and an integer value. Symbols that
appear after an initializer are assigned values greater
than the preceding value.
61) You can use enum in switch-case control.
62) In C,you must precede a tag with the keyword struct,
union,or enum when declaring objects. In C++, you don't
need the keyword.
63) The size of a structure may be equal to or greater
than the sum of the sizes of its members.
64) Using typedef The new name you define is in addition
to, not a replacement for, the existing type name.
65) getchar( ) returns EOF if an error occurs.The EOF
macro is defined in <stdio.h> and is
often equal to -1
66) getch() and getche() are not defined by Standard C,the
prototypes for these functions may found in
the <conio.h>
67) getch() does not echo the character to the screen while
getche() does.
68) gets() return address where it store the string.
69) Format specifier %a-Hexadecimal output in the form
0xh.hhhhp+d (C99 only).%i same as %d.%g-Uses %e or %f,
whichever is shorter and truncate unnecessary zeroes.
%n-something bhaukali i can't realize yet but use to
perform dynamic formatting and The associated argument
must be a pointer to an integer. This specifier causes
the number of characters written
(up to the point at which the %n is encountered) to be
stored in that integer.
70) Format Modifiers-goes between the
percent sign and the format code.
I-Minimum Field Width- "%5d" print in atleast 5 char
width and default padding is space otherwise
you can use 0 as "%05d".
II-Precision Specifier-"%.5" with float represent digit
after decimal,with string represent maximum char to be
printed in case of larger string end character will be
truncated,with int represent minimum digit.
III-By default, all output is right justified but you
can reverse it by putting '-'
just after '%' like "%-10d".
IV- l for long and h for short. lc and ls can be used
for wide char and string.L forlong double.
V- # add prefix to constant and * replace with
arguments like ("%x %#x\n", 10, 10);
and("%*.*f", 10, 4, 1234.34);
71) C99 also includes %a, which reads
a floating-point number.
72) scanf("%d%s",&a,s); if we give inpt 12jlh it will parse
into 12 and "jlh" and scanf("%d",&a);if we give inpt 12jlh
it will asign 12 to a and ignore rest of it.
73) Scanset- is a RegExp within [] reads untill char
is found in RE. scanf("%d%[abcdefg]%s", &i, str, str2);
for 123abcdxzx.
74) Format Modifiers for scanf-
I- maximum field length-"%5s" truncate end characters
if larger than 5 and truncated char store in next scanf.
II- Suppressing Input * read a field but not to assign
anyone.scanf("%d%*c%d", &x, &y); for input "10,12" then
',' will not assign to anyone.
75) C I/O system provides a level of abstraction between
the programmer and the device. This abstraction is
called a stream, and the actual device is called a file.
76) FOPEN_MAX defines an integer value that determines
the number of files that may be open at any one time.
77) r+ will not create a file if it does not exist;
however, w+ will. Further, if the file already exists,
opening it with w+ destroys its contents;
opening it with r+ does not.
78) The most I/O functions returns EOF if an error occurs.
79) putc() and fputc() are same, both exist only for
compatibility concerned. putc() implemented as macro
and recommended. Same with getc() and fgetc().
80) Use of feof() is recommended over checking EOF to
return of getc().
81) fgets() reads a string until either a newline character
is read or length–1 characters have been read.
If a newline is read, it will be part of the string
unlike the gets() function.
82) The rewind( ) function resets the file position
indicator to the beginning of the file.
83) fwrite() use to write block of data, which is not
human readble in notepad.
84) You should use random access only on binary files.
Open text in binary mode.
85) fprintf( ) and fscanf( ) are the easiest way to write
and read assorted data,they are not always the most
efficient.
86) In C when a program starts execution, three streams
are opened automatically stdin, stdout and stderr.
87) the console I/O functions are simply special
versions of their parallel file functions.
88) Using freopen( ) to Redirect the Standard Streams.
freopen("OUTPUT", "w", stdout);
89) All preprocessor directives begin with a # sign.
Each preprocessing directive must be on its own line
and not followed by semicolon.
90) A macro may be used as part of the definition
of other macro names.
91) No text substitutions occur if the macro identifier is
within a quoted.
92) You may continue the line by placing a backslash '\' at
the end of the line.
93) The #error directive forces the compiler to
stop compilation.
#error error-message (error-message should not be in "").
94) for #include< > the file is searched for
in a manner defined by the creator of the compiler
and "" looked for in another implementation-defined
manner.
95) Conditional Compilation Directives #if, #else, #elif,
and #endif.The expression that follows the #if is
evaluated at compile time.
Therefore, it must contain only previously
defined identifiers and constants.
96) C89 states that #ifs and #elifs may be nested at least
8 levels. C99 states that at least 63 levels of
nesting be allowed.
97) #ifdef and #ifndef (if defined and if not defined) for
conditional compilation of macros.
98) Both #ifdef and #ifndef may use an #else or
#elif and #endif statement.
99) The #undef directive removes a previously
defined definition.#undef macro-name.
100) defined is a unary operator which operate on macros and
returns if macro is defined.#if defined DEBUG
) The #line directive changes the contents of
_ _LINE_ _ (contain line number) and
_ _FILE_ _(contain name of sourcefile).
#line number "filename"
101) #pragma is an implementation-defined directive that
allows various instructions to be given to the compiler.
102) The # operator called the stringize operator,
turns the argument it precedes into a quoted string.
103) The ## operator, called the pasting operator,
concatenates two tokens. a ## b results ab.
104) There must be no spaces between the
asterisk and the slash /* comment */.
Multiline comments may not be nested.
A single-line comment can be nested within
a multiline comment.
105) Comments may be placed anywhere in a program,
as long as they do not appear in the middle of a
keyword or identifier.
x=10+/* add the numbers */5; is valid.
106) Whenever sizeof use with variable length array the
sizeof evaluate at runtime instead of compile time.
107) You can use Type Qualifiers in an Array Declaration
in C99 but only in function declartion.
108) Variable Argument Lists for macro:
#define MyMax(. . .) max(__VA_ARGS__)
109) C99 allows you to specify an unsized array as the
last member of a structure.The structure must have
at least one other member prior to the
flexible array member.
110) Designated Initializers:
int a[10] = { [0] = 100, [3] = 200 };
for struct and union use .member-name instead of indexs.
111) C99 defines __func__ which specifies the
name (as a string literal) of the function
in which __func__ occurs.
112) Max no. of arguments in function calls:
31 in C89 and 127 in C99.
113) The output of the compiler is an object file,
and the output of the linker is an executable file.
114) The C language is a context free language,
not (context,sensitve or regular).
115) C language has 6 tokens:
identifiers,keywords,constants,string literal,operator
and seprators.
116) Activation record is a data structure that
composes a call stack.
117) If there are statements like [ int *a; int *b=a; ]
then in second statement the address of
'a' assigned to 'b'.
For example Declartion {int* a;} is more
coperhensive than {int *a}.
118) [int a; int *p=&a;] works while[int a; int *p; *p=&a;]
gives segmentation fault.
119) Like [int a=5;] you can't use
[int *a=5;//Segmentaion fault] Because in this line
*a require a address and you gave a constant.
120) Comparison operator is faster than divison operator.
121) In math.h for natural log we use double log(double x);
and log2 and log10 for base-2 & base-10 respectively.
122) In command line argument the arguments argc and argv
have 'c' as count and 'v' as vecotr.
123) ungetc(char,FILE*) used to put a charcter into stream.
124) Functions are decleared within header file.That is
function prototype exist in header file,not the function
bodies.They are defined in library(lib).
125) For dynamic memory allocation there are four functions
decleared in stdlib.h and prototype of each function are
below.
1)void *calloc(size_t num, size_t size); it sets the
value zero.
2)void *malloc(size_t size); it contains grabage value.
3)void *realloc(void *ptr, size_t size); It reallocates
with different size on the given address.
4)void free(void *ptr); It delets the allocated memory.
Note:-If you find any discrepancy in concept or typing error. Please suggest in the comment.
Big billion day wale din big Billion post. :)
ReplyDeleteRat k 12 bazne aur big billion day start hone k intzar me likha gaya tha ye
Delete