c语言将字符输入至数组为啥不执行呢
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言将字符输入至数组为啥不执行呢相关的知识,希望对你有一定的参考价值。
这种情况通常发生在前面已经有了输入语句,而当前的scanf是在接收字符(即用%c控制输入)时。由于前面的输入语句(不一定是scanf)把最后输入的'\n'遗留在了输入缓冲区,而当前的scanf("%c",...);又会把'\n'当一个字符接收,又由于scanf在%c控制下只接收一个字符,所以就不能接收正式输入的字符了。解决这一问题的最简单办法是在接收字符的scanf的控制符"%c"中的%前加一个空格写成" %c",把前一次输入遗留在输入缓冲区里的所有广义空格(' '、'\t'、'\n'等)都吸收掉。在接收字符的scanf前加getchar()等的办法其实是有漏洞的——当缓冲区里只遗留了一个广义字符时可正常工作,若多于一个则同样出错。 参考技术A 因为在scanf("%d",&a);读取a的值以后,数字被读走了,但是输入的回车符还留在键盘缓冲区中。等到下一次读取字符或字符串时,就会读到这个残留的回车符。解决的办法是读走这个字符或清空键盘缓冲区(使用fflush(stdin);)本回答被提问者采纳DateTime.UtcNow.ToString() 和DateTime.Now.ToString()输出的字符为啥不一样呢?
DateTime.UtcNow.ToString() 和DateTime.Now.ToString()输出的字符为什么不一样呢?
DateTime.UtcNow.ToString()输出的是0时区的事件,DateTime.Now.ToString()输出的是当前时区的时间,我们中国使用的是东八区的时间,所以差8个小时。 参考技术A utcnow获取的是计算机上的当前日期和时间,表示为协调世界时 (UTC)。从 .NET Framework 2.0 版开始,返回值是 DateTime,它的 Kind 属性返回 DateTimeKind..::.Utc。
这里有一段示例代码 希望可以帮到你
// This code example demonstrates the DateTime Kind, Now, and
// UtcNow properties, and the SpecifyKind(), ToLocalTime(),
// and ToUniversalTime() methods.
using System;
class Sample
public static void Main()
// Get the date and time for the current moment, adjusted
// to the local time zone.
DateTime saveNow = DateTime.Now;
// Get the date and time for the current moment expressed
// as coordinated universal time (UTC).
DateTime saveUtcNow = DateTime.UtcNow;
DateTime myDt;
// Display the value and Kind property of the current moment
// expressed as UTC and local time.
DisplayNow("UtcNow: ..........", saveUtcNow);
DisplayNow("Now: .............", saveNow);
Console.WriteLine();
// Change the Kind property of the current moment to
// DateTimeKind.Utc and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc);
Display("Utc: .............", myDt);
// Change the Kind property of the current moment to
// DateTimeKind.Local and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Local);
Display("Local: ...........", myDt);
// Change the Kind property of the current moment to
// DateTimeKind.Unspecified and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified);
Display("Unspecified: .....", myDt);
// Display the value and Kind property of a DateTime structure, the
// DateTime structure converted to local time, and the DateTime
// structure converted to universal time.
public static string datePatt = @"M/d/yyyy hh:mm:ss tt";
public static void Display(string title, DateTime inputDt)
DateTime dispDt = inputDt;
string dtString;
// Display the original DateTime.
dtString = dispDt.ToString(datePatt);
Console.WriteLine("0 1, Kind = 2",
title, dtString, dispDt.Kind);
// Convert inputDt to local time and display the result.
// If inputDt.Kind is DateTimeKind.Utc, the conversion is performed.
// If inputDt.Kind is DateTimeKind.Local, the conversion is not performed.
// If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
// performed as if inputDt was universal time.
dispDt = inputDt.ToLocalTime();
dtString = dispDt.ToString(datePatt);
Console.WriteLine(" ToLocalTime: 0, Kind = 1",
dtString, dispDt.Kind);
// Convert inputDt to universal time and display the result.
// If inputDt.Kind is DateTimeKind.Utc, the conversion is not performed.
// If inputDt.Kind is DateTimeKind.Local, the conversion is performed.
// If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
// performed as if inputDt was local time.
dispDt = inputDt.ToUniversalTime();
dtString = dispDt.ToString(datePatt);
Console.WriteLine(" ToUniversalTime: 0, Kind = 1",
dtString, dispDt.Kind);
Console.WriteLine();
// Display the value and Kind property for DateTime.Now and DateTime.UtcNow.
public static void DisplayNow(string title, DateTime inputDt)
string dtString = inputDt.ToString(datePatt);
Console.WriteLine("0 1, Kind = 2",
title, dtString, inputDt.Kind);
/*
This code example produces the following results:
UtcNow: .......... 5/6/2005 09:34:42 PM, Kind = Utc
Now: ............. 5/6/2005 02:34:42 PM, Kind = Local
Utc: ............. 5/6/2005 02:34:42 PM, Kind = Utc
ToLocalTime: 5/6/2005 07:34:42 AM, Kind = Local
ToUniversalTime: 5/6/2005 02:34:42 PM, Kind = Utc
Local: ........... 5/6/2005 02:34:42 PM, Kind = Local
ToLocalTime: 5/6/2005 02:34:42 PM, Kind = Local
ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
Unspecified: ..... 5/6/2005 02:34:42 PM, Kind = Unspecified
ToLocalTime: 5/6/2005 07:34:42 AM, Kind = Local
ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
以上是关于c语言将字符输入至数组为啥不执行呢的主要内容,如果未能解决你的问题,请参考以下文章
c语言如果用字符串类型输出字符数组,字符数组最后一个是0,那么为啥会出现乱码
C语言 为啥字符数组倒置输出,输入的最后一个字符没有输出 样例输入 7 ABCDEFG 样例输出 G F E D C B A