关于K&R C中变量名的内容,我有些困惑.原文如下:
至少内部名称的前31个字符是重要的.对于函数名和外部变量,该数字可能小于31,因为外部名称可能由语言无法控制的汇编程序和加载程序使用.对于外部名称,该标准仅保证6个字符和单个案例的唯一性.保留if,else,int,float等关键字:不能将它们用作变量名.它们必须是小写的.选择与变量的目的相关的变量名称是明智的,并且不太可能在排版上混淆.我们倾向于为局部变量使用短名称,尤其是循环索引,以及外部变量的较长名称.
令我困惑的是外部名称,标准保证仅6个字符和单个案例的唯一性.这是否意味着对于外部名称,只有6个前导字符有效且剩余字符都被忽略?例如,我们定义了两个外部变量myexvar1和myexvar2,编译器会将这两个变量视为一个?如果这是真的,为什么他们建议我们使用更长的名称作为外部变量?
有人可以建议为什么Visual Studio 2017不支持C11新功能_Generic吗?我发现它是一个非常有用的功能,但不能在Visual Studio 2017中使用.
以下是示例代码:
#include <stdio.h>
#define MYTYPE(X) _Generic((X),\
int:"int",\
float:"float",\
double:"double",\
default:"other")
int main(void)
{
int d = 5;
printf("%s\n", MYTYPE(d));
printf("%s\n", MYTYPE(2.0*D));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器会给出以下警告和错误:
1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>predef.c
1>c:\users\mia\documents\c\listing 16.13\project1\project1\predef.c(11): warning C4013: '_Generic' undefined; assuming extern returning int
1>c:\users\mia\documents\c\listing 16.13\project1\project1\predef.c(11): error C2059: syntax error: 'type'
1>c:\users\mia\documents\c\listing 16.13\project1\project1\predef.c(12): error C2065: 'D': undeclared identifier
1>c:\users\mia\documents\c\listing 16.13\project1\project1\predef.c(12): error C2059: syntax error: 'type'
1>Done building project "Project1.vcxproj" -- FAILED.
========== …Run Code Online (Sandbox Code Playgroud) 我正在读斯蒂芬普拉塔的"c primer plus".有链表的示例程序.程序使用malloc为结构数组分配内存空间,示例程序的代码如下.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TSIZE 45
struct film{
char title[TSIZE];
int rating;
struct film * next;
};
char * s_gets(char * st,int n);
int main(void)
{
struct film * head =NULL;
struct film * prev, * current;
char input[TSIZE];
puts("Enter first movie title:");
while(s_gets(input,TSIZE)!=NULL && input[0]!='\0')
{
current=(struct film *)malloc(sizeof(struct film));
if(head==NULL)
head=current;
else
prev->next=current;
current->next=NULL;
strcpy(current->title,input);
puts("Enter your rating <0-10>:");
scanf("%d",¤t->rating);
while(getchar()!='\n')
continue;
puts("Enter next movie title (empty line to stop):");
prev=current;
} …Run Code Online (Sandbox Code Playgroud) 我正在网上阅读GNU C PROGRAMMING TUTORIAL,并对低级读写的代码示例感到困惑.
代码如下:
#include <stdio.h>
#include <fcntl.h>
int main()
{
char my_write_str[] = "1234567890";
char my_read_str[100];
char my_filename[] = "snazzyjazz.txt";
int my_file_descriptor, close_err;
/* Open the file. Clobber it if it exists. */
my_file_descriptor = open (my_filename, O_RDWR | O_CREAT | O_TRUNC);
/* Write 10 bytes of data and make sure it's written */
write (my_file_descriptor, (void *) my_write_str, 10);
fsync (my_file_descriptor);
/* Seek the beginning of the file */
lseek (my_file_descriptor, 0, SEEK_SET);
/* Read 10 bytes …Run Code Online (Sandbox Code Playgroud)