The following code is corrupting "month" variable. If you try 2-2009 it prints 0-2009.
It happens even when I use diferent scanf instructions to read both variables separately.
I just downloaded Dev-Cpp,so I think it is the latest release.
Regards,
Inês
include <stdio.h>
int main() {
unsigned short month, year;
printf("Insert month and year (mm-yyyy):");
scanf("%d-%d", &month, &year);
printf("%d-%d", month, year);
system("pause");
return 1;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes it is actually due to the %d (I should have been more precise and use %hu).
I didn´t think in that because the sample in question worked fine in a previous version.
Thank you!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
By the way, I tend to give answers that are "hints", where you figure out what is going on. This is not some sadistic game on my part, though it might seem that way to some. Congrats on using the hint to REALLY see what the problem was.
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
and the GCC compiler will be able to trap format specifier mismatches.
Better yet use C++ iostream instead of stdio, it determines the format directly from the type through overloading. Note that there is no real benefit in using a short unless you need to match some specific file format, communication protocol or hardware register size. In general use plain int anywhere you can, you code will probably end up more robust for that.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The following code is corrupting "month" variable. If you try 2-2009 it prints 0-2009.
It happens even when I use diferent scanf instructions to read both variables separately.
I just downloaded Dev-Cpp,so I think it is the latest release.
Regards,
Inês
include <stdio.h>
int main() {
}
Yes it is actually due to the %d (I should have been more precise and use %hu).
I didn´t think in that because the sample in question worked fine in a previous version.
Thank you!
Note also that the exact version number for Dev is right on the window frame...
Wayne
By the way, I tend to give answers that are "hints", where you figure out what is going on. This is not some sadistic game on my part, though it might seem that way to some. Congrats on using the hint to REALLY see what the problem was.
Wayne
Add the following compiler options:
-Wformat -Werror
and the GCC compiler will be able to trap format specifier mismatches.
Better yet use C++ iostream instead of stdio, it determines the format directly from the type through overloading. Note that there is no real benefit in using a short unless you need to match some specific file format, communication protocol or hardware register size. In general use plain int anywhere you can, you code will probably end up more robust for that.
Clifford
Memory leak?
Try the following code, see if it works. (It does for me) The fflushes are there for me for the environment (Pure MinGW/MSYS) that I am running...
include <stdio.h>
int main() {
int month, year;
printf("Insert month and year (mm-yyyy):");
fflush(stdout);
scanf("%d-%d", &month, &year);
printf("%d %d\n", month, year);
fflush(stdout);
system("pause");
return 1;
}