Command Line Arguments
Command Line Arguments
Command Line Arguments
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.
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
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.
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.
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
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.
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
#include <stdio.h>
int main()
{
int num1 ;
float num2;
double num3;
char num4;
int* ptr;
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.
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; // 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.
Output:
Enter 2 numbers for Relational evaluation operation
2
3
================RELATIONAL EXPRESSION EVALUATION==============
Relational expression evaluation of 2 and 3 is = 1
===============================================================
#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 :
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