1. Home
  2. Explore
  3. Computer Science
  4. C Programming Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

C Programming Fundamentals

  • 106Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: C Foundations
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is C, and why can learning it help when studying other programming languages?

Tap to flipClick to flipspace

C is an old, widely influential programming language; many modern languages are based on it, and its fundamentals can help when learning languages such as C++.

Or swipe the card

106 cards

  1. What is C, and why can learning it help when studying other programming languages?

    C is an old, widely influential programming language; many modern languages are based on it, and its fundamentals can help when learning languages such as C++.

  2. What two basic tools are needed to write and run C programs?

    A text editor or IDE for writing code, and a C compiler for translating it into a form the computer can execute.

  3. What does IDE stand for, and what does an IDE provide?

    Integrated Development Environment. It is a specialized environment for writing, running, and managing programs.

  4. What does a C compiler do?

    It translates C source code into a form the computer can understand and execute.

  5. What is the purpose of the main function in a C program?

    It is the function whose code is executed when the program runs.

  6. How does a computer execute the instructions inside main?

    It processes them in order, from the first instruction to the next, unless control-flow code changes that order.

  7. What does a semicolon signify in C?

    It marks the end of an instruction or statement.

  8. What is the difference between building and running a C program?

    Building compiles the source code; running executes the compiled program.

  9. What does Code::Blocks' “Build and Run” action do?

    It compiles the program and then executes it.

  10. What does printf do?

    It prints text or formatted values to the program's output, commonly the console.

  11. What does the escape sequence \n do in printed text?

    It moves subsequent output to a new line.

  12. What is a C comment, and what are two uses for it?

    A comment is text ignored by the compiler. It can document code or temporarily disable code without deleting it.

  13. What is the difference between an integer and a double in C?

    An integer (int) represents a whole number; a double represents a decimal, or floating-point, number.

  14. What is a float in C?

    A data type for decimal, floating-point numbers. The material recommends using double for most beginner purposes.

  15. What does the char type store?

    One character, conventionally written between single quotation marks, such as 'A'.

  16. How is a string represented in the C examples?

    As an array of char values, with the text written between double quotation marks.

  17. How does declaring a variable differ from initializing one?

    Declaring identifies the variable and its type; initializing also gives it an initial value.

  18. Why are descriptive variable names useful?

    They indicate what data a variable stores and make code easier to understand and maintain.

  19. How can variables make repeated data easier to update?

    Store a value once in a variable and refer to the variable wherever needed; changing its value in one place updates later uses.

  20. How can a program change a variable's value while it runs?

    Assign a new value to the variable; later uses of it will use the updated value.

  21. What is a format specifier in printf?

    A marker beginning with % that tells printf what kind of value to insert into the output.

  22. Which printf format specifiers are used for an integer, a double, a string, and a single character?

    Use %d for an integer, %f for a double, %s for a string, and %c for a character.

  23. When printf has several format specifiers, how should its values be supplied?

    Supply values after the format string, separated by commas, in the same order as the corresponding specifiers.

  24. What does scanf do?

    It reads input supplied by the user and stores it in the program.

  25. How do the examples use scanf to read an integer, a double, and a character?

    Use %d for an integer, %lf for a double, and %c for a character. The character example puts a space before %c.

  26. Why does scanf use &variable when reading a number or a single character?

    The ampersand supplies the variable's address so scanf knows where to store the input.

  27. Why do the examples omit & when scanf reads a string into a character array?

    The array name is used as the location for the string input; the examples pass it directly.

  28. What limitation does scanf have when reading a string with %s?

    It stops reading at the first whitespace, so it reads only the first word of input such as a full name.

  29. How is fgets used to read a line of text from standard input?

    Pass the destination character array, its maximum size, and stdin to fgets.

  30. Why should a program set a maximum size when reading a string?

    To limit input to the capacity of the destination array and reduce the risk of overflowing its buffer.

  31. What input-related behavior of fgets should a program account for?

    It can retain the newline produced when the user presses Enter, affecting subsequent output.

  32. What is a constant in C?

    A value that is not meant to change; a variable declared with const cannot be reassigned.

  33. What happens if code tries to change a variable declared with const?

    The modification is invalid and produces a compilation error.

  34. What naming convention is often used for constants?

    Use uppercase names, sometimes separating words with underscores; this is a convention, not a requirement.

  35. Which basic arithmetic operators are shown for C numbers?

    Addition +, subtraction -, multiplication *, and division /.

  36. What happens when an integer and a decimal number are used together in arithmetic?

    The result is a floating-point value, as in 5+4.5=9.55 + 4.5 = 9.55+4.5=9.5.

  37. What is the result type of arithmetic performed on two integers?

    It is an integer; a decimal fraction can be discarded, as in integer division 5/45 / 45/4, which yields 111.

  38. How can a program preserve the fractional part of a division such as 5/45 / 45/4?

    Make at least one operand a floating-point value, such as 5/4.05 / 4.05/4.0; the result is 1.251.251.25.

  39. What does the pow function calculate?

    It raises its first argument to the power of its second; for example, pow(2, 3) returns 888.

  40. What does the sqrt function calculate?

    The square root of its argument; for example, sqrt(36) gives 666.

  41. What do the ceil and floor functions do?

    ceil rounds a number up to the next integer; floor rounds it down to the preceding integer.

  42. Which printf specifier is used to display a floating-point value in the examples?

    Use %f.

  43. What is the purpose of an if statement?

    It tests a condition and executes its block of code when the condition is true.

  44. What does an else block do?

    It executes when the condition in the associated if statement is false.

  45. When is else if useful?

    When a program should test another condition after an earlier if condition is false.

  46. Which comparison operators are shown for conditions in C?

    Less than <, greater than >, less than or equal to <=, greater than or equal to >=, equal to ==, and not equal to !=.

  47. How does the logical AND operator affect a combined condition?

    Both conditions must be true for the combined condition to be true.

  48. How does the logical OR operator affect a combined condition?

    At least one of the conditions must be true for the combined condition to be true.

  49. What does the ! negation operator do to a condition?

    It reverses the condition's truth value: true becomes false, and false becomes true.

  50. What is a switch statement useful for?

    Comparing one value against multiple possible cases and executing code for the matching case.

  51. What is the purpose of break in a switch case?

    It exits the switch so execution does not continue into subsequent cases.

  52. What does the default branch of a switch do?

    It runs when none of the listed cases matches the switch value.

  53. What is a while loop?

    A loop that repeats its block while its condition is true, checking the condition before each iteration.

  54. What is an infinite loop, and how can a loop accidentally become one?

    It is a loop whose condition never becomes false. For example, failing to update a counter used in the condition can cause it to repeat indefinitely.

  55. How does a do-while loop differ from a while loop?

    A do-while loop executes its body once before checking its condition, so it runs at least once.

  56. What three components are placed in a for loop's header?

    An initialization, a continuation condition, and an update expression, in that order.

  57. What is the role of a loop's initialization, condition, and update in a for loop?

    Initialization sets up the loop variable; the condition determines whether to continue; the update changes the variable after each iteration.

  58. How can a guessing game use a loop to keep asking for guesses?

    Repeat while the user's guess differs from the secret number; exit when the guess matches.

  59. How can a guessing game impose a maximum number of attempts?

    Track the number of guesses, compare it with a guess limit, and stop the loop when the limit is reached or the secret number is guessed.

  60. What is an array?

    A data structure that stores multiple values of the same type under one name.

  61. How does an array differ from an ordinary variable?

    A variable stores one value; an array can store multiple values of the same type.

  62. How are array elements separated when an array is initialized with values?

    They are separated by commas inside braces.

  63. How are array elements accessed in C?

    Use the array name followed by the element's index in square brackets.

  64. At what index does a C array begin?

    At index 000; the first element is at index 000, the second at index 111, and so on.

  65. How can a program change one element of an array?

    Assign a new value to the element at its index, such as array[1] = 200.

  66. What must be specified when declaring an array without initializing its elements?

    Its capacity, so the compiler can allocate enough memory for the intended number of elements.

  67. What should a program assume about an array element that has not been assigned a value?

    It should not assume the element contains a meaningful value; assign a value before relying on it.

  68. Why are arrays useful for collections such as many numbers or names?

    They keep related values organized under one name, avoiding the need for a separate variable for every value.

  69. How is a C string related to an array?

    A string is represented as an array of characters.

  70. What is a two-dimensional array?

    An array whose elements are themselves arrays.

  71. How are elements of a two-dimensional array accessed?

    Use two indices: one for the inner array and one for the element within it, such as nums[1][0].

  72. How can nested loops process every element of a two-dimensional array?

    Use an outer loop to visit each inner array and an inner loop to visit each element within it.

  73. Why is a for loop commonly used to traverse an array?

    Its index variable can advance through the array's positions, allowing the program to process each element in turn.

  74. What is a function in C?

    A named collection of code designed to perform a specific task.

  75. What does it mean to call a function?

    To request that the program execute the code in that function.

  76. Why is main special among C functions?

    It is the function that runs when program execution begins.

  77. What does the return type void indicate?

    The function does not return a value.

  78. What is a function parameter?

    An input variable declared in a function's definition that receives a value when the function is called.

  79. How can a function behave differently on different calls?

    Pass it different argument values; its parameters receive those values and the function can use them in its work.

  80. What does a function's return type describe?

    The type of value the function gives back to its caller.

  81. What does a return statement do?

    It sends a value back to the caller and ends execution of the current function.

  82. Why can a function need to be defined before the function that calls it?

    So the compiler knows about the function when it encounters the call.

  83. What is a function prototype?

    A declaration that gives a function's name, return type, and parameter types before its full definition.

  84. What is a struct in C?

    A data structure that groups related fields, potentially of different data types, under one type.

  85. How can a struct model a real-world entity such as a student?

    Define fields for its attributes, such as name, major, age, and GPA, then create individual student values using that structure.

  86. How does a structure differ from an array?

    A structure can group fields of different types with named fields; an array holds multiple values of one type, accessed by index.

  87. How do you access a field of a structure variable?

    Use the structure variable's name, a dot, and the field name, such as student1.age.

  88. Why can't a character-array field be assigned a string with ordinary array assignment?

    It is an array, and arrays are not assigned new values that way; the example uses strcpy to copy text into the field.

  89. What does strcpy do in the structure example?

    It copies a source string into a destination character array, such as a student's name field.

  90. Where does a program's variable data reside while it runs?

    In computer memory, commonly called RAM.

  91. What is a memory address?

    A location in computer memory where data is stored.

  92. How can a C program obtain a variable's memory address?

    Use the address-of operator & before the variable name.

  93. What does the printf format specifier %p display?

    A pointer value, such as a memory address.

  94. What is a pointer?

    A variable whose value is a memory address.

  95. How is a pointer to an integer declared in the examples?

    With an integer type and an asterisk, for example int *p_age.

  96. How can a pointer variable be made to point to an existing variable?

    Assign it that variable's address using &, such as p_age = &age.

  97. What does it mean to dereference a pointer?

    Use * to access the value stored at the memory address held by the pointer.

  98. What type of value does dereferencing a pointer to an integer produce?

    The integer value stored at the address the pointer points to.

  99. What does the FILE * variable represent in the file examples?

    A file pointer used to refer to an opened file for file operations.

  100. What do the file modes r, w, and a mean?

    r opens a file for reading; w creates or overwrites a file for writing; a opens it to append data.

  101. What does fopen do?

    It opens a file using a specified filename and mode, returning a file pointer for working with it.

  102. Why should a program close a file after using it?

    To release the file resource and ensure changes are finalized.

  103. What does fprintf do when given a file pointer?

    It writes formatted text to the file associated with that pointer.

  104. How does writing with mode w differ from appending with mode a?

    Mode w replaces existing file contents or creates a file; mode a adds new content at the end.

  105. How can a program read a file one line at a time in the examples?

    Call fgets with a destination buffer, its maximum size, and the file pointer.

  106. What happens to the file position when fgets reads a line?

    The file position advances, so a subsequent read can retrieve the next line.

Card 1 of 106
Card 1 of 106

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent