cprogrammingtutorial–parti内容摘要:
ions • Any nontrivial program will have multiple functions • C functions look like methods in Java • Functions have return types – int, float, void, etc. • Functions have unique names • Functions have parameters passed into them • Before a function can be used, it must be declared and/or defined – a function declaration alone is called a prototype – prototypes can be in a separate header file or included in the file their definition appears in Function Example include define PI float calcArea(float)。 // prototype for function to be defined later int main() { float radius, area。 printf(“Enter radius of a circle: “)。 scanf(“%f”, amp。 radius)。 area = calcArea(radius)。 // call function printf(“Area of circle with radius %f is: %f\n”, radius, area)。 return 0。 } float calcArea(float radius) { return PI * radius * radius。 } Arrays • Like Java, C has arrays – they are declared slightly different – indexes still go from 0 to size1 • C arrays have some major differences from Java – if you try to access an index outside of the array, C will probably let you – C arrays are kept on the stack • this limits the maximum size of an array – size of a C array must be statically declared • no using variables for the size Declaring Arrays • Legal array declarations int scores[20]。 define MAX_LINE 80 char line[MAX_LINE]。 // place 80 inside [ ] at pile time • Illegal array declaration int x = 10。 float nums[x]。 // using variable for array size Initializing Arrays • Legal initializations int scores[5] = { 2, 3, 10, 0, 4 }。 char name[20] = { “Jane Doe” }。 int totals[5]。 int i。 for(i=0。 i5。 i++) totals[i] = 0。 char line[MAX_LINE]。 scanf(“%s”, line)。 • Illegal initialization int scores[5]。 scores = { 2, 3, 10, 0, 4 }。 More on Arrays • Accessing arrays – exactly like Java except: • no .length parameter in array。cprogrammingtutorial–parti
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。