Type Casting, Command Line Arguments and Defining Constants

 


Type Casting

• Type cast is an instruction to the compiler to convert one type into another. 
• For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. • Here, target-type specifies the desired type to convert the specified expression to.
 • You can convert the values from one type to another explicitly using the cast operator as follows: (type_name) expression



• The typical arithmetic conversions are implicitly performed to cast their values to a common type. 
 • The compiler first performs integer promotion; if the operands still have different types, then they are converted to the type that appears highest in the given hierarchy.





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.







Defining Constants



There are two ways to define constant in C programming.

  1. const keyword
  2. #define preprocessor

1) C const keyword

The const keyword is used to define constant in C programming.

  1. const float PI=3.14;  

Now, the value of PI variable can't be changed.

  1. #include<stdio.h>    
  2. int main(){    
  3.     const float PI=3.14;    
  4.     printf("The value of PI is: %f",PI);    
  5.     return 0;  
  6. }     

Output:

The value of PI is: 3.140000







Math Functions 

• 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. 
• You can find documentation via: ▫ http://devdocs.io/c/numeric/math







Post a Comment

0 Comments