0% found this document useful (0 votes)
10 views8 pages

Command Line Arguments

The document explains the syntax of the main() function in C for handling command line arguments, detailing parameters like argc and argv. It provides example programs demonstrating addition, multiplication, string manipulation, and ASCII character handling. Additionally, it includes a brief overview of ASCII and its representation in C programming.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Command Line Arguments

The document explains the syntax of the main() function in C for handling command line arguments, detailing parameters like argc and argv. It provides example programs demonstrating addition, multiplication, string manipulation, and ASCII character handling. Additionally, it includes a brief overview of ASCII and its representation in C programming.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Syntax of main() with Command Line Arguments

int main(int argc, char *argv[])

🔹 Parameters:

 argc (Argument Count):


Number of arguments passed (including the program name).
 argv[] (Argument Vector):
Array of strings (character pointers) that contain the arguments.

✅ Example Program: Add Two Numbers Using Command


Line Arguments

#include <stdio.h>
#include <stdlib.h> // for atoi()

int main(int argc, char *argv[]) {


if (argc != 3) {
printf("Usage: %s <num1> <num2>\n", argv[0]);
return 1; // Exit with error
}

int num1 = atoi(argv[1]); // convert string to int


int num2 = atoi(argv[2]);

int sum = num1 + num2;


printf("Sum of %d and %d is %d\n", num1, num2, sum);

return 0;
}

✅ How to Run This Program


If you save this as add.c, compile and run it like this:

gcc add.c -o add


./add 12 8

🟩 Output:
Sum of 12 and 8 is 20

✅ Explanation:
Code Meaning
argc != 3 Checks if 2 numbers (and program name) are provided
argv[0] Program name (e.g., ./add)
argv[1], argv[2] Command line inputs (e.g., "12", "8")
atoi(argv[1]), atoi(argv[2]) Convert string to integer

1. Add Two Numbers

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {


if (argc != 3) {
printf("Usage: %s <num1> <num2>\n", argv[0]);
return 1;
}

int a = atoi(argv[1]);
int b = atoi(argv[2]);

printf("Sum = %d\n", a + b);


return 0;
}

Run:

./program 10 20

2. Multiply Two Numbers

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <num1> <num2>\n", argv[0]);
return 1;
}

int a = atoi(argv[1]);
int b = atoi(argv[2]);

printf("Product = %d\n", a * b);


return 0;
}

Run:

./program 5 6

✅ 3. Find Length of a String

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {


if (argc != 2) {
printf("Usage: %s <string>\n", argv[0]);
return 1;
}

printf("Length = %lu\n", strlen(argv[1]));


return 0;
}

Run:

./program hello

✅ 4. Reverse a String

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {


if (argc != 2) {
printf("Usage: %s <string>\n", argv[0]);
return 1;
}
int len = strlen(argv[1]);
printf("Reversed: ");
for (int i = len - 1; i >= 0; i--) {
putchar(argv[1][i]);
}
printf("\n");

return 0;
}

Run:

./program world

✅ 5. Count Number of Command Line Arguments

#include <stdio.h>

int main(int argc, char *argv[]) {


printf("Number of arguments: %d\n", argc);
printf("Arguments:\n");
for (int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}

What is ASCII in C
ASCII stands for American Standard Code for Information Interchange.

 It is a standard encoding system used to represent characters using numeric codes.


 In C programming, every character is internally stored as an integer (ASCII value).
 The standard ASCII range is 0 to 127.
o For example:
 'A' = 65
 'a' = 97
 '0' = 48
 ' ' (space) = 32
✅ 5 Example Programs Using ASCII in C

1. Print ASCII Value of a Character

#include <stdio.h>

int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);

printf("ASCII value of '%c' = %d\n", ch, ch);


return 0;
}

Output:

Enter a character: A
ASCII value of 'A' = 65

2. Convert Lowercase to Uppercase


#include <stdio.h>

int main() {
char ch;
printf("Enter a lowercase letter: ");
scanf("%c", &ch);

if (ch >= 'a' && ch <= 'z') {


char upper = ch - 32; // ASCII difference
printf("Uppercase: %c\n", upper);
} else {
printf("Not a lowercase letter.\n");
}
return 0;
}

Output:

Enter a lowercase letter: m


Uppercase: M
3. Check if Character is a Digit

#include <stdio.h>

int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);

if (ch >= '0' && ch <= '9') {


printf("'%c' is a digit.\n", ch);
} else {
printf("'%c' is not a digit.\n", ch);
}
return 0;
}

Output:

Enter a character: 5
'5' is a digit.

4. Print ASCII Table (0 to 127)

#include <stdio.h>

int main() {
for (int i = 0; i < 128; i++) {
printf("ASCII %3d = %c\n", i, i);
}
return 0;
}

🟢 Output:

ASCII 0 =
ASCII 1 =
...
ASCII 65 = A
ASCII 97 = a
...
ASCII 127 = ⌂

5. Count Uppercase Letters in a String

#include <stdio.h>
int main() {
char str[] = "Hello ASCII World!";
int count = 0;

for (int i = 0; str[i] != '\0'; i++) {


if (str[i] >= 'A' && str[i] <= 'Z') {
count++;
}
}

printf("Number of uppercase letters: %d\n", count);


return 0;
}

🟢 Output:

Number of uppercase letters: 3

ASCII Abbr Name


0 NUL Null
1 SOH Start of Heading
2 STX Start of Text
3 ETX End of Text
4 EOT End of Transmission
5 ENQ Enquiry
6 ACK Acknowledge
7 BEL Bell (Beep)
8 BS Backspace
9 HT Horizontal Tab
10 LF Line Feed (New Line)
11 VT Vertical Tab
12 FF Form Feed (New Page)
13 CR Carriage Return
14 SO Shift Out
15 SI Shift In
16 DLE Data Link Escape
17 DC1 Device Control 1
18 DC2 Device Control 2
19 DC3 Device Control 3
20 DC4 Device Control 4
21 NAK Negative Acknowledge
22 SYN Synchronous Idle
23 ETB End of Transmission Block
ASCII Abbr Name
24 CAN Cancel
25 EM End of Medium
26 SUB Substitute
27 ESC Escape
28 FS File Separator
29 GS Group Separator
30 RS Record Separator
31 US Unit Separator

You might also like