3 Data Input and Output
3 Data Input and Output
COMMENTS
MY FIRST PROGRAM
DATA INPUT/OUTPUT
2
INTENDED LEARNING OUTCOMES (ILOs)
To be able To To
to use and Understand Understand
identify how to how to Input
Comments Output Data Data via the
on a Display Keyboard
3
1
COMMENTS
4
COMMENTS
5
COMMENTS
7
“ Commenting your code is like
cleaning your bathroom - you
never want to do it, but it really
does create a more pleasant
experience for you and your
guests. — Ryan Campbell
8
2
FIRST PROGRAM
Understanding and Running Your First
Program in C
9
MY FIRST PROGRAM – HELLO WORLD
10
MY FIRST PROGRAM – HELLO WORLD
/*
Finally My Very
first C program
*/
#include <stdio.h>
int main()
{
printf("Hello, World!\n"); // displays Hello, World! on the screen
return 0;
}
11
MY FIRST PROGRAM – HELLO WORLD
12
#include <stdio.h>
▪ The program seen above displays to the screen the text “Hello
World!”
▪ To do this successfully the program needs to speak to the
computer’s hardware responsible for display.
▪ This requires an elaborate sequence of actions. However, this
heavy lifting has been done for us and bundled in a Header File
called stdio (Standard Input/Output) header file.
13
#include <stdio.h>
15
The main() Function
▪ The data type int that comes before the main() function is
simply to cater for the returning of the integer 0 from the
statement return 0.
▪ A lot more would be shared on the returning value when
studying the topic FUNCTIONS.
16
The printf() Function
17
The return Statement
18
ESCAPE SEQUENCES
19
ESCAPE SEQUENCES
20
RUNNING THE CODE
21
RUNNING THE CODE
22
RUNNING THE CODE
23
3
DATA INPUT AND
OUTPUT
24
DATA INPUT/OUTPUT
25
DATA INPUT/OUTPUT
26
PRINTF
27
PRINTF
28
PRINTF
29
CONTROL STRINGS
30
PRINTF
31
PRINTF
32
PRINTF
▪ For example:
int age = 50; //the variable is age and the value stored is 50
printf(“%d \n”, age); // to print the value 50 as an integer
Printf(“I am %d years old”, age); // printing the age from memory
33
PRINTF
▪ For example:
int score = 4; //the variable is score and the value stored is 4
float weight = 90.61;
printf(“Zlatan Ibrahimovic who scored %d goals in the last game
weighs %g kilograms”, score, weight);
// combining various various variables in one print statement
34
PRINTF
35
PRINTF
36
SCANF
37
SCANF
38
SCANF
39
SCANF
41
& - THE “ADDRESS OF” OPERATOR
42
& - THE “ADDRESS OF” OPERATOR
▪ Without the ampersand (&) the scanf statement would not work
as we want it to.
▪ The statement &personAge would provide the compiler with the
address of the variable personAge.
▪ This address of operator can also be used in the printf
statement to print out the memory addresses of variables using
the control string %p. More of this would be covered later when
treating the topic Pointers.
43
SCANF – MULTIPLE USER INPUT
44
SCANF – MULTIPLE USER INPUT
45
SCANF – MULTIPLE USER INPUT
46
GETCHAR AND PUTCHAR
47
GETCHAR AND PUTCHAR
▪ The gets() and puts() functions are also input and output
functions targeted mainly for string values.
▪ How they are used would be covered later when treating the
topic Strings.
50
THANKS!
Any questions?
You can find me at
elielkeelson@gmail.com & ekeelson@knust.edu.gh
51