Secure C Programming: Week #3

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the primary advantage of using the puts() function over printf() when displaying a string?

  • puts() is more versatile and can handle different data types.
  • puts() automatically adds a newline character, simplifying output. (correct)
  • puts() requires a format string, offering greater control over output.
  • puts() is more complex, allowing for more powerful formatting options.

Why is the printf() function considered more prone to errors compared to the puts() function in C programming?

  • printf() lacks the ability to print different data types.
  • printf() automatically adds a newline, leading to unexpected output.
  • printf() is simpler and thus more difficult to debug.
  • printf() requires a format string, which can lead to vulnerabilities. (correct)

Which of the following best describes pseudocode?

  • A formal language strictly for mathematical computations.
  • An artificial and informal language used to develop algorithms. (correct)
  • A low-level language used for direct hardware manipulation.
  • An actual computer programming language.

In the context of algorithms, what are the two key elements that define a procedure for solving a computing problem?

<p>The actions to be executed and the order in which they are executed. (A)</p> Signup and view all the answers

What is sequential execution in programming?

<p>Executing statements in the order they are written. (D)</p> Signup and view all the answers

According to Bohm and Jacopini's work, what is the minimum number of control structures required to write any program?

<p>Three (C)</p> Signup and view all the answers

What is a flowchart primarily used for in programming?

<p>Providing a graphical representation of an algorithm. (B)</p> Signup and view all the answers

In a flowchart, what are the arrows connecting the symbols called?

<p>Flowlines (C)</p> Signup and view all the answers

What is the purpose of an if statement in C programming?

<p>To make a decision based on the truth or falsity of a condition. (D)</p> Signup and view all the answers

What happens in a C program if the condition within an if statement is false?

<p>The statement in the body of the <code>if</code> is skipped. (C)</p> Signup and view all the answers

What is the correct syntax for an if statement in C?

<p>if (condition) {code} (A)</p> Signup and view all the answers

Consider the following C code:

int test = 5;
if (test > 10) {
 // some code
}
// after if

What will be the program's flow of execution if test is 5?

<p>The code inside the <code>if</code> block will be skipped, and only the code after the <code>if</code> block will be executed. (C)</p> Signup and view all the answers

What is the purpose of selection statements in programming?

<p>To choose among alternative courses of action. (D)</p> Signup and view all the answers

In an if...else statement, when is the code within the else block executed?

<p>When the condition in the <code>if</code> statement is false. (A)</p> Signup and view all the answers

What is the primary difference between an if statement and an if...else statement?

<p>An <code>if</code> statement allows for a single action when a condition is true, while <code>if...else</code> allows for different actions when the condition is true or false. (C)</p> Signup and view all the answers

Consider the following pseudocode:

If student's grade is greater than or equal to 60
 Print "Passed"
else
 Print "Failed"

What will be printed if the student's grade is 59?

<p>&quot;Failed&quot; (B)</p> Signup and view all the answers

In the C code if (number % 2 == 0), what does the % operator do?

<p>Divides <code>number</code> by 2 and returns the remainder. (B)</p> Signup and view all the answers

If number is 7, what will be the output of the below C code snippet?

if (number % 2 == 0) {
 printf("%d is an even integer.", number);
} else {
 printf("%d is an odd integer.", number);
}

<p>7 is an odd integer. (D)</p> Signup and view all the answers

What is the correct way to check if two variables, num1 and num2, are equal in C?

<p>num1 == num2 (C)</p> Signup and view all the answers

If num1 is 5 and num2 is 10, what will the following C code print?

if (num1 < num2) {
 printf("%d is less than %d\n", num1, num2);
}

<p>5 is less than 10 (C)</p> Signup and view all the answers

Flashcards

puts() function

A function that displays a string followed by a newline character.

Pseudocode

An artificial and informal language that helps in developing algorithms.

Algorithm

A step-by-step procedure for solving a problem, defined by actions and their order.

Sequential Execution

The order in which statements are executed, one after the other.

Signup and view all the flashcards

Control Structures

Fundamental patterns controlling the flow of execution in a program.

Signup and view all the flashcards

Flowchart

A visual representation of an algorithm using special symbols and flowlines.

Signup and view all the flashcards

if statement

A statement that allows a program to make decisions based on a condition.

Signup and view all the flashcards

if...else Statement

An if statement includes an optional block of code that executes only when the if statement is false

Signup and view all the flashcards

Study Notes

  • Introduction to Programming C, EEF110E, Week #3

Secure C Programming

  • When displaying a string that ends with a newline, use the puts function.
  • The puts function displays the string argument follows by a newline character.
  • printf( "Welcome to C!\n" ); should be written as puts("Welcome to C!" );
  • The puts() function prints a string to the console.
  • The printf() function prints a variety of data types, including formatted strings.
  • printf() is more powerful, more complex, and more prone to errors.
  • A programmer could accidentally specify an incorrect format string when using the printf() function.
  • This could lead to the function attempting to print data to an invalid location, potentially crashing the program or allowing an attacker to execute arbitrary code.
  • The puts() function doesn't require a format string, making it less prone to errors and more secure.

Pseudocode & Algorithms

  • Pseudocode is an artificial and informal language that helps develop algorithms.
  • Pseudocode is useful for developing algorithms that will be converted to structured C programs, and convenient and user-friendly.
  • Pseudocode is not an actual computer programming language.
  • The solution to any computing problem involves executing a series of actions in a specific order.
  • A procedure for solving a problem involves the actions to be executed, and the order in which these actions are to be executed, which is called an algorithm

Control Structures

  • Ordinarily, statements in a program are executed sequentially, i.e. in the order in which they are written.
  • Bohm and Jacopini's work demonstrated that all programs could be written using just three control structures: the sequence structure, the selection structure, and the iteration structure.

Flowcharts

  • A flowchart is a graphical representation of an algorithm or a portion of one.
  • Flowcharts use special-purpose symbols like rectangles, diamonds, rounded rectangles, and small circles, connected by arrows called flowlines

Decision Making

  • This section introduces the simple version of C's if statement.
  • The if statement allows a program to make a decision based on the truth or falsity of a statement of fact called a condition.
  • If the condition in an if statement is true, then the statement in the body of the if statement is executed.
  • Whether the body statement is executed or not, after the if statement completes, execution proceeds with the next statement after the if statement.

C if Statement

  • Syntax of the if statement:
if (test expression) {
    // code
}
  • The if statement evaluates the test expression inside the parentheses ().
  • If the test expression is evaluated to true, statements inside the body of if are executed.
  • If the test expression is evaluated to false, statements inside the body of if are not executed.

The if Selection Statement

  • Selection statements are used to choose among alternative courses of action.
  • If student's grade is greater than or equal to 60; Print "Passed".
  • The preceding pseudocode of an if statement may be written in C as follows:
if (grade >= 60) {
    puts("Passed");
} // end if

The if...else Selection Statement

  • The if selection statement performs an action only if the condition is true; otherwise, the action is skipped.
  • The if...else selection statement allows different actions to be performed based on whether the condition is true or false.
  • If student's grade is greater than or equal to 60; Print "Passed"
  • Else; Print "Failed" prints Passed if the student's grade is greater than or equal to 60, and Failed if the student's grade is less than 60.

C if...else Statement

  • The if statement may have an optional else block.
  • The syntax of the if...else statement is:
if (test expression) {
    // run code if test expression is true
} else {
    // run code if test expression is false
}
  • The corresponding If...else can be written in C as follows:
if (grade >= 60) {
    puts("Passed");
} // end if
else {
  puts("Failed");
} // end else

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser