C Basics
This section is an overview of the basic constructs in any C program.
Variable Declarations
C is a statically typed languages, so when you declare a variable, you must also declare its type.
int x;
int y;
Variable declarations contain the type (int
in this example) and the variable name (x
and y
in this example).
Like every statement in C, they end with a semicolon.
Assignment
Use =
to assign new values to variables:
int x;
x = 4;
As a shorthand, you can also include the assignment in the same statement as the declaration:
int y = 6;
Expressions
An expression is a part of the code that evaluates to a value, like 10
or 7 * (4 + 2)
or 3 - x
.
Expressions appear in many places, including on the right-hand side of an =
in an assignment.
Here are a few examples:
int x;
x = 4 + 3 * 2;
int y = x - 6;
x = x * y;
Functions
To define a function, you need to write these things, in order: the return type, the function name, the parameter list (each with a type and a name), and then the body. The syntax looks like this:
<return type> <name>(<parameter type> <parameter name>, ...) {
<body>
}
Here’s an example:
int myfunc(int x, int y) {
int z = x - 2 * y;
return z * x;
}
Function calls look like many other languages:
you write the function name and then, in parentheses, the arguments.
For example, you can call the function above using an expression like myfunc(10, 4)
.
The main
Function
Complete programs must have a main
function, which is the first one that will get called when the program starts up.
main
should always have a return type of int
.
It can optionally have arguments for command-line arguments (covered later).
Here’s a complete program:
int myfunc(int x, int y) {
int z = x - 2 * y;
return z * x;
}
int main() {
int z = myfunc(1, 2);
return 0;
}
The return value for main
is the program’s exit status.
As a convention, an exit status of 0 means “success” and any nonzero number means some kind of exceptional condition.
So, most of the time, use return 0
in your main
.
Includes
To use functions declared somewhere else, including in the standard library, C uses include directives. They look like this:
#include <hello.h>
#include "goodbye.h"
In either form, we’re supplying the filename of a header file.
Header files contain declarations for functions and variables that C programs can use.
The standard filename extension for header files in C is .h
.
You should use the angle-bracket version for library headers and the quotation-mark version for header files you write yourself.
Printing
To print output to the console, use printf
, a function from the C standard library which takes:
- A string to print out, which may include format specifiers (more on these in a moment).
- For each format specifier, a value to fill in for each format specifier.
The first string might have no format specifiers at all, in which case the printf
only has a single argument.
Here’s what that looks like:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
}
The \n
part is an escape sequence that indicates a newline, i.e., it makes sure the next thing we output goes on the next line.
Format specifiers start with a %
sign and include a few more characters describing how to print each additional argument.
For example, %d
prints a given argument as a decimal integer.
Here’s an example:
#include <stdio.h>
int main() {
int x = 3;
int y = 4;
printf("x + y = %d.\n", x + y);
}
Here are some format specifiers for printing integers in different bases:
Base | Format Specifier | Example |
---|---|---|
decimal | %d | printf("%d", i); |
hexadecimal | %x | printf("%x", i); |
octal | %o | printf("%o", i); |
And here are some common format specifiers for other data types:
Data Type | Format Specifier | Example |
---|---|---|
string | %s | printf("%s", str); |
char | %c | printf("%c", c); |
float | %f | printf("%f", f); |
double | %lf | printf("%lf", d); |
long | %ld | printf("%ld", l); |
long long | %lld | printf("%lld", ll); |
pointers | %p | printf("%p", ptr); |
See the C reference for details on the full set of available format specifiers.