8 Critical Types of Errors in C: Resolving Common Coding Mistakes

types of errors in C

Let’s discuss different types of errors in C and it’s causes while handling errors during input and output operations in C.

During input and output operation, errors in C occurs due to missing of

  • Semicolon(;)
  • Comma(,)
  • Quotation marks(“)
  • Incompatible format specifier(%)
  • Spelling mistake and brackets

Other than these Types of Errors in C , the commonly occurring errors during input/output are explained below with their causes:



1) Error: Undefined symbol (types of errors in c)
Cause:
The variable not defined but used in program.

2) Error: Floating point formats not linked (types of errors in c)
Cause:
a) The variable defined as integer or character and accessed float.
b) Format mismatched.

3) Abnormal program termination (types of errors in c)
Cause:
a) Output format is not according to the data types used.
b) Wrong output format.

4) Error: Null pointer assignment (types of errors in c)
Cause:
Variable list is not attached with scanf()

5) Error: Declaration syntax error (types of errors in c)
Cause:
error is syntax or } is missing.

6) Error: Lvalue required generated by compiler (types of errors in c)
Cause:
Cast operator used before parameter.

7) Error: Illegal structure operation (types of errors in c)
Cause:
period(.) or arrow(>) operator is necessary to access the member of structure but not specified.

8) Error: Misplaced else in function main (types of errors in c)
Cause:
else is used without if statement

Escape Sequence: Purpose & Characteristics

  • The escape sequence characters are also called as backslash character constants.
  • These are used for formatting the output.
  • The escape sequence characters are:
    \a audible bell
    \b back space
    \f form feed
    \n new line
    \r carriage return
    \t horizontal tab
    \v vertical tab
    \\ back slash
    \0 null character
    \’ single quote
    \” double quote
    \000 octal number
    \xhh hexadecimal number

Characteristics

  • Although it consists of two characters, it represents single character.
  • Every combination starts with back slash(\)
  • They are non-printing characters.
  • It can also be expressed in terms of octal digits or hexadecimal sequence.
  • Each escape sequence have unique ASCII value.
  • Escape sequence in character constants and string literals are replaced by their equivalent and then adjacent string literals are concatenated.

Trigraph Characters

  • The C character set is contained within seven-bit ASCII.
  • In order to write programs in the reduced set,corresponding single characters replace the occurrences of the trigraph characters.
  • This replacement occurs before any processing.
Trigraph CharacterReplaced by
??=#
??//
??’^
??([
??)]
??!|
??<{
??>}
}}-~

Comments in ‘C’

  • As per google In C, comments are ignored by the compiler and have no impact on the execution of the code.
  • There are two types of comments in C programming language: Single-line comments, denoted by // Multi-line comments, denoted by /* and */
  • Many times in the program, it is necessary to insert some remarks for the programmer.
  • Theses remarks can be inserted as comments.
  • Comments are used to describe the particular statements.
  • Comments in C begins with /* and end with */
  • The comment may be of one or more lines.
  • Compiler does not pay any attention towards what is written in comments.
  • That is, compiler ignores comments from execution.
  • In a program, logical statements must be commented to make program logic more understandable
  • It is a good practice to insert maximum comments in a program.
  • Example:
    /*This is Single Line Comment */
    /* This is
    multi-line comment*/

Commonly used input /output functions in C

Function PurposePrototype in
getc()gets one character from streamstdio.h
getch()gets character from consoleconio.h
getche()gets a character from the console and echoes to screenconio.h
getchar()gets a character from standard inputstdio.h
gets()gets a string from standard input stdio.h
getw()gets an integer from streamstdio.h
fgets()gets a string from a streamstdio.h
putc()output a character to streamstdio.h
putch()output a character to the text window on the screenconio.h
putchar()outputs a character on standard outputstdio.h
putw()output an integer on screenstdio.h
puts()outputs a string to standard outputstdio.h
cputs()writes a string to the text window on the screenconio.h
fputs()outputs a string to a streamstdio.h
fputc()output a character to a streamstdio.h

Output using printf() statement in C

Syntax: int printf(const char *format,…);
Prototype in: stdio.h
Use: to send formatted output to standard output

  • When printf() statement encounter for execution, it formats a variable number of arguments according to the format and sends the output to stdout i.e. monitor.
  • After successful execution, it returns the number of bytes output.
  • The general syntax for specifying the format is
    % [flag] [width] [.precision] type

    Where,
    % specifies the conversion specification which is used to provide data type and size
    [flag] specifies + or –
    [width] specifies allowed width to print value
    [.precision] specifies the number of decimal place type takes following characters
TypeMeaning
d,iSigned decimal integer
oUnsigned octal integer
uUnsigned decimal integer
X, xUnsigned hexadecimal integer
f,e,EFloating point number
cSingle character
sString character
%% character

Example: Consider following variable declarations
int a = 10;
float b = 1234.56789;


Statements and their output is given below:

Statement : printf(“%d”, a);
Output: 10

Statement: printf(“%x”, a);
Output: a

Statement: printf(“%x”, a);
Output: A

Statement: printf(“%0”, a);
Output: 12

Statement: printf(“%05d”, a);
Output: 00010

Statement: printf(“%f”,b);
Output: 12345.678711

Statement: printf(“% .2f”,b);
Output: 12345.65

Statement: printf(“%8 .3f”,b);
Output: 12345.679

Statement: printf(“% .e”,b);
Output: 1.23457e+04

Statement: printf(“% .5e”, b);
Output: 1.2346e+04

Input(reading) using scanf() statement in C

Syntax: int scanf(const char *format, …);
Prototype in: stdio.h
Use: to take formatted input from stdin

  • When scanf() statement encounters, it pauses the execution till user enter required number of values.
  • On execution it returns the number of input fields processed successfully,
  • It processes the input according to the format and places the result in the memory location pointed to by the arguments.
  • The number of values entered may be equal to or more than the arguments of the function.
  • Example:
    scanf(“%d, &a);
    scanf(“%d%f%c”, &a,&b,&c);
    scanf(“%d%s”,roll,name);
    scanf(“%d%f”,&roll,&per);
  • Example:
    printf(“Give 2 numbers: “);
    scanf(“%d%d”,&a,&b);
    Run Time: Give 2 numbers: 25 50
  • Example:
    printf(“Give 2 numbers: “);
    scanf(“%d and %d”, &a,&b);
    Run Time: Give 2 numbers: 25 and 50
  • Example:
    printf(“Give 2 numbers: “);
    scanf(“%d,%d”,&a,&b);
    Run Time: Give 2 numbers: 25, 50

Leave a Reply

Your email address will not be published. Required fields are marked *