Chapter 2: Overview of C

¡Supera tus tareas y exámenes ahora con Quizwiz!

printf("%-3d", x) where x = 100

-100, minus sign before count of digits makes negative number

Data can be stored in memory in two different ways. What are they?

1) By assignment to a variable 2) Copying the data from an input device to a variable using a function like scanf

A program contains 2 parts. What are they?

1) pre-processor directives 2) main function

How can we retrieve an original value using only / and % operators?

7 equals (7/2) * 2 + (7%2) equals 3 * 2 + 1 299 equals (299/100) * 100 + (299%100) equals 2 * 100 + 99

Can we write scientific notation in C?

In C scientific notation, we write this number as 1.23e5 or 1.23E5. Read the letter e or E as "times 10 to the power"

Input redirection

Instead of using the keyboard, we can instruct programs to take input from files bu placing <data.txt at end of the command line. This causes compiled and linked program to execute.

Can all integers be represented by type int?

No, because of the finite size of a memory cell. ANSI C specifies that the range of int must include at leas the values -32767 through 32767

When forcing a program to perform actions, are all operations defined directly by C?

No, rather every C implementation contains libraries that contain useful functions and symbols that may be accessed by the program.

Rules for Evaluating Expressions

Parentheses --- All expressions in parentheses must be evaluated separately. Nested parenthesized expressions must be evaluated from the inside out, with the innermost expression evaluated first. Operator precedence --- Operators in the same expression are evaluated in the following order: unary +,- first *, /, % next binary +, - last Associativity rule --- Unary operators in the same subexpression and at the same precedence level (such as + and − ) are evaluated right to left (right associativity). Binary operators in the same subexpression and at the same precedence level (such as + and − ) are evaluated left to right (left associativity).

Numerical inaccuracies

Represenational/round-off error Cancellation error --- When larger numbers "cancel out" the smaller number. for example, if x > y and x = 1000.0 and y = 0.000000123, then x + y => 1000.0

What does the ANSI (American National Standards Institute) require for a C?

That certain STANDARD LIBRARIES be provided in every ANSI C implementation.

The \n newline escape sequence

The cursor is a moving place marker that indicates the next position on the screen where information will be displayed. When executing a printf function call, the cursor is advanced to the start of the next line on the screen if \n is encountered in the format string.

%md: what does m stand for?

The number of digits to be printed

output redirection

Try to redirect program output to disk file rather on screen. Then send output file to printer (using OS command) to obtain hard copy. You can do this by using the symbols >myoutput.txt to redirect output from the screen to file. There is no need to create the file myoutput.txt before running the program, the file will be made upon redirection.

How can we define a constant?

Under the pre-processor directives, use #define, followed by the name of the constant and value of the constant

Arithmetic overflow

When two/more large numbers are manipulated such that the result is far too large.

Arithmetic underflow

When two/more very small numbers are arithmetically manipulated such that the result is so small that it's represented as zero.

Echo prints vs prompts

Whenever you convert an interactive program to a batch program, make sure you replace each prompt with an echo print that follows the call to scanf

printf("%5.2f", x) where x = 3.14159 makes...

_3.14 if it was %4.2 -> 3.14 (no underscore for blank space)

preprocessor directive

a C program line beginning with # that provides an instruction to the preprocessor

constant macro

a name that is replaced by a particular constant value before the program is sent to the compiler

the C preprocessor

a system program that modifies the text of a C program BEFORE it is compiled

input operation

an instruction that copies data from an input device into memory

output operation

an instruction that displays information stored in memory

Reserved words/Key words

auto - one of the four storage classes in C; default storage for all variables declared inside function or block. Auto variables can only be accessed within the block/function they have been declared and not outside them (defines scope). However, they can be accessed outside their scope via pointers. Auto variables are assigned a garbage value by default whenever declared. break case - decision control char const continue default - decision control do - loops for - loops int long register - storage class declares register variables which have same functionality of auto variables. Unlike auto, the compiler tries to store register variables in register of microprocessor if a free register is available. This makes the faster memory allocation during runtime of program. If a free register is not available, register variables are stored in the memory only. * we cannot obtain address of register variable using pointers return short signed sizeof static - storage class used to declare static variables that have the property of preserving their value even after they are out of scope. So, this means variables are initialized only once and exist until the termination of the program. struct switch - decision control typedef union - allows storing of different data types in the same memory location unsigned - always non-negative numbers void - return type volatile - tells a compiler that the value of the variable can change at any time -- without any action being taken by the code the compiler finds nearby while - loops double else - decision control enum - set of constants extern - storage class that tells us that a variable is defined elsewhere and not within the same block where it is used. It is pretty much a global variable. float goto - redirects flow of execution if - decision control

What are the two parts of a function body?

declarations & execution statements

Range of floating-point types

float: 10^−37 to 10^38 double: 10^−307 to 10^308 long double: 10^−4931 to 10^4932

Program Style: Choosing Identifier Names

identifier must consist of two or more words, placing underscore character between words, so that they are long enough and readable

main function

int main(void) { (function body) } Program execution begins here. braces enclose the main function body, which contains declarations and execution statements. The int indicates that the main function returns an integer value to the OS when it finishes normal execution. The symbols (void) means that the function receives no data from the OS before execution.

Range of int vs unsigned int

int: −2,147,483,647 to 2,147,483,647 unsigned int: 0 to 4,294,967,295

Range of long vs unsigned long

long: −2,147,483,647 to 2,147,483,647 unsigned long: 0 to 4,294,967,295

What is the general form of a C program?

preprocessor directives main function heading { declarations; executable statements; }

Range of short vs unsigned short

short: -32,767 to 32,767 unsigned short: 0 to 65,535

With each library, there is a ____.

standard header file whose name ends with .h

comment

text beginning with /* and ending with */ that provides supplementary information but is ignored by the pre-processor and compiler /* this is a comment */

The ampsersand character (&)

the C address-of-operator; tells the function WHERE to find the variable intro which it is to store a new value, not its location in memory

What does #define NAME value mean?

the C preprocessor is notified that it is to replace each use of the identifier NAME by value.

field width

the number of columns used to display a value

What does the format #include <standard header file> mean?

the preprocessor directive tells the preprocessor where to find the meanings of standard identifiers used in the program. for example, standard identifiers printf and scanf are found in the header file stdio.h


Conjuntos de estudio relacionados

Risk Management and Insurance Exam 3

View Set

Financial Markets - Introduction to Financial Management

View Set

Personal Fitness Unit 2 Lesson 4: Types of Muscles

View Set