You are on page 1of 6

Study Guides for midterm 1

Chapter 2
a)What is an identifier? Be able to tell if an identifier is valid or
not?
An identifier is a word used to name a memory location that
holds data, program results and name operations that are defined by a
programmer or user.

An identifier is only valid if it:

1. starts with a letter


2. consists of letters, numbers and underscores
3. is not a reserved word

b) What is a reserved word in C?

A reserve word is an identifier that has special meaning in C


and cannot be used for different purposes. Reserve words are often
identifiers from standard libraries and names of memory cells.

Examples are int, void, double, return

c) What is a data type in C means? Different data types in C?

Data types in C tell what type of data is in a variable.


The different data types are Integer(int), real numbers(double) and
characters(char). You can use common arithmetic operations like
addition, subtraction, multiply and divide also you can compare two
integers.

Integers are only whole numbers no decimals are allowed. The range
of int is from -32767 to 32767. You can use common arithmetic
operations like addition, subtraction, multiply and divide also you
can compare two doubles (real numbers).

Double consist of a real numbers and can even have decimal


numbers. Ex 2.14, .0056

Data type char represents an individual character value like a


letter or a number. Each character must be stored in two apostrophe
like so ‘a’, ’1’, ’3’, ‘b’. You can compare chararacters like this
‘9’> ‘1’. You can also preform math operations on them.
d) What is a variable? And how to declare variables in C?

A variable is a memory cell used to store a programs input or output


data.
Declare a variable like so:
Double temp;
Char letters;
int age;

e) What does it mean to initialize a variable?

To initialize a variable means that you are telling the C


compiler the name of a variable, the kind of data stored in the
variable and how the data is represented in memory.

f) How to use scanf() function for various data types?

Scanf for datatype double:


Scanf(“%lf”, &example);

Scanf for datatype int:


Scanf(“%d”, &example);

Scanf for datatype char:


Scanf(“%c”, &example);

g) How to use the printf() function for various data types?

Printf for datatype double:


Printf(“Example number is %f”,example);

Printf for datatype int:


Printf(“Example for int number %d” ,example);

Printf for datatype char


Printf(“Example for char types %c” ,example);

h) Given a printf() statement(s) with placeholders and variable


names, identify the output

i) Given a set of input data, identify how to input them using


scanf()
j) What does it mean by preprocessor statements?

A preprocessor statement is a statement that is used before the


program is initiated. Different types are the:
#include <stdio.h>
#define pi 3.14
#if

k) What does it mean by global declaration? And local declaration?

Global declaration is when a variable is declared outside of the


main function. A globally declared variable is initialized for any
function.

Local declaration is when a variable is declared in a function.


The variable is only initialized in the function and its value varies
from function to function.

l) How to declare constants in C?

To declare a constant you declare a variable outside the main


function.
Ex.
# define pi 3.14

int main()

Or

double pi = 3.14;
int main()

m) How to indicate comments in a C program means?

To indicate a comment you use /* all*/ or \\

n) Arithmetic expressions using + - / * and %

+ = addition
- = subtraction
/ = division
* = multiplication
% = division but the remainder is given to the variable instead
of the answer
Example of arithmetic operation

Example = 1*2

o) Type Conversion through casting and coercion. How to indicate


casting in C?
Casting allows you to convert type of data by placing
the desired type in parenthesis in front of the expression.
Ex. rounded_x = (int)(x + 0.5)

Implicit type conversion, also known as coercion, is an automatic type conversion by the
compiler. Some languages allow, or even require, compilers to provide coercion. In a mixed-type
expression, data of one or more subtypes can be converted to a supertype as needed at runtime so
that the program will run correctly. In some cases such in a forced conversion from double to int
data can be lost. As in the case of double to int the decimal value is lost only the whole number is
kept.

p) Evaluating result of mixed type expressions.

int and double = double


int and int = int
double and double =double

q) Know the precedence and associativity of operators.

1. Unary operators -,+ example -5 or +5


2. *,/,%
3. Binary +,-

r) Writing mathematical expressions in C

z - (a + b / 2) + w * -y

s) What is syntax error? Logic error? And run time errors?

Syntax errors occur when code violates one or more grammar rules
of C. A statement with a syntax error cannot be translated and the
program will not run.
Types of syntax errors:
1. Missing semicolon at the end of a statement
2. Undeclared variables
3. End of a comment using /* is not closed in */

Logic errors occurs when a program follows a faulty algorithim


the program runs but the output is wrong.

Run-time errors are displayed by the pc when the program is run.


It occurs when the program directs the pc to perform an illegal
operation like dividing a number by 0.

t) Given a snippet of code, identify the output or identify an error


or errors

Chapter 3
a) What is a function?
b) What are advantages of using functions?
c) What is driver-stub method of problem solving?
d) How to define a function prototype in C?
e) How to implement a function in C?
f) How to call a function?
g) Given a function definition, what is the function header?
h) What is an argument of a function?
i) What is a parameter or parameter list of a function?
j) What is a void function?
k) What is a value-return function?
l) How to pass data to a function?
m) How to pass data out from a function?
n) How to access mathematical functions in C? Name at least two
mathematical functions in C.
o) Given a snippet of codes showing a function prototype, function
definition and function called, identify result of the function call.
p) Write a function prototype and function definition for a simple
problem.

Chapter 4
a) What is a selection control structure?
b) What is a compound statement?
c) What are the relational operators in C?
d) How to expression a condition for testing in C?
e) Comparing numerical or character values?
f) What is considered true and false in C?
g) What are the logical operators in C? and their precedence?
h) Know how && || and ! operators are evaluated
i) Precedence of logical operators compared to relational operators
j) What is short circuit evaluation?
k) What is an unary operator?
l) How to write compound condition for values in a range?
m) How to write compound condition for values outside a range?
n) How to write if-condition in C?
o) How to write if-else condition in C?
p) How to write nested if-else in C?
q) How to write if-else structure using switch statements?
r) How does a break statement works in switch statements?
s) Given a snippet of codes with some if, if-else, nested if-else or
switch statements, identify the output.
t) Write a function definition that involves the use of selection
control structure.

You might also like