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

int main() 
{
    char a[10],b[10]; 
    int i,j,n=0,m=0; 

    scanf("%s %s",a,b); 

    for(i=0;i<strlen(a);i++) 
        n+=a[i]; 

    for(i=0;i<=strlen(b);i++) 
        m+=a[i]; 

    printf("%d",n*m); 

    return 0;
}

input 123 45 means output 54(which came by (1+2+3)*(4+5)), but it doesn't HELP

At line 8 and 9, it shows comparison between signed and unsigned integer expressions, and i get the wrong awnser

input 123 45 means output 54(which came by (1+2+3)*(4+5)), but it doesn't HELP

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    The character '1' is NOT the same thing as the number 1. You are adding ASCII values. – 3Dave Mar 16 '15 at 12:49
  • 1
    1) this is C, not C++; 2) please, format your code, especially when you're talking about line numbers (but not only); 3) see what ASCII code is; 4) it's not very clear what you're asking – Kiril Kirov Mar 16 '15 at 12:51
  • To address the problem in the subject title you'd have to change your `int` variables to `size_t`, which is the type that `strlen` returns. – Jens Gustedt Mar 16 '15 at 13:12
  • Possible duplicate of [How can I fix warnings like: "comparison between signed and unsigned"?](http://stackoverflow.com/questions/859943/how-can-i-fix-warnings-like-comparison-between-signed-and-unsigned) – phuclv Mar 24 '17 at 13:53

2 Answers2

2

Instead of these two loops

for(i=0;i<strlen(a);i++) n+=a[i]; 
for(i=0;i<=strlen(b);i++) m+=a[i]; 
         ^^                  ^^^^

write

for ( i = 0; a[i] != '\0'; i++ ) n += a[i] - '0'; 
for ( i = 0; b[i] != '\0'; i++ ) m += b[i] - '0'; 

Take into account that function scanf is unsafe when is used with character strings. Also you should check whether a next character is a digit for example using standard C function isdigit.

As for the compiler message then function strlen has return type size_t that is some unsigned integral type. You are comparing the value returned by strlen with variable i that you declared as having type signed int. So the compiler warns that the result of the comparison can be not what you are expecting because variable i will be converted to an rvalue of type size_t

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1

strlen is return size_t, you should casting to int. change your code like this:

for(i=0;i<(int) strlen(a);i++) n+=a[i];
  • This does only answer one of the problems and does not add any more value than the other answer and the comments already provide – Michael Kotzjan May 03 '21 at 06:59