2

So I'm just starting to learn C, and after setting up CodeBlocks with the SmallDevice C compiler I began working on some of the programs in the book I'm learning through. it keeps returning this error. Here is the code:

#include <stdio.h>
int main()
{
    int num1, num2, sum;
    printf("Enter two integers: \n");
    scanf("%d %d",&num1,&num2);
    sum=num1+num2;
    printf("Sum: %d",sum);
    return(0);
}

The error it's giving me is

Warning 112: Function 'scanf' implicit declaration
error 101: too many parameters

I went and found a text written up to do the exact same (which gave me the exact same code) and when I placed it in it still gives me this error. Is this a problem with my compiler?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Justc25
  • 23
  • 4

3 Answers3

4

Yes, it's a problem with your compiler or/and your installation. The code has no syntax errors.

gsamaras@pythagoras:~$ pico Justc25_main.c
gsamaras@pythagoras:~$ gcc Justc25_main.c
gsamaras@pythagoras:~$ 

As Werner Henze stated: "It looks like Small Device C compiler is for small embedded devices and does not come with scanf function.".

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • It's not a problem of the compiler. SDCC compiler is designed for a bunch of embedded devices and implementing scanf for standard input is hardware dependent. SDCC developers want to maintain their compiler hardware independent, so no scanf is present. – ronaldo Oct 18 '15 at 11:57
  • 1
    @ronaldo input is hardware dependent, but output is hardware independent?! – Polluks Mar 06 '19 at 17:31
  • Yes, it is, @Polluks. Different hardware platforms have different ways of outputting to their screens (if they even have a screen at all) – ronaldo Mar 07 '19 at 18:14
  • @Polluks sorry man, didn't catch it. It didn't occur to me that your question was rhetorical. I'm not a native speaker :) – ronaldo Mar 08 '19 at 20:46
2

No, it is not a problem with your compiler.

Warning 112: Function 'scanf' implicit declaration

This means that the prototype for scanf is not available in its normal location: stdio.h. Since the compiler cannot find a prototype, it creates one with default parameters and issues the warning. Although it's a warning and not an error, this still may ultimately fail when linking.

The most likely reason is that its standard library does not contain scanf.

Jongware
  • 22,200
  • 8
  • 54
  • 100
0

@jongware is right. SDCC has no scanf implementation, and that's the reason for your warning 112 and subsequent error 101. You can check this yourself by looking for scanf in stdio.h header and in the full source code.

Basically, SDCC developers want their compiler to be platform independent and implementing scanf for embedded devices requires hardware specific knowledge about each platform. For instance, scanf's implementation for an Amstrad CPC is different of that for a MSX, even having both same Z80 processor.

Depending on the platform you are targetting, you may find scanf implementations available. Other option would be to implement getchar for your platform and then use gets.

ronaldo
  • 455
  • 3
  • 9
  • 1
    But printf is also platform dependent! – Polluks Mar 26 '19 at 11:17
  • Well, yes and no. printf itself is usually platform independent, but relies on putchar/putc which have to be implemented for your platform. However, implementing putchar/putc is usually straight forward on many platforms. – ronaldo Mar 27 '19 at 13:25
  • The same for getchar/getc. – Polluks Mar 27 '19 at 14:34