COMMON PROGRAMMING ERRORS - POINTERS

COMMON PROGRAMMING ERRORS - POINTERS

COMMON PROGRAMMING ERRORS - POINTERS
  • The asterisk(*) notation used to declare pointer variables does not distribute to all variable names in a declaration. Each pointer must be declared with the * prefixed to the name. Eg: int *x,*y;
  • Dereferencing a pointer that has not been properly initialized or that has not been assigned to point to a specific location in memory is an error. This could cause a fatal execution time error, or it could accidently modify important data and allow the program to run to completion with incorrect result.
  • Being unaware that a  function is expecting pointers as arguments for pass-by-reference and passing arguments by value. Some compilers take the values assuming they're pointers and dereference the values as pointers. At run time, memory-access violation or segmentation faults are often generated. Other compilers catch the mismatch in types between arguments and parameters and generate error messages.
  • Using pointer arithmetic on a pointer that does not refer to an element in an array.
  • Subtracting or comparing two pointers that do not refer to elements in the same array.
  • Running off either end of an array when using pointer arithmetic.
  • Assigning pointer of one type to a pointer of another type if neither is of type void * is a syntax error.
  • Dereferencing a void * pointer is a syntax error.
  • Attempting to modify an array name with pointer arithmetic is a compilation error. 
More Informative Posts:
COMMON PROGRAMMING ERRORS - ARRAY

COMMON PROGRAMMING ERRORS - ARRAY

COMMON PROGRAMMING ERRORS - ARRAY
  • Declaring an array without specifying any value as size of array.
  • Declaring an array taking variable n as size of array. C does not allow a variable length array.
  • Initializing more values than the specified size.
  • Accessing array elements beyond the range limits. 
  • Array elements are to be used from 0 to maxsize-1. 
  • C does not report any error if user tries to access elements beyond this range but some garbage value is printed.
  • Declaring array of type void.
COMMON PROGRAMMING ERROR - FUNCTION

COMMON PROGRAMMING ERROR - FUNCTION

Here is a list of few common programming errors committed by us in function. For complete list of common programming errors visit : Common Programming Errors In C
  • Forget to put a semicolon at the end of prototype or function declaration.
  • Specifying function parameters of the same type as double x,y instead of double x, double y results in a compilation error.
  • Put a semicolon at the end of function header while defining the function.
         For example:
         float division(float a, int b);  // error
        {
              return a/b;
        }
  • Type mismatch error due to difference in the types in function declaration and function definition. The types of parameter may differ.
  • Type mismatch error due to difference in the order of parameters in function declaration and function definition.
  • Type mismatch error due to difference in the number of actual arguments and the number of formal arguments.
  • Defining a function inside another function is a syntax error.
  • Defining a local variable within a function with the same name as formal argument name.
         For example:
         float division(float a, int b)
        {
                  int a; //error defining the same variable
        }
  • Not returning any value when the function return type is not valid.
        For example:
        float division(float a, int b)
       {
              return ;
       }

COMMON PROGRAMMING ERRORS - CONTROL STRUCTURES

COMMON PROGRAMMING ERRORS - CONTROL STRUCTURES

COMMON PROGRAMMING ERRORS - CONTROL STRUCTURES
  1. Inserting semicolons at the end of expression in for loop or while loop or if or switch. 
For example:
for ( a = 4; a <= 10; a++);
{
   printf ( "%d", a );
}

    2.   Not inserting semicolon at the end of expression of while in do-while loop.
    3.   Not including curly braces to include if or loop statements.
    4.   Using an assignment operator in if expression instead of equality operator.
BASIC COMPONENT OF A C PROGRAM

BASIC COMPONENT OF A C PROGRAM

Now lets begin exploring the C programming language. Before we start executing programs in C Turbo C/C++ Compiler we must know some basic component of a C program.

#include - The #include is known as a preprocessor directive and is used to tell the C preprocessor to find the stdio file with extension .h. <stdio.h> stand for standard input output stream header file and contains information for printf, scanf etc.

main() - Exectuion of a a program starts from a main() function. It defines the point from where the execution of the program starts. Anything written between opening curly brace and ending curly brace of main is executed.

printf() - This is the standard way of producing output. The functionality of printf() is referenced in stdio.h by the C compiler, thus it always work in the same way.

scanf() - This is the standard way of taking input from user. The functionality of scanf() is also referenced in stdio.h by the C compiler.

comments: Comments are information given by the program to make a program readable and easy to understand. It reduces the complexity of a program. Anything written as comments is ignored by the compiler.

There are two ways of writing comments:

1. Single line comment

Syntax:
//Your comments here

example:
 // Hello this is my first C program

2. Multiline comment  

Syntax:
/* Your comment
Your comment*/

example: 
/* This is my first C program
and i am very excited about it */


SYNTAX:

Note : It should be noted that some compiler does not include header file "conio.h". It is also not included in compiler of Linux or any other Unix based operating system. So in case using it displays an error remove this and the function associated with it i.e getch().

#include<stdio.h> //This tells the compiler about the input/output functions such as printf(), scanf() 
#include<conio.h> //It is used for getch() function
int  main() // It is the entry point of a program
{ // Progam begins with this curly braces
printf("Congratulation you successfully run your  first program"); // to print in the output screen
getch(); // to hold the output screen
return 0; // tell the OS that the program exited without error
} // Program end with this curly braces

OUTPUT:

Congratulation you successfully run your  first program

EXPLANATION:

When we execute this program (press Ctrl+F9 to execute in Turbo C or F9 in Dev C++).

NOTE: If you want to execute this program in Dev C++ then there is a minor change. Replace void main() with int main() as Dev C++ compiler does not support void as return type for main(). Also replace getch() with return 0.

return 0 means that the program is terminated successfully and the compiler is returning back the control to the computer

Now on execution the program will display "Congratulation you successfully run your  first program" (without double quotes) as output on monitor. Any string you pass within double quotes through printf is sent to console output (monitor) i.e it is displayed as it is on the monitor.

PROGRAM EXAMPLE:

#include<stdio.h>
int main()
{
printf("%d", 5+6); // %d is conversion character for integer
return 0;
}

OUTPUT:

11

EXPLANATION:

The above program contains only one statement to be executed within main() function. As this statement contains 5+6 without quotes, it will add these two numbers and pass the result in integer ( as indicated by conversion character %d ) to console window i.e 11 is sent to the monitor. So in this case the output is 11.

So whenever we use double quotes, compiler just prints the message ignoring what has been include there but when no quotes are used, actual values are processed according to instructions and executed accordingly.
LOGIC BOOSTER AND BRAINSTORMING QUESTION

LOGIC BOOSTER AND BRAINSTORMING QUESTION

LOGIC BOOSTER AND BRAINSTORMING QUESTION

LOGIC BOOSTER

  1. When an operation is to be performed on two values ( may be of different types ), the type of smaller value will automatically be converted into larger one and the result will be in large one's type.
  2. char or short values will get converted into integer ( int ) type if involved in calculation part.
  3. float value is converted into double value:
  • If either operand is double, the other is converted to double and the result is double.
  • Otherwise, if either operand is long, the other is converted to long, and the result is long.
  • Otherwise, if either operand is unsigned, the other is converted to unsigned, and the result is unsigned.
  • Otherwise, the operand must be int.
For example: Suppose
c: char
d: double
i: int
u: unsigned
s: short int
l: long
f: float

 Expressions   Final data type                                               Explanation
       i + d
         double
 One value is of type int and second is of type double, so smaller one  
 (int) will be converted into double first ( without loss of information ) 
 and then the result type of addition of two double is double.
   20 * s +3             int First short will be converted into int. All values are of type int, so
 result is of type int.
    12.0 + c         double First "c" is converted into int then to double. Result will be of
 type double.    
    3 * i / 4            int As 3 and "i" are of typ int, so result of 3*i is of type int, then int
 divided by int is again int.
    3 * i / 4.0         double The result of 3 * i is of type int, then int divided by double is
 double.


  • You must use "&" ( ampersand ) with scanf to enter integer or character value ( while using format specifier "%d" or "%c" or "%f" ). Omitting it will make the program not work correctly.
  • You must not use "&" with printf while printing the values of any variable.
  • If you use comma between format specifiers in scanf, user must have to provide comma between two values to differentiate them.
  • All statements must be terminated with semicolon.
  • C is case sensitive - all keywords and standard library functions are in lowercase.


BRAINSTORMING QUESTIONS

1. How can we display the float number in exponential notation.
  • We can use the format specifier %e to print float value in exponential notation.
2. Is int a; a declaration or definition.
  • The declaration of a variable means how it works but it does not allocate memory to an identifier. Since int a; statement introduces type of "a" and also allocates memory, it becomes definition of variable "a".
3. The following code finds the average of three numbers:

#include<stdio.h>
#include<conio.h> // Linux user - Remove this
int main()
{
int a=10, b=20, c=30, avg;
avg= a+b+c/3;
printf("Average is: %d", avg);
getch(); // Linux user - Remove this
return 0;
}
Does the user get the correct answer?

  • No, because of following reasons:
  • the division operator have higher precedence than the addition operator. Hence expression is evaluated as "avg = a+b+(c/3);"
  • The division operator operates on integer operand. So the result will be in integer. When "c" is divided by 3, only integer part is returned and fraction part is removed.
4. Every statement in C is terminated by a semicolon. Can we write the following codes which the first statement inside main is terminated by comma instead of a semicolon. Will it work?

#include<stdio.h>
#include<conio.h> // Linux user - Remove this
int main()
{
printf("This is C "),
printf("Programming."); 
getch(); // Linux user - Remove this
return 0;
}

  • Yes, the code will work. The code has only one statement containing two expression separated by comma, i.e printf ("This is C ") and printf ("Programming."); As the expression separated by comma are evaluated in left to right order, printf("This is C ") is evaluated followed by printf("Programming."). Therefore, the output is: This is C Programming.
5. Will the following code work. The two expressions are separated by two commas.

#include<stdio.h>
#include<conio.h> 
// Linux user - Remove this
int main()
{
int x=10, , y=32;
printf("%d %d"), x, y);
getch();// Linux user - Remove this
return 0;
}

  • No. The compiler gives compile time error. This is because two comma operators cannot appear consecutively.
COMMON PROGRAMMING ERRORS - FUNDAMENTALS

COMMON PROGRAMMING ERRORS - FUNDAMENTALS

COMMON PROGRAMMING ERRORS - FUNDAMENTALS
  1. Not putting semicolon after C statements like printf() or scanf().
  2. Not including required header files.
  3. Parenthesis not matching in printf() or scanf().
  4. Not including double quotes in printf().
  5. Not putting comma between identifiers.
  6. Including space in identifiers.
  7. using wrong conversion character in scanf() or printf().
  8. Not including address of operator ( & ) in scanf().
  9. Using address of operator ( & ) in printf().
  10. Not giving precedence to expression wherever required like a + b / 2 is different from ( a + b ) / 2.