Thread: help with scanf, is this the most efficient way.

  1. #1
    DIY
    Guest

    help with scanf, is this the most efficient way.

    Is this the correct way to use scanf. And is there a more efficient way or is there another way that uses less memory. I know this is a very simple example but i'm just starting out. I want to learn to write correct, effiecient and portable code. BTW I am using Dev 4.0. Thanks for your input.


    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int a = 0, b = 0;

    scanf("%d %d", &a, &b);

    printf("The numbers are %d and %d.\nThe sum is %d.\nThe differenceis %d.\nThe product is %d.\n\n", a, b, a + b, abs(a - b), abs(a * b));

    system("PAUSE");
    return 0;
    }

  2. #2
    DIY
    Guest
    Does putting the computation in the statement use less memory than asigning the computation to a variable?

    Code:
    printf("The numbers are %d and %d.\nThe sum is %d.\nThe differenceis %d.\nThe product is %d.\n\n", a, b, a + b, abs(a - b), abs(a * b));

  3. #3
    Unregistered
    Guest
    You might want to prompt the user to make an input.

  4. #4
    DYI
    Guest
    I understand that. It is just for an example.

  5. #5
    Unregistered
    Guest
    I want to learn to write correct, effiecient and portable code.
    That's why I pointed out the need to prompt the user.

    Does putting the computation in the statement use less memory than asigning the computation to a variable?
    It certainly makes your code harder to read.

    The best way to write code is to present it in as simple a style as possible.

    Your computations should be separate and distinct, so it becomes more intuitive to the unfamiliar reader.

  6. #6
    DIY
    Guest
    I did not find it hard to read but I take note of your suggestion. I rewrote it and hope to have my original concerns addressed. Thank you for the advice.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is this the correct way to use scanf.
    Very close:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int a = 0, b = 0;
      
      printf ( "Enter two numbers: " );
      /*
      ** Always check return values.
      */
      if ( scanf ( "%d %d", &a, &b ) == 2 ) {
        printf ( "A\tB\tSum\tDiff\tProd\n" );
        printf ( "%d\t%d\t%d\t%d\t%d\n",
          a, b, a + b, abs ( a - b ), abs ( a * b ) );
      }
      else
        perror ( "Invalid input" );
      /*
      ** system() is not very portable because it's
      ** argument is platform dependent. This works
      ** just as well.
      */
      printf ( "Press return to continue" );
      (void)getchar();
      /*
      ** When you include stdlib.h, it never hurts to
      ** use the EXIT_SUCCESS macro to improve readability.
      */
      return EXIT_SUCCESS;
    }
    >I want to learn to write correct, effiecient and portable code.
    You will want to invest in several good books on C as well as the ISO C standard. Refer to them for everything until you are confident in your knowledge. Another excellent idea is to get a Lint program for C which will warn you about all kinds of subtle problems concerning syntax, semantics, portability, etc... My favorite Lint is LCLint/Split which can be found here. It takes some time to get used to so many warnings, but you'll find your code improves dramatically with regular Linting.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    My mistake about the system call. I was just using it to keep the console open. Didn't know any other way to do it and I forgot to delete it before I posted.

    So it is there is no need to assign a variable to the computation:

    Code:
    answer = a + b ;    //does this uses more memory? Or the same?
    printf("%d", answer) ;
    It is beter to:

    Code:
    printf("%d", a + b) ;

  9. #9
    DIY
    Guest
    The more variables the more memory I guess. That makes sense.
    Thanks for all the posts.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >does this uses more memory?
    It uses more, but in this day and age the increase is negligible so you should concentrate more on readability than memory usage and speed. There is a time for such optimizations, but not often for the average programmer. Use what you find easiest to read and most comfortable to write and those who maintain your code with thank you for it.

    -Prelude
    My best code is written with the delete key.

  11. #11
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    >> I want to learn to write correct, effiecient and portable code.<<

    I know prelude has already brought this up, but I strongly discourage people from using a system calls. System calls reduce the efficiency of your program - the CPU time dedicated to a system call is considerable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 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