Type Casting, Command Line Arguments and Defining Constants

 Type Casting





Type cast in c programming is used to convert a value from a data type to another data type.
There are two type of type casting. 

  1. Implicit type casting;
    • In this process compiler convert the arithmetic conversion automatically by computer without user permission.
    • In this case, smaller size data type is promoted to a higher ranked data type to avoid loss of data.
    • Eg; if we assigning an integer to a floating point variable the integer convert into float.
       2.Explicit type Casting;
    • This called manual casting or casting operator.
    • In this case programmer involves to specifying the conversion.
    • This is useful to convert higher ranked data type to lower ranked data type. 


Command Line Arguments



It is possible to pass some values from the command line to your C programs when they are executed.
These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code
The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program


According to the above theory you can  get any of word you entered as the output you need change only the number of word that you need to display.

How to run this?

there is a simple way. First save the file and code the statements.(I will put the statements below.)Then build it and in top tab layer there is tab called "tool" click it and select "command line" then type your saved file and ".exe" and type your sentence and give"enter".
Now you can see your output.

              #include <stdio.h>
      int main( int argc, char *argv[] )
     {
    printf("The first word is %s\n", argv[1]);
    printf("The second word is %s\n",argv[2]);
    return 0;
      }


Define a Constant

There is two ways to define a constant
  1. Using Directive
                    #include <stdio.h>
                    #define PI 3.1412
                    int main ()
                    {
                            float radius;
                            printf ("Enter the radius in mm : \n");
                            scanf ("%f",&radius);
                            printf("The perimeter is : %.2f", 2*PI*radius);
                            return 0;
                       }

  • According to the above statement #Define PI 1412 is the constant and Define is the directive that using to introduce the constant to the  program. 

        2. Using Keyword
                
                #include <stdio.h>
                int main ()
                {
                        const float PI = 3.1412;
                        float radius;
                        printf ("Enter the radius in mm : \n");
                        scanf ("%f",&radius);
                        printf("The perimeter is : %.2f", 2*PI*radius);
                        return 0;
                }
  • According to the above statement const  float PI 1412 is the constant and const is the keyword that using to introduce the constant to the program. 


Math Function

The math.h header defines various mathematical functions.
All the functions available in this library take double as an argument and return double as the result.





That's all about the Type Casting, Command Line Arguments and Defining Constants. Is there  question regarding to this lesson put a comment in comment section.

-Team CodeQuasar-

Post a Comment

Previous Post Next Post