当我要求查看cc的当前版本时,我得到了这个.
$ cc --version
cc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$
Run Code Online (Sandbox Code Playgroud)
我想知道的是使用c89,c90,c99或c11中的哪一个.
为了真正符合标准,C中的所有函数(main除外)都必须有原型,即使它们只是在同一个翻译单元中定义之后才使用它们吗?
所以我以前的一个考试有这个问题,到现在为止我一直在读你不需要任何语言的声明?
哪个是对的?如果没有声明,C++会出错,还是会运行?
我收到与err_sys()此代码相关的错误:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
int sockfd;
if ((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
err_sys("can't create socket");
close(sockfd);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到链接器错误:
/tmp/cciuUVhz.o: In function `main':
getsockopt.c:(.text+0x38): undefined reference to `err_sys'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
功能在哪里err_sys()定义?
我知道函数原型在C++中是强制性的,如果函数是在main()函数之后定义的,但它在C中是可选的(但推荐).我最近编写了一个简单的程序,它执行了2个数字的加法,但错误地使用了点运算符代替传递参数时的逗号.
#include <stdio.h>
int main()
{
printf("sum is: %d",add(15.30)); // oops, uses dot instead of comma
}
int add(int a,int b)
{
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,如果在add(int,int)函数之前定义了main()函数,那么程序肯定无法编译.这是因为调用函数时传递的参数少于所需的参数.
但是,为什么上面的程序编译运行正常 - 给出一些大的垃圾值作为输出?是什么原因?使用函数原型设计是否更好,以便编译器检测到类型不匹配以及与函数调用相关的任何其他错误?
这是未定义的行为吗?
可能的重复:
必须在 C 中声明函数原型吗?
我正在学习 C,在我正在阅读的这本书中,这段代码有一个声明void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols], int scalar);. 即使我不包括这一行,该程序似乎也能工作?
int main(void)
{
void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols], int scalar);
void displayMatrix(int nRows, int nCols, int matrix[nRows][nCols]);
int sampleMatrix[3][5] = {
{ 7, 16, 55, 13, 12},
{ 12, 10, 52, 0, 7 },
{ -2, 1, 2, 4, 9 }
};
scalarMultiply(3, 5, sampleMatrix, 2);
} void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols], int scalar){
int row, column;
for (row …Run Code Online (Sandbox Code Playgroud) 我读到了这个答案: 必须在C中声明函数原型吗?
我的问题更具体:
在使用系统中的程序调用一样access(),open(),creat(),write(),read()...我必须声明每个系统调用函数?这是C的工作方式吗?因为我得到以下内容:
hw1.c: In function ‘main’:
hw1.c:50:9: warning: implicit declaration of function ‘access’ [-Wimplicit-function-declaration]
hw1.c:131:9: warning: implicit declaration of function ‘lseek’ [-Wimplicit-function-declaration]
hw1.c: In function ‘writeFile’:
hw1.c:159:17: warning: implicit declaration of function ‘write’ [-Wimplicit-function-declaration]
Run Code Online (Sandbox Code Playgroud)
基本上,似乎C对我正在使用的每个系统调用函数都很生气.我对C有点新鲜,虽然我知道我必须声明我编写的函数,但我觉得C会知道系统调用函数并且不需要我在代码中明确声明它们.
我需要做这样的事情:
int access(const char *pathname, int mode);
如果是这样,为什么这有意义呢?我使用其他语言,从来没有必要这样做.
我使用系统调用编写了简单的c程序,当我用gcc编译它时它工作得很好。但是当我尝试使用命令生成 shell 代码 (shellforge) 时
./sf.py username.c
Run Code Online (Sandbox Code Playgroud)
我收到错误说明
Undefined reference to memset
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?这是我的代码
#define O_CREAT 0100
#define O_WRONLY 01
#define O_APPEND 02000
#define IDLEN 100
int main(void)
{
char user_name[IDLEN]="";
char pass1[IDLEN]="";
char pass2[IDLEN]="";
int fd,a,b,c;
//to give user direction
char user[]="Please type your username and hit enter:\n";
char p1[]="Please type password and hit enter:\n";
char p2[]="Please type password and hit enter:\n";
write(1,user,sizeof(user));
a=read(0,user_name,IDLEN);
user_name[a-1]='\0';
while(1)
{
write(1,p1,sizeof(p1));
b=read(0,pass1,IDLEN);
pass1[b-1]='\0';
write(1,p2,sizeof(p2));
c=read(0,pass2,IDLEN);
pass2[c-1]='\0';
int temp=0;
while(pass1[temp] == pass2[temp])
{
if(pass1[temp] …Run Code Online (Sandbox Code Playgroud) 我正在阅读 K & R 的书The C programming Language来学习 c。它说
由于函数调用的参数是表达式,因此当参数传递给函数时也会发生类型转换。在没有函数原型的情况下,char和short变成int,float变成double。
在过去的几天里,我一直在努力理解这条线。我认为这是很重要的一点。无论我做出什么假设,它都不会实现。有人可以帮助我清楚地理解它吗?
阅读这个解释了Stack Overflow上找不到的页面上的多语言程序的答案,我很惊讶地读到putchar was used because you don't need any #include to use it。尽管en.cppreference.com 参考和www.cplusplus.com 参考显示putchar如stdio.h标题中所定义,但情况似乎确实如此。
如何在没有声明的情况下(正确)使用函数C?或者是putchar编译器内置的东西(比如sizeof运算符)?