Errors in a program
• are the problems or the faults that occur in
the program, which makes the behavior of the
program abnormal.
• Programming errors are also known as the
bugs or faults, and the process of removing
these bugs is known as debugging.
05/18/2025 2
Errors(CO1)
KCS101 Module 2
1. Syntax Error/ Compile time Errors
2. Runtime Errors
3. Logical Errors
1. Syntax error
• Syntax errors are also known as the compilation errors as they occurred at the
compilation time, or we can say that the syntax errors are thrown by the
compilers. These errors are mainly occurred due to the mistakes while typing or
do not follow the syntax of the specified programming language. These
mistakes are generally made by beginners only because they are new to the
language. These errors can be easily debugged or corrected.
Commonly occurred syntax errors are:
• If we miss the parenthesis (}) while writing the code.
• Displaying the value of a variable without its declaration.
• If we miss the semicolon (;) at the end of the statement.
For example: eg2
1. If we want to declare the variable of
type integer,
• int a; // this is the correct form
• Int a; // this is an incorrect form.
2. Run-time error
Sometimes the errors exist during the execution-time even after the successful
compilation known as run-time errors. When the program is running, and it is not
able to perform the operation is the main cause of the run-time error. The division
by zero, array index out of bounds, string index out of bounds, etc. are the
common example of the run-time error. These errors are very difficult to find, as
the compiler does not point to these errors.
Eg
3. Logical error
The logical error is an error that leads to an undesired output. These errors
produce the incorrect output, but they are error-free, known as logical errors.
These types of mistakes are mainly done by beginners. The occurrence of these
errors mainly depends upon the logical thinking of the developer. If the
programmers sound logically good, then there will be fewer chances of these
errors.
V.v.imp
• Storage clss
int m; // global var
main()
{
int i; //local var
float balance;
function1();
}
function1()
{
int i;
float sum;
}
Storage Class Specifiers
1. Auto
2. Register
3. Static
4. Extern
(added to the variable declaration)
eg.
• The following program illustrates the work of automatic variables.
void test();
void main()
{
test();
test();
test();
}
void test()
{
auto int k=10;
printf(“%dn”‖,k);
k++;
}
Output: 10 10 10
In the above program when the function test() is called for the first time ,variable k is
created and initialized to 10. When the control returns to main(), k is destroyed. When
function test() is called for the second time again k is created , initialized and destroyed
after execution of the function. Hence automatic variables came into existence each
time the function is executed and destroyed when execution of the function completes.
• Now look at the previous program with k is declared as static instead of automatic.
void test();
void main()
{
test();
test();
test();
}
void test()
{
static int k=10;
printf(“%dn”, k);
k++;
}
Output:10 11 12
Here in the above program the output is 10, 11, 12, because if a variable is declared as static
then it is initialized only once and that variable will never be initialized again. Here variable k is
declared as static, so when test() is called for the first time k value is initialized to 10 and its value
is incremented by 1 and becomes 11. Because k is static its value persists. When test() is called
for the second time k is not reinitialized to 10 instead its old value 11 is available. So now 11 is
get printed and it value is incremented by 1 to become 12. Similarly for the third time when test()
is called 12(Old value) is printed and its value becomes 13 when executes the statement ‗k++;‘
• the main difference between automatic and static variables is that static variables are
initialized to zero by default if not initialized but automatic variables contain an
unpredictable value(Garbage Value) if not initialized and static variables does not disappear
when the function is no longer active , their value persists, i.e. if the control comes back to
the same function again the static variables have the same values they had last time.
int main()
{
register int a;
for(a=0;i<50000;i++)
printf(“%dt”‖,a);
return 0;
}
In the above program, variable a was used frequently as a loop
counter so the variable a is defined as a register variable. Register is
a not a command but just a request to the compiler to allocate the
memory for the variable into the register. If free registers are
available than memory will be allocated in to the registers. And if
there are no free registers then memory will be allocated in the
RAM only (Storage is in memory i.e. it will act as automatic variable).
storage classes.pptx programming in C language

storage classes.pptx programming in C language

  • 1.
    Errors in aprogram • are the problems or the faults that occur in the program, which makes the behavior of the program abnormal. • Programming errors are also known as the bugs or faults, and the process of removing these bugs is known as debugging.
  • 2.
    05/18/2025 2 Errors(CO1) KCS101 Module2 1. Syntax Error/ Compile time Errors 2. Runtime Errors 3. Logical Errors
  • 3.
    1. Syntax error •Syntax errors are also known as the compilation errors as they occurred at the compilation time, or we can say that the syntax errors are thrown by the compilers. These errors are mainly occurred due to the mistakes while typing or do not follow the syntax of the specified programming language. These mistakes are generally made by beginners only because they are new to the language. These errors can be easily debugged or corrected. Commonly occurred syntax errors are: • If we miss the parenthesis (}) while writing the code. • Displaying the value of a variable without its declaration. • If we miss the semicolon (;) at the end of the statement. For example: eg2 1. If we want to declare the variable of type integer, • int a; // this is the correct form • Int a; // this is an incorrect form.
  • 4.
    2. Run-time error Sometimesthe errors exist during the execution-time even after the successful compilation known as run-time errors. When the program is running, and it is not able to perform the operation is the main cause of the run-time error. The division by zero, array index out of bounds, string index out of bounds, etc. are the common example of the run-time error. These errors are very difficult to find, as the compiler does not point to these errors. Eg
  • 5.
    3. Logical error Thelogical error is an error that leads to an undesired output. These errors produce the incorrect output, but they are error-free, known as logical errors. These types of mistakes are mainly done by beginners. The occurrence of these errors mainly depends upon the logical thinking of the developer. If the programmers sound logically good, then there will be fewer chances of these errors.
  • 6.
  • 9.
    int m; //global var main() { int i; //local var float balance; function1(); } function1() { int i; float sum; }
  • 10.
    Storage Class Specifiers 1.Auto 2. Register 3. Static 4. Extern (added to the variable declaration) eg.
  • 12.
    • The followingprogram illustrates the work of automatic variables. void test(); void main() { test(); test(); test(); } void test() { auto int k=10; printf(“%dn”‖,k); k++; } Output: 10 10 10 In the above program when the function test() is called for the first time ,variable k is created and initialized to 10. When the control returns to main(), k is destroyed. When function test() is called for the second time again k is created , initialized and destroyed after execution of the function. Hence automatic variables came into existence each time the function is executed and destroyed when execution of the function completes.
  • 16.
    • Now lookat the previous program with k is declared as static instead of automatic. void test(); void main() { test(); test(); test(); } void test() { static int k=10; printf(“%dn”, k); k++; } Output:10 11 12 Here in the above program the output is 10, 11, 12, because if a variable is declared as static then it is initialized only once and that variable will never be initialized again. Here variable k is declared as static, so when test() is called for the first time k value is initialized to 10 and its value is incremented by 1 and becomes 11. Because k is static its value persists. When test() is called for the second time k is not reinitialized to 10 instead its old value 11 is available. So now 11 is get printed and it value is incremented by 1 to become 12. Similarly for the third time when test() is called 12(Old value) is printed and its value becomes 13 when executes the statement ‗k++;‘
  • 17.
    • the maindifference between automatic and static variables is that static variables are initialized to zero by default if not initialized but automatic variables contain an unpredictable value(Garbage Value) if not initialized and static variables does not disappear when the function is no longer active , their value persists, i.e. if the control comes back to the same function again the static variables have the same values they had last time.
  • 19.
    int main() { register inta; for(a=0;i<50000;i++) printf(“%dt”‖,a); return 0; } In the above program, variable a was used frequently as a loop counter so the variable a is defined as a register variable. Register is a not a command but just a request to the compiler to allocate the memory for the variable into the register. If free registers are available than memory will be allocated in to the registers. And if there are no free registers then memory will be allocated in the RAM only (Storage is in memory i.e. it will act as automatic variable).