| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5851 人关注过本帖
标题:c语言初学,int和float的区别
只看楼主 加入收藏
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:0 
这是《C primer plus》的教材,很详细
Listing 2.1. The first.c Program
#include <stdio.h>
int main(void) /* a simple program */
{
int num; /* define a variable called num */
num = 1; /* assign a value to num */
printf("I am a simple "); /* use the printf() function */
printf("computer.\n");
printf("My favorite number is %d because it is first.\n",num);
return 0;
}
If you think this program will print something on your screen, you're right! Exactly what will be
printed might not be apparent, so run the program and see the results. First, use your favorite
editor (or your compiler's favorite editor) to create a file containing the text from Listing 2.1.
Give the file a name that ends in .c and that satisfies your local system's name requirements.
You can use first.c, for example. Now compile and run the program. (Check Chapter 1,
"Getting Ready," for some general guidelines to this process.) If all went well, the output should
look like the following:
I am a simple computer.
My favorite number is 1 because it is first.
All in all, this result is not too surprising, but what happenedto the \ns and the %d in the
program? And some of the lines in the program do look strange. It's time for an explanation
The Example Explained
We'll take two passes through the program's source code. The first pass ("Pass 1: Quick
Synopsis") highlights the meaning of each line to help you get a general feel for what's going on.
The second pass ("Pass 2: Program Details") explores specific implications and details to help
you gain a deeper understanding.
Figure 2.1 summarizes the parts of a C program; it includes more elements than our first
example uses.
Figure 2.1. Anatomy of a C program.
Pass 1: Quick Synopsis
This section presents each line from the program followed by a short description; the next
section (Pass 2) explores the topics raised here more fully.
#include <stdio.h> include another file
This line tells the compiler to include the information found in the file stdio.h, which is a
standard part of all C compiler packages; this file provides support for keyboard input and for
displaying output.
int main(void) a function name
C programs consist of one or more functions, the basic modules of a C program. This program
consists of one function called main. The parentheses identify main() as a function name. The
int indicates that the main() function returns an integer, and the void indicates that main()
doesn't take any arguments. These are matters we'll go into later. Right now, just accept both
int and void as part of the standard ISO/ANSI C way for defining main(). (If you have a pre-
ISO/ANSI C compiler, omit void; you may want to get something more recent to avoid
incompatibilities.)
/* a simple program */ a comment
The symbols /* and */ enclose comments, remarks that help clarify a program. They are
intended for the reader only and are ignored by the compiler.
{ beginning of the body of the function
This opening brace marks the start of the statements that make up the function. The function
definition is ended with a closing brace (}).
int num; a declaration statement
This statement announces that you are using a variable called num and that num will be an int
(integer) type.
num = 1; an assignment statement
The statement num = 1; assigns the value 1 to the variable called num.
printf("I am a simple "); a function call statement
The first statement using printf() displays the phrase I am a simple on your screen, leaving
the cursor on the same line. Here printf() is part of the standard C library. It's termed a
function, and using a function in the program is termed calling a function.
printf("computer.\n"); another function call statement
The next call to the printf() function tacks on computer to the end of the last phrase printed.
The \n is code telling the computer to start a new line—that is, to move the cursor to the
beginning of the next line.
printf("My favorite number is %d because it is first.\n", num);
The last use of printf() prints the value of num (which is 1) embedded in the phrase in quotes.
The %d instructs the computer where and in what form to print the value of num.
return 0; a return statement
A C function can furnish, or return, a number to the agency that used it. For the present, just
regard this line as part of the ISO/ANSI C requirement for a properly written main() function.
} the end
As promised, the program ends with a closing brace.
Now that you have an overview of Listing 2.1, we'll take a closer look. Once again, we'll examine
the individual lines from the program, this time using each line of code as a starting point for
going deeper into the details behind the code and as a basis for developing a more general
perspective of C programming features.

收到的鲜花
  • 李晨曦2016-03-21 22:37 送鲜花  2朵  
2016-03-21 22:35
李晨曦
Rank: 2
等 级:论坛游民
帖 子:16
专家分:10
注 册:2016-3-21
收藏
得分:0 
回复 21楼 hjx1120
虽然没看懂,但还是谢了
2016-03-21 22:36
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
以下是引用李晨曦在2016-3-21 22:32:40的发言:

int定义整数float定义小数对吧?

是的

授人以渔,不授人以鱼。
2016-03-21 22:38
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
作爲main()函數的返回値類型,沒有float的,多數編譯器都會對此報錯。

授人以渔,不授人以鱼。
2016-03-21 22:46
李晨曦
Rank: 2
等 级:论坛游民
帖 子:16
专家分:10
注 册:2016-3-21
收藏
得分:0 
回复 24楼 TonyDeng
原来是这样,知道了,谢谢
2016-03-21 22:51
处处蝴蝶飞
Rank: 2
等 级:论坛游民
帖 子:5
专家分:10
注 册:2016-3-25
收藏
得分:3 
回复 4楼 李晨曦
我也这么干过也
2016-03-25 16:35
处处蝴蝶飞
Rank: 2
等 级:论坛游民
帖 子:5
专家分:10
注 册:2016-3-25
收藏
得分:0 
回复 10楼 李晨曦
对,对,对!
#include<stdio.h>
int main()
{
    int a=100;
    float d;
    d=a*(a+1)/2;
    printf("%d\n",d);
    return 0;
}
为什么浮点型变量的值用整形输出结果不对啊?
2016-03-25 16:41
仙剑青霞庄
Rank: 2
等 级:论坛游民
帖 子:5
专家分:10
注 册:2016-3-17
收藏
得分:3 
回复 楼主 李晨曦
返回值不同而已,float main返回小数,int main返回整数。
2016-03-25 16:54
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
以下是引用处处蝴蝶飞在2016-3-25 16:41:37的发言:

对,对,对!
#include<stdio.h>
int main()
{
    int a=100;
    float d;
    d=a*(a+1)/2;
    printf("%d\n",d);
    return 0;
}
为什么浮点型变量的值用整形输出结果不对啊?

見13樓

授人以渔,不授人以鱼。
2016-03-25 18:22
hhphhp
Rank: 1
等 级:新手上路
帖 子:2
专家分:6
注 册:2016-3-27
收藏
得分:3 
float是浮点型,可以算小数。int是整型用来算整数,在int中如果用除号则得到的结果为商的整数
2016-03-27 21:02
快速回复:c语言初学,int和float的区别
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017762 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved