0
#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
void main(void)
{
    int a,b,sub,i,count=0;char buffer[20],w[20];
    scanf("%d",&a);
    scanf("%d",&b);
    sub=a-b;
    if(sub>0)
    {
        scanf("%s",w); /*wrong answer*/
        itoa(sub,buffer,10);
        int l=strlen(buffer);
        for(i=0;i<l;i++)
        {
            if(w[i]==buffer[i])
                count++;
        }
        ((count==l || count==l-1) && w[0]!='0') ? printf("accepted") : printf("Not accepted");
    }
    else printf("Sub operation returned negative value");
}

The output of the compiler is

prog.c:4:6: warning: return type of 'main' is not 'int' [-Wmain]
 void main(void)
      ^
prog.c: In function 'main':
prog.c:13:1: warning: implicit declaration of function 'itoa' [-Wimplicit-function-declaration]
 itoa(sub,buffer,10);
 ^
/home/HCDVgj/ccHBnrc7.o: In function `main':
prog.c:(.text.startup+0x61): undefined reference to `itoa'
collect2: error: ld returned 1 exit status

I did not use any long int variables but i still get ld return 1 exists. how to debug these errors

3
  • 1
    1) Fix the return type of main. It's not relevant to the question, but is just bad practice IMO. 2) How did you build your program? Commented Jan 3, 2017 at 6:37
  • It's not 'the output of this program'. Your program does not make any output, because it does not run. It's output of a compiler. Commented Jan 3, 2017 at 6:38
  • Read your warnings they spell it out. See See What should main() return in C and C++? Commented Jan 3, 2017 at 6:39

2 Answers 2

2
  • int main() This is teh first thing you should do.

  • scanf("%19s",w); This is better.

  • itoa is non standard (so you will not find it in any standard implementaion) better use snprintf()

  • printf('%s",((count==l || count==l-1) && w[0]!='0') ? "accepted" : "Not accepted"); More compact I would say.

Use of snprintf

snprintf(target, size_of_target1, "%d", source2);

sprintf takes no parameter specifying the number of bytes to write which may lead to buffer overflow which is not a good thing.

1 : In bytes
2 : source is in integer here

Few things worth mentioning to clear your idea or to be more precise

  • The output you specified is not output of the c program ... in the process of compilation your compiler run into error and then it generates those output. -CiaPan

  • main() shouldn't be of void return type it is expected to return 0 in case of normal termination. Abnormal termination is usually signaled by a non-zero.-David C. Rankin

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know why you're talking about "long int variables".

The first warning can be fixed by changing void main(void) to int main(void) and adding a return 0; at the end.

The second warning tells you that the compiler doesn't know what itoa is.

The following linker error tells you that the linker (ld) also doesn't know what itoa is, and that's why compilation fails.

The reason itoa is unknown is that it's not a standard function and not available on your platform.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.