C Tutorial
C User Input
scanf reads from the keyboard. Pair it with printf so the user knows what to type.
scanf fills a variable
scanf reads from standard input. You pass a format string that matches the data, then theaddress of a variable you already declared. The & operator takes that address. Without it, gcc may warn, and the program will not store the value where you think.
Example
#include <stdio.h>
int main(void) {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You entered %d\n", age);
return 0;
}In /c/try, type the input in the stdin box on the left, then compile. That box is what scanf reads. The C editor uses gcc.
Always prompt with printf
A program that waits on scanf with no message looks frozen. Print a question first. End the prompt with a space or a newline so the typed value does not jam against the label.
Example
#include <stdio.h>
int main(void) {
double price;
printf("Price in euros: ");
scanf("%lf", &price);
printf("Recorded: %f\n", price);
return 0;
}scanf uses %lf for double. printf uses %f for the same type. Mixing those two is a common bug.
Several values in a row
One scanf can read more than one value if the format has more than one specifier. Each variable still needs &. On StudyGrid, put each number on its own line in stdin, or separate them with spaces.
Example
#include <stdio.h>
int main(void) {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("sum is %d\n", a + b);
return 0;
}Sample stdin for that program: 4 9 or two lines, 4 then 9. Both work.
The format must match
If the variable is int and the user types hello, conversion fails. In a small beginner program the rest of the run is then unreliable. Ask for the kind of value you declared, and test with matching stdin.
| Variable | scanf format | Typical stdin |
|---|---|---|
int n; | %d with &n | 42 |
double x; | %lf with &x | 3.14 |
char c; | %c with &c | A |
stdio.h covers both sides
One include, #include <stdio.h>, gives you printf and scanf. Output asks. Input answers. Most interactive programs are that pair, repeated.
Declare the variable before scanf. scanf cannot create n. The type has to exist already. Next: the types you can store in those variables.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Physics
Kinetic energy from the keyboard
Translational kinetic energy for a rigid body is half m v². Mass must be kilograms, speed metres per second, or the joule on the end of the print is a lie. 2 kg at 3 m/s is 9 J.
The 1/2 is not a fudge factor. It appears when you integrate F = ma with constant mass from rest. scanf("%lf", &mass) writes into mass. Forget the ampersand and the program does not store what you typed.
KE = ½ m v²
This is not rotational KE (½ I ω²), and it ignores rest energy. Fine for a trolley on a bench.
Example
#include <stdio.h>
int main(void) {
double mass;
double speed;
printf("mass in kg: ");
scanf("%lf", &mass);
printf("speed in m/s: ");
scanf("%lf", &speed);
printf("KE = %.2f J\n", 0.5 * mass * speed * speed);
return 0;
}Maths
Circumference from a typed radius
The circumference of a circle is 2πr. π here is 3.14159, which is already more digits than a school ruler can justify. For r = 2, C is about 12.57.
cin-style tools in C are scanf. Pair each read with a prompt. If you only scanf, the screen sits there and looks hung.
C = 2 π r
Example
#include <stdio.h>
int main(void) {
double r;
printf("radius: ");
scanf("%lf", &r);
printf("C = %.3f\n", 2.0 * 3.14159 * r);
return 0;
}