Basics of C Programming Language Flashcards
What are the 3 general steps in creating an executable?
- Preprocessing
- Compiling
- Linking
What is preprocessing?
Before code is compiled it is given to a program called the preprocessor. The preprocessor edits and manipulates code, setting it up for the compiler. Typically the preprocessor looks for lines that begin with # and alters the code based on these directives.
What is compiling?
Preprocessed code is given to the compiler, a program that translates C statements into machine instructions.
What is linking?
A linker combines the object code produced by the compiler with any additional code needed to create a complete execuatable. This additional code includes things like library functions.
What are the three fundamental language features that C relies on?
- Directives
- Fucntions
- Statements
What is a function?
A series of statements grouped together and given a name
What does main return?
An integer status code
What is a statement?
A command to be executed when the program runs.
Definition: String literal
A sequence of characters surrounded by “”
What are the valid ways to write comments in C?
/* this is the only valid comment */
some compilers allow // but it is not part of standard C
Definition: variable
A data storage location
What is a variable’s type?
A specification of what type of data it will hold
Definition: expression
a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then produces (returns, in a stateful environment) another value.
True/False: An expression of the same type can be substituted wherever a value is needed.
True
Definition: format string
A string literal which specifies a method for rendering an arbitrary number of varied data type parameter(s) into a string
Definition: constant
A value that doesn’t change during execution
Definition: Identifier
A symbol that represents an entity such as variables, functions, and macros.
Definition: token
A group of characters that can’t be broken up without changing their meaning
Definition: conversion specification
A placeholder, preceded by a %, representing a value to be filled in during printed. The value following the % specifies how the value is convereted from internal form to printed form
What forms can a conversion specification have? What do the values mean?
%m.pX or %-m.pX m: minimum field width p: precision X: conversion specifier -: left align
What does p specify when used with %m.pd?
What if p is bigger than the length of the value to be printed?
Minimum number of digits. Output is zero-filled
What is the conversion specifier: d
decimal integer
What is the conversion specifier: e
floating point in exponential form
What does p indicate in %m.pe? What if p == 0?
How many digits appear after the decimal. The decimal is not displayed
What is the conversion specifier: f
Floating point in fixed-decimal format
What is the meaning of p in %m.pf? What if p == 0?
Number of digits after the decimal. Decimal is not shown
What is the conversion specifier: g
Floating point in either exponential or fixed decimal format depending on the number of significant digits.
Whats the difference between %f and %g when displaying data in fixed decimal format?
%g won’t show trailing zeros
How does scanf work?
Starts at the left of the format string. For each conversion specification it tries to locate an item of the appropriate type, skipping blank spaces if necessary. It reads until it finds a character that cannot be part of the string which it puts back into the buffer.
What happens if scanf cannot read an item successfully?
scanf returns immediately without looking at the rest of the format string or remaining buffer data.
What does scanf look for when reading an integer?
First: a digit or minus sign
Reads until a nondigit
What does scanf look for when reading a floating point?
- a plus or minus sign (optional)
- a sequence of digits possibly containing a decimal point
- an exponent (optional) consisting of the letter e, and optional sign, and one or more digits
What does scanf do when it encounters a whitespace character?
One whitespace character in the format string will match any number of whitespace characters in the input
What does scanf do if it encounters a non-white-space character in a format string?
It compares it with the next input character? If the two match, then the non-white-space character is discarded and scanf continues processing the input string. If they do not match, scanf puts the character back in the buffer and aborts.
Definition: arithmetic operators
Operators that perform some mathematical functions (+, -, *, /, %)
What is the precedence for arithmetic operators?
unary +, unary -, *, /, %, +, -
What is operator associativity?
When dealing with multiple operators, precedence is not enough to determine order of operations. Associativity comes into play when an expression contains multiple operators of the same level of precedence, and determines which operation will be evaluated first.
What is a left associative operator?
An operator that groups from left to right. Ex: i - j - k == (i -j) - k
Which arithmetic operators are left associative?
binary +, binary -, *, /, %
What is right associativity?
An operator that groups from right to left.. ex: -+x == -(+x)
Which arithmetic operators are right associative?
unary +, unary -
Definition: lvalue
Lvalues are values that have addresses being programmatically accessible to the running program. They are variables or dereferenced references
Definition: compound assignment
An assignment that uses the old value as part of the computation for the new value. Ex += *= …
What is a logical expression?
An expression that results in true or false (nonzero or 0)
What are relational operators?
A binary operator that tests a relationship between two entities and produces true or false:
, <=, >=
What is an equality operator?
A binary operator that tests equality or lack of equality between two entities:
!=
What associativity do relational and equality operators have in C?
left
What are the C logical operators? What do they produce? How do they evaluate operands?
!, &&, ||
They produce a 1 (true) or a 0 (false)
They evaluate any non-zero as true and 0 as false
What is a logical short circuit? Which operators perform this?
In something of the form: expr1 && expr2,
if expr1 is 0, expr2 is not evaluated and 0 is returned.
In an expression of the form:
expr1 || expr2, if expr1 is non-zero, expr2 is not evaluated and 1 is returned
What is the associativity of the logical operators?
! is right associative
&&, || or left associative
What is a compound statement?
A group of statements enclosed by {}, indicating to the compiler that the group is to be treated as a single statement
What is the rule for determining which if statement an else belongs to?
An else is paired with the nearest unpaired if statement
What is a conditional expression? What is the conditional operator in C?
Conditional expression is a compound expression that chooses between two expressions based on the truth value of the first one. evalExpr is evaluated, if its value is non-zero, trueExpr is evaluated and its result is the result of the entire expression. Viceversa for falseExpr.
evalExpr ? trueExpr : falseExpr ;
What is a controlling expression
An expression that is evaluated at some point during each iteration and determines if the loop will continue (expr != 0) or if it will discontinue(expr == 0)
What is the comma operator?
expr1, expr2
expr1 is evaluated and its result is dicarded. Then expr2 is evaluated and its value and type is returned.
How does the break statement work?
When executed, it transfers control just past the end of innermost loop or switch
How does the continue statement work?
When executed, it transfers control to just before the end of the loop
What is a null statement? What is it good for?
An empty statement -just a ;
It’s useful for creating a loop body that does nothing.
What are the two categories of integers?
Signed (default) and unsigned
What are the six combinations of modifiers with int?
short int unsigned short int int unsigned int long int unsigned long int
What is a heuristic for choosing modifiers with int to improve portability?
For values between -32,768 and 32,767, use int. For all other values use long int
What is an integer constant?
A number that appears in the text of the program.
ex:
10, xABC, 017
How are decimal, octal, and hexadecimal constants differentiated?
Decimal - contain digits 0-9 and do not start with a 0
Octal - contain 0-7 and start with a 0
Hex - contain 0-9 and a-f and start with a 0x
How do you force the compiler to treat a an integer constant as long? Unsigned?
Follow it with L. Follow it with U.
ex: 15UL == 15ul == 15LU == 15lu
What is the conversion specifier for reading/writing an unsigned decimal? unsigned octal? unsigned hex?
What if we want the short or long versions?
decimal: %u
octal: %o
hex: %x
short: %h[u,o,x,d]
long: %l[u,o,x,d]
What is the default type for floating point constants?
double
What are the conversion specifications for single precision floating point values? Double precision? Long double?
single: %f, %g, %e
double: %lf, %lg, %le
long double: %Lf, %Lg, %Le
When should you use l in conjunction with a floating point conversion specification?
Only with scanf. printf %[e,f,g] can print single and double precision
How does C treat chars?
8 bit integers. The C specification does not specify whether they are signed or unsigned.
What is the conversion specifier for characters?
%c
What functions can you for characters instead of scanf and printf?
getchar() and putchar(char ch)
What is the sizeof operator?
Allows a program to determine how much memory is required by a particular type
What considerations are there when printing a sizeof value?
The return type is implementation defined. The trick is cast it to a known type before printing.
What is the difference between the two types of conversions in C?
Implicit conversions are performed automatically by the compiler. Explicit conversions are mandated by the programmer.
When is implicit conversion performed? (4)
- the operands in an arithmetic or logical expression do not match
- the type of the right side of an assignment doesn’t match the type of the lvalue
- the type of an argument of a function doesn’t match the type of the corresponding parameter
- the expression in a return statement doesn’t match the function’s return type
What is usual arithmetic conversion?
When binary operators have mixed types, their values are converted to the narrowest type that will accommodate both values. The narrowest type is the one that requires the least number of bytes to store.
What is integral promotion?
When an integer type is converted to a less narrow integer type. If a short int is added to a long int, the short int is promoted to long.
When does integral promotion occur?
Whenever the compiler says it should occur:
Logically: when operating with mixed integer types. ex: int + long.. int is promoted to long
Also: short a = 1; short b = 2; a += 2; Will throw a warning because C specification says += returns an int and we are trying to store an int in a short.
What are the two cases for usual arithmetic conversions?
- The type of either operand is a floating type
2. Neither operand has a floating type
What is the promotion chart for floating point types?
long double ^ double ^ float
What is the promotion chart for integers?
unsigned long int ^ long int ^ unsigned int ^ int
What happens when an unsigned integer is mixed with a signed operand?
Signed operand will be treated as unsigned. The sign bit of the signed operand will be treated a normal bit and produce unexpected results. ex:
signed int i = -10;
unsigned int j = 10;
printf(“%d\n”, (i < j));
will print 0 instead of 1
What conversion happens in an assignment statement?
If the rvalue type is different from the lvalue type, the rvalue type is converted to the value type
How do you cast?
( cast-to-type ) expression
What is the purpose of typedef? How do you typedef?
To give alternate names to existing types.
typdef existing-type symbol
Why not just use macros instead of typedefs?
Type definitions are more powerful. Array and pointer types cannot be defined as macros. Typedef names are subject to the same scope rules as variables. Macros are not and cause unexpected errors.
Definition: array
A data structure containing a number of data values, all of which have the same type.
What is needed to declare an array?
- a type
- an identifier
- the size of the array enclosed in brackets OR empty brackets with an initializer
What is an array initializer?
a list of constant expressions enclosed in {} and separated by commas
How are multidimensional arrays stored in memory?
Row major order. Row 1 is ascending index order, then row 2, so on and so on.
What is a constant array?
An array declared const cannot be modified.
What is the assumed return type of a function that does not specify its return type?
int
What is a function prototype?
A way to provide the compiler the way to call a function before we implement it.
What is the difference between parameters and arguments?
Parameters appear in function definitions. They are dummy names for the values passed in the function call.
Arguments are expressions that appear in function calls.
Arguments supply parameters their value.
What does “pass by value mean”?
In each function call, the evaluated argument’s value is assigned to the corresponding parameter
What is main the implication of pass by value?
Changes made to the parameter does not affect the value of the argument.
How can you write a function that changes values?
Define a function that takes pointers to the values you want to alter.
What does the compiler do if it encounters arguments that don’t match the types of the parameters?
Case 1:
The compiler has seen a prototype for the function - The value of each argument is implicitly converted to the corresponding parameter’s type
Case 2:
The compiler has not seen a prototype for the function - The compiler performs default argument conversion. Floats are promoted to doubles, integral promotions are performed (char/short to int)
What happens when an array is passed to a function?
The function is given a pointer to the first element of the array.
What is the value that main returns?
A status code to the operating system
What is the difference between return and exit?
Exit can be called from any function
What is a local variable?
A variable only accessible in the block in which it is defined. Typically inside a function
What are the default properties of a local variable?
Automatic storage duration
Block scope
What is storage duration?
The portion of program execution during which storage for the variable exists.
What is automatic storage duration?
Storage for an auto variable is allocated when the program flow enters its block and is deallocated when flow leaves its block
What is scope?
The portion of the program text in which a variable can be referenced.
What is block scope?
A variable with block scope is only visible from its point of declaration to the end of its block
What is a static variable?
A variable with static storage duration has a permanent storage location. It retains its value throughout the execution of the program.