C Programming Fundamentals
- 113Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Why is C considered a useful language to learn, including for someone who wants to study C++?
113 questions
- 1
Why is C considered a useful language to learn, including for someone who wants to study C++?
Model answer
C is one of the oldest programming languages, and many modern languages are based on it. Learning C provides a foundation for understanding C++ and other languages.
Explanation
The content highlights C’s history and its influence on modern languages as reasons to learn its fundamentals.
- 2
What two things are needed to start writing and running C programs?
Model answer
You need an environment for writing C code, such as a text editor or IDE, and a C compiler to translate the code for execution.
Explanation
The editor is where code is written; the compiler translates it into a form the computer can execute.
- 3
What basic capabilities must a text editor provide to be usable for writing C programs?
Model answer
It must let you write text and save it in a specified file format.
Explanation
A special-purpose editor is not required; any editor with these basic capabilities can be used.
- 4
What does IDE stand for, and what is its purpose?
Model answer
IDE stands for integrated development environment. It is a specialized environment for writing, running, and managing programs.
Explanation
An IDE provides more programming support than a basic text editor.
- 5
What does a C compiler do, and why is it needed?
Model answer
A C compiler translates C code into a form the computer can understand and execute. It is needed because the computer cannot directly execute the human-readable C instructions.
Explanation
Compilation transforms the source instructions into executable computer code.
- 6
Which Code::Blocks installer option was recommended for Windows, and why?
Model answer
The Code::Blocks installer bundled with MinGW was recommended because it installs both the IDE and a C compiler.
Explanation
The bundled installer provides the two required tools together on Windows.
- 7
How can a Mac user check whether a C compiler is already installed?
Model answer
Open Terminal and run
cc -v. If compiler information is displayed, a C compiler is installed.Explanation
The command checks for the compiler and displays its version information when available.
- 8
What command-line tools installation command is given for a Mac that does not have a C compiler?
Model answer
Run
xcode-select --installin Terminal to install the command-line tools needed for the compiler.Explanation
The installation provides the tools needed to get the C compiler working.
- 9
How can a Mac user install the Code::Blocks application after downloading it?
Model answer
Unzip the downloaded file and drag Code::Blocks into the Applications folder.
Explanation
This is the installation method described for the Mac application.
- 10
What type of Code::Blocks project is appropriate for a basic C program that runs on a computer?
Model answer
A console application is appropriate for a basic C program that runs on a computer.
Explanation
The console application project is presented as the basic project type needed for the course examples.
- 11
When creating a Code::Blocks project for a C course, which language option should be selected: C or C++?
Model answer
Select C, because C and C++ are different programming languages and the project is for C.
Explanation
Choosing C sets up the project for the language being studied.
- 12
What is the role of the
mainfunction in a C program?Model answer
mainis the function that is executed when the program runs. The computer executes the instructions inside its braces.Explanation
The program begins by carrying out the code contained in
main. - 13
What is a function in C, and how does
mainrelate to that concept?Model answer
A function is a named collection of code that performs a task.
mainis a function, and it is the one that runs when the program starts.Explanation
The content describes
mainas a function and explains that functions contain code. - 14
What is the purpose of the
#includelines in a basic C file, according to the content?Model answer
They are instructions needed for the program to use certain features, although their specific operation is covered later.
Explanation
The content identifies
#includelines as necessary but postpones a detailed explanation of them. - 15
What does a basic “Hello, world” C program do?
Model answer
It prints the text “Hello, world” to the screen.
Explanation
The example program uses a print instruction to display this message.
- 16
What are the two main steps required to execute a C program?
Model answer
First build or compile the program, then run the resulting executable code.
Explanation
Building translates the C code; running tells the computer to execute the translated code.
- 17
What is the difference between building a C program and running it?
Model answer
Building translates the C source into code the computer can execute; running executes that code.
Explanation
Compilation and execution are separate stages, even when an IDE offers an option to perform both.
- 18
What does the “Build and Run” option in an IDE do?
Model answer
It builds the program and then runs it, allowing the programmer to see the result in the console.
Explanation
The option combines the two steps needed to test a program.
- 19
What is the purpose of the console window when a C program runs?
Model answer
The console displays information that the program outputs, such as text printed by
printf.Explanation
A program that prints output displays it in the console; a program with no output may show nothing there.
- 20
What does a semicolon signify at the end of a C instruction?
Model answer
It marks the end of that instruction so C can move on to the next one.
Explanation
C instructions are terminated with semicolons.
- 21
Why does the order of instructions matter in a C program?
Model answer
The computer executes instructions in order, so changing their order can change the program’s output or behavior.
Explanation
The content demonstrates that rearranging print instructions changes the shape printed to the screen.
- 22
What is a variable in C, and what is its main purpose?
Model answer
A variable is a named container for data, such as a number or text. It helps a program store, use, and manage information.
Explanation
Variables make it easier to keep track of data used throughout a program.
- 23
Why can variables make a program easier to maintain when a value is used repeatedly?
Model answer
The value can be changed in one place, and every use of the variable will then use the updated value.
Explanation
Without a variable, a programmer may need to find and change every occurrence of the value.
- 24
Why should a variable have a descriptive name?
Model answer
A descriptive name helps explain what information the variable stores.
Explanation
Names such as
character_agemake the purpose of a stored value clearer. - 25
What information must be specified when declaring a variable in C?
Model answer
The variable’s data type and its name must be specified.
Explanation
The data type tells C what kind of information the variable will hold, while the name identifies it.
- 26
What is an integer in C?
Model answer
An integer is a whole number, such as 40, without a decimal part.
Explanation
Integers are used for whole-number values.
- 27
What is a
doubleused to represent in C?Model answer
A
doublerepresents a decimal, or floating-point, number, such as 3.7.Explanation
The content recommends
doublefor decimal numbers when starting out. - 28
How does a
floatrelate to adoublein the content?Model answer
Both are types used for decimal numbers; the content notes that beginners will generally want to use
doubleand does not explain their detailed differences.Explanation
The distinction between the two types is explicitly left for later study.
- 29
What is the difference between the integer values
40and40.0in C?Model answer
40is an integer, while40.0is a decimal value represented as a floating-point number.Explanation
The decimal point determines whether the value is treated as an integer or a decimal number.
- 30
What does the
chartype store?Model answer
A
charstores one character, such as the letterA.Explanation
A single character value is written between single quotation marks in the examples.
- 31
How is a string of characters represented in the examples, and how does it relate to
char?Model answer
A string is represented using a
chararray, written with square brackets, and stores a collection of characters rather than just one character.Explanation
The content describes strings as collections of characters and later identifies them as character arrays.
- 32
What is a constant in C?
Model answer
A constant is a value that cannot be modified. In a declaration, the
constkeyword marks a variable as unchangeable.Explanation
Attempting to assign a new value to a constant variable results in an error.
- 33
What naming convention for constant variables is mentioned, and is it required?
Model answer
Developers often use all-uppercase names for constants, but this convention is not required.
Explanation
Uppercase names make constants recognizable, while the content identifies this as a practice rather than a rule.
- 34
What does the
printffunction do?Model answer
printfdisplays text or other specified information on the screen.Explanation
It is used to print both ordinary text and values such as variables.
- 35
How can a C string include a line break when printed with
printf?Model answer
Use the newline escape sequence
\n.Explanation
The sequence moves subsequent output onto a new line.
- 36
How can a quotation mark be printed as part of text in a C string?
Model answer
Escape the quotation mark with a backslash, as in
\".Explanation
The backslash prevents the quotation mark from being treated as the end of the string.
- 37
What is a format specifier in
printf?Model answer
A format specifier is a marker beginning with
%that tellsprintfwhat kind of value to insert into the output.Explanation
The letter following
%identifies the kind of data to print. - 38
Which
printfformat specifier is used for an integer?Model answer
Use
%dto print an integer.Explanation
The
dspecifier is used for integer values in the examples. - 39
Which
printfformat specifier is used for a string?Model answer
Use
%sto print a string of characters.Explanation
The string value is supplied after the format string as an additional argument.
- 40
Which
printfformat specifier is used for a decimal number such as adouble?Model answer
Use
%fto print a decimal or floating-point value.Explanation
The examples use
%fto display floating-point numbers. - 41
Which
printfformat specifier is used for one character?Model answer
Use
%cto print a single character.Explanation
The examples use
%cfor acharvalue. - 42
When a
printfstring contains several format specifiers, how should the corresponding values be supplied?Model answer
Supply values after the format string in the same order as the specifiers appear, separating them with commas.
Explanation
Each placeholder is matched with its corresponding argument by position.
- 43
Which operators perform addition, subtraction, multiplication, and division in C?
Model answer
Use
+for addition,-for subtraction,*for multiplication, and/for division.Explanation
These are the four basic arithmetic operations demonstrated in the content.
- 44
What type of result is produced when an integer is combined arithmetically with a floating-point number?
Model answer
The result is a floating-point number.
Explanation
The example of adding an integer to a decimal returns a decimal result.
- 45
What happens when two integers are divided using integer arithmetic in C?
Model answer
The result is an integer, so any fractional part is discarded; for example, integer division of 5 by 4 produces 1.
Explanation
To preserve a fractional result such as 1.25, one operand must be a floating-point value and the result printed as one.
- 46
What does the
powfunction do?Model answer
powraises its first argument to the power given by its second argument.Explanation
For example,
pow(2, 3)computes 2 raised to the third power. - 47
What does the
sqrtfunction do?Model answer
sqrtreturns the square root of a number.Explanation
The example uses it to calculate the square root of 36.
- 48
How do the
ceilandfloorfunctions differ?Model answer
ceilrounds a decimal number up to the next integer, whilefloorrounds it down to the lower integer.Explanation
For example, the content shows both functions being applied to decimal values around 36.
- 49
What does the
scanffunction do?Model answer
scanfreads information entered by the user and stores it in program variables.Explanation
It performs input, in contrast to
printf, which displays output. - 50
Why is an ampersand used with a variable when
scanfreads an integer, decimal number, or character?Model answer
The ampersand provides the variable’s address so
scanfcan store the input in that variable.Explanation
The content connects this use of
&to pointers and shows it with numeric and character input. - 51
What is the difference between the
scanfformat specifiers%fand%lfin the examples?Model answer
%lfis used byscanfto read adouble, whileprintfuses%fto print a floating-point value.Explanation
The input and output format specifiers differ for a
doublein the examples. - 52
Why does
scanfcommonly read only the first word when used to get a string?Model answer
It stops reading the string when it encounters whitespace, such as a space.
Explanation
As a result, an input such as “John Smith” may be read only as “John.”
- 53
How does
fgetsdiffer fromscanfwhen reading a line of text?Model answer
fgetscan read a line containing spaces and stores it in a character array, while the demonstratedscanfstring input stops at the first space.Explanation
The content recommends
fgetsfor reading user-entered strings that may contain multiple words. - 54
What three arguments are passed to
fgetsin the user-input example, and what do they represent?Model answer
The arguments are the destination character array, the maximum number of characters to read, and
stdin, the standard input source.Explanation
The size limit helps keep input within the capacity of the destination array.
- 55
What input-related detail should a programmer account for when using
fgets?Model answer
The newline produced when the user presses Enter may be included in the stored string.
Explanation
The content demonstrates that this newline can affect what appears after the input is printed.
- 56
What is the purpose of an
ifstatement?Model answer
An
ifstatement checks a condition and executes its block of code when the condition is true.Explanation
It allows a program to respond differently depending on whether a condition is met.
- 57
What does an
elseblock do?Model answer
An
elseblock executes when the precedingifcondition is false.Explanation
It provides an alternative action for the condition not being true.
- 58
When is an
else ifclause useful?Model answer
It checks another condition when the preceding
iforelse ifcondition is false.Explanation
It allows a program to test several possible cases in sequence.
- 59
What does the logical AND operator require for a combined condition to be true?
Model answer
Both conditions joined by AND must be true.
Explanation
The example uses AND to verify that one number is at least as large as two other numbers.
- 60
What does the logical OR operator require for a combined condition to be true?
Model answer
At least one of the conditions joined by OR must be true.
Explanation
The combined condition is false only when both component conditions are false.
- 61
What do the comparison operators
<,>,<=, and>=test?Model answer
They test whether one value is less than, greater than, less than or equal to, or greater than or equal to another value.
Explanation
These operators are used to compare values in conditional expressions.
- 62
How does a program test equality and inequality between two values?
Model answer
Use
==to test equality and!=to test inequality.Explanation
A single
=assigns a value, whereas the two-character operators perform comparisons. - 63
What does the negation operator
!do when applied to a condition?Model answer
It reverses the condition’s truth value: true becomes false, and false becomes true.
Explanation
The content demonstrates negating a complete comparison by placing
!before it. - 64
What is the purpose of a
switchstatement?Model answer
A
switchcompares one value against several possible cases and runs the code for the matching case.Explanation
It offers a convenient alternative to writing a long chain of conditions for one value.
- 65
What does
breakdo inside aswitchcase?Model answer
It exits the switch statement so execution does not continue into later cases.
Explanation
A break is used after a case’s action to prevent subsequent cases from running.
- 66
What is the purpose of the
defaultbranch in aswitchstatement?Model answer
It handles the situation in which none of the listed cases matches the value.
Explanation
The default branch can be used to report an invalid or unexpected value.
- 67
How can an
if/else ifchain and aswitchstatement be similar?Model answer
Both can select different actions based on which of several possible conditions or values applies.
Explanation
A switch is especially convenient when comparing one value to several alternatives.
- 68
What is a
whileloop, and when does it stop repeating?Model answer
A
whileloop repeatedly executes its body while its condition is true. It stops when the condition becomes false.Explanation
The condition is checked before each iteration.
- 69
What is an infinite loop?
Model answer
An infinite loop is a loop whose condition never becomes false, so its body keeps executing.
Explanation
For example, failing to update an index in a loop that depends on that index can cause it to repeat indefinitely.
- 70
How does a
do-whileloop differ from awhileloop?Model answer
A
do-whileloop executes its body before checking the condition, so its body runs at least once.Explanation
A regular
whileloop checks its condition before the first execution of its body. - 71
What is a
forloop particularly useful for tracking?Model answer
It is useful for tracking the iteration of a loop with an indexing variable that changes on each pass.
Explanation
The indexing variable can be used to determine which iteration is underway.
- 72
What three components are placed in the parentheses of the
forloop shown in the content?Model answer
An initialization, a loop condition, and an update expression are placed there, separated by semicolons.
Explanation
The example initializes an index, checks whether it is within a limit, and increments it after each iteration.
- 73
When does a
forloop test its condition, and when does it perform its update expression?Model answer
It tests the condition before each iteration and performs the update expression after each completed iteration.
Explanation
This ordering allows the index to change between passes through the loop.
- 74
How can a
forloop be used to print every element of an array?Model answer
Loop an index from 0 up to, but not including, the array’s element count, and use the index to access each element.
Explanation
The content demonstrates this method for printing all values in an integer array.
- 75
What is an array, and how does it differ from a single variable?
Model answer
An array is a structure that stores multiple values, while a typical variable stores one value.
Explanation
Arrays are useful for keeping related collections of data together rather than creating a separate variable for each item.
- 76
What data-type rule applies to the elements of an array in the examples?
Model answer
All elements in an array have the same data type.
Explanation
For example, an integer array holds integer values.
- 77
What is an element of an array?
Model answer
An element is one individual value stored in an array.
Explanation
The content refers to each stored value as an element and separates initialized elements with commas.
- 78
At what index does the first element of a C array appear?
Model answer
The first element is at index 0.
Explanation
C array indexes begin at zero rather than one.
- 79
How can a programmer access or change an individual array element?
Model answer
Use the array name followed by the element’s index in square brackets; assign a new value to that indexed expression to change it.
Explanation
The examples access elements by index and modify an element by assigning to it.
- 80
When an array is declared without initial values, why must its capacity be specified in the declaration shown?
Model answer
The capacity tells C how many elements the array can hold so it can allocate enough memory.
Explanation
The examples specify an array size even when its elements will be assigned later.
- 81
How is a string represented in C according to the content?
Model answer
A string is an array of characters.
Explanation
The content explains that character strings use array storage, even though they are often treated as a distinct concept.
- 82
What is a two-dimensional array?
Model answer
It is an array whose elements are themselves arrays.
Explanation
The example places several smaller arrays inside one outer array.
- 83
How are two indexes used to access an element in a two-dimensional array?
Model answer
The first index selects an inner array, and the second index selects an element within that inner array.
Explanation
For example, an expression with indexes 1 and 1 accesses the second element of the second inner array.
- 84
How can nested loops process all elements in a two-dimensional array?
Model answer
Use an outer loop to visit each inner array and an inner loop to visit each element within it.
Explanation
The content uses this approach to print every value in a two-dimensional array.
- 85
What is a function in C?
Model answer
A function is a collection of code designed to perform a specific task.
Explanation
Grouping related code into a function makes it possible to call and reuse that task.
- 86
What does calling a function mean?
Model answer
Calling a function tells C to execute the code inside that function.
Explanation
Defining a function does not execute it; the program must call it to run its code.
- 87
What is a function parameter?
Model answer
A parameter is an input value that a function receives and can use when performing its task.
Explanation
A function can use different parameter values on different calls to perform its task differently.
- 88
What does the
voidreturn type indicate for a function?Model answer
It indicates that the function does not return information to its caller.
Explanation
The content uses
voidfor a function that prints a greeting but returns no value. - 89
What does a function’s return type specify?
Model answer
It specifies the type of information the function returns to the code that called it.
Explanation
For example, a function that returns a decimal result can be declared with a
doublereturn type. - 90
What does the
returnstatement do?Model answer
It sends a value back to the caller and exits the function.
Explanation
Code after a return statement in that function is not executed.
- 91
What is a function prototype used for?
Model answer
A prototype declares a function’s signature before its definition, allowing the function to be defined later in the file and called earlier.
Explanation
The signature identifies the function’s return type, name, and parameters.
- 92
What is a structure (
struct) in C?Model answer
A structure groups related fields, potentially of different data types, into one data structure.
Explanation
The student example groups a character-array name and major, an integer age, and a double GPA.
- 93
How does a structure differ from an array?
Model answer
A structure can group fields of different types and identify them by names; an array holds elements of the same type and accesses them by indexes.
Explanation
The content contrasts named fields such as
agewith indexed array elements. - 94
How are fields of a structure accessed?
Model answer
Use the structure variable’s name, a dot, and the field name, such as
student1.age.Explanation
The dot operator selects a named field within a structure instance.
- 95
Why does the student example use a string-copy function to set a structure’s name field?
Model answer
The name field is a character array, and the example uses a string-copy function to copy text into that array.
Explanation
The content notes that the character array cannot be assigned a new string in the same way as a simple numeric field.
- 96
Where are variable values stored while a C program is running, and what is this memory commonly called?
Model answer
They are stored in the computer’s memory, commonly called RAM, which programs use while running.
Explanation
The content describes variable values as occupying physical locations in RAM.
- 97
What is a memory address?
Model answer
A memory address identifies a location in computer memory where information is stored.
Explanation
C uses such addresses to locate the values held by variables.
- 98
How can a C program obtain a variable’s memory address, and how can it print that address?
Model answer
Use
&before the variable name to obtain its address, and use%pwithprintfto display the address.Explanation
The examples apply this to variables of different types.
- 99
What is a pointer?
Model answer
A pointer is a value that represents a memory address.
Explanation
It is a data type used to work with addresses of locations in memory.
- 100
How is a pointer variable declared in the examples?
Model answer
Place an asterisk after the data type, such as
int *p_age.Explanation
The pointer’s declared type corresponds to the type of variable whose address it stores.
- 101
What value is stored in a pointer variable in the examples?
Model answer
It stores the address of another variable, obtained by using
&with that variable’s name.Explanation
For instance, a pointer assigned
&agestores the address ofage. - 102
What does it mean to dereference a pointer?
Model answer
Dereferencing a pointer accesses the value stored at the memory address it points to.
Explanation
The content uses
*to dereference an integer pointer and retrieve the integer value. - 103
How does the value obtained by dereferencing a pointer relate to the pointer’s type?
Model answer
It has the type of the value stored at the address; for example, dereferencing an
intpointer yields an integer value.Explanation
The pointer holds an address, while dereferencing accesses the data at that address.
- 104
What does the
fopenfunction do, and what are its two arguments in the examples?Model answer
fopenopens a file; its arguments are the file name and a mode specifying how the file will be used.Explanation
The mode determines whether the program reads, writes, or appends to the file.
- 105
What do the file modes
r,w, andamean?Model answer
ropens a file for reading,wopens it for writing, andaopens it for appending.Explanation
The examples use
wto write or overwrite andato add information at the end. - 106
What happens when a file is opened in write mode in the examples?
Model answer
A new file can be created, or an existing file’s contents can be overwritten.
Explanation
The content demonstrates that writing new text in
wmode replaces the previous contents. - 107
What happens when a file is opened in append mode?
Model answer
New information is added to the end of the existing file rather than replacing its contents.
Explanation
The example appends another employee record to the file.
- 108
What is the purpose of
fclose?Model answer
It closes the file after the program has finished working with it.
Explanation
The content recommends closing a file after use so changes can be finalized and the file removed from active use.
- 109
What does
fprintfdo when given a file pointer?Model answer
It writes formatted text or other specified information to the file associated with that pointer.
Explanation
It works similarly to
printf, but directs the output to a file. - 110
Where is a file created if the program specifies only its file name and no path?
Model answer
It is created in the same directory as the C file in the example setup.
Explanation
Specifying a path would instead direct file creation to the indicated location.
- 111
What does the
fgetsfunction do when used to read a file?Model answer
It reads a line from the file into a character array.
Explanation
The examples use it to read file contents one line at a time.
- 112
What three arguments does
fgetsreceive in the file-reading example?Model answer
It receives the destination character array, the maximum number of characters to read, and the file pointer.
Explanation
These arguments identify where to store the line, limit the read size, and identify the file.
- 113
How does the file position change when
fgetsis called repeatedly on the same file?Model answer
Each call reads the next line and advances the file position, allowing successive calls to retrieve successive lines.
Explanation
The example’s first calls retrieve the first line and then the second line.

Ask anything
We've read this whole study set, so questions about it get the best answers.