C is a procedural language. The main features of C language include low-level access to memory, simple set of keywords, and clean style. These features make it suitable for system programming like operating system or compiler development.
-
-
Both functions are to retrieve absolute value. abs() is for integer values and fabs() is for floating type numbers. Prototype for abs() is under the library file < stdlib.h > and fabs() is under < math.h >.
-
A function, which has a function definition prefixed with a static keyword is defined as a static function. The static function should call within the same source code. In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
-
A local static variable is a variable whose lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables. Static variables can be used to count the number of times a function is called. Also, static variables get the default value as 0. For example, the following program prints “0 1” #include <stdio.h> void fun() { // static variables get the default value as 0. static int x; printf("%d ", x); x = x + 1; } int main()…
-
The words that are part of the slandered C language library are called reserved words. Those reserved words have special meaning and it is not possible to use them for any activity other than its intended functionality. Example void, return int.
-
Break can appear only with in the looping control and switch statement. The purpose of the break is to bring the control out from the said blocks.
-
There are two possible methods to perform this task. 1) Use increment (++) and decrement (-) operator. Example When x=4, x++ returns 5 and x- returns 3. 2) Use conventional + or – sign. When x=4, use x+1 to get 5 and x-1 to get 3.
-
Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate. #include <stdlib.h> void f() { int* ptr = (int*)malloc(sizeof(int)); return; /* Return without freeing ptr*/ }
-
Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. Following are examples. int* ptr = (int*)malloc(sizeof(int)); ..........................free(ptr); // ptr is a dangling pointer now and operations like following are invalid *ptr = 10; // or printf("%d", *ptr); int* ptr = NULL { int x = 10; ptr = &x; } // x goes out of scope and memory allocated to x is free now. //…
-
The mistakes when creating a program called syntax errors. Misspelled commands or incorrect case commands, an incorrect number of parameters when called a method /function, data type mismatches can identify as common examples for syntax errors.