C RS
C RS
The control unit generates control signals and sends them to the different
components of a computer.
The arithmetic logic unit has many circuits inside it that are capable of
performing various arithmetic and logic operations.
Input devices are used by the user to feed data and other information to the
computer
Output devices are used by the computer to display an output or give the result of
an operation to the user.
The operating system acts as an interface between the user and the hardware.
Before writing a program, the algorithm and/or the flowchart for the same should be
developed.
The three main programming constructs are imperative, conditional and iterative
statements.
System softwares are those which interact with the hardware of the computer.
CH-2
C combines the capabilities of assembly language with the features of a high level
language.
Every C program requires a main() function (Use of more than one main() is
illegal). The place main is where the program execution begins.
The execution of a function begins at the opening brace of the function and ends at
the corresponding closing brace.
C programs are written in lowercase letters. However, uppercase letters are used
for symbolic names and output strings.
All the words in a program line must be separated from each other by at least one
space, or a tab, or a punctuation mark.
All variables must be declared for their types before they are used in the program.
A comment can be inserted almost anywhere a space can appear. Use of appropriate
comments in proper places increases readability and understandability of the
program and helps users in debugging and testing. Remember to match the symbols /
and appropriately.
Compiler directives such as define and include are special instructions to the
compilet to h it compile a program. They do not end with s semicołon.
The sign if of compiler directives must appetu the first column of the line.
We must make sure to include header files using #include directive when the program
refers t special names and functions that it does define.
The command used for running a C program MS-DOS system is file.exe where file is
the name of the program that has already been compiled
When braces are used to group statement make sure that the opening brace has a
corresponding closing brace.
CH-3
Do not use the underscore as the first character of identifiers (or variable names)
because many of the identifiers in the system library start with underscore.
Use only 31 or less characters for identifiers. This helps ensure portability of
programs
Do not create variable names that differ only by one or two letters.
Integer constants, by default, assume int types. To make the numbers long or
unsigned, we must append the letters Land U to them.
Floating point constants default to double. To make them to denote float or long
double, we must append the letters For L to the numbers.
Use single quote for character constants and double quotes for string constants.
Do not use lowercase I for long as it is usually confused with the number 1
scanf: is a predefined standard C. function that reads formatted input from stdin
(standard input) stream.
C does not provide any warning or indication of overflow. It simply gives incorrect
results. Care should be exercised in defining correct data type.
Each variable used must be declared for its type at the beginning of the program or
function.
All variables must be initialized before they are used in the program.
A variable defined before the main function is available to all the functions in
the program.
A variable defined inside a function is local to that function and not available to
other functions.
A variable can be made constant either by using the preprocessor command #define at
the beginning of the program or by declaring it with the qualifier const at the
time of initialization.
CH-4
Use decrement and increment operators carefully. Understand the difference between
postfix and prefix operations before using them
Do not use increment or decrement operators with any expression other than a
variable Identifier.
The result of an expression is converted to the type of the variable on the left of
the assignment before assigning the value to it. Be careful about the loss of
information during the conversion.
It is an error if any space appears between the two symbols of the operators ==, !
=, <= and >=
Use spaces on either side of binary operator to improve the readability of the
code.
Use sizeof operator to determine the length arrays and structures where their sizes
are no already known.
Avoid any attempt to divide by zero. It is normaly undefined. It will either result
in a fatal error or in incorrect results.
Integer division always truncates the decima part of the result. Use it carefully.
Use casting where necessary.
Add parentheses wherever you feel they would help to make the evaluation order
clear.
Associativity is applied when more than one operator of the same precedence are
used in an expression. Understand which operators associate from right to left and
which associate from left to right.
CH-5
While using getchar, care should be exercised to clear any unwanted characters in
the input stream.
Do not forget to include <stdio.h> headerfiles when using functions from standard
input/output library.
Do not forget to include <ctype.h> header file when using functions from character
handling library.
Do not forget to close the format string in the scanf or printf statement with
double quotes.
Using an incorrect conversion code for data type being read or written will result
in runtime error.
Do not forget to use address operator & for basic type variables in the input list
of scanf.
Do not forget the comma after the format string in scanf and printf statements.
Do not provide any white-space at the end of format string of a scanf statement.
Using an address operator & with a variable in the printf statement will result in
runtime error.
CH-6
Do not give any spaces between the two symbols of relational operators = =, !=, >=
and <=,
Writing !=> and <= operators like !, => and < is an error.
Remember to use two ampersands (88) and two bars (11) for logical operators. Use of
single operators will result in logical errors.
Do not use the equal operator to compare two floating-point values. They are seldom
exactly equal.
Avoid using operands that have side effects in a logical binary expression such as
(x--&&++y).
1. Range of Numbers
Use braces to encapsulate the statements in If and else clauses of an if.... else
statement
Do not forget to use a break statement when the cases in a switch statement are
exclusive
Do not use the same constant in two case labels in a switch statement.
Try to use simple logical expressions. Be careful while placing a goto label in a
program as it may lead to an infinite loop condition.
CH-7
Indent the statements in the body of loops properly to enhance readability and
understandability.
Use of blank spaces before and after the loops and terminating remarks are highly
recommended.
Do not forget to place the increment statement in the body of a while or do...while
loop.
Avoid using while and for statements for implementing exit-controlled (post-test)
loops. Use do...while statement. Similarly, do not use do...while for pre-test
loops.
in a while or for statement is not a syntax error [LO 7.3] but it is most likely a
logic error
Using commas rather than semicolon in the header of a for statement is an error.
Do not change the control variable in both the for statement and the body of the
loop. It is a logic error.
Although statements preceding a for and statements in the body can be placed in the
for header, avoid doing so as it makes the program more difficult to read.
The use of break and continue statements in any of the loops is considered
unstructured programming. Try to eliminate the use of these jump statements, as far
as possible
Use the function exit() only when breaking out a program is necessary.
CH-8
We need to specify three things, namely, name, type and size, when we declare an
array.
Use of invalid subscript is one of the common errors. An incorrect or invalid index
may cause unexpected results.
Always remember that subscripts begin at 0 (not 1) and end at size -1.
Defining the size of an array as a symbolic constant makes a program more scalable.
Be aware of the difference between the "kth element" and the "element k". The kth
element has a subscript k-1, whereas the element k has a subscript of k itself.
Do not forget to initialize the elements; otherwise they will contain "garbage".
When using expressions for subscripts, make sure that their results do not go
outside the permissible range of 0 to size-1. Referring to an element outside the
array bounds is an error.
eliminate "off-by-one" errors. For example, for an array of size 5, the following
for statements
are wrong:
When initializing character arrays, provide enough space for the terminating null
character.
Make sure that the subscript variables have been properly initialized before they
are used.
While using static arrays, choose the array size in such a way that the memory
space is efficiently utilized and there is no overflow condition.
When using control structures for looping through an array, use proper relational
expressions
CH-9
Character constants are enclosed in single quotes and string constants are enclosed
in double quotes.
Allocate sufficient space in a character array to hold the null character at the
end.
The header file <stdio.h> is required when using standard I/O functions.
The header file <stdlib.h> is required when using general utility functions.
Using the address operator & with a string variable in the scanf function call is
an error.
CH-10
A function that returns a value can be used in expressions like any other C
variable.
Where more functions are used, they may be placed in any order.
It is a syntax error if the types in the declaration and function definition do not
match.
It is a syntax error if the number of actual parameters in the function call do not
match the number in the declaration statement.
It is a logic error if the parameters in the function call are placed in the wrong
order.
A function definition may be placed either after or before the main function.
A return statement is required if the return type is anything other than void.
If a function does not return any value, the return type must be declared void.
Using void as return type when the function is expected to return a value is an
error.
Trying to return a value when the function type is marked void is an error.
It is an error if the type of data returned does not match the return type of the
function.
It will most likely result in logic error if there is a mismatch in data types
between the actual and formal arguments.
A function without a return statement cannot return a value, when the parameters
are passed by value.
When the value returned is assigned to a variable, the value will be converted to
the type of the variable receiving it.
CH-11
Do not place the structure tag name after the dosing brace in the definition. That
will be treated as a structure variable. The tag name must be placed before the
opening brace but after the keyword struct.
When we use typedef definition, the type_name comes after the closing brace but
before the semicolon
• Declaring a variable using the tag name only (without the keyword struct) is an
error.
When using scanf for reading values for members, we must use address operator &
with non-string members.
Avoid using same names for members of different structures (although it is not
illegal).
When accessing a member with a pointer and dot notation, parentheses are required
around the pointer, like (*ptr).number.
The selection operator (->) is a single token. Any space between the symbols - and
> is an error.
Forgetting to include the array subscript when referring to individual structures
of an array of structures is an error
CH-12
Do not store the address of a variable of one type into a pointer variable of
another type.
A very common error is to use (or not to use) the address operator (&) and the
indirection operator (*) in certain places. Be careful. The compiler may not warn
such mistakes.
Remember that the definition for a pointer variable allocates memory only for the
pointer variable, not for the variable to which it is pointing.
CH-13
Remember, when an existing file is open using wmode, the contents of file are
deleted.
When a file is used for both reading and writing, we must open it in 'w+' mode.
It is an error to omit the file pointer when file function.
It is an error to access a file with its name rather than its file pointer.
EOF is integer type with a value -1. Therefore, we must use an integer variable to
test EOF.
It is an error to try to read from a file that is in write mode and vice versa.
To avoid I/O errors while working with files, it is a good practice to include
error handling code in programs by using functions such as feof and ferror
It is an error to attempt to place the file marker before the first byte of a file.
It is a good practice to check the return value of fseek function every time it is
invoked. A positive return value ensures that the file pointer is within the file
boundaries.