Question

In: Computer Science

What are the different parts of a typical program and how do they affect memory usage?

  • What are the different parts of a typical program and how do they affect memory usage?

Solutions

Expert Solution

The main() Function

The only component required in every executable C program is the main() function. In its simplest form, the main() function consists of the name main followed by a pair of parentheses containing the word void ((void)) and a pair of braces ({}). You can leave the word void out and the program still works with most compilers. The ANSI Standard states that you should include the word void so that you know there is nothing sent to the main function.

Within the braces are statements that make up the main body of the program. Under normal circumstances, program execution starts at the first statement in main() and terminates at the last statement in main().

The #include and #define Directives

The #include directive instructs the C compiler to add the contents of an include file into your program during compilation. An include file is a separate disk file that contains information that can be used by your program or the compiler. Several of these files (sometimes called header files) are supplied with your compiler. You rarely need to modify the information in these files; that’s why they’re kept separate from your source code. Include files should all have an .h extension (for example, stdio.h).

You use the #include directive to instruct the compiler to add a specific include file to your program during compilation.

The #define directive instructs the C compiler to replace a specific term with its assigned value throughout your program. By setting a variable at the top of your program and then using the term throughout the code, you can more easily change a term if needed by changing the single #define line as opposed to every place throughout the code. For example, if you wrote a payroll program that used a specific deduction for health insurance and the insurance rate changed, tweaking a variable created with #define named HEALTH_INSURANCE at the top of your program (or in a header file) would be so much easier than searching through lines and lines of code looking for every instance that had the information.

The Variable Definition

A variable is a name assigned to a location in memory used to store information. Your program uses variables to store various kinds of information during program execution. In C, a variable must be defined before it can be used. A variable definition informs the compiler of the variable’s name and the type of information the variable is to hold.

The Function Prototype

A function prototype provides the C compiler with the name and arguments of the functions contained in the program. It appears before the function is used. A function prototype is distinct from a function definition, which contains the actual statements that make up the function.

Program Statements

The real work of a C program is done by its statements. C statements display information onscreen, read keyboard input, perform mathematical operations, call functions, read disk files, and all the other operations that a program needs to perform. Most of this book is devoted to teaching you the various C statements. For now, remember that in your source code, C statements are generally written one per line and always end with a semicolon.

The printf() Statement

The printf() statement is a library function that displays information onscreen. The printf() statement can display a simple text message or a message mixed with the value of one or more program variables.

The scanf() Statement

The scanf() statement is another library function. It reads data from the keyboard and assigns that data to one or more program variables.

The return Statement

The return statement calculates the year a person would be a specific age by adding the #define constant TARGET_AGE to the variable year1 and returns the result to the program that called calcYear().

Note that in a real C program, you probably wouldn’t use a function for a task as simple as adding two numbers. It has been done here for demonstration purposes only.

Program Comments

Any part of your program that starts with /* and ends with */ or any single line that begins with // is called a comment. The compiler ignores all comments, so they have absolutely no effect on how a program works. You can put anything you want into a comment, and it won’t modify the way your program operates.


Related Solutions

​How do different assumptions about network usage affect ROI? Do they affect the cost of the investment or the amount of the return?
How do different assumptions about network usage affect ROI? Do they affect the cost of the investment or the amount of the return? 
What is false memory and how does it affect our memory?
What is false memory and how does it affect our memory?
explain how emotions affect memory
explain how emotions affect memory
What are the different types of sampling, and how do they affect the statistical data analysis...
What are the different types of sampling, and how do they affect the statistical data analysis decisions?
What are the steps in completing the accounting cycle? How do the different steps affect the...
What are the steps in completing the accounting cycle? How do the different steps affect the financial statements? What is the effect on the financial statements of missing a step when completing the accounting cycle? How do these steps play a roll in accrual basis accounting?
What are the different stages of memory in a heap?
What are the different stages of memory in a heap?
  1. How do different environmental standards affect industry location and international trade? a. What are the...
  1. How do different environmental standards affect industry location and international trade? a. What are the benefits of intra-industry trade? b. How does a tariff lead to production cost inefficiencies? c. How can trade be based on technological gaps and product cycles? Your answer should include    an explanation of each. d. What would happen to the US standard of living if the United States withdrew completely from    international trade?
1)What are the steps in completing the accounting cycle? 2)How do the different steps affect the...
1)What are the steps in completing the accounting cycle? 2)How do the different steps affect the financial statements? 3)What is the effect on the financial statements of missing a step when completing the accounting cycle? 4)How do these steps play a roll in accrual basis accounting?
What happens to memory as we age? How might this affect our quality of life?
What happens to memory as we age? How might this affect our quality of life?
How many bits are required to address the program memory of PIC16F887? What is PCLATH? For...
How many bits are required to address the program memory of PIC16F887? What is PCLATH? For PC absolute addressing, describe how to write assembly program to jump to code located in a different program memory page.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT