C Basic Constructs (2-4)) Flashcards
Variables and constants in C
How are variables defined in C?
Example: int age; double salary; char gender;
How would we reassign the following:
int age = 23; double salary = 3000.0
age = 24; salary = 4000.0
What are the naming rules in C?
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 ( / , ; . & * + - # = " ' @ )
What is a constant in C? How is a constant assigned?
A constant must be initialized at the time of declaration, and its value CANNOT be changed. const int myconst = 34;
When does integer and real division occur?
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 can we explicitly convert to types in C?
Example: double ratio = (double) x/y;
When does implicit type conversion occur?
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.
What does the modulus operator do? What is the result of 5%2?
The modulus operator returns the remainder of an integer division on it operands.
5%2 = 1
What is evaluated as true or false in C?
Any number other than zero is evaluated as true.
Zero is evaluated as false.
In C, what will need to be included for bool to operate correctly.
Unlike C++, to be able to use this data type you need to do
#include <stdbool.h>
What are the following equivalent to?
x += 4;
x -= 5;
x /= 7;
x %= 2;
x = x + 4
x = x - 5
x = x / 7
x = x % 2
What is the difference between x– and –x
x– first uses x then decrements x
–x first decrements x and then uses x
What are the format specifers for:
integer
floating point number
character
double
%d: integer
(%i can also be used for an integer)
%f: floating point number
%c: character
%lf: double
What is the general format for an if statement?
if (test condition) { statements }
What can we eliminate if your if block has only one executable line?
If your if block has only one executable line, you have the option of eliminating the opening and closing braces.
What is the format for a loop?
while (condition) { statements updating expression }
What is a do while loop often called? What is the format for a do while loop?
A do while loop is called a post test-loop.
The format is
~~~
do {
statements
} while (test_condition);
~~~
What is the for loop often called? How do we implement a for loop?
The for loop is often called a pre test loop.
~~~
for (initalization_expression; Test_expression; Updating_expression)
{
statements
}
~~~