Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.

Similar presentations


Presentation on theme: "Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are."— Presentation transcript:

1 Topic 3 – The General Form of a C Program

2 CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are available. So, how do all of these components fit together? What does a C program look like?

3 CISC 105 – Topic 3 The General Form of a C Program The first component of a C program are the preprocessor directives. After these directives, the main function is found. This first statement of the main function is the first statement that runs when the program is executed. Thus, the main function is the starting point of the program.

4 CISC 105 – Topic 3 The General Form of a C Program After the function header, the main function starts with an open curly brace, “{“. Whatever is inside that brace is the main function. It ends with a closed curly brace, “}”. Inside the main function, variables are first declared. After the variable declarations, executable statements are found.

5 CISC 105 – Topic 3 The General Form of a C Program Thus, a C program follows the form: Preprocessor directives Main function header { variable declarations executable statements }

6 CISC 105 – Topic 3 Function Headers A function header is composed of a return type, the function name, an open paren “(“, an input parameter list, and a close paren “)”. The return type is the data type that is returned by the function. For the main function, the return type should always be an integer ( int ).

7 CISC 105 – Topic 3 Function Headers After the return type appears the name, in this case, main. In between the parentheses is the parameter list, which specifies the data that you will pass into the function. For the main function, this parameter list is blank (for now).

8 CISC 105 – Topic 3 The Composition of a Function After the function header, the function body is contained within the curly braces. After the variables declarations are the executable statements. The function body must end with a return function call. This returns control to the caller of the function.

9 CISC 105 – Topic 3 The return Function Call The return function is called with one parameter, of the data type specified in the function header as the return type. For the main function, as the return type must be an integer, an integer must be passed to the return function call at the end of the main function body. The return value of the main function gets returned to the operating system. A return value of 0 (zero) indicates successful program completion.

10 CISC 105 – Topic 3 The main Function Header Thus, a general C program looks like: Preprocessor directives int main() { variable declarations executable statements return(0); }

11 CISC 105 – Topic 3 Review (Problem #1) Write a complete C program that asks the user for a floating-point number, multiplies the number by 4 * PI (with PI set to 3.14159 in a constant macro) and the outputs the result. #include #define PI 3.14159 int main() { float number, answer; printf(“Number?”); scanf(“%f”,&number); answer = number * 4 * PI; printf(“The answer is:%f\n”,answer); return (0); }

12 CISC 105 – Topic 3 Review (Problem #2) Write a complete C program that asks for the radius of a circle and outputs the area and circumference of the circle.


Download ppt "Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are."

Similar presentations


Ads by Google