Command Line Arguments

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Range of Data Types in C Programming Language

Datatypes in C help the programmers, to work with different types of data in a program.
Using which we can accept and print different types of data. These data types have a
definite range of data, and we have to choose the datatype according to their range and our
data set. Every datatype has a range according to ASCII values of the characters in C. In
this , we will be talking about the range of the data types.
Range of various Data Types
As we have discussed above that there are many different data types in C, and every data type
has a pre-defined range of data, under which it works, so we need to be smart while choosing
a datatype in our program, below is a table which represents the different data-type, and their
data range.

Name Of The Datatype Range of Datatype


Double 2.3E-308 to 1.7E+308
Long Double 3.4E-4932 to 1.1E+4932
Long -9223372036854775808 to 9223372036854775807
Unsigned Long 0 to 18446744073709551615
Float .2E-38 to 3.4E+38
int -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 0 to 65,535 or 0 to 4,294,967,295
short -32,768 to 32,767
unsigned short 0 to 65,535
char -128 to 127 or 0 to 255
signed char -128 to 127
unsigned char 0 to 255

C code for finding the range of Datatypes


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main()
{
printf("CHAR_BIT : %d\n", CHAR_BIT);
printf("CHAR_MAX : %d\n", CHAR_MAX);
printf("CHAR_MIN : %d\n", CHAR_MIN);
printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", (long) LONG_MAX);
printf("LONG_MIN : %ld\n", (long) LONG_MIN);
printf("SCHAR_MAX : %d\n", SCHAR_MAX);
printf("SCHAR_MIN : %d\n", SCHAR_MIN);
printf("SHRT_MAX : %d\n", SHRT_MAX);
printf("SHRT_MIN : %d\n", SHRT_MIN);
printf("UCHAR_MAX : %d\n", UCHAR_MAX);
printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
printf("ULONG_MAX : %lu\n", (unsigned long) ULONG_MAX);
printf("USHRT_MAX : %d\n", (unsigned short) USHRT_MAX);

return 0;
}
Output:
CHAR_BIT : 8
CHAR_MAX : 128
CHAR_MIN : -127
INT_MAX : 2147483647
INT_MIN : -2147483648
LONG_MAX : 9223372036854775807
LONG_MIN : -9223372036854775808
SCHAR_MAX : 127
SCHAR_MIN : -128
SHRT_MAX : 32767
SHRT_MIN : -32768
UCHAR_MAX : 255
UINT_MAX : 4294967295
ULONG_MAX : 18446744073709551615
USHRT_MAX : 65535

OPERATORS IN C
The operator is a symbol that tells the compiler to perform any mathematical or logical
operation. There are a number of built-in-operators in C programming.
The symbols, however, are used to perform any type of mathematical and logical operation.
In C programming, they are called Operators. In the programming, operators are used to
manipulate data and variables.
The combination of operands and operators is called expression. Operands are variables
that perform some operations in conjunction with the operators.

Arithmetic operator
The arithmetic operators is used to perform mathematical operations like addition, subtraction
division and multiplication etc. Below is a list for the type and function of these operators.
Assume A=5 and B=10
Operato Function performed by operator Example
r
+ Adds two operands (float, integer etc.) A + B = 15
− Subtracts  2nd operand from the 1st one. A − B = -5
* Multiplies the operands. A * B = 50
/ Divides numerator by denominator. B/A=2
% Returns remainder after dividing 1st operand with second one. B % A = 0
++ Increment operator increases the integer value by one. ++A = 6
-- Decrement operator decreases the integer value by one. A– = 4

C program for arithmetic operator


#include<stdio.h>
int main()
{
int a = 5,b = 10,c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = b%a;
printf("b modulus a= %d \n",c);
c=++a;
printf("a++= %d \n",c);
a=5;
c=a--;
printf("a--= %d \n",a);
return 0;
}

Output:
a+b = 15
a-b = -5
a*b = 50
a/b = 0
b modulus a= 0
a++= 6
a--= 4

Relational operator
Relational operators are used to compare values of two variables. As you can use these
operators, you can find out, whether the values of any two variables are equal or not. If not
equal, then the value of which variable is big and which variable’s value is small. Such
operators are used with conditional statements. These operators are used to check the
condition. when the condition is true, the value becomes true and the values becomes false
when the condition is false. All the relational operators that are being used in C language are
given below. Assume A=5, B=10
Operato Function performed by operator Example
r
== Compares if the values of two operands are equal or not. If yes, then (A == B) is
the condition becomes true, false otherwise. not true.
!= Compares if the values of two operands are equal or not. If the (A != B) is
values are not equal, then the condition becomes true, false true.
otherwise.
> Compares if the value of left operand is greater than the value of (A > B) is not
right operand. If yes, then the condition becomes true, false true.
otherwise.
< Compares if the value of left operand is less than the value of right (A < B) is
operand. If yes, then the condition becomes true, false otherwise. true.
>= Compares if the value of left operand is greater than or equal to the (A >= B) is
value of right operand. If yes, then the condition becomes true, false not true.
otherwise.
<= Compares if the value of left operand is less than or equal to the
(A <= B) is
value of right operand. If yes, then the condition becomes true, false
true.
otherwise.

C program for Relational operator


#include <stdio.h>
int main()
{
int a = 5, b = 10;
printf("a=5, b=10\n");
printf("%d == %d is %d \n", a, b, a == b);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d < %d is %d \n", a, b, a < b); printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d <= %d is %d \n", a, b, a <= b);
return 0;
}
Output
a=5, b=10
5 == 10 is 0
5 != 10 is 1
5 > 10 is 0
5 < 10 is 1 5 >= 10 is 0
5 <= 10 is 1

Logical operator
The logic gate can be applied (finds application in mathematical operations too) with these
operator. Logical operators are used with decision making statements. These operators are
used to check two condition together in control statements The table about logical operators
is being given. Assume A=5, B=10.

Operato Function performed by operator Example


r
&& Logical AND operator. If both the conditions are satisfied then (A>8 &&
the condition becomes true . B==10) is false.
|| Logical OR Operator. If any of the conditions are satisfied, then (A>8 || B==10)
the condition becomes true. is true.
! Logical NOT Operator. It is used to reverse the logical state of its !(A>8 || B==10)
operand. If a condition is true, then Logical NOT operator will is false.
make it false.
 
C program for logical operator
#include <stdio.h>
int main()
{
int a = 5, b = 10;
printf("(a>8 && b==10) will return %d",(a>8)&&(b==10));
printf("\n(a>8 || b==10) will return %d",(a>8)||(b==10));
printf("\n!(a>8 || b==10) will return %d",!((a>8)||(b==10)));
return 0;
}
Output
(a>8 && b==10) will return 0
(a>8 || b==10) will return 1
!(a>8 || b==10) will return 0 
 

Bitwise operator
The bitwise operators work on bits of a integer value. Bitwise operators are used to perform
bit level operations on given variables. The decimal values of the variables are converted to
bits. After this the operations are performed on those bits, to understand it lets recall some
basics –
p q p&q p|q
0 0 0 0
0 1 0 1
1 1 1 1
1 0 0 1
Assume A = 50 and B = 10 in binary, they can be written as: A = 0011 0010 B = 0000 1010
Now in the case of A and B we compare 2 bits one from A and the corresponding bit of B
And bitwise comparison A&B= 0000 0010 Or bitwise comparison A|B=0011 1010 Assume
A=50, B=10

Operato Function performed by operator Example


r
& Binary AND Operator copies a bit to the result if it exists in both (A & B) = 2
operands else copies zero.
| Binary OR Operator copies a bit if it exists in either operand. (A | B) = 58
^ Bitwise exclusive OR Operator. If the corresponding bit of any of (A ^ B) = 56
the operand is 1 then the output would be 1, otherwise 0.
~ Binary compliment operator i.e.- 0 changes to 1 and 1 to 0 (~A ) = -51
<< Binary Left shift Operator which rotates the number of bits to the A << 2 = 200
left by specified positions as mentioned on the right.
>> Binary Right Rotation Operator which rotates the number of bits A >> 2 = 12
to right by specified positions as mentioned on the right.
 
C program for Bitwise operator
#include <stdio.h>
int main()
{
int a =50, b =10;
printf ("a&b = %d", a&b);
printf ("\na|b = %d", a|b);
printf ("\na^b = %d", a^b);
printf ("\n~a = %d", ~a);
printf ("\na<<2 = %d",a<<2);
printf("\na>>2 = %d", a>>2);
return 0;
}

Output
 a&b = 2
a|b = 58
a^b = 56
~a = -51
a<<2 = 200
a>>2 = 12
 
Assignment operator
Assignment operators are used to assign values of variables to each other. The operator which
changes the value of the operands themselves, here is how they work.

Operato Function performed by operator Example


r
= Simple assignment operator. Assigns values from right C = A + B will assign
side operands to left side operand the value of A + B to C
+= Add AND assignment operator. It adds the right operand C += A is equivalent to
to the left operand and assign the result to the left C=C+A
operand.
-= Subtract AND assignment operator. It subtracts the right C -= A is equivalent to
operand from the left operand and assigns the result to the C = C – A
left operand.
*= Multiply AND assignment operator. It multiplies the right C *= A is equivalent to
operand with the left operand and assigns the result to the C = C * A
left operand.
/= Divide AND assignment operator. It divides the left C /= A is equivalent to
operand with the right operand and assigns the result to C = C / A
the left operand.
%= Modulus AND assignment operator. It takes modulus C %= A is equivalent to
using two operands and assigns the result to the left C=C%A
operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C =
C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C =
C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C =
C&2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C
^2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C
|2
 
C Program for assignment operator
#include <stdio.h>
int main()
{
int a = 10, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c - = a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}

Output
c = 10
c = 20
c = 10
c = 100
c = 10
c=0

Miscellaneous Operators
Except for Arithmetic and Logical Operator, there are some special Operators provided by C
language which performs special operations and description of them summarized in the
following table.
 
Operato
Description Example
r
sizeof(a), where a is
sizeof() Yields the size of a specified variable
the integer will return 4.
&a; returns the actual
& Returns the address of a variable
address of the variable.
* Pointer to a variable *a;
If Condition is true ?
?: Conditional Expression then value X :
otherwise value Y

/*A C Program for Misc. Operators*/

#include <stdio.h>
int main()
{
int num1 ;
float num2;
double num3;
char num4;
int* ptr;

/* example of sizeof operator */


printf("Line 1 - Size of variable num1 = %lu\n", sizeof(num1) );
printf("Line 2 - Size of variable num2 = %lu\n", sizeof(num2) );
printf("Line 3 - Size of variable num3= %lu\n", sizeof(num3) );
printf("Line 4 - Size of variable num3= %lu\n", sizeof(num4) );

/* example of & and * operators */


ptr = &num1; /* 'ptr' now contains the address of 'a'*/
printf("Value of num1 is %d\n", num1);
printf("*ptr is %d.\n", *ptr);

/* example of ternary operator */


num1 = 10;
num2 = (num1 == 1) ? 20: 30;
printf( "Value of num2 is %d\n", num2 );
num2 = (num1 == 10) ? 20: 30;
printf( "Value of num2 is %d\n", num2 );
return 0;
}

Comparison Operators
Comparison operators are used to compare two values (or variables). This is important in
programming, because it helps us to find answers and make decisions. The return value of a
comparison is either 1 or 0, which means true (1) or false (0).

#include <stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d\n", x > y);
printf("%d\n", x < y);
printf("%d\n", x != y); // returns 1 (true) because 5 is not equal to 3
printf("%d\n", x == y);
printf("%d\n", x <= y);
printf("%d", x >= y);
return 0;
}

Output:
1
0
1
0
0
1
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example,
the multiplication operator has a higher precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicativ */% Left to right


e

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right


Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right

Example:
#include <stdio.h>
int main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );

e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );

e = (a + b) * (c / d); // (30) * (15/5)


printf("Value of (a + b) * (c / d) is : %d\n", e );

e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );

return 0;
}

Output:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
Expression evaluation
While knowing about expression evaluation we must understand what is an expression in C
and what is an expression means. An expression in C is defined as 2 or more operands are
connected by one operator and which can also be said to a formula to perform any operation.
An operand is a function reference, an array element, a variable, or any constant. An operator
is symbols like “+”, “-“, “/”, “*” etc. Now expression evaluation is nothing but operator
precedence and associativity. Expression precedence in C tells you which operator is
performed first, next, and so on in an expression with more than one operator with different
precedence. This plays a crucial role while we are performing day to day arithmetic
operations. If we get 2 same precedences appear in an expression, then it is said to be
“Associativity”. Now in this case we can calculate this statement either from Left to right or
right to left because this both are having the same precedence.

Types of Expression Evaluation in C


In C there are 4 types of expressions evaluations

1. Arithmetic expressions evaluation

2. Relational expressions evaluation

3. Logical expressions evaluation

4. Conditional expressions evaluation

Arithmetic expression Evaluation


Addition (+), Subtraction(-), Multiplication(*), Division(/), Modulus(%), Increment(++)
and Decrement(–) operators are said to “Arithmetic expressions”. These operators work in
between operands. like A+B, A-B, A–, A++ etc. While we perform the operation with these
operators based on specified precedence order as like below image.
A+B*C/D-E%F

1. Arithmetic Expression evaluation order:


#include <stdio.h>
//main method for run c application
int main()
{
//declaring variables
int a,b,result_art_eval;
//Asking the user to enter 2 numbers
printf("Enter 2 numbers for Arithmetic evaluation operation\n");
//Storing 2 numbers in varaibles a and b
scanf("%d\n%d",&a,&b);
//Arithmetic evaluation operations and its result displaying
result_art_eval = a+b*a/b-a%b;
printf("================ARITHMETIC EXPRESSION
EVALUATION==============\n");
printf("Arithmetic expression evaluation of %d and %d is = %d \n",a,b,result_art_eval);
printf("============================================================
==");
return 0;
}
Output:
Enter 2 numbers for Arithmetic evaluation operation
3
2
================ARITHMETIC EXPRESSION EVALUATION========
Arithmetic expression evaluation of 3 and 2 is = 5
============================================================

2. Relational expression evaluation


== (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to),
<= (less than or equal to) operators are said to “Relational expressions”. This operator
works in between operands. Used for comparing purpose. Like A==B, A!=B, A>B, A<B
etc. While we perform the operation with these operators based on specified precedence
order as like the below image.
#include <stdio.h>
//main method for run c application
int main()
{
//declaring variables
int a,b;
int result_rel_eval;
//Asking the user to enter 2 numbers
printf("Enter 2 numbers for Relational evaluation operation\n");
//Storing 2 numbers in varaibles a and b
scanf("%d\n%d",&a,&b);
//Relational evaluation operations and its result displaying
//If we get result as 1 means the value is true and if it is 0 then value is false in C
result_rel_eval = (a<b||a<=b||a>=b||a>b||a!=b);
printf("================RELATIONAL EXPRESSION
EVALUATION==============\n");
if(result_rel_eval==1)
{
printf("Relational expression evaluation of %d and %d is = %d \n",a,b,result_rel_eval);
}
else
{
printf("Relational expression evaluation of %d and %d is = %d \n",a,b,result_rel_eval);
}
printf("============================================================
===");
return 0;
}

Output:
Enter 2 numbers for Relational evaluation operation
2
3
================RELATIONAL EXPRESSION EVALUATION==============
Relational expression evaluation of 2 and 3 is = 1
===============================================================

3. Logical expression evaluation


&&(Logical and), ||(Logical or) and !(Logical not) operators are said to “Logical
expressions”. Used to perform a logical operation. These operators work in between
operands. Like A&&B, A||B, A!B etc. While we perform the operation with these operators
based on specified precedence order as like the below image.

//used to include basic C libraries


#include <stdio.h>
//main method for run c application
int main()
{
//declaring variables
int a,b;
int result_log_eval;
//Asking the user to enter 2 numbers
printf("Enter 2 numbers for Logical evaluation operation\n");
//Storing 2 numbers in varaibles a and b
scanf("%d\n%d",&a,&b);
//Logical evaluation operations and its result displaying
//If we get result as 1 means the value is true and if it is 0 then value is false in C
result_log_eval = (a||b&&a);
printf("====LOGICAL EXPRESSION EVALUATION==========\n");
if(result_log_eval==1)
{
printf("Logical expression evaluation of %d and %d is = %d \n",a,b,result_log_eval);
}
else
{
printf("Logical expression evaluation of %d and %d is = %d \n",a,b,result_log_eval);
}
printf("============================================================
===");
return 0;
}
Output:
Enter 2 numbers for Logical evaluation operation
2
3
====LOGICAL EXPRESSION EVALUATION==========
Logical expression evaluation of 2 and 3 is = 1
==============================================

4. Conditional expression Evaluation


?(Question mark) and :(colon) are said to “Conditional expressions”. Used to perform a
conditional check. It has 3 expressions first expression is condition. If it is true then execute
expression2 and if it is false then execute expression3. Like (A>B)?”A is Big”:”B is Big”.
While we perform the operation with these operators based on specified precedence order
as like the below image.

#include <stdio.h>
//main method for run c application
int main()
{
//declaring variables
int a,b,c;
int result_con_eval;
//Asking the user to enter 3 numbers
printf("Enter 3 numbers for Conditional evaluation operation\n");
//Storing 3 numbers in varaibles a, b and c
scanf("%d\n%d\n%d",&a,&b,&c);
//Conditional evaluation operations and its result displaying
result_con_eval=(a>b && a>b)? a: (b>a && b>c)? b:c;
printf("=======CONDITIONAL EXPRESSION EVALUATION======\n");
printf("Maximum value of %d, %d and %d is = %d \n",a,b,c,result_con_eval);
printf("==================================================");
return 0;
}
Output:
Enter 3 numbers for Conditional evaluation operation
2
3
4
=======CONDITIONAL EXPRESSION EVALUATION======
Maximum value of 2, 3 and 4 is = 4
==================================================

Storage classes
A storage class defines the scope (visibility) and life-time of variables and/or functions within
a C Program. They precede the type that they modify. We have four different storage classes
in a C program −

 auto
 register
 static
 extern
The main method and command line arguments
The most important function of C/C++ is main() function. It is mostly defined with a return
type of int and without parameters :

int main () { /* ... */ }


We can also give command-line arguments in C and C++. Command-line arguments are
given after the name of the program in command-line shell of Operating Systems.
To pass command line arguments, we typically define main() with two arguments : first
argument is the number of command line arguments and second is list of command-line
arguments.
int main(int argc, char *argv[]) { /* ... */ }
or
int main(int argc, char **argv) { /* ... */ }
 argc (ARGument Count) is int and stores number of command-line arguments
passed by the user including the name of the program. So if we pass a value to a
program, value of argc would be 2 (one for argument and one for program name)
 The value of argc should be non-negative.
 argv(ARGument Vector) is array of character pointers listing all the
arguments.
 If argc is greater than zero,the array elements from argv[0] to argv[argc-1] will
contain pointers to strings.
 Argv[0] is the name of the program , After that till argv[argc-1] every element is
command -line arguments.
Properties of Command Line Arguments:
1. They are passed to main() function.
2. They are parameters/arguments supplied to the program when it is invoked.
3. They are used to control program from outside instead of hard coding those
values inside the code.
4. argv[argc] is a NULL pointer.
5. argv[0] holds the name of the program.
6. argv[1] points to the first command line argument and argv[n] points last
argument.

//C program to illustrate


// command line arguments
#include<stdio.h>
int main(int argc,char* argv[])
{
     int counter;
     printf("Program Name Is: %s",argv[0]);
     if(argc==1)
         printf("\nNo Extra Command Line Argument Passed Other Than Program
Name");
if(argc>=2)
     {
         printf("\nNumber Of Arguments Passed: %d",argc);
         printf("\n----Following Are The Command Line Arguments Passed----");
         for(counter=0;counter<argc;counter++)
             printf("\nargv[%d]: %s",counter,argv[counter]);
     }
     return 0;
}

Output in different scenarios:

Without argument: When the above code is compiled and executed without passing
any argument, it produces following output.
$ ./a.out
Program Name Is: ./a.out
No Extra Command Line Argument Passed Other Than Program Name

Three arguments: When the above code is compiled and executed with a three
arguments, it produces the following output.
$ ./a.out First Second Third
Program Name Is: ./a.out
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First
argv[2]: Second
argv[3]: Third

Single Argument: When the above code is compiled and executed with a single
argument separated by space but inside double quotes, it produces the following output.
$ ./a.out "First Second Third"
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third

Single argument in quotes separated by space : When the above code is compiled
and executed with a single argument separated by space but inside single quotes, it
produces the following output.
$ ./a.out 'First Second Third'
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third

You might also like