简体   繁体   中英

(C) Displaying multiple variables on the printf line, having some trouble

I am testing a program midway through, and noticed that printf is displaying some of my stored values as 0's. Particularly when I try to, using printf, display them in one command. They work fine individually, I have checked this. Here's my code:


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

/* Initialization of variables */
double int1;
double int2;
double int3;
double int4;
double int5;

/* Initialization of calculated values */
double a_mean;
double g_mean;
double h_mean;
double st_dev;
n = 5; /* Total number of integers for summation use */

int main()
{
    printf("Enter 5 integers, pressing 'Enter' after each:\n"); /* Prompts user input */

        /* Scans in entered integer values (5) */
        scanf("%d", &int1);
        scanf("%d", &int2);
        scanf("%d", &int3);
        scanf("%d", &int4);
        scanf("%d", &int5);

                    /* Calculations */

        /* Output */
        printf("The five integers entered were:\n");
        printf("%d, %d, %d, %d, and %d", int1, int2, int3, int4, int5);    

    return 0;
    }

Command Prompt Input:

Enter 5 integers, pressing 'Enter' after each: 1 2 3 4 5


Command Prompt Output:

The five integers entered were: 1, 0, 2, 0, and 3


By the way, ignore the missing /* Calculations */ section in the middle, I am just testing my current progress... But see how I entered 1,2,3,4,5 and only some values print correctly? On individual printf lines they work. What am I doing wrong??

Though you names these variables int1 etc, their type is double ! So either declare them as int or if you choose to use double :

Change

scanf("%d", &int1);

to

scanf("%lf", &int1);

and give them better names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM