C Basic Constructs (2-4)) Flashcards

Variables and constants in C

1
Q

How are variables defined in C?

A
Example:
int age;
double salary;
char gender;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How would we reassign the following:

int age = 23;
double salary = 3000.0
A
age = 24;
salary = 4000.0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the naming rules in C?

A
An identifier can start either by a letter (a to z, or A to Z), or an underscore:
int myint;
int $y, y$; //this is valid
double 34xyz; //this is invalid

Also note that an identifier cannot have any of the following characters ( / , ; . & * + - # = " ' @ )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a constant in C? How is a constant assigned?

A
A constant must be initialized at the time of declaration, and its value CANNOT be changed.
const int myconst = 34;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When does integer and real division occur?

A

If both operands in a division operation are integers then the result will be integer division.

If one or both operands in a division operation are real numbers, the result will be a real division.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we explicitly convert to types in C?

A
Example:
double ratio = (double) x/y;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When does implicit type conversion occur?

A
Implicit type conversion happens automatically by the compiler.

For example int y = 7.00/2.00;
The result of real division is 3.5, but by implicit type conversion the value is demoted to 3. 

A type can also be promoted. 
int a = 4;
int b = 2;
double c = 3.0;
double y = a*b*c;
//In this example all operands will be promoted to the highest rank before each operation is completed and the result as a double will be saved in variable y.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the modulus operator do? What is the result of 5%2?

A

The modulus operator returns the remainder of an integer division on it operands.

5%2 = 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is evaluated as true or false in C?

A

Any number other than zero is evaluated as true.

Zero is evaluated as false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In C, what will need to be included for bool to operate correctly.

A

Unlike C++, to be able to use this data type you need to do

#include <stdbool.h>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the following equivalent to?
x += 4;
x -= 5;
x /= 7;
x %= 2;

A

x = x + 4
x = x - 5
x = x / 7
x = x % 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between x– and –x

A

x– first uses x then decrements x
–x first decrements x and then uses x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the format specifers for:
integer
floating point number
character
double

A

%d: integer
(%i can also be used for an integer)
%f: floating point number
%c: character
%lf: double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the general format for an if statement?

A
if (test condition)
{
statements
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What can we eliminate if your if block has only one executable line?

A

If your if block has only one executable line, you have the option of eliminating the opening and closing braces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the format for a loop?

A
while (condition) {
statements
updating expression
}
17
Q

What is a do while loop often called? What is the format for a do while loop?

A

A do while loop is called a post test-loop.
The format is
~~~
do {
statements
} while (test_condition);

~~~

18
Q

What is the for loop often called? How do we implement a for loop?

A

The for loop is often called a pre test loop.
~~~
for (initalization_expression; Test_expression; Updating_expression)
{
statements
}
~~~