C Programming Session 6
C Programming Session 6
Static is a keyword used in C programming language. It can be used with both variables
and functions, i.e., we can declare a static variable and static function as well. An
ordinary variable is limited to the scope in which it is defined, while the scope of the
static variable is throughout the program.
o Static function
When a function is declared with a static keyword known as a static function. Its
lifetime is throughout the program.
o Static method
The member function of a class declared with a static keyword is known as a
static method. It is accessible by all the instances of a class, not with a specific
instance.
In the above code, the func() function is called. In func(), count variable gets updated.
As soon as the function completes its execution, the memory of the count variable will
be removed. If we do not want to remove the count from memory, then we need to use
the count variable as static. If we declare the variable as static, then the variable will
not be removed from the memory even when the function completes its execution.
Output
1
1
Static variable
A static variable is a variable that persists its value across the various function calls.
Syntax
1. #include <stdio.h>
2. int main()
3. {
4. printf("%d",func());
5. printf("\n%d",func());
6.
7. return 0;
8. }
9. int func()
10. {
11. static int count=0;
12. count++;
13. return count;
14. }
In the above code, we have declared the count variable as static. When the func() is
called, the value of count gets updated to 1, and during the next function call, the value
of the count variable becomes 2. Therefore, we can say that the value of the static
variable persists within the function call.
Output
1
2
Static Function
As we know that non-static functions are global by default means that the function can
be accessed outside the file also, but if we declare the function as static, then it limits
the function scope. The static function can be accessed within a file only.
Static variables are limited to the source file in which they are defined, i.e., they are
not accessible by the other source files.
Both the static and global variables have static initialization. Here, static initialization
means if we do not assign any value to the variable then by default, 0 value will be
assigned to the variable.
If the variable declared with a static keyword outside the function, then it is known as a
static global variable. It is accessible throughout the program.
The variable with a static keyword is declared inside a function is known as a static local
variable. The scope of the static local variable will be the same as the automatic local
variables, but its memory will be available throughout the program execution. When the
function modifies the value of the static local variable during one function call, then it
will remain the same even during the next function call.
o Its memory is available throughout the program, but the scope will remain the
same as the automatic local variables. Its
o If we do not assign any value to the variable, then the default value will be 0.
o A global static variable cannot be accessed outside the program, while a global
variable can be accessed by other source files.
Programming Errors in C
Errors are the problems or the faults that occur in the program, which makes the
behavior of the program abnormal, and experienced developers can also make these
faults. Programming errors are also known as the bugs or faults, and the process of
removing these bugs is known as debugging.
These errors are detected either during the time of compilation or execution. Thus, the
errors must be removed from the program for the successful execution of the program.
There are mainly five types of errors exist in C programming:
o Syntax error
o Run-time error
o Linker error
o Logical error
o Semantic error
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.
For example:
1. #include <stdio.h>
2. int main()
3. {
4. a = 10;
5. printf("The value of a is : %d", a);
6. return 0;
7. }
Output
In the above output, we observe that the code throws the error that 'a' is undeclared.
This error is nothing but the syntax error only.
There can be another possibility in which the syntax error can exist, i.e., if we make
mistakes in the basic construct. Let's understand this scenario through an example.
1. #include <stdio.h>
2. int main()
3. {
4. int a=2;
5. if(.) // syntax error
6.
7. printf("a is greater than 1");
8. return 0;
9. }
In the above code, we put the (.) instead of condition in 'if', so this generates the
syntax error as shown in the below screenshot.
Output
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 is
the common example of the run-time error. These errors are very difficult to find, as
the compiler does not point to these errors.
#include <stdio.h>
int main()
{
int a=2;
int b=2/0;
printf("The value of b is : %d", b);
return 0;
}
Output
In the above output, we observe that the code shows the run-time error, i.e., division
by zero.
Linker error
Linker errors are mainly generated when the executable file of the program is not
created. This can be happened either due to the wrong function prototyping or usage of
the wrong header file. For example, the main.c file contains the sub() function whose
declaration and definition is done in some other file such as func.c. During the
compilation, the compiler finds the sub() function in func.c file, so it generates two
object files, i.e., main.o and func.o. At the execution time, if the definition
of sub() function is not found in the func.o file, then the linker error will be thrown.
The most common linker error that occurs is that we use Main() instead of main().
1. #include <stdio.h>
2. int Main()
3. {
4. int a=78;
5. printf("The value of a is : %d", a);
6. return 0;
7. }
Output
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.
1. #include <stdio.h>
2. int main()
3. {
4. int sum=0; // variable initialization
5. int k=1;
6. for(int i=1;i<=10;i++); // logical error, as we put the semicolon after loop
7. {
8. sum=sum+k;
9. k++;
10. }
11. printf("The value of sum is %d", sum);
12. return 0;
13. }
Output
In the above code, we are trying to print the sum of 10 digits, but we got the wrong
output as we put the semicolon (;) after the for loop, so the inner statements of the for
loop will not execute. This produces the wrong output.
Semantic error
Semantic errors are the errors that occurred when the statements are not
understandable by the compiler.
The following can be the cases for the semantic error:
o Type compatibility
int b = "javatpoint";
o Errors in expressions
int a, b, c;
a+b = c;
1. #include <stdio.h>
2. int main()
3. {
4. int a,b,c;
5. a=2;
6. b=3;
7. c=1;
8. a+b=c; // semantic error
9. return 0;
10. }
In the above code, we use the statement a+b =c, which is incorrect as we cannot use
the two operands on the left-side.
Output
Compile time vs Runtime
Compile-time and Runtime are the two programming terms used in the software
development. Compile-time is the time at which the source code is converted into an
executable code while the run time is the time at which the executable code is started
running. Both the compile-time and runtime refer to different types of error.
Compile-time errors
Compile-time errors are the errors that occurred when we write the wrong syntax. If we
write the wrong syntax or semantics of any programming language, then the compile-
time errors will be thrown by the compiler. The compiler will not allow to run the
program until all the errors are removed from the program. When all the errors are
removed from the program, then the compiler will generate the executable file.
o Syntax errors
o Semantic errors
Syntax errors
When the programmer does not follow the syntax of any programming language, then
the compiler will throw the syntax error.
For example,
int a, b:
The above declaration generates the compile-time error as in C, every statement ends
with the semicolon, but we put a colon (:) at the end of the statement.
Semantic errors
The semantic errors exist when the statements are not meaningful to the compiler.
For example,
a+b=c;
The above statement throws a compile-time errors. In the above statement, we are
assigning the value of 'c' to the summation of 'a' and 'b' which is not possible in C
programming language as it can contain only one variable on the left of the assignment
operator while right of the assignment operator can contain more than one variable.
c=a+b;
Runtime errors
The runtime errors are the errors that occur during the execution and after compilation.
The examples of runtime errors are division by zero, etc. These errors are not easy to
detect as the compiler does not point to these errors.
Compile-time Runtime
The compile-time errors are the errors which are The runtime errors are the errors which
produced at the compile-time, and they are by the compiler and produce an unpredi
detected by the compiler. execution time.
In this case, the compiler prevents the code from In this case, the compiler does not detec
execution if it detects an error in the program. cannot prevent the code from the execu
It contains the syntax and semantic errors such as It contains the errors such as division by
missing semicolon at the end of the statement. the square root of a negative number.
1. #include <stdio.h>
2. int main()
3. {
4. int a=20;
5. printf("The value of a is : %d",a):
6. return 0;
7. }
In the above code, we have tried to print the value of 'a', but it throws an error. We put
the colon at the end of the statement instead of a semicolon, so this code generates a
compile-time error.
Output
1. #include <stdio.h>
2. int main()
3. {
4. int a=20;
5. int b=a/0; // division by zero
6. printf("The value of b is : %d",b):
7. return 0;
8. }
In the above code, we try to divide the value of 'b' by zero, and this throws a runtime
error.
Output