A beginner C tutorial should include programs that actually compile, explain their outputs, and show the input bounds that matter. C offers precise control over memory and data representation, but that control makes unchecked indexes, uninitialized variables and invalid input especially dangerous. The examples here use standard C11 and focus on short skills you can verify in a terminal.
Compile and run a first C program
Save this as hello.c. A standard hosted program uses int main(void) as an entry point and returns zero when it finishes normally.
#include <stdio.h>
int main(void) {
puts("Hello, C");
return 0;
}
cc -std=c11 -Wall -Wextra hello.c -o hello
./hello
# Hello, C
Build a function and test its result
Functions turn a repeated calculation into named behavior. Pass numeric inputs by value and return a result. The function below does not modify its caller’s variables.
#include <stdio.h>
int double_value(int input) { return input * 2; }
int main(void) {
printf("%d\n", double_value(7));
return 0;
}
/* Output: 14 */
This example uses a small value. Signed integer overflow is undefined behavior in standard C; validate ranges before multiplying data drawn from external inputs.
Process an array without crossing its bounds
Array indexes start at zero. A fixed-size array has positions from zero to its element count minus one. Use a loop bound based on the array size rather than a hard-coded value that may later become wrong.
#include <stdio.h>
int main(void) {
int values[] = {4, 6, 9};
size_t count = sizeof values / sizeof values[0];
int sum = 0;
for (size_t i = 0; i < count; i++) sum += values[i];
printf("%d\n", sum);
return 0;
}
/* Output: 19 */
The sizeof count technique works here because values is an array in this scope. A function parameter written int values[] is adjusted to a pointer; pass its length separately.
Read input and check the return value
Functions such as scanf report how many conversions succeeded. Never use an uninitialized output variable after a failed conversion. Production input handling often uses fgets followed by validated parsing for more control over malformed input.
#include <stdio.h>
int main(void) {
int age;
if (scanf("%d", &age) != 1 || age < 0) {
fputs("invalid age\n", stderr);
return 1;
}
printf("Age: %d\n", age);
return 0;
}
Know the difference between types and formatting
int represents a signed integer; double represents a floating-point value; char is a character-sized integer type. A char array can hold a C string when terminated by a null byte. Format strings must match argument types: %d expects an int and %f in printf expects a double. Mismatches may result in undefined behavior.
Practice deliberately, not by copying
Change the values and predict outputs before running the examples. Try an empty input, a negative input and an array of a different size. Compile with warnings and, when available, run additional sanitizer-enabled test builds to help catch memory errors. Do not present a successful sample run as proof that code is safe for untrusted input.