C Command Line Arguments
What are Command Line Arguments in C?
Command Line Arguments in C allow users to pass input values to a program when it is executed from the command line.
These arguments are received by the main() function through its parameters, making programs more flexible and interactive.
Command line arguments are commonly used to pass file names, configuration options, and user inputs without modifying the program code.
Example
#include <stdio.h>
int main(int argc, char *argv[])
{
printf(“Number of Arguments = %d”, argc);
return 0;
}
Explanation
• argc stores the total number of command line arguments.
• argv is an array of character pointers.
• argv[] stores each argument as a string.
• argv[0] contains the program name.
• Additional arguments are stored from argv[1] onwards.
Why are Command Line Arguments Important?
Command Line Arguments are important because they:
• Allow input without modifying the source code.
• Make programs flexible and reusable.
• Support automation and scripting.
• Simplify file and data processing.
• Improve program usability.
Components of Command Line Arguments
C provides several bitwise operators to perform operations on individual bits of integer values.
The most commonly used bitwise operators are:
• Bitwise AND (&)
• Bitwise OR (|)
• Bitwise XOR (^)
• Left Shift (<<)
• Right Shift (>>)
Each operator performs a different binary operation.
Bitwise AND (&)
The Bitwise AND (&) operator compares each corresponding bit of two numbers.
The result is 1 only if both bits are 1; otherwise, the result is 0.
Example
int a = 5;
int b = 3;
int result = a & b;
Explanation
• a = 5 (Binary: 0101)
• b = 3 (Binary: 0011)
• The AND operation compares each bit.
• Result = 0001 (Decimal: 1)
• The value stored in result is 1.
Bitwise OR (|)
The Bitwise OR (|) operator compares each corresponding bit of two numbers.
The result is 1 if at least one of the bits is 1.
Example
int a = 5;
int b = 3;
int result = a | b;
Explanation
• a = 5 (Binary: 0101)
• b = 3 (Binary: 0011)
• The OR operation compares each bit.
• Result = 0111 (Decimal: 7)
• The value stored in result is 7.
Bitwise XOR (^)
The Bitwise XOR (^) operator compares each corresponding bit of two numbers.
The result is 1 only when the two bits are different.
Example
int a = 5;
int b = 3;
int result = a ^ b;
Explanation
• a = 5 (Binary: 0101)
• b = 3 (Binary: 0011)
• XOR returns 1 when bits are different.
• Result = 0110 (Decimal: 6)
• The value stored in result is 6.
Left Shift (<>)
The Left Shift (<<) operator shifts bits to the left, while the Right Shift (>>) operator shifts bits to the right.
Shifting bits changes the binary value and is commonly used for fast multiplication and division by powers of 2.
Example
int a = 5;
int left = a << 1;
int right = a >> 1;
Explanation
• a = 5 (Binary: 0101)
• a << 1 shifts all bits one position to the left.
• a >> 1 shifts all bits one position to the right.
• Left shift generally multiplies by 2.
• Right shift generally divides by 2 for positive integers.
Advantages of Bitwise Operators
• Perform operations faster than many arithmetic operations.
• Manipulate individual bits efficiently.
• Improve program performance.
• Reduce memory usage in low-level applications.
• Widely used in embedded systems, networking, and device programming.
Example Program
Program
#include <stdio.h>
int main()
{
int a = 5;
int b = 3;
printf(“AND = %d\n”, a & b);
printf(“OR = %d\n”, a | b);
printf(“XOR = %d\n”, a ^ b);
printf(“Left Shift = %d\n”, a << 1);
printf(“Right Shift = %d\n”, a >> 1);
return 0;
}
Output
AND = 1
OR = 7
XOR = 6
Left Shift = 10
Right Shift = 2
Explanation
• Bitwise AND (&) returns 1.
• Bitwise OR (|) returns 7.
• Bitwise XOR (^) returns 6.
• Left Shift (<<) shifts bits to the left and doubles the value.
• Right Shift (>>) shifts bits to the right and halves the value for positive integers.
• Bitwise operators work directly on binary values.
Key Points
• Bitwise operators work on binary data.
• Common operators are &, |, ^, <<, and >>.
• They perform fast low-level operations.
• They are widely used in embedded systems and hardware programming.
• Left Shift generally multiplies by 2.
• Right Shift generally divides by 2 for positive integers.
• Bitwise operators improve performance in many applications.
