CReviewPart1 - Structure of a C Program /* File: powertab.c...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Overview of C 2 Structure of a C Program /* File: powertab.c * ---------------- * This program generates a table comparing values * of the functions n^2 and 2^n. */ #include <stdio.h> /* * Constants * --------- * LowerLimit – Starting value for the table * UpperLimit – Final value for the table */ #define LowerLimit 0 #define UpperLimit 12 /* Function prototypes */ int RaiseIntPower(int n, int k); /* Main program */ int main() { int n; printf(“ | 2 | N\n”); printf(“ N | N | 2\n”); printf(“----+-----+------\n”); for (n = LowerLimit; n <= UpperLimit; n++){ printf(“ %2d | %3d | %4d\n”, n, RaiseIntPower(n, 2), RaiseIntPower(2, n) ); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3 /* * Function: RaiseIntPower * This function returns n to the kth power. */ int RaiseIntPower(int n, int k) { int i, result; result = 1; for (i = 0; i < k; i++){ result *= n; } return (result); } 4 Variables, values and types A variable can be thought of as a named box, or cell, in which one or more data values are stored and may be changed by the algorithm. The act of creating a variable is called declaring the variable . For every variable that is declared it must be explicitly typed. In other words, each variable has an associated data type . Examples: int age; int test_score; float average; double result = 0.0; An identifier is simply the algorithmic terminology for a name that we make-up to “identify” the variable. Every variable must be given a unique identifier so that there will be no ambiguity as to which piece of data we are referencing. Rules for Variable Identifiers (in C) A sequence of letters, digits, and the special character _. A letter or underscore must be the 1 st character of an identifier. C is case-sensitive : Apple and apple are two different identifiers.
Background image of page 2
5 Data Types A data type is defined by two properties: a domain , which is a set of values that belong to that type a set of operations , which defines the behavior of that type e.g. Type int includes all integers ( … -2, -1, 0, 1, 2, …) up to the limits established by the hardware of the computer. The set of operations includes the standard arithmetic operations like addition, multiplication. C includes several fundamental types that are defined as part of the language. These types are called atomic types . Atomic types can be grouped into 3 categories: integer, floating point and character. Integer Types: - short : 2 bytes - int : 4 bytes - long: 4 bytes - unsigned: 4 bytes Floating-point Types: - float : 4 bytes - double : 8 bytes - long double: 8 bytes - signed/unsigned The range of values for each type depends on the particular computer’s hardware. 6 Characters: - char: 1 byte The domain of type char is the set of symbols that can be displayed on the screen or typed on the keyboard: the letters, digits, punctuation symbols, spacebar, Return key, etc. In addition to the standard characters, C allows you to write
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 16

CReviewPart1 - Structure of a C Program /* File: powertab.c...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online