luc098.c¶
Problem Statement
Write a calculator utility using command line arguments.\nUsage: calc
Metadata¶
| Property | Detail |
|---|---|
| Author | Amit Dutta amitdutta4255@gmail.com |
| Date | 08 Feb 2026 |
| License | MIT License (See the LICENSE file for details) |
| Difficulty | Beginner (index: 0 / 10) |
Concepts¶
Beta Feature
This concept detection system is still in beta and may occasionally show incorrect or incomplete results.
- Array
- Pointers
Actions¶
You can print or save this file by opening Raw and using your browser.
Source Code¶
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
float n, m, res;
char operator;
if (argc != 4)
{
printf("Usage: %s <switch> <n> <m>\n", argv[0]);
printf("Example: %s + 10 20\n", argv[0]);
printf("Note: For multiplication (*), use '*' or x to avoid shell expansion.\n");
exit(1);
}
operator = argv[1][0]; // First character of the switch argument
n = atof(argv[2]);
m = atof(argv[3]);
switch (operator)
{
// Arithmetic
case '+':
printf("%.2f\n", n + m);
break;
case '-':
printf("%.2f\n", n - m);
break;
case 'x':
case '*':
printf("%.2f\n", n * m);
break;
case '/':
if (m == 0) printf("Error: Division by zero\n");
else printf("%.2f\n", n / m);
break;
case '%':
printf("%d\n", (int)n % (int)m);
break;
// Comparison
case '<':
printf("%s\n", (n < m) ? "True" : "False");
break;
case '>':
printf("%s\n", (n > m) ? "True" : "False");
break;
// Handling symbols that might be multi-char (e.g. <=, >=, ==) is tricky
// with argv[1][0], but basic logic for typical single char switches:
case '=':
printf("%s\n", (n == m) ? "True" : "False");
break;
default:
printf("Unknown operator: %c\n", operator);
break;
}
return 0;
}
Explanation¶
Explain with AI
Copy the prompt below and paste it into any AI assistant.
You are explaining a C programming code to a beginner.
STRICT RULES:
- Only use the given code. Do NOT assume anything not present.
- Do NOT add extra examples.
- Keep explanation clear and short.
- If something is unclear, say "Not clear from code".
- Follow the exact format below. Do NOT change headings.
FORMAT:
[START]
## What it does
(Explain the overall purpose in 1-2 sentences)
## Step-by-step
(Explain how the code works in steps, simple language)
## Key Concepts
(List concepts like loop, condition, function, etc.)
## Notes
(Mention any limitations, errors, or assumptions)
[END]
CODE (luc098.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
float n, m, res;
char operator;
if (argc != 4)
{
printf("Usage: %s <switch> <n> <m>\n", argv[0]);
printf("Example: %s + 10 20\n", argv[0]);
printf("Note: For multiplication (*), use '*' or x to avoid shell expansion.\n");
exit(1);
}
operator = argv[1][0]; // First character of the switch argument
n = atof(argv[2]);
m = atof(argv[3]);
switch (operator)
{
// Arithmetic
case '+':
printf("%.2f\n", n + m);
break;
case '-':
printf("%.2f\n", n - m);
break;
case 'x':
case '*':
printf("%.2f\n", n * m);
break;
case '/':
if (m == 0) printf("Error: Division by zero\n");
else printf("%.2f\n", n / m);
break;
case '%':
printf("%d\n", (int)n % (int)m);
break;
// Comparison
case '<':
printf("%s\n", (n < m) ? "True" : "False");
break;
case '>':
printf("%s\n", (n > m) ? "True" : "False");
break;
// Handling symbols that might be multi-char (e.g. <=, >=, ==) is tricky
// with argv[1][0], but basic logic for typical single char switches:
case '=':
printf("%s\n", (n == m) ? "True" : "False");
break;
default:
printf("Unknown operator: %c\n", operator);
break;
}
return 0;
}