将char存储到整数变量中 [英] Store a char into an integer variable

查看:68
本文介绍了将char存储到整数变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个关于scanf的简单C问题()


/ *如果用户在他/她应该输入字符时会发生什么情况

*输入一个整数?

* /


#include< stdio.h>


int main( void){

int n;

printf(" Please enter an integer:\ n");

scanf("%) d",& n);

printf(&n;%is%d。\ n",n);


返回0;

}

_____________________


../a.out


请输入一个整数:

Awheofhoeafh

n是2468992


任何非整数都会在我的
$上产生这个神奇的数字2468992 b $ b实验室工作站,无论我提供给它的是什么非整数值。

这里有一些硬件规格。


spidermn:/> ; cat / proc / cpuinfo

处理器:0

vendor_id:GenuineIntel

cpu系列:15

型号:4

型号名称:英特尔(R)奔腾(R)4 CPU 2.80GHz

踩:1

cpu MHz:2793.823

缓存大小:1024 KB

fdiv_bug:没有

hlt_bug:没有

f00f_bug:没有

coma_bug:没有

fpu:是

fpu_exception:是

cpuid等级:5

wp:是

国旗:FPU VME德PSE TSC MSR PAE MCE CX8 APIC月MTRR PGE

MCA CMOV轻拍PSE36 CLFLUSH DTS ACPI MMX FXSR SSE SSE2 SS HT TM PBE PNI

监视器ds_cpl cid xtpr

bogomips:5537.79


实际上,我仍然有这个神奇的数字。


在同一房间的另一台机器上,随机字符串

字符每次产生不同的负整数。


任何id为什么?在此先感谢。

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.

推荐答案

obdict写道:
您好,我有一个关于scanf的简单C问题()

/ *如果用户在输入一个整数时输入一个字符怎么办?
*输入一个整数?
* /

#包括< stdio.h>

int main(void){
int n;
printf("请输入一个整数:\ n");
scanf("%d",& n);
printf(" n is%d。\ n",n);

返回0;
}
_____________________

./a.out

请输入一个整数:
Awheofhoeafh
n是2468992
任何非整数都会在我的
实验室工作站上产生这个幻数2468992,无论我提供给它的是什么非整数值。
这里有一些硬件规格。
<蜘蛛网:/> cat / proc / cpuinfo
处理器:0
vendor_id:GenuineIntel
cpu系列:15 cpu MHz:2793.823
缓存大小:1024 KB
fdiv_bug:没有
hlt_bug:没有
f00f_bug:没有
coma_bug:没有
FPU:是
fpu_exception:是
CPUID级别:5 <无线电通信/> WP:是
国旗:FPU VME德PSE TSC MSR PAE MCE CX8 APIC九月MTRR PGE
MCA CMOV轻拍PSE36 CLFLUSH DTS ACPI MMX FXSR SSE SSE2 SS HT TM PBE PNI
监视器ds_cpl cid xtpr
bogomips:5537.79

我原本以为如果我输入大写字母A,它的ASCII码就会输出。
实际上,我仍然有那个神奇的数字。然而,在同一个房间的另一台机器上,随机字符串的
字符每次产生不同的负整数。

任何想法为什么?在此先感谢。
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.



因为那是在你的scanf调用之前的n。 (尝试检查返回

scanf值)


Robert


Because that was in n before your scanf call. (try checking the return
value of scanf)

Robert


感谢您的回复。


我输入了Awheofhoeafh在提示符下并得到该号码。

如果我输入其他字符串,它仍会输出相同的数字。

如何在字符串*转换*为整数期间scanf()电话?

Thanks for the reply.

I entered "Awheofhoeafh" at the prompt and got that number.
If I enter other character strings, it still outputs the same number.
And how is a string *converted* to integer during the scanf() call?


2006-02-08,obdict< ha ****** @ gmail.com>写道:
On 2006-02-08, obdict <ha******@gmail.com> wrote:
你好,我有一个关于scanf的简单C问题()

/ *如果用户在他/她被假设时输入一个字符会怎样?
*输入一个整数?
* /
#include< stdio.h>

int main(void){
int n;
printf(请输入一个整数:\ n);
scanf("%d",& n);
printf(" n is%d。\ n" ,n);

返回0;
}
_____________________

./a.out

请输入一个整数:
Awheofhoeafh
n是2468992


如果没有给出

整数,scanf不会修改整数变量。 2468992就是之前已经存在的任何东西。

任何非整数都会在我的
实验室工作站上产生这个幻数2468992,无论我提供给它的是什么非整数值。
这里有一些硬件规格。
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
scanf does not modify the integer variable if it is not given an
integer. 2468992 is just whatever was already in n before.
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.



这篇关于将char存储到整数变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆