SAK
SAK

Reputation: 13

Simple C program is not storing the correct value of the input taken in the variable

Can anybody tell me where why the correct value of input is not being stored in this program?

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

int main()
{
    int num, sum=0;
    printf("Enter the number: ");
    while(num!=0)
    {
        sum = sum+num;
        scanf("%d", &num);
    }
    printf("Answerr = %d", sum);
    return 0;
}

Here's the output:

Enter the number: 2
0
Sum = 10

Upvotes: 0

Views: 308

Answers (5)

Marievi
Marievi

Reputation: 5011

You cannot know what exactly this part will do :

while (num != 0)
{
    sum = sum + num;
    scanf("%d", &num);
}

because num is not initialized, so you are adding to sum a value which you do not know. Change it to :

while(num != 0)
{
    scanf("%d", &num);
    sum = sum + num;
}

so that num has a value when you add it, and also initialize num to something different than 0, for example :

int num = 2;

so that your while loop is executed at least one (in other words, so that you get the chance to read num).


A better approach would be to use a loop like this :

int num = 0;
do
{
    scanf("%d", &num);
    sum = sum + num;
}while (num != 0);

so as to be sure that your loop will be executed at least once. Even with this approach you should still initialize num in case scanf fails (and therefore num does not get a value).

In order to check the return value of scanf, use this piece of code :

if ( scanf("%d", &num) == 1)
    sum = sum + num;

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

The variable num was not initialized. As result the loop has undefined behavior. Also you should check whether the input was valid before adding values. Take into account that neither declaration from the header <stdlib.h> is used. So you may remove the header.

The program can look the following way

#include <stdio.h>

int main( void )
{
    long long int sum = 0;

    while ( 1 )
    {
        int num;

        printf( "Enter number (0 - exit): " );

        if ( scanf( "%d", &num) != 1 || num == 0 ) break;

        sum += num;
    }

    printf( "\nAnswer = %lld\n", sum );

    return 0;
}

Or you could place the prompt before the loop as for example

#include <stdio.h>

int main( void )
{
    long long int sum = 0;

    printf( "Enter numbers (0 - exit): " );

    while ( 1 )
    {
        int num;

        if ( scanf( "%d", &num) != 1 || num == 0 ) break;

        sum += num;
    }

    printf( "\nAnswer = %lld\n", sum );

    return 0;
}

And according to the C Standard function main without parameters shall be declared like

int main( void )

Upvotes: 0

Mohammad Wasim
Mohammad Wasim

Reputation: 199

you are adding value of num before reading it do like this

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

int main()
{
    int num, sum=0;
    printf("Enter the number: ");
    do{
        scanf("%d", &num);
         sum = sum+num;

    }
    while(num!=0);

    printf("Answerr = %d", sum);
    return 0;
}

Upvotes: 0

Moerwald
Moerwald

Reputation: 11254

Change your code to:

int main()
{
    int num = 0, sum = 0;
    printf("Enter the number: ");
    do
    {
        scanf_s("%d", &num);
        sum = sum + num;
    } while (num != 0);

    printf("Answer = %d", sum);
    return 0;
}

I replaced the whileloop with a do while one. You've to initialize sum, otherwise you'll work with an undefined value in your first run ( if working with a while loop).

Upvotes: 0

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

Better do:

num= 0;
do
{
    scanf("%d", &num);
    sum = sum+num;
} while(num!=0);

Note the initialization of num is still needed as scanf could fail which would not affect num.

Upvotes: 3

Related Questions