C Programming - Variables and I/O Lecture 2

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary purpose of the printf function in C?

  • To print output to the screen (correct)
  • To perform arithmetic computations
  • To read input from the user
  • To declare variables

Which escape sequence in printf is used to start a new line?

  • \b
  • \r
  • \n (correct)
  • \t

What is the correct syntax to read an integer from the keyboard using scanf?

  • scanf('.d', &i);
  • scanf('%d', i);
  • scanf('%d', &i); (correct)
  • scanf('%i', &i);

What will happen if a variable is read before it has been assigned a value?

<p>The variable will contain garbage data. (D)</p> Signup and view all the answers

In mixed expressions, what happens to numbers when one of them is a float or double?

<p>Everything is converted to double. (A)</p> Signup and view all the answers

Which of the following is NOT a valid operator in C?

<p>$ (D)</p> Signup and view all the answers

What must be included in the program to use scanf and printf functions?

<p>#include &lt;stdio.h&gt; (A)</p> Signup and view all the answers

When performing division with integers in C, what will be the result?

<p>An integer result (A)</p> Signup and view all the answers

Flashcards

printf function

A C function used to display output on the screen.

printf format specifiers

Special characters like '%d' (integer), '%f' (float) used in printf to specify data types for printing.

Type conversion in C

C automatically adjusts data types during calculations involving both integers and floats. Floats usually take precedence.

scanf function

A C function for reading input from the keyboard.

Signup and view all the flashcards

scanf format specifiers

Special sequences in scanf indicating data type expectations (e.g., '%d' for integers).

Signup and view all the flashcards

C Arithmetic Operators

Symbols like +, -, *, / represent fundamental mathematical operations in C.

Signup and view all the flashcards

Variable Initialization

Assigning a value to a variable before using it in a C program.

Signup and view all the flashcards

Integer Division

C's division operator '/' returns integer results when both operands are integers.

Signup and view all the flashcards

Study Notes

Variables and I/O

  • Today's Topics: Variables, input/output formats, arithmetic computations.

Output - printf

  • printf: Used to display text and data on the screen.
  • printf is a family of functions, including fprint, vsprintf, sprintf, to print to files and variables.
  • Arguments: Specify what to print inside parentheses.
  • Format Specifiers: Use placeholders like %d (integers) and %f (floating-point numbers) within strings to display values from variables. Example string: "The value is %d", %d is replaced with an integer variable.
  • Multiple Output: Can print multiple variables in one printf statement

printf Special Characters

  • \n: Newline
  • \r: Carriage return
  • \b: Backspace
    • \n: important when flushing the buffer, crucial for debugging

Type Conversion and Arithmetic Operations

  • Combine different data types in expressions, result will be a double
  • Multiplying by 1.0 converts to a double, effectively changing the operation's type.
  • Converting data types may cause rounding. c = a + b, then d = c - a, d may not equal b due to rounding.

Input - scanf

  • scanf: A function from the standard library to read input from the keyboard.
  • Syntax: It follows a similar format to printf using specifiers.
  • Pointers: Crucial: Input functions like scanf require pointers & to reference variables.

Operators

  • Standard mathematical operators +, -, *, / are used.
  • Follow the usual mathematical order of operations..
  • Note that integer division results in an integer value.

Initialization

  • Reading a variable before assigning a value might lead to unexpected results or errors (especially in larger programs).
  • Some compilers issue warnings for this potential problem (using flags, for example -W with GCC).

math.h

  • math.h: A mathematical library providing standard functions.
  • Includes: Header file #include <math.h>
  • Constants: Predefined values like pi, e
  • Functions: Functions like log, sqrt, sin and so on.
  • Compilation: Needs a special compilation flag (-lm, for example) for the compiler to include math.h correctly.

Conclusion

  • printf and scanf are necessary to communicate with the screen and keyboard.
  • Variable declaration must be done before use.
  • While spaces and tabs are not required by the compiler, they improve code readability.

Studying That Suits You

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

Quiz Team

More Like This

Use Quizgecko on...
Browser
Browser