Thread: scanf

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    24

    scanf

    Code:
    #include <stdio.h>
    void main() {
    
        char operator,junk;
        float num1,num2,result;
        printf("enter two numbers\n");
        scanf("%f%f",&num1,&num2);
        printf("enter operator\n");
        scanf("%s", &operator);
        switch(operator) {
        case '+' : result=num1+num2;
            break;
        case '-' : result=num1-num2;
            break;
        case '/' : result=num1/num2;
            break;
        case '*' : result=num1*num2;
            break;
        default : printf("error in operator");
            
        }
        
        printf("%f\n",result);
    
    
    }
    hello all
    whatever i enter for operator , the result is always default ? i guess i have issue at getting character with scanf. can anybody tell my mistake ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    can anybody tell my mistake ?
    Perhaps you need to use the correct format specifier for a char?

    Edit: Also main() should be defined to return an int, not void.
    Last edited by jimblumberg; 02-09-2018 at 11:46 AM.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    24
    Quote Originally Posted by jimblumberg View Post
    Perhaps you need to use the correct format specifier for a char?

    Edit: Also main() should be defined to return an int, not void.
    is there important difference between void main and int main ?

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by uryenugurkem View Post
    is there important difference between void main and int main ?
    Yes one is "standard C" and the other is implementation defined

    From the C standard:
    5.1.2.2.1 Program startup

    1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
    Code:
             int main(void) { /* ... */ }
    or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
    Code:
             int main(int argc, char *argv[]) { /* ... */ }
    or equivalent;10) or in some other implementation-defined manner.

    Footnotes
    10) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.

    So if you want to write C that will work on a range of compilers or machines then you should stick to one of the two ways to implement main defined by the standard

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    24
    Quote Originally Posted by Hodor View Post
    Yes one is "standard C" and the other is implementation defined

    From the C standard:


    So if you want to write C that will work on a range of compilers or machines then you should stick to one of the two ways to implement main defined by the standard
    thank you for your intention

  6. #6
    Registered User
    Join Date
    Feb 2018
    Posts
    6
    Hey there
    In my opinion since you only need a single character input to work with your switch block you should maybe try using the getChar() instead of the scanf("%s",...).
    The point is instead of using a String, use a Character.
    Hope this helps.
    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf
    By mnml in forum C Programming
    Replies: 9
    Last Post: 11-19-2011, 07:52 PM
  2. Scanf
    By meandmyipod in forum C Programming
    Replies: 15
    Last Post: 10-03-2011, 03:43 AM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. Help with scanf
    By newprog in forum C Programming
    Replies: 6
    Last Post: 01-29-2003, 09:12 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

Tags for this Thread