O
obdict
Hello, I have a simple C question regarding scanf()
/* What happens if user inputs a char when he/she is supposed
* to input an integer?
*/
#include<stdio.h>
int main(void){
int n;
printf("Please enter an integer:\n");
scanf("%d", &n);
printf("n is %d.\n", n);
return 0;
}
_____________________
../a.out
Please enter an integer:
Awheofhoeafh
n is 2468992
Any non-integer will result in this magic number 2468992 on my
lab workstation, no matter what non-integer value I provided to it.
Here's some hardware spec.
spidermn:/>cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Pentium(R) 4 CPU 2.80GHz
stepping : 1
cpu MHz : 2793.823
cache size : 1024 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge
mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe pni
monitor ds_cpl cid xtpr
bogomips : 5537.79
I had thought if I input uppercase A, its ASCII code 65 will be output.
In reality, I still got that magic number.
On a different machine in the same room, though, random strings of
characters produced different negative integer each time.
Any idea why? Thanks in advance.
/* What happens if user inputs a char when he/she is supposed
* to input an integer?
*/
#include<stdio.h>
int main(void){
int n;
printf("Please enter an integer:\n");
scanf("%d", &n);
printf("n is %d.\n", n);
return 0;
}
_____________________
../a.out
Please enter an integer:
Awheofhoeafh
n is 2468992
Any non-integer will result in this magic number 2468992 on my
lab workstation, no matter what non-integer value I provided to it.
Here's some hardware spec.
spidermn:/>cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Pentium(R) 4 CPU 2.80GHz
stepping : 1
cpu MHz : 2793.823
cache size : 1024 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge
mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe pni
monitor ds_cpl cid xtpr
bogomips : 5537.79
I had thought if I input uppercase A, its ASCII code 65 will be output.
In reality, I still got that magic number.
On a different machine in the same room, though, random strings of
characters produced different negative integer each time.
Any idea why? Thanks in advance.