user1661303
user1661303

Reputation: 539

Passing a char as an argument in c

I'm just trying to learn c and I'm stuck. I'm trying to create a program that can take an input of two numbers, together with an operator and print out the answer.

The notation is reversed polish notation. That is to say, the input 2 1 + should give the output 3, the input 2 1 * should give the output 2.

Later, I will expand it so that you can enter longer expressions in rpn with some stack based stuff but lets focus on the case with just two operands for now.

This is what I have done:

#include <stdio.h>

main()
{
    int number1;
    int number2;
    char operator;
    scanf("%d %d %c", &number1, &number2, &operator);
    printf("%d", calculate(number1, number2));
}

int calculate(int number1, int number2)
{
    return number1+number2;
}

This works at suspected and it writes out the sum of number1 and number2. However, when I try passing a character as an argument to the function calculate, like this

#include <stdio.h>

main()
{
    int number1;
    int number2;
    char operator;
    scanf("%d %d %c", &number1, &number2, &operator);
    printf("%d", calculate(number1, number2, operator));
}

int calculate(int number1, int number2, char operator)
{
    return number1+number2;
}

I get a compile error

rpn.c:12:5: error: conflicting types for ‘calculate’
rpn.c:13:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
rpn.c:9:15: note: previous implicit declaration of ‘calculate’ was here

Is it not possible to pass a char as an argument in c? I don't understand why this isn't working when it works with int. I have googled on this a lot, but the questions usually only covers passing an array of chars as an argument, not a char.

Or am I doing this all wrong?

Upvotes: 4

Views: 12721

Answers (4)

Ryhan
Ryhan

Reputation: 1885

You have to tell the compiler about the prototype for the function calculate.

#include <stdio.h>

int calculate(int,int, char); // This is your prototype.
main()
{
    int number1;
    int number2;
    char operator;
    scanf("%d %d %c", &number1, &number2, &operator);
    printf("%d", calculate(number1, number2, operator));
}

int calculate(int number1, int number2, char operator)
{
    return number1+number2;
}

Upvotes: 2

Keith Thompson
Keith Thompson

Reputation: 263307

When the compilers sees your call to calculate, it hasn't yet seen a declaration of calculate.

For starters, add this line before your definition of main:

int calculate(int number1, int number2, char operator);

(Note the semicolon after the ).)

Without that pre-declaration, the call to calculate creates an implicit declaration at the call; that implicit declaration doesn't work the same way (for obscure historical reasons having to do with argument promotions and old-style function declarations; don't worry about the details). That's for the 1990 version of the language. Starting with the 1999 ISO C standard, such implicit function declarations don't even exist. Consider invoking your compiler so it complies to the C99 standard (or even C11 if it supports it). You appear to be using gcc; if so, gcc -std=c99 -pedantic -Wall -Wextra will generally give you more thorough diagnostics.

Some other improvements:

main() should be int main(void).

The format string "%d" should be "%d\n", so it prints a complete line.

(Of course you'll want calculate to pay attention to the value of operator, but what you have is a good start.)

Upvotes: 1

AusCBloke
AusCBloke

Reputation: 18492

Since the compiler doesn't see the definition of calculate() before it's called, it has to create an implicit declaration of it and hope that the declaration is right. It turns out that it's not.

To fix this, either move the definition of calculate() up above main() where it's called, or alternatively provide a function prototype of calculate() to declare what the function looks like before it's called, before main():

int calculate(int number1, int number2, char operator);

Also be aware that if you use the identifier operator for your variables and use a C++ compiler to compile you're code you'll get an error, since operator is a C++ keyword.

Upvotes: 2

T Percival
T Percival

Reputation: 8674

Put the calculate() function above the main() function. The compiler needs to know about the function before it can call it.

Alternatively you can forward-declare it above main by including this line before main():

int calculate(int number1, int number2, char operator);

It will also help to turn on warnings in your compiler. With GCC you can use -Wall.

Upvotes: 3

Related Questions