第一个C程序 [英] First C program

查看:73
本文介绍了第一个C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这是我的第一个C程序。我开始用Python编程。请查看

来源,如果我做错了,请告诉我。我想

从C开始吧,所以如果你看到任何错误,请告诉我,当我在学习时,我需要学习!


谢谢!!!

Hi Guys,

This is my first C program. I started programming in Python. Please look
over this source and let me know if I''m doing anything wrong. I want to
start out right with C, so if you see anything wrong tell me now while I
am learning!

Thanks !!!

#include< stdio.h>

int octet0; / *声明我们的IP范围的第一个八位字节* /
int octet1; / *声明我们的IP范围的第二个八位字节* /
int octet2; / *声明我们的IP范围的第三个八位字节* /
int octet3; / *声明我们的IP范围的第四个八位字节(我们生成的那个)* /

FILE * outputFile; / *声明我们将IP范围写入的文件* /

int main()/ *这是声明main的正确方法...不要使用void main()* /
{

octet0 = 192; / *第一个八位字节的值* /
octet1 = 168; / *第二个八位字节的值* /
octet2 = 1; / *第三个八位字节的值* /

outputFile = fopen(" ips_c.txt"," wt"); / *创建一个文本文件,在写入模式下打开它* /
for(octet3 = 1; octet3< 255; ++ octet3){
/ * printf("%d。%d。 %d。%d \ n",octet0,octet1,octet2,octet3); * /
fprintf(outputFile,"%d。%d。%d。%d \ n",octet0,octet1,octet2,octet3);
}
fclose(outputFile) ;
printf(\ n'文件''ips_c.txt''已成功生成... \ n \ n");
返回(0);
}
#include <stdio.h>

int octet0; /* Declare first octet of our IP range */
int octet1; /* Declare second octet of our IP range */
int octet2; /* Declare third octet of our IP range */
int octet3; /* Declare fourth octet (the one we generate) of our IP range */

FILE *outputFile; /* Declare the file in which we will write the IP range to */

int main() /* This is the correct way to declare main... do not use void main() */
{

octet0 = 192; /* Value of first octet */
octet1 = 168; /* Value of second octet */
octet2 = 1; /* Value of third octet */

outputFile = fopen("ips_c.txt", "wt"); /* Create a text file, open it in write mode */
for (octet3 = 1; octet3 < 255; ++octet3) {
/* printf ("%d.%d.%d.%d\n", octet0, octet1, octet2, octet3); */
fprintf (outputFile, "%d.%d.%d.%d\n", octet0, octet1, octet2, octet3);
}
fclose(outputFile);
printf ("\nThe file ''ips_c.txt'' was generated successfully...\n\n");
return(0);
}





推荐答案

" hokiegal99" <豪******** @ hotmail.com>在留言中写道

新闻:3F ************** @ hotmail.com ...
"hokiegal99" <ho********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
大家好,

这是我的第一个C程序。我开始用Python编程。请查看
这个来源,如果我做错了,请告诉我。我想用C开始吧,所以如果你看到任何错误,请告诉我,而我正在学习!


我的儿子已经犯了你的第一个罪。 :-)


第一个应该编写的C程序是经典的

Hello world。


但是让我们一起来看看。


首先,如果你在这里共享代码,如果可能发布

* compilable *代码。当然,对于任何人来说,发现任何语法错误的最快方法是将代码粘贴到

编译器中并进行编译。我这样做了,但首先我花了很多时间来删除所有那些>字符。实际上,

他们的存在让我怀疑你复制了

a新闻组的帖子或电子邮件。


另外,学会了缩进你的代码。您的教科书中的示例

代码是什么样的?它是否全部保持一致

那样的?您正在阅读哪些教科书?

谢谢!!!
Hi Guys,

This is my first C program. I started programming in Python. Please look
over this source and let me know if I''m doing anything wrong. I want to
start out right with C, so if you see anything wrong tell me now while I
am learning!
Thou hast already committed thy first sin, my son. :-)

The first C program one should write is the classic
"Hello world".

But let''s take a look.

First, if you share code here, if at all possible post
*compilable* code. Of course the fastest way for anyone
to spot any syntax errors is to paste your code into a
compiler and compile it. This I did, but first I had
to spend time deleting all those > characters. Actually,
their presence makes me suspect you copied that out of
a newsgroup post or email message.

Also, learn to indent your code. What does the example
code in your textbook look like? Is it all left aligned
like that? Which textbook(s) are you reading?


Thanks !!!
#include< stdio.h>

int octet0; / *声明我们的IP范围的第一个八位字节* /
int octet1; / *声明我们的IP范围的第二个八位字节* /
int octet2; / *声明我们的IP范围的第三个八位字节* /
int octet3; / *声明我们的IP
范围的第四个八位字节(我们生成的那个)* /


1.这些评论比无用的更糟糕。它们只会使代码混乱,并且不会提供额外的信息。您正在使用的标识符

名称(btw相当不错)已经

在评论中传达信息(IP部分除外)


如果我评论这部分,我会写一些类似的东西:


/ * IP地址八位字节* /

int octet0;

int octet1;

int octet2;

int octet3;


实际上我会将这些值放在一个数组中,但是你可能还没有那么多的语言。


2。没有理由将这些放在文件范围内。他们应该在你的''main()''函数里面进行它们的使用。

FILE * outputFile; / *声明我们将IP
范围写入的文件* /


另一个无用的评论。你的(又好)标识符名称

''outputFile''讲述了这个故事:它是一个输出文件。如果你想要传达该文件将包含''IP地址'',

或许像''IPFileOut'这样的名字可能更接近。


这个对象也应该在''main()''

函数中定义,而不是在这里。

int main() / *这是声明main的正确方法...不要使用void
main()* /


正确。虽然首选的是


int main(无效)


{

octet0 = 192; / *第一个八位字节的值* /
octet1 = 168; / *第二个八位字节的值* /
octet2 = 1; / *第三个八位字节的值* /


更多无用的评论。


此外,这些值可以在定义时指定
带有初始化程序的
(这些定义应该在

这个函数里面):


int octet0 = 192;

int octet1 = 168;

int octet2 = 1;

int octet3 = 0;

/ *技术上初始化''octet3''是没必要,

因为你在使用之前给它分配了一个值。但是

我发现*总是*初始化*每个*对象

*一些*有效值是一个很好的防御练习。如果你没有给出一个初始值,并且在尝试访问它之前忘了这么做,那么你就会得到未定义的行为。

不好(tm)* /

outputFile = fopen(" ips_c.txt"," wt"); / *创建一个文本文件,在
写模式下打开它* /


wt没有标准的打开模式。除非另有说明,否则C流默认为'b $ b''文本模式'。

只需使用w。


另外,非常重要的是你在尝试触摸文件之前检查对

''fopen()'的调用是否成功。

如果''fopen() ''失败了,任何i / o的尝试都会导致,

你猜对了,未定义的行为。 :-)


if(outputFile == NULL)

{

puts(无法打开输入);

返回EXIT_FAILURE; / *''EXIT_FAILURE''需要#include< stdlib.h>

}


} for(octet3 = 1; octet3< 255; + + octet3){


这将迭代''octet3''的值为1到

254(含)。如果您想要包含255,请更改

< to< =或与<对比256我也看到你

从1开始而不是零。这个价值范围可能是你想要的价值,我不知道。请注意

以防万一。

/ * printf("%d。%d。%d。%d \ n",octet0,octet1 ,octet2,octet3); * /
fprintf(outputFile,"%d。%d。%d。%d \ n",octet0,octet1,octet2,
octet3);


你需要检查''fprintf()''在这里成功。

查看编译器手册或教科书以了解具体方法。

}
fclose(outputFile);
printf(\ n'文件''ips_c.txt''已成功生成... \ n \ n");
return(0);


Nitpick:这是有效的,但括号不需要
。 ''return''不是一个函数,它是一个关键字。

}
#include <stdio.h>

int octet0; /* Declare first octet of our IP range */
int octet1; /* Declare second octet of our IP range */
int octet2; /* Declare third octet of our IP range */
int octet3; /* Declare fourth octet (the one we generate) of our IP range */

1. Those comments are worse than useless. They only clutter
the code and give no additional information. The identifier
names you''re using (which are pretty good ones btw) already
convey the information in the comments (except the ''IP'' part)

If I commented this part at all, I''d write something like:

/* IP address octets */
int octet0;
int octet1;
int octet2;
int octet3;

Actually I''d put these values in an array, but you''re probably
not that far with the language yet.

2. There''s no reason to put these at file scope. They should
go inside your ''main()'' function, where they''re used.

FILE *outputFile; /* Declare the file in which we will write the IP range to */

Another useless comment. Your (again good) identifier name
''outputFile'' tells the story : it''s an output file. If you
want to convey that the file will contain ''IP addresses'',
perhaps a name like ''IPFileOut'' or similar might be closer.

This object should also be defined inside your ''main()''
function, not here.

int main() /* This is the correct way to declare main... do not use void main() */

Correct. Though preferred would be

int main(void)

{

octet0 = 192; /* Value of first octet */
octet1 = 168; /* Value of second octet */
octet2 = 1; /* Value of third octet */
More useless comments.

Also, these values could be specified at definition time
with initializers (these definitions should be inside
this function anyway):

int octet0 = 192;
int octet1 = 168;
int octet2 = 1;
int octet3 = 0;
/* technically initialization of ''octet3'' is not necessary,
since you assign it a value below before using it. But
I find that *always* initializing *every* object with
*some* valid value is a good defensive practice. If
you don''t give an initial value, and forget to do so
before trying to access it, you get ''undefined behavior.''
Not Good(tm) */

outputFile = fopen("ips_c.txt", "wt"); /* Create a text file, open it in write mode */

There''s no standard ''open mode'' of "wt". C streams are
''text mode'' by default, unless otherwise specified.
Just use "w".

Also, very important is that you check that the call to
''fopen()'' succeeded before trying to touch the file.
If ''fopen()'' failed, any attempts at i/o result in,
you guessed it, undefined behavior. :-)

if(outputFile == NULL)
{
puts("Cannot open input");
return EXIT_FAILURE; /* ''EXIT_FAILURE'' needs #include <stdlib.h>
}

} for (octet3 = 1; octet3 < 255; ++octet3) {
This will iterate for values of ''octet3'' of 1 through
254 inclusive. If you want to include 255, change the
< to <= or compare against 256 with <. I also see you
start with 1 and not zero. This range of values may be
what you want, I don''t know. Just drawing attention to
it just in case.
/* printf ("%d.%d.%d.%d\n", octet0, octet1, octet2, octet3); */
fprintf (outputFile, "%d.%d.%d.%d\n", octet0, octet1, octet2, octet3);

You need to check that ''fprintf()'' succeeded here.
See your compiler manual or textbook to find out how.
}
fclose(outputFile);
printf ("\nThe file ''ips_c.txt'' was generated successfully...\n\n");
return(0);
Nitpick: This is valid, but the parentheses are not
needed. ''return'' is not a function, it''s a keyword.
}




总而言之,不是坏。但是,你首先应该写下Hello world和Hello world。程序。现在出去吧

罪不再了。 :-)


-Mike



All in all, not bad. But again, you first should have
written the "Hello world" program. Now go forth and
sin no more. :-)

-Mike


On Tue,2003年9月30日16:25:14 +0000,hokiegal99写道:
On Tue, 30 Sep 2003 16:25:14 +0000, hokiegal99 wrote:
大家好,
这是我的第一个C程序。我开始用Python编程。请查看
这个来源,如果我做错了,请告诉我。我想用C开始吧,所以如果你看到任何错误,请告诉我我正在学习的时候!

谢谢!!!
Hi Guys,

This is my first C program. I started programming in Python. Please look
over this source and let me know if I''m doing anything wrong. I want to
start out right with C, so if you see anything wrong tell me now while I
am learning!

Thanks !!!
#include< stdio.h>

int octet0; / *声明我们的IP范围的第一个八位字节* /
int octet1; / *声明我们的IP范围的第二个八位字节* /
int octet2; / *声明我们的IP范围的第三个八位字节* /
int octet3; / *声明我们的IP范围的第四个八位字节(我们生成的那个)* /

FILE * outputFile; / *声明我们将IP范围写入的文件* /

int main()/ *这是声明main的正确方法...不要使用void main()* /
{

octet0 = 192; / *第一个八位字节的值* /
octet1 = 168; / *第二个八位字节的值* /
octet2 = 1; / *第三个八位字节的值* /

outputFile = fopen(" ips_c.txt"," wt"); / *创建一个文本文件,在写入模式下打开它* /
for(octet3 = 1; octet3< 255; ++ octet3){
/ * printf("%d。%d。 %d。%d \ n",octet0,octet1,octet2,octet3); * /
fprintf(outputFile,"%d。%d。%d。%d \ n",octet0,octet1,octet2,octet3);
}
fclose(outputFile) ;
printf(\ n'文件''ips_c.txt''已成功生成... \ n \ n");
返回(0);
}
>
#include <stdio.h>

int octet0; /* Declare first octet of our IP range */
int octet1; /* Declare second octet of our IP range */
int octet2; /* Declare third octet of our IP range */
int octet3; /* Declare fourth octet (the one we generate) of our IP range */

FILE *outputFile; /* Declare the file in which we will write the IP range to */

int main() /* This is the correct way to declare main... do not use void main() */
{

octet0 = 192; /* Value of first octet */
octet1 = 168; /* Value of second octet */
octet2 = 1; /* Value of third octet */

outputFile = fopen("ips_c.txt", "wt"); /* Create a text file, open it in write mode */
for (octet3 = 1; octet3 < 255; ++octet3) {
/* printf ("%d.%d.%d.%d\n", octet0, octet1, octet2, octet3); */
fprintf (outputFile, "%d.%d.%d.%d\n", octet0, octet1, octet2, octet3);
}
fclose(outputFile);
printf ("\nThe file ''ips_c.txt'' was generated successfully...\n\n");
return(0);
}
>




Mike Wahler已批评该代码。我只想补充说fclose()

返回一个值,所以如果你要声称你成功写了

这个文件,你应该检查一下值fclose()。


Mac

-



Mike Wahler already critiqued the code. I''ll just add that fclose()
returns a value, so if you are going to claim that you successfully wrote
the file, you should probably check the return value of fclose().

Mac
--


hokiegal99写道:
hokiegal99 wrote:
大家好,
这是我的第一个C程序。我开始用Python编程。请查看
这个来源,如果我做错了,请告诉我。我想用C开始吧,所以如果你看到任何错误,请告诉我我正在学习的时候!

谢谢!!!
Hi Guys,

This is my first C program. I started programming in Python. Please look
over this source and let me know if I''m doing anything wrong. I want to
start out right with C, so if you see anything wrong tell me now while I
am learning!

Thanks !!!
#include< stdio.h>

int octet0; / *声明我们的IP范围的第一个八位字节* /
int octet1; / *声明我们的IP范围的第二个八位字节* /
int octet2; / *声明我们的IP范围的第三个八位字节* /
int octet3; / *声明我们的IP范围的第四个八位字节(我们生成的那个)* /

FILE * outputFile; / *声明我们将在其中写入IP
范围的文件* /

int main()/ *这是声明main的正确方法...不要
使用void main()* /
{

octet0 = 192; / *第一个八位字节的值* /
octet1 = 168; / *第二个八位字节的值* /
octet2 = 1; / *第三个八位字节的值* /

outputFile = fopen(" ips_c.txt"," wt"); / *创建一个文本文件,
以写入模式打开它* /
for(octet3 = 1; octet3< 255; ++ octet3){
/ * printf("%d) 。%d。%d。%d \ n",octet0,octet1,octet2,octet3); * /
fprintf(outputFile,"%d。%d。%d。%d \ n",octet0,octet1,octet2,
octet3);
}
fclose(outputFile);
printf(\ n'文件''ips_c.txt''已成功生成... \ n \ nn");
return(0);
}
#include <stdio.h>

int octet0; /* Declare first octet of our IP range */
int octet1; /* Declare second octet of our IP range */
int octet2; /* Declare third octet of our IP range */
int octet3; /* Declare fourth octet (the one we generate) of
our IP range */

FILE *outputFile; /* Declare the file in which we will write the IP
range to */

int main() /* This is the correct way to declare main... do not
use void main() */
{

octet0 = 192; /* Value of first octet */
octet1 = 168; /* Value of second octet */
octet2 = 1; /* Value of third octet */

outputFile = fopen("ips_c.txt", "wt"); /* Create a text file,
open it in write mode */
for (octet3 = 1; octet3 < 255; ++octet3) {
/* printf ("%d.%d.%d.%d\n", octet0, octet1, octet2, octet3); */
fprintf (outputFile, "%d.%d.%d.%d\n", octet0, octet1, octet2,
octet3);
}
fclose(outputFile);
printf ("\nThe file ''ips_c.txt'' was generated successfully...\n\n");
return(0);
}




C新手的好工作。唯一的错误我可以看到fopen在写入你的文件之前没有检查

- 哦,并给出了

的模式wt而不是w到fopen()。并且可能获得for的范围

一个接一个地循环。


然而,正如你所料,这不是非常惯用的C.这里是

a翻译。您的代码在

精神中接近原始,但这更为惯用:


#include< stdio.h>

#include< stdlib.h> / *用于退出状态* /


int main(无效)

{

const char * outfile =" ips_c。 txt" ;;

FILE * fp; / *它通常不需要给流提供非常具有描述性的
名称 - 它通常很清楚

上下文它们的用途。

* /

int ip_octet [4];

int i;


ip_octet [0 ] = 192;

ip_octet [1] = 168;

ip_octet [2] = 1;


fp = fopen( outfile," w");

if(fp == NULL){

fprintf(stderr,无法打开''%s''用于写入\\ \\ n",outfile);

退出(EXIT_FAILURE);

}


for(i = 0; i< 255 ; i ++){

ip_octet [3] = i + 1;

fprintf(fp,"%d。%d。%d。%d \ n" ,ip_octet [0],ip_octet [1],

ip_octet [2],ip_octet [3]);

}


fclose(fp);


printf("文件''%s''是成功生成的\ n,

outfile);


返回0;

}

-

Allin Cottrell

经济系

北卡罗来纳州维克森林大学



Nice work for a C newbie. The only "error" I can see is not checking
that fopen has worked before writing to your file -- oh, and giving
a mode of "wt" rather than "w" to fopen(). And maybe getting the range
of your "for" loop off-by-one.

As you might expect, though, this is not very idiomatic C. Here''s
a "translation" of your code that sticks close to the orginal in
spirit, but that is more idiomatic:

#include <stdio.h>
#include <stdlib.h> /* for exit statuses */

int main (void)
{
const char *outfile = "ips_c.txt";
FILE *fp; /* it''s not usually necessary to give very descriptive
names to streams -- it''s generally pretty clear from
context what they''re for.
*/
int ip_octet[4];
int i;

ip_octet[0] = 192;
ip_octet[1] = 168;
ip_octet[2] = 1;

fp = fopen(outfile, "w");
if (fp == NULL) {
fprintf(stderr, "Failed to open ''%s'' for writing\n", outfile);
exit(EXIT_FAILURE);
}

for (i=0; i<255; i++) {
ip_octet[3] = i + 1;
fprintf(fp, "%d.%d.%d.%d\n", ip_octet[0], ip_octet[1],
ip_octet[2], ip_octet[3]);
}

fclose(fp);

printf("The file ''%s'' was was generated successfully\n",
outfile);

return 0;
}
--
Allin Cottrell
Department of Economics
Wake Forest University, NC


这篇关于第一个C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆