goto语句:
C语言中提供了随意滥用的goto语句和标记跳转的符号
最常见的用法终止程序在某些深度嵌套的结构的处理过程,例如一次跳出两层或者多层循环
需要运用到system函数和strcmp函数
system, _wsystem
Execute a command.
int system( const char *command );
int _wsystem( const wchar_t *command );
Routine | Required Header | Compatibility |
system | <process.h> or <stdlib.h> | ANSI, Win 95, Win NT |
_wsystem | <process.h> or <stdlib.h> or <wchar.h> | Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
If command is NULL and the command interpreter is found, the function returns a nonzero value. If the command interpreter is not found, it returns 0 and sets errno to ENOENT. If command is not NULL, system returns the value that is returned by the command interpreter. It returns the value 0 only if the command interpreter returns the value 0. A return value of – 1 indicates an error, and errno is set to one of the following values:
shutdown -s -t -60
-s设置关机 -t设置时间关机 60 60秒之后关机
shutdown -a
-a设置取消关机
system()执行系统命令
strcmp, wcscmp, _mbscmp
Compare strings.
int strcmp( const char *string1, const char *string2 );
int wcscmp( const wchar_t *string1, const wchar_t *string2 );
int _mbscmp(const unsigned char *string1, const unsigned char *string2 );
Routine | Required Header | Compatibility |
strcmp | <string.h> | ANSI, Win 95, Win NT |
wcscmp | <string.h> or <wchar.h> | ANSI, Win 95, Win NT |
_mbscmp | <mbstring.h> | Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
The return value for each of these functions indicates the lexicographic relation of string1 to string2.
Value | Relationship of string1 to string2 |
< 0 | string1 less than string2 |
0 | string1 identical to string2 |
> 0 | string1 greater than string2 |
On an error, _mbscmp returns _NLSCMPERROR, which is defined in STRING.H and MBSTRING.H.
Parameters
string1, string2
Null-terminated strings to compare
Remarks
The strcmp function compares string1 and string2 lexicographically and returns a value indicating their relationship. wcscmp and _mbscmp are wide-character and multibyte-character versions of strcmp. The arguments and return value of wcscmp are wide-character strings; those of _mbscmp are multibyte-character strings. _mbscmp recognizes multibyte-character sequences according to the current multibyte code page and returns _NLSCMPERROR on an error. (For more information, see Code Pages.) These three functions behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine | _UNICODE & _MBCS Not Defined | _MBCS Defined | _UNICODE Defined |
_tcscmp | strcmp | _mbscmp | wcscmp |
The strcmp functions differ from the strcoll functions in that strcmp comparisons are not affected by locale, whereas the manner of strcoll comparisons is determined by the LC_COLLATE category of the current locale. For more information on the LC_COLLATE category, see setlocale.
In the “C” locale, the order of characters in the character set (ASCII character set) is the same as the lexicographic character order. However, in other locales, the order of characters in the character set may differ from the lexicographic order. For example, in certain European locales, the character 'a' (value 0x61) precedes the character 'ä' (value 0xE4) in the character set, but the character 'ä' precedes the character 'a' lexicographically.
In locales for which the character set and the lexicographic character order differ, use strcoll rather than strcmp for lexicographic comparison of strings according to the LC_COLLATE category setting of the current locale. Thus, to perform a lexicographic comparison of the locale in the above example, use strcoll rather than strcmp. Alternatively, you can use strxfrm on the original strings, then use strcmp on the resulting strings.
_stricmp, _wcsicmp, and _mbsicmp compare strings by first converting them to their lowercase forms.Two strings containing characters located between 'Z' and 'a' in the ASCII table ('[', '\', ']', '^', '_', and '`') compare differently, depending on their case. For example, the two strings "ABCDE"
and "ABCD^"
compare one way if the comparison is lowercase ("abcde"
> "abcd^"
) and the other way ("ABCDE"
< "ABCD^"
) if the comparison is uppercase.
//写一个程序控制电脑的开关机及取消开关机 //需要运用到system函数和strcmp函数 #include <stdlib.h> #include <string.h> #include <stdio.h> int main() { char input[20]={0}; system("shutdown -s -t 60"); flag: printf("请注意:\n你的设备将在一分钟之后关机,请输入Treasure取消关机\n"); scanf("%s",input); if (strcmp(input,"Treasure")==0) { system("shutdown -a"); } else { goto flag; } return 0; }