Lecture02 - Copy
Lecture02 - Copy
University of Ottawa
Winter 2025
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
First Program Explained
Outline
2 Variables
3 More on printf
4 scanf
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
First Program Explained
First Program
#include <stdio.h>
/* This is our first program */
int main()
{
printf("Hello World!\n");
return 0;
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
First Program Explained
Preprocessing Directives
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
First Program Explained
Comments
printf
The line
printf("Hello World\n");
calls a standard I/O function printf(), which prints out the
desired message to the screen.
standard output (“stdout”) = screen
standard input (“stdin”) = keyboard
The message to be written is included in a pair of double quotes.
The message together with the quotation marks is the input to the
function printf()
“\n” is called an “escape sequence”; it positions the cursor at the
beginning of the next line after the message is printed.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables
Outline
2 Variables
3 More on printf
4 scanf
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables
Highlight
The memory can be thought logically as an array of bytes, each having
an address. If we think of each byte as a house, the memory is a street
lined up with houses, and street number of a house is the address of
the byte.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables
Variable Name
Outline
2 Variables
3 More on printf
4 scanf
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on printf
printf("%d", 100);
printf("%f", 101.3);
printf("%d", integer_1);
printf("%f", real_1);
The %d is the conversion specifier used to print out an integer and
the %f is the specifier used to print out a real number.
The conversion specifier must match the data type.
A conversion specifier always begins with a %
The specifier must be enclosed in a pair of double quotes
A conversion specifier is like a “space holder” for the object, in the
to-be-printed message.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on printf
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
scanf
Outline
2 Variables
3 More on printf
4 scanf
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
scanf
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
scanf
Programming Example
Write a program that does the following. It asks the user to guess and
enter the temperatures of tomorrow at noon and at night. It then
prints a message about this information. (Also try to print a message
showing the temperature difference —we have not formally introduced
arithmetic operations, such as subtraction, but why not give it a guess
and try?).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
scanf
Coding Demonstration
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .