This C code compiles successful, however when I execute, it asks for the number, but in the end it doesnt show the final result, instead it shows an empty line:
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int number, u, t, h, sum;
char parity[7] = "";
float result;
printf("Number : ");
scanf("%u", &number);
u = (int)number % 10;
t = (int)number % 100 / 10;
h = (int)number / 100;
sum = u t h;
if (number >= 100 && number <= 999)
{
if (number % 2 == 0)
{
strcpy(parity[7], "even");
result = pow(sum, 2);
printf("%u %u %u = %u : %s ---> %u*%u = %.0f \n", u, t, h, sum, parity[7], sum, sum, result);
}
else if (number % 2 == 1)
{
strcpy(parity[7], "odd");
result = sqrt(sum);
printf("%u %u %u = %u : %s ---> Sqrt(%u) = %.2f \n", u, t, h, sum, parity[7], sum, result);
}
}
return 0;
}
CodePudding user response:
Try this one
#include <string.h>
#include <math.h>
#include <stdio.h>
int main()
{
int number, u, t, h, sum;
char parity[7];
float result;
printf("Number : ");
scanf("%u", &number);
u = (int)number % 10;
t = (int)number % 100 / 10;
h = (int)number / 100;
sum = u t h;
if (number >= 100 && number <= 999)
{
if (number % 2 == 0)
{
strcpy(parity, "even");
result = pow(sum, 2);
printf("%d %d %d = %d : %s ---> %u*%u = %.0f \n", u, t, h, sum, parity, sum, sum, result);
}
else if (number % 2 == 1)
{
strcpy(parity, "odd");
result = sqrt(sum);
printf("%d %d %d = %d : %s ---> Sqrt(%u) = %.2f \n", u, t, h, sum, parity, sum, result);
}
}
return 0;
}
CodePudding user response:
here is the results of your posted code.
This cannot be considered a successful compile. regardless of what the compiler says
gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" -o "untitled.o"
untitled.c: In function ‘main’:
untitled.c:11:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
11 | printf("Number : ");
| ^~~~~~
untitled.c:11:5: warning: incompatible implicit declaration of built-in function ‘printf’
untitled.c:4:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
3 | #include <math.h>
| #include <stdio.h>
4 |
untitled.c:12:5: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
12 | scanf("%u", &number);
| ^~~~~
untitled.c:12:5: warning: incompatible implicit declaration of built-in function ‘scanf’
untitled.c:12:5: note: include ‘<stdio.h>’ or provide a declaration of ‘scanf’
untitled.c:12:13: warning: format ‘%u’ expects argument of type ‘unsigned int *’, but argument 2 has type ‘int *’ [-Wformat=]
12 | scanf("%u", &number);
| ~^ ~~~~~~~
| | |
| | int *
| unsigned int *
| %u
untitled.c:23:26: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
23 | strcpy(parity[7], "even");
| ~~~~~~^~~
| |
| char
In file included from untitled.c:2:
/usr/include/string.h:122:14: note: expected ‘char * restrict’ but argument is of type ‘char’
122 | extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
| ^~~~~~
untitled.c:24:22: warning: conversion from ‘double’ to ‘float’ may change value [-Wfloat-conversion]
24 | result = pow(sum, 2);
| ^~~
untitled.c:25:42: warning: format ‘%s’ expects argument of type ‘char *’, but argument 6 has type ‘int’ [-Wformat=]
25 | printf("%u %u %u = %u : %s ---> %u*%u = %.0f \n", u, t, h, sum, parity[7], sum, sum, result);
| ~^ ~~~~~~~~~
| | |
| char * int
| %d
untitled.c:29:26: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
29 | strcpy(parity[7], "odd");
| ~~~~~~^~~
| |
| char
In file included from untitled.c:2:
/usr/include/string.h:122:14: note: expected ‘char * restrict’ but argument is of type ‘char’
122 | extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
| ^~~~~~
untitled.c:30:22: warning: conversion from ‘double’ to ‘float’ may change value [-Wfloat-conversion]
30 | result = sqrt(sum);
| ^~~~
untitled.c:31:42: warning: format ‘%s’ expects argument of type ‘char *’, but argument 6 has type ‘int’ [-Wformat=]
31 | printf("%u %u %u = %u : %s ---> Sqrt(%u) = %.2f \n", u, t, h, sum, parity[7], sum, result);
| ~^ ~~~~~~~~~
| | |
| char * int
| %d
Compilation finished successfully.