C/Cpp Pre-test

Ace your homework & exams now with Quizwiz!

Operations involving integer types are more efficient than operations with float types.

Are operations involving integer types more efficient than floating types?

1: floor(3.9*a - 0.6) - b 2: (39*a - 6)/10 - b

Assuming a & b are both integer types and a is non-negative, which operation is more efficient? (Sec.2,Note 2.7)

bool can be implicitly converted to int. true = 1, and false = 0

Can bool variable be converted to int?

numeric and pointer values can be implicitly converted to bool: zero value is converted to false and any non-zero is true.

Can numeric values be converted to bool? (Sec.3, note 3.1)

No

Can you take the address of a register?

C: int type Cpp: char type

Data type of character literal

in C, const is no different than any other variable except it cannot be changed at run-time. in Cpp, const variable is a true constant and can be used whenever a constant expression is needed (i.e an array dimension) in C, a const external variable has external linkage while Cpp has internal linkage

Differences in const between C & Cpp

No, register declarations with optimizing compilers might actually lead to inefficient programs - register variables should not be used in general application programs

Do Register declarations always lead to more efficient programs?

This is considered an ill-formed expression as x is modified twice. (x is increased then x is assigned x). value is undefined.

For int x = 1; predict the value in x after: x = ++x;

octal: begins with 0 hex: begins with 0x

How to write octal & hexadecimal numbers?

Implementation defined

If value of character literal containing more than one character, or containing a character or escape sequence not represented in the basic execution char set or representing a value not within the range of unsigned char - how is its value defined?

Whether char means signed or unsigned is implementation dependent

Is char, unsigned or signed?

for shorts, omitting unsigned means signed

Is short signed or unsigned?

For some Cpp objects, in some situations, post-incrementing/decrementing can require significantly more resources than pre-incrementing/decrementing

Is there a difference in computation resources when post-incrementing/decrementing vs pre-incrementing/decrementing?

No, only trailing parameters can be omitted. either default char value is last parameter or int needs default value

Is this function prototype legal: void PringChar(char var = 'X', int number)

int is the most machine efficient type

Most efficient data type?

Prints "4" to stdout. Formatting is irrelevant to the compiler, each else belongs to the previous "if" since the first "if" is false, everything else is skipped and "putchar('4')" is executed

Predict output: if (5 < 4) if (6 > 5) putchar('1'); else if (4 > 3) putchar('2'); else putchar('3'); putchar('4');

Would print "World" as expression is false

Predict the output: cout<<(12<5?"Hello":"World"

Value of x is 4 (sizeof(int)). Commas just define left->right evaluation and x takes the value of the expression that is evaluated last

Predict value of x after: int x = (4,printf("hello"),sqrt(64.),sizeof(int));

A false EOF might be detected or real EOF might be missed. ch should be an int to use the (... != EOF) implementation. would need to use the eof() function to get this implementation to work properly

Predict what will happen: char ch; while ( (ch = cin.get()) != EOF ) cout.put(ch); (note 4.3)

No, because two quotients are possible and which occurs is implementation-defined

Should portable programs do integer division with one or both operands negative? (Sec.2, Note 2.8)

Can use #define but const int is preferred. preferred because, const values have limited scope (macros have "remaining file" scope) debugger can inspect const values, can take address of const variable, no parentheses are needed around initialization

Should you use #define in Cpp?

Good practice says: no. The function assumes constant input, has no way to stop buffer overflow

Should you use gets()?

At minimum, char (signed or unsigned) is represented by 8-bits

Size of a char?

at minimum short is represented using 16 bits

Size of short?

65 if the ASCII character set is used

The value of sizeof('A') is:

implementation dependent: either -5/4==-1 and -5%4==-1 or -5/4==-2 and -5%4==3

The values of -5/4 and -5%4 are, respectively:

variables declared within a block without keywords static or extern; also, formal parameters

What are Automatic Variables?

Automatic variables whose declarations are preceded by the word register - kept in internal machine registers rather than main storage, means much faster access

What are register variables?

char, short, int, long, long long

What are the Integer Arithmetic Data Types?

float, double, long double

What are the floating arithmetic data types?

C: expressions produce type int (0/1) Cpp: expressions produce type bool (true/false)

What data type are the values produced by Relational/Equality expressions in C/Cpp?

The operators && and || guarantee that the operand evaluation will stop as soon as the expressions value (true/false) is known.

What is "Short Circuiting"? (Sec.3 Note 3.2)

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all "side effects", which are changes in the state of the execution environment. - - basically anything that changes a variable, file. ex: int y; y--;)

What is "Side Effects"?

C: <stdio.h> cpp: <iostream> Returns int when it attempts to read and there is nothing left to read. Has implementation-defined negative value.

What is EOF and where is it defined?

An object that is modified twice between sequence points. This leads to undefined behavior

What is an ill-formed expression?

returns an int (1)

What is the data type returned by this expression: '\25'<100.25F?

Remainder of division: a%b = a-(a/b)*b

What is the modulo definition?

Private members of a class are being accessed by other than a member/friend function.

What is wrong? struct Svalues {char x; int y;} s1 = { 25, 30 }; class Cvalues {char x; int y;} c1 = { 25, 30 };

Only scalar types

What types can be declared Register?

Use these functions for systems where sizeof(char)==sizeof(int), as EOF would be indistinguishable from a legal character. Should also use these functions when input functions (like fgets() fread()) do not return a special value to indicate EOF.

When to use foef() or eof() functions?

C: <limits.h> Cpp: <climits>

Where are Integer arithmetic implementation-specific characteristics found?

C: <float.h> Cpp: <cfloat>

Where are implementation specific characteristics of floating arithmetic data types found?

in the parameter list of the prototype. Can also use function signature, but prototype is better style/recommended

Where is it best to define default values in for functions?

Allows calling function to modify a variable not within its scope. Its also more efficient if its a large object

Why would you want to return a pointer rather than what it points to?

Compound Assignment Operator

combines assignment with some other operation. aka +=, *= etc

String Literal

double quote enclosed sequence. Treated as tokens by the compiler

All sub-integers are converted to type int or unsigned All type float are converted to double All other types are unaltered.

if a function is called before its formal parameter types have been declared (via function def or prototype), all arguments will be converted according to:

EOF cannot be a char as EOF cannot be a member of the 256 legal character set, would cause for lost or false EOF return

is EOF a char?

cannot change value of i, code will not compile because of unassigned constant variable and because code is attempting to change a read-only variable

predict output: const int i; for(i=0;i<5;++i){ cout<<i<<""; }

Will print out: "1 2" due to "short circuiting" - && expression is true so it automatically stops operator evaluation (doesnt get to "or" operand)

predict the output: printf("1") && printf("2") || printf("3")

Character Literal

single quote enclosed sequence. if character is in the basic execution character set, it has that value. aka 'A' = 65 base 10

doubles are commonly represented using 64 bits, with typical precision of 15 digits.

size of double & its precision?

Floats are commonly represented using 32 bits, with a typical precision of 7 digits

size of float & its precision?

at minimum, 16 bits

size of int?

long doubles are commonly represented using 64 or 80 bits. typical 80 bit precision is 19 digits

size of long double & its precision?

at minimum, 64 bits

size of long long?

at minimum, 32 bits

size of long?

C: <stddef.h> Cpp: <cstddef>

where is size_t defined?

increment and decrement operators

y = ++x assigns y = x+1 y = x++ assigns y = x, then increments x = x+1


Related study sets

Chapter 22 Nursing Management of the Postpartum Woman at Risk

View Set

Imperialism (overview, africa, india, china, japan)

View Set

MASTERING BIOLOGY-DNA REPLICATION

View Set

Bio 8e(Cmp-2) Q&A-Ch1 to Ch5 (Final)

View Set

Chemistry Hybridization and Bonding

View Set

Grade 12 Biology; Photosynthesis

View Set

Systematic Review and Meta Analysis

View Set

development and learning test one

View Set

Parasitology Yr1- Plasmodia spp.

View Set