0% found this document useful (0 votes)
11 views13 pages

C RS

The document provides an overview of computer architecture, programming concepts, and the C programming language. It covers topics such as the CPU, memory types, programming constructs, and common programming errors in C. Additionally, it discusses the structure of C programs, data types, functions, and the use of arrays and strings.

Uploaded by

krishvadher2407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

C RS

The document provides an overview of computer architecture, programming concepts, and the C programming language. It covers topics such as the CPU, memory types, programming constructs, and common programming errors in C. Additionally, it discusses the structure of C programs, data types, functions, and the use of arrays and strings.

Uploaded by

krishvadher2407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

CH-1

The central processing unit is a VLSI circuit

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.

It is not possible to execute a program written in a high level language or


assembly language directly without first translating it.

A high level language uses a compiler for translation,

A compiler is always typical of a programming language.

Before writing a program, the algorithm and/or the flowchart for the same should be
developed.

Algorithms and flowcharts should be independent of programming languages.

Memory is of two types the primary and the secondary memory.

The three main programming constructs are imperative, conditional and iterative
statements.

The CPU has direct connections to the primary memory.

The RAM is a volatile memory.

. Algorithms do not have a fixed style of writing.

The ROM is a non-volatile memory.

The secondary memory serves as a data reservoir

Algorithms should be finite and correct and the statements in it should be


unambiguous.

System softwares are those which interact with the hardware of the computer.

flow Flowcharts give us a visual depiction of logic.

CH-2

C is a structured, high-level, machine independent language


ANSI C and C99 are the standardized verslons of C language

C combines the capabilities of assembly language with the features of a high level
language.

C is robust, portable and structured programming 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.

Every program statement in a C language must end with a semicolon.

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 structure of a C program comprises various sections including Documentation,


Link Definition, Global Declaration, main() funcion and Sub program section.

C is a free-form language and therefore a prope form of indentation of various


sections wou improve legibility of the program.

The execution of a C program involves a series of steps including: creating the


program, compling the program, linking the program with functions from C library
and executing the program.

The command used for running a C programing.

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 use keywords or any system library names for identifiers.

Use meaningful and intelligent variable names.

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.

A character is stored as an integer. It is therefore possible to perform arithmetic


operations on characters.

Do not use lowercase I for long as it is usually confused with the number 1

Constants, Variables and Data Types

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.

Do not combine declarations with executable statements

Do not use semicolon at the end of #define directive.

The character # should be in the first column.

Do not give any space between # and define.

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.

It is illegal to apply modules operator % with anything other than integers.

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 >=

It is an error if the two symbols of the operators 1, and are reversed.

Use spaces on either side of binary operator to improve the readability of the
code.

Do not use increment and decrement operators to floating point variables.

Do not confuse the equality operator == with the assignment operator =.

Use sizeof operator to determine the length arrays and structures where their sizes
are no already known.

Be aware of side effects produced by some expressions.

Avoid any attempt to divide by zero. It is normaly undefined. It will either result
in a fatal error or in incorrect results.

Do not forget a semicolon at the end of an statement.

Do not use a variable in an expression before has been assigned a value.

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.

Understand clearly the precedence of operators in an expression. Use parentheses,


if necessary

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.

Use double quotes for character string constants.


Use single quotes for single character constants.

Do not forget to include <stdio.h> headerfiles when using functions from standard
input/output library.

Provide sufficient field width to handle a value to be printed

Do not forget to include <ctype.h> header file when using functions from character
handling library.

Be aware of the situations where output may be imprecise due to formatting.

Provide proper field specifications for every variable to be read or printed.

Enclose format control strings in double quotes.

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 specify any precision in input field specifications.

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.

Do not use commas in the format string of a scanf statement.

Not separating read and write arguments is an error.

Using an address operator & with a variable in the printf statement will result in
runtime error.

CH-6

Be aware of any side effects in the control expression such as if(x++)

Check the use of operator in place of the equal operator ==

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 forget to place parentheses for the if expression.


It is an error to place a semicolon after the if expression.

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

The second operand may not be evaluated at at

Be aware of dangling else statements.

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

Although it is optional, it is a good programming practice to use the default


clause in a switch statement.

It is an error to use a variable as the value in a case label of a switch


statement. (Only integral constants are allowed.)

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

It is a common error to use wrong relational operator in test expressions. Ensure


that the loop is evaluated exactly the required number of times.

Avoid a common error using in place of = = operator.

Do not compare floating-point values for equality.

When performing an operation on a variable repeatedly in the body of a loop, make


sure that the variable is initialized properly before entering the loop.

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 semicolon at the end of do.....while statement.

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.

Placing a semicolon after the control expression

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 it is legally allowed to place the initialization, testing, and increment


sections outside the header of a for statement, avoid them as far as possible.

Although it is permissible to use arithmetic expressions in initialization and


increment section, be aware of round off and truncation errors during their
evaluation.

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

Avoid the use of goto anywhere in the program.

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".

Supplying more initializers in the initializer list is a compile time error.

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:

for (i=1; i<=5; i++)

for (i = 0; i<=5; i++)

for (i=0; i ==5; i++)

for (i=0; i<4; i++)

Referring a two-dimensional array element like x[i, j] instead of x[i][j] is a


compile time error.

Leaving out the subscript reference operator [] in an assignment operation is


compile time error.

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.

During initialization of multi-dimensional arrays, it is an error to omit the array


size for any dimension other than the first.

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.

Avoid processing single characters as strings.

It is a compile time error to assign a string to a character variable.

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.

Use %s format for printing strings or character arrays terminated by null


character. Using a string variable name on the left of the assignment operator is
illegal. 26011

When accessing individual characters in a string variable, it is logical error to


access outside the array bounds.

Strings cannot be manipulated with operators

Use string functions.

Donotusestringfunctionsonan arraychartypethat is not terminated with the null


character. [L09.4] Do not forget to append the null character to the target string
when the number of characters copied is less than or equal to the source string.

CH-10

A function that returns a value can be used in expressions like any other C
variable.

A function that returns a value cannot be used as a stand-alone statement.

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.

Placing a semicolon at the end of header line is illegal.

Forgetting the semicolon at the end of a prototype declaration is an error.

A return statement can occur anywhere within the body of a function.

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.

If a function has no parameters, the parameter list 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.

Defining a function within the body of another function is not allowed.

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.

Functions return integer value by default.

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

Remember to place a semicolon at the end of definition of structures and unions.

We can declare a structure variable at the time of definition of a structure by


placing it after the closing brace but before the semicolon.

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

We cannot declare a variable at the time of creating a typedef definition. We must


use the type_name to declare a variable in an independent statement.

It is an error to use a structure variable as a member of its own struct type


structure.

• Declaring a variable using the tag name only (without the keyword struct) is an
error.

It is illegal to refer to a structure member using only the member name.

When using scanf for reading values for members, we must use address operator &
with non-string members.

Always provide a structure tag name when creating a structure. It is convenient to


use tag name to declare new structure variables later in the program.

Use short and meaningful structure tag names.

Avoid using same names for members of different structures (although it is not
illegal).

It is an error to compare two structure variables

Assigning a structure of one type to a structure of another type is an error.

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

Only an address of a variable can be stored in a pointer variable.

Do not store the address of a variable of one type into a pointer variable of
another type.

The value of a variable cannot be assigned to a pointer variable.

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.

A pointer variable contains garbage until it is initialized. Therefore, we must not


use a pointer variable before it is assigned, the address of a variable.

It is an error to assign a numeric constant to a pointer variable.

It is an error to assign the address of a variable to a variable of any basic data


types.

A proper understanding of a precedence and associativity rules is very important in


pointer applications. For example, expressions like *p++, "p[ ], (*p)[ ],
(p).member should be carefully used.

If we want a called function to change the value of a variable in the calling


function, we must pass the address of that variable to the called function.

When we pass a parameter by address, the corresponding formal parameter must be a


pointer variable.

It is an error to assign a pointer of one type to a pointer of another type without


a cast (with an exception of void pointer).

CH-13

Do not try to use a file before opening it.

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 open a file for reading when it does not exist.

It is an error to access a file with its name rather than its file pointer.

It is a good practice to close all files before terminating a program.

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.

You might also like