为什么第一次而不是第二次正确打印变量"秒"?

Ric*_*gni 1 c printf

首先要说的是,我在编程中是一个绝对的菜鸟,所以它可能是一个非常简单的事情,我没有得到它.

我想知道从一天开始以来已经过了多少时间,为此我使用了这个time()功能.

那么我想打印出来,这里是我的问题:与第一个printf变量seconds打印正确,但在第二个printf(其中我打印mills和seconds)它给了我一个错误的输出.

代码:

#include <stdio.h>
#include <time.h>

int main(void) {
  long long int mills, seconds;

  mills = time(NULL);   
  printf("Mills: %i\n", mills );

  seconds = mills / 1000;

  //here the variable is printed correctly
  printf("Seconds: %i\n", seconds );  

  //here mills gets printed correctly but seconds gets printed as 0
  printf("Milliseconds since midnight: %i\nSeconds since midnight: %i\n", mills, seconds);

  return 0;

}
Run Code Online (Sandbox Code Playgroud)

输出:

Mills: 1486143107
Seconds: 1486143
Milliseconds since midnight: 1486143107
Seconds since midnight: 0
Run Code Online (Sandbox Code Playgroud)

为什么变量第一次正确打印而不是第二次?它应该不一样吗?

AnT*_*AnT 6

long long int通过打印值printf需要%lld(或%lli)格式.你正在使用%i,这是错误的.行为未定义.