Additional Topics

C Programming Enumeration


An enumeration is a user-defined data type consists of integral constants and each integral constant is give a name. Keyword enum is used to defined enumerated data type.
enum type_name{ value1, value2,...,valueN };
Here, type_name is the name of enumerated data type or tag. And value1value2,....,valueN are values of type type_name.
By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can change the default value as below:
enum suit{
    club=0;
    diamonds=10;
    hearts=20;
    spades=3;
};

Declaration of enumerated variable

Above code defines the type of the data but, no any variable is created. Variable of type enum can be created as:
enum boolean{
    false;
    true;
};
enum boolean check;
Here, a variable check is declared which is of type enum boolean.

Example of enumerated type

#include <stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main(){
    enum week today;
    today=wednesday;
    printf("%d day",today+1);
    return 0;
   }
Output
4 day
You can write any program in C language without the help of enumerations but, enumerations helps in writing clear codes and simplify programming.

C Programming Preprocessor and Macros


Preprocessor extends the power of C programming language. Line that begin with # are called preprocessing directives.

Use of #include

Let us consider very common preprocessing directive as below:
#include <stdio.h>
Here, "stdio.h" is a header file and the preprocessor replace the above line with the contents of header file.

Use of #define

Preprocessing directive #define has two forms. The first form is:
#define identifier token_string
token_string part is optional but, are used almost every time in program.

Example of #define

#define c 299792458 /*speed of light in m/s */
The token string in above line 2299792458 is replaced in every occurance of symbolic constant c.
C Program to find area of a cricle. [Area of circle=πr2]
#include <stdio.h>
#define PI 3.1415
int main(){
    int radius;
    float area;
    printf("Enter the radius: ");
    scanf("%d",&radius);
    area=PI*radius*radius;
    printf("Area=%.2f",area);
    return 0;
}
Output
Enter the radius: 3 
Area=28.27

Syntactic Sugar

Syntactic sugar is the alteration of programming syntax according to the will of programmer. For example:
#define LT <
Every time the program encounters LT, it will be replaced by <.
Second form of preprocessing directive with #define is:

Macros with argument

Preprocessing directive #define can be used to write macro definitions with parameters as well in the form below:
#define identifier(identifier 1,.....identifier n) token_string
Again, the token string is optional but, are used in almost every case. Let us consider an example of macro definition with argument.
#define area(r) (3.1415*r*r)
Here, the argument passed is r. Every time the program encounters area(argument), it will be replace by (3.1415*argument*argument). Suppose, we passed (r1+5) as argument then, it expands as below:
area(r1+5) expands to (3.1415*(r1+5)(r1+5))
C Program to find area of a circle, passing arguments to macros. [Area of circle=πr2]
#include <stdio.h>
#define PI 3.1415
#define area(r) (PI*r*r)
int main(){
    int radius;
    float area;
    printf("Enter the radius: ");
    scanf("%d",&radius);
    area=area(radius);
    printf("Area=%.2f",area);
    return 0;
}

Predefined Macros in C language

Predefined macroValue
__DATE__String containing the current date
__FILE__String containing the file name
__LINE__Integer representing the current line number
__STDC__If follows ANSI standard C, then value is a nonzero integer
__TIME__String containing the current date.

How to use predefined Macros?

C Program to find the current time
#include <stdio.h>
int main(){
   printf("Current time: %s",__TIME__);   //calculate the current time
}
Output

Current time: 19:54:39

C Standard Library Functions

C Standard library functions or simply C Library functions are inbuilt functions in C programming. Function prototype and data definitions of these functions are written in their respective header file. For example: If you want to use printf() function, the header file <stdio.h> should be included.
#include <stdio.h>
int main()
    {

/* If you write printf() statement without including  header file, this program will show error. */
        printf("Catch me if you can."); 
    }
There is at least one function in any C program, i.e., the main() function (which is also a library function). This program is called at program starts.
There are many library functions available in C programming to help the programmer to write a good efficient program.
Suppose, you want to find the square root of a number. You can write your own piece of code to find square root but, this process is time consuming and the code you have written may not be the most efficient process to find square root. But, in C programming you can find the square root by just using sqrt() function which is defined under header file "math.h"

Use Of Library Function To Find Square root

#include <stdio.h>
#include <math.h>
int main(){
   float num,root;
   printf("Enter a number to find square root.");
   scanf("%f",&num);
   root=sqrt(num);          /* Computes the square root of num and stores in root. */
   printf("Square root of %.2f=%.2f",num,root);
   return 0;
}

List of Standard Library Functions Under Different Header Files in C Programming

C Header Files
<ctype.h>
<math.h>
<stdio.h>
<stdlib.h>
<string.h>
<time.h>

No comments:

Post a Comment