I'm new to C programming. I write a Basic C code like
我是C编程的新手。我写了一个基本的C代码
#include<stdio.h>
main()
{
int t =
5
;
printf("%d\n",t
);
}
Is it going to produce a error?
是否会产生错误?
Well, my questing is like
好吧,我的任务就像
int x = 9;
and
int x = //Finish this instruction in 3 lines instead of one!
9
;
Both are correct? Can i end a instruction in by using multiple line
两者都是正确的?我可以使用多行结束指令吗?
3 个解决方案
#1
3
Re-written answer after question has been clarified.
澄清问题后重写的答案已经澄清。
Yes, the position of the semi-colon doesn't matter. Whitespace (space, newline, tab) generally don't matter in C. It's not a line-oriented language, where source code must be rigidly structured into lines. There are lines, but they're not very important. Almost always, a newline can be changed into a space (or even removed) without changing the meaning of the code.
是的,分号的位置无关紧要。空格(空格,换行符,制表符)通常在C中无关紧要。它不是面向行的语言,源代码必须严格地构造成行。有线,但它们不是很重要。几乎总是可以在不改变代码含义的情况下将换行符更改为空格(甚至删除)。
These are all equivalent:
这些都是等价的:
int x = 1;
int y=1;
int z=1 ;
int
w
=
1
;
You should check out some Obfuscated C entries for how this can be used to creative effect.
您应该查看一些混淆的C条目,了解如何将其用于创造性效果。
#2
1
In olden K&R days before the ANSI C standard, the default type for any function or variable was int
so you could (and people frequently did) omit the type specifier in a declaration if the type was meant to be int
. This was idiomatic, particularly in the case of the main()
function (Even "The C Programming Language second edition" used the convention even though it was allegedly based on ANSI C). There was also no void
type so you could not specify that a function returned nothing. Functions always returned something, even if it was a garbage value.
在ANSI C标准之前的旧K&R天数中,任何函数或变量的默认类型都是int,因此如果类型是int,则可以(并且人们经常这样做)省略声明中的类型说明符。这是惯用的,特别是在main()函数的情况下(即使“C编程语言第二版”使用了惯例,即使它据称基于ANSI C)。也没有void类型,因此您无法指定函数什么都不返回。函数总是返回一些东西,即使它是垃圾值。
Beginning with (I think) C99, it was decided that defaulting the type to int
was a really bad idea and so it is technically not allowed. However, for backward compatibility, compilers just emit a warning when they come across it.
从(我认为)C99开始,决定将类型默认为int是一个非常糟糕的主意,因此从技术上讲是不允许的。但是,为了向后兼容,编译器只会在遇到它时发出警告。
You can silence the warning with either
您可以使用其中一个来静音警告
int main(void)
or
int main(int argc, char*argv[])
Those are the two "official" portable definitions in C11.
这些是C11中的两个“官方”便携式定义。
While you are at it, although you do not need to specify a return
statement in main
, it's good practice to do so. Add
虽然你在这里,虽然你不需要在main中指定一个return语句,但最好这样做。加
return 0;
to the end of the main
function.
到主要功能的结尾。
EDIT
The question has changed while I was writing the answer.
在我写答案时,问题已经改变了。
As regards splitting statements across lines like in the question. There is no problem with doing this as far as the compiler is concerned. White space (space, tab, newline, form feed) is not significant in C. The only place where you need it is where it would make two tokens run in to one e.g. intmain
obviously wouldn't work and everywhere one white space is legal so is any amount of white space.
关于在问题中跨行分割语句。就编译器而言,这样做没有问题。白色空间(空格,制表符,换行符,换页符)在C中并不重要。唯一需要它的地方是它会使两个令牌运行到一个例如intmain显然不会工作,到处都是一个空白区是合法的,所以任何数量的空白区域。
Also comments can appear pretty much anywhere you can put white space. e.g.
此外,评论可以出现在任何可以放置空格的地方。例如
int/* blah */foo = 5;
Is legal
#3
-1
in C your do function declaration like that :
在C你做的函数声明:
int main(int argc, char **argv)
int -> return type of the function. This function will return an interger
int - >返回函数的类型。此函数将返回一个整数
main -> function's name (here the main function of your programm)
main - > function的名字(这里是你的程序的主要功能)
(int argc, char **argv) -> the paranthesis, showing it's a function, and the parameters of the function; what it takes as input.
(int argc,char ** argv) - > paranthesis,显示它是一个函数,以及函数的参数;它需要什么作为输入。
just few advices don't write semicolon on a new line it's a bad practice
只有少数建议不会在新行上写分号,这是一个不好的做法
#1
3
Re-written answer after question has been clarified.
澄清问题后重写的答案已经澄清。
Yes, the position of the semi-colon doesn't matter. Whitespace (space, newline, tab) generally don't matter in C. It's not a line-oriented language, where source code must be rigidly structured into lines. There are lines, but they're not very important. Almost always, a newline can be changed into a space (or even removed) without changing the meaning of the code.
是的,分号的位置无关紧要。空格(空格,换行符,制表符)通常在C中无关紧要。它不是面向行的语言,源代码必须严格地构造成行。有线,但它们不是很重要。几乎总是可以在不改变代码含义的情况下将换行符更改为空格(甚至删除)。
These are all equivalent:
这些都是等价的:
int x = 1;
int y=1;
int z=1 ;
int
w
=
1
;
You should check out some Obfuscated C entries for how this can be used to creative effect.
您应该查看一些混淆的C条目,了解如何将其用于创造性效果。
#2
1
In olden K&R days before the ANSI C standard, the default type for any function or variable was int
so you could (and people frequently did) omit the type specifier in a declaration if the type was meant to be int
. This was idiomatic, particularly in the case of the main()
function (Even "The C Programming Language second edition" used the convention even though it was allegedly based on ANSI C). There was also no void
type so you could not specify that a function returned nothing. Functions always returned something, even if it was a garbage value.
在ANSI C标准之前的旧K&R天数中,任何函数或变量的默认类型都是int,因此如果类型是int,则可以(并且人们经常这样做)省略声明中的类型说明符。这是惯用的,特别是在main()函数的情况下(即使“C编程语言第二版”使用了惯例,即使它据称基于ANSI C)。也没有void类型,因此您无法指定函数什么都不返回。函数总是返回一些东西,即使它是垃圾值。
Beginning with (I think) C99, it was decided that defaulting the type to int
was a really bad idea and so it is technically not allowed. However, for backward compatibility, compilers just emit a warning when they come across it.
从(我认为)C99开始,决定将类型默认为int是一个非常糟糕的主意,因此从技术上讲是不允许的。但是,为了向后兼容,编译器只会在遇到它时发出警告。
You can silence the warning with either
您可以使用其中一个来静音警告
int main(void)
or
int main(int argc, char*argv[])
Those are the two "official" portable definitions in C11.
这些是C11中的两个“官方”便携式定义。
While you are at it, although you do not need to specify a return
statement in main
, it's good practice to do so. Add
虽然你在这里,虽然你不需要在main中指定一个return语句,但最好这样做。加
return 0;
to the end of the main
function.
到主要功能的结尾。
EDIT
The question has changed while I was writing the answer.
在我写答案时,问题已经改变了。
As regards splitting statements across lines like in the question. There is no problem with doing this as far as the compiler is concerned. White space (space, tab, newline, form feed) is not significant in C. The only place where you need it is where it would make two tokens run in to one e.g. intmain
obviously wouldn't work and everywhere one white space is legal so is any amount of white space.
关于在问题中跨行分割语句。就编译器而言,这样做没有问题。白色空间(空格,制表符,换行符,换页符)在C中并不重要。唯一需要它的地方是它会使两个令牌运行到一个例如intmain显然不会工作,到处都是一个空白区是合法的,所以任何数量的空白区域。
Also comments can appear pretty much anywhere you can put white space. e.g.
此外,评论可以出现在任何可以放置空格的地方。例如
int/* blah */foo = 5;
Is legal
#3
-1
in C your do function declaration like that :
在C你做的函数声明:
int main(int argc, char **argv)
int -> return type of the function. This function will return an interger
int - >返回函数的类型。此函数将返回一个整数
main -> function's name (here the main function of your programm)
main - > function的名字(这里是你的程序的主要功能)
(int argc, char **argv) -> the paranthesis, showing it's a function, and the parameters of the function; what it takes as input.
(int argc,char ** argv) - > paranthesis,显示它是一个函数,以及函数的参数;它需要什么作为输入。
just few advices don't write semicolon on a new line it's a bad practice
只有少数建议不会在新行上写分号,这是一个不好的做法