C programming: Backslash, Percentage, and Data Types

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

Which of the following code snippets will correctly print a single backslash character (\) to the console in C?

  • `printf("\\");` (correct)
  • `printf("\\");` (correct)
  • `printf("\ ");`
  • `printf("\b");`

An unsigned 6-bit integer can represent the same range of values as a signed 6-bit integer.

False (B)

What is the purpose of the system("PAUSE"); command in a C program, and why is it often used?

It pauses the program execution until the user presses a key, preventing the console window from closing immediately after the program finishes.

The __________ statement skips the current iteration of a loop and proceeds to the next iteration.

<p>continue</p> Signup and view all the answers

Match the following C code elements with their descriptions:

<p><code>#include &lt;stdio.h&gt;</code> = Includes standard input/output library <code>return 0;</code> = Indicates successful program execution <code>\n</code> = Represents a newline character <code>break;</code> = Terminates a loop or switch statement</p> Signup and view all the answers

Which of the following actions will improve software speed?

<p>Maximizing memory locality. (D)</p> Signup and view all the answers

In C, the expression 100 > n > 20 is a valid way to check if the variable n is between 20 and 100.

<p>False (B)</p> Signup and view all the answers

When determining if a number n is prime, up to what integer should you check for divisibility?

<p>The square root of <code>n</code> (D)</p> Signup and view all the answers

Flashcards

Printing Backslash

To print a backslash () in C, use two backslashes ().

\t in C

Inserts a tab.

Unsigned Integer

A data type that represents only non-negative numbers.

Signed Integer

A data type that can represent positive and negative numbers.

Signup and view all the flashcards

C Naming Rules

A set of rules that allows letters, digits, and underscores, must begin with letters, cannot use keywords, must not contain spaces and is case sensitive.

Signup and view all the flashcards

Continue Statement

Skips the remaining code in the current iteration of a loop and proceeds with the next iteration.

Signup and view all the flashcards

Break Statement

Used to exit from a loop, switch statement, or other control structure.

Signup and view all the flashcards

Do-While Loop

This type of loop executes its body at least once, regardless of the initial condition.

Signup and view all the flashcards

Study Notes

  • To print a backslash, use \\
  • To print a percentage sign, use %%
  • \t creates a tab space
  • \n creates a new line
  • system("PAUSE"); prevents the program from quitting immediately, waiting for user input instead

Program Initialization

  • #include <stdio.h> is included at the start of every program, allowing functions like printf and scanf to be used
  • return 0; is used before ending the main program, confirming that everything ran successfully

Integer Data Types

  • If an integer data type is 6-bit, it supports 64 unique combinations
  • An unsigned integer can only represent non-negative values, its values range from 0 to 2n-1
  • A 6-bit unsigned integer's range is 0 to 63
  • A signed integer can represent both positive and negative values, its values range from -2n-1 to 2n-1-1
  • A 6-bit signed integer's range is -32 to 31

Software Optimization

  • Software speed is improved when HDD access is reduced, memory locality is maximized, and CPU computations are minimized

Character Encoding

  • High voltage is not necessarily represented by "1"
  • ASCII is 8 bit, and can represent 256 unique characters
  • Unicode supports all of the world's languages, representing over a million unique characters

Assembly Language

  • Assembly language is a technical form of high-level code, reflecting CPU operations, but is platform dependent, and not portable

Code Execution

  • A bug free piece of C code execution sequence goes like this: High-level C code compiles to assembly code, which compiles into machine code

Naming conventions

  • C variable and function naming rules: only letters, digits, and underscores are allow
  • Variables must begin with letters and cannot contain spaces
  • Keywords are not allowed (e.g., for, while, int)
  • C is case sensitive

Operators

  • Cannot write 100 > n > 20 in C, use logical operators && (and) || (or)
  • A ternary/conditional operator is formatted as: (condition ? "if true print" : "if false print");

Switch Statements

  • If break;is not added between each case of a switch statement, the code continues to execute the following cases until a break is encountered

Loops

  • break; terminates the current loop
  • continue; skips the current iteration of the loop and moves to the next iteration of the loop

Prime Numbers

  • To determine if a number is prime, check if the number is divisible by integers from 2, to the square root of the number

Functions

  • Functions do not always need to return a value in C
  • do while loop executes the loop at least once, even if the condition is false
  • In a function, parameters are transferred by value

Main Function

  • The main function cannot be called in the program, it calls other functions within it
  • All functions are not required to be called by the main function

Function Parameters

  • Given long func(int n, int i) is a function, an invalid call would be func1("5", 3) because the function takes integer values
  • Valid calls include func1(5,10) or long result = func1(3,2) and int result = func1(7,4)

Data Types

  • The result of 5 / 2 in C is 2
  • Double or long float data types can store the largest value
  • The default return type of main() in C is int
  • Variables defined as float use more memory than int variables
  • The switch statement cannot check conditions using relational operators
  • const keyword is used to define a constant variable in C
  • scanf takes input from the user
  • Converting a float to an int using (int) is called type casting

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