Simple C programs returns too many parameters error

1.7k views Asked by At

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?

3

There are 3 answers

4
gsamaras On BEST ANSWER

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.".

2
Jongware On

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.

3
ronaldo On

@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.