C Primer (Part 1)
Welcome to learning C! This primer contains syntax of basic features of C.
For most students, this primer will be mostly review, but be sure to take a look at the highlighted sections for important/interesting details!
Contents:
When defining variables in C, we define them based on their type. Note that the end of each line in C is marked by a semi-colon (;).
Single-line comments start with //
, while multi-line comments start with /*
and when with */
.
Types are not known after the program is compiled, only the amount of bytes the variable takes up in memory.
char c = '\t';
short s = 2;
int n = 10;
long l = 300;
float f = 0.12f;
double d = 1220934.93419;
Variables in C are mutable, so we can change their value once defined:
You can also leave a variable undefined, without initializing the variable to an initial value. If you access the value of the variable before it is given a value, this behavior is undefined in C – you can get any random value!
short s;
char ch = 'p';
short e = s;
s = 4;
Naming Conventions
General guidelines for good naming:
- Multiple variables cannot share the same name in the same scope
- Variable names cannot start with a number, but can start with any letter or an underscore
- Variable names should not be a reserved keyword (like “int” or “char”)
- Variable names clearly indicate purpose of variable
Signed vs Unsigned Numbers
The range of values that can be represented by each numerical type is dependent on the number of bytes allocated and whether or not the variable is signed or unsigned.
unsigned int x;
int y;
signed int z;
Note that the signed nature of y
is implicit in the declaration of the variable (i.e. numbers are signed by default in C). These ranges fluctuate dependent on the type and whether or not decimals are supported.
Booleans
Booleans in C are defined by the bool
keyword. You can combine them together using boolean logic, where &&
, ||
, !
are the and, or and not operations respectively in C.
bool x = true;
bool y = false;
bool a = x && y;
bool b = x || y;
bool c = !x;
In C, booleans are really just hidden integers. 0
is treated as false
, while any non-zero integer is treated as true
(typically represented by 1
).
You can also use relational operators to produce boolean values:
Operator |
Meaning |
A > B |
True (1) if A is greater than B, false (0) otherwise |
A < B |
True (1) if A is less than B, false (0) otherwise |
A >= B |
True (1) if A is greater than or equal to B, false (0) otherwise |
A <= B |
True (1) if A is less than or equal to B, false (0) otherwise |
A == B |
True (1) if A is exactly equal to B, false (0) otherwise |
A != B |
True (1) if A is not equal to B, false (0) otherwise |
Conditionals: Using if and else
We can use if
and else
to determine what code is executed based on boolean values.
int y = 2;
char ch;
if (3 > 5) {
ch = '4'
}
else if (y) {
ch = 'p'
}
else {
ch = '\n'
}
The code above sets the value of ch
to 'p'
.
Defining functions and scope
To define a function, we specify a return type (or void
if there is none), a function name, and function arguments.
int oper(char ch, short r) {
return 4;
}
You can also call a function using its name and the appropriate passed arguments, by their number and type:
int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
C passes arguments ‘by value’ to a function when it is called. For any variable passed to the function as an argument, a copy is made of its value in a different place in memory, which is then given to the function. Thus any changes to the argument in the function is not reflected in the variable passed in.
void changeVar(int n) {
n = 3;
}
int main() {
n = 10;
changeVar(n);
}
The main
function is the first function in your program that is executed, at program startup. There are two widely accepted signatures for main
:
int main() {
...
}
int main(int argc, char* argv[]) {
...
}
These arguments are how command-line arguments, given when the program is run in terminal (see Compiling and Running your Code), are passed into the program. These arguments are given as an array to argv
, with the first element being the name of C executable. (argv
is an array of strings - see part 2 of this primer for more details). argc
is the number of elements in argv
- 1 more than the number of command-line arguments.
For example, if the C program test.c
is compiled to the executable test
, then we can run the program as ./test 3 I love CS 300!
. The command line arguments are spaces separated - 3
, I
, love
, CS
, and 300!
. Thus argc
is 6
and argv
is ["./test", "3", "I", "love", "CS", "300!"]
.
Scope
Each variable is accessible only in some portion of code, called its scope. You can typically think of a variable’s scope as starting where it is declared and ending at the next closing curly bracket }
. Global variables are those defined outside any functions, and can be accessed and modified inside all functions. Local variables are those defined inside functions, and are only accessible within those said functions.
int a = 0;
int main() {
int b = 3;
a = 10;
b = 2;
}
void f(int x) {
short c = 2;
b--;
a = 3;
}
Scopes are also defined by if
/else
statements, and for
and while
loops. If two variables are in scope at the same time, the one with the inner-most scope supercedes the other.
int a = 0;
int main() {
int b = 4;
int c, d;
for(int x = 1; x < b; x++) {
a += x;
}
{
int b = 10;
c = b;
}
int d = b;
}
Printing
Input and output operations in C are defined by C Standard Input and Output Library. This can be imported by including #include <stdio.h>
at the top of your C file (refer to part 2 for more information on headers). The library includes the function printf
:
#include <stdio.h>
int main(){
printf("Hello World!\n");
}
To print variables to the console, include a format specifier in the string to be printed, at the place where you want the variable to be. Then pass the variable as an additional argument to printf
. All variables passed in addition to printf
will replace the format specifiers in the string their given order.
The format specifier determines how to format the variable as a string, which includes:
%d
- formats the variable as a decimal (base 10) integer
%c
- formats the variable as an ASCII character
%x
- formats the variable as a hexidecimal (base 16) integer
%p
- formats the variable as a memory address (or pointer)
%s
- formats the variable as a string
#include <stdio.h>
int main() {
int numOfTAs = 18;
short numOfHTAs = 4;
char ch = 'A';
int* pointer = &numOfHTAs;
printf("There are %d TAs and %d HTAs, in CS300\n", numOfTAs, numOfHTAs);
printf("My favorite letter is %c, whose ASCII code is %d.\n", ch, ch);
printf("The pointer to a variable I wrote above is %p.\n", pointer);
}
Example output:
There are 18 TAs and 4 HTAs, in CS300
My favorite letter is A, whose ASCII code is 65.
The pointer to a variable I wrote above is 0x7ffd1e2a375a.
Loops
You can define for
and while
loops in C, just as you can in many other languages. for
loops are often used for iteration, and define an initial condition, a condition to continue iteration, and an operation to complete each iteration. while
loops simply include a condition to continue iteration.
#include <stdio.h>
int main() {
int myIterator = 1;
while (myIterator <= 5) {
printf("Did you know that myIterator is now: %d!\n", myIterator);
myIterator += 2;
}
for (int outer = 2; outer < 5; outer++) {
printf("Look at how high I can count!\n");
for (int inner = 1; inner <= outer; inner++) {
printf("%d ", inner);
}
printf("\n");
}
}
Output:
Did you know that myIterator is now: 1!
Did you know that myIterator is now: 3!
Did you know that myIterator is now: 5!
Look at how high I can count!
1 2
Look at how high I can count!
1 2 3
Look at how high I can count!
1 2 3 4
Compiling and Running your Code
Programs written in the C lanuage are converted into executable files by programs called compilers. In this class, we use the GCC compiler (which stands for GNU Compiler Collection). To compile a C file, all you need to do is pass the file’s path as an argument into the gcc command. For example, for the file loops.c
, we would enter the following in the bash terminal:
Barring any errors in compiling, this should produce an executable binary file called a.out
. This is just the default name for a file that has been compiled. It is an abbreviated form of name “assembler output”, as the last stage of the compilation process.
To run an executable binary file, type a ./
followed by the name of the file, like so:
To give the compiler a target file name for the executable, you have to provide the -o
flag followed by the target name:
This will create another executable binary named loops
, which can be executed by:
C Primer (Part 1)
Welcome to learning C! This primer contains syntax of basic features of C.
For most students, this primer will be mostly review, but be sure to take a look at the highlighted sections for important/interesting details!
Contents:
Variables and comments
When defining variables in C, we define them based on their type. Note that the end of each line in C is marked by a semi-colon (;).
Single-line comments start with
//
, while multi-line comments start with/*
and when with*/
.Types are not known after the program is compiled, only the amount of bytes the variable takes up in memory.
Variables in C are mutable, so we can change their value once defined:
You can also leave a variable undefined, without initializing the variable to an initial value. If you access the value of the variable before it is given a value, this behavior is undefined in C – you can get any random value!
Naming Conventions
General guidelines for good naming:
Signed vs Unsigned Numbers
The range of values that can be represented by each numerical type is dependent on the number of bytes allocated and whether or not the variable is signed or unsigned.
Note that the signed nature of
y
is implicit in the declaration of the variable (i.e. numbers are signed by default in C). These ranges fluctuate dependent on the type and whether or not decimals are supported.Booleans
Booleans in C are defined by the
bool
keyword. You can combine them together using boolean logic, where&&
,||
,!
are the and, or and not operations respectively in C.In C, booleans are really just hidden integers.
0
is treated asfalse
, while any non-zero integer is treated astrue
(typically represented by1
).You can also use relational operators to produce boolean values:
A > B
A < B
A >= B
A <= B
A == B
A != B
Conditionals: Using if and else
We can use
if
andelse
to determine what code is executed based on boolean values.The code above sets the value of
ch
to'p'
.Defining functions and scope
To define a function, we specify a return type (or
void
if there is none), a function name, and function arguments.You can also call a function using its name and the appropriate passed arguments, by their number and type:
C passes arguments ‘by value’ to a function when it is called. For any variable passed to the function as an argument, a copy is made of its value in a different place in memory, which is then given to the function. Thus any changes to the argument in the function is not reflected in the variable passed in.
The
main
function is the first function in your program that is executed, at program startup. There are two widely accepted signatures formain
:These arguments are how command-line arguments, given when the program is run in terminal (see Compiling and Running your Code), are passed into the program. These arguments are given as an array to
argv
, with the first element being the name of C executable. (argv
is an array of strings - see part 2 of this primer for more details).argc
is the number of elements inargv
- 1 more than the number of command-line arguments.For example, if the C program
test.c
is compiled to the executabletest
, then we can run the program as./test 3 I love CS 300!
. The command line arguments are spaces separated -3
,I
,love
,CS
, and300!
. Thusargc
is6
andargv
is["./test", "3", "I", "love", "CS", "300!"]
.Scope
Each variable is accessible only in some portion of code, called its scope. You can typically think of a variable’s scope as starting where it is declared and ending at the next closing curly bracket
}
. Global variables are those defined outside any functions, and can be accessed and modified inside all functions. Local variables are those defined inside functions, and are only accessible within those said functions.Scopes are also defined by
if
/else
statements, andfor
andwhile
loops. If two variables are in scope at the same time, the one with the inner-most scope supercedes the other.Printing
Input and output operations in C are defined by C Standard Input and Output Library. This can be imported by including
#include <stdio.h>
at the top of your C file (refer to part 2 for more information on headers). The library includes the functionprintf
:To print variables to the console, include a format specifier in the string to be printed, at the place where you want the variable to be. Then pass the variable as an additional argument to
printf
. All variables passed in addition toprintf
will replace the format specifiers in the string their given order.The format specifier determines how to format the variable as a string, which includes:
%d
- formats the variable as a decimal (base 10) integer%c
- formats the variable as an ASCII character%x
- formats the variable as a hexidecimal (base 16) integer%p
- formats the variable as a memory address (or pointer)%s
- formats the variable as a stringExample output:
Loops
You can define
for
andwhile
loops in C, just as you can in many other languages.for
loops are often used for iteration, and define an initial condition, a condition to continue iteration, and an operation to complete each iteration.while
loops simply include a condition to continue iteration.Output:
Compiling and Running your Code
Programs written in the C lanuage are converted into executable files by programs called compilers. In this class, we use the GCC compiler (which stands for GNU Compiler Collection). To compile a C file, all you need to do is pass the file’s path as an argument into the gcc command. For example, for the file
loops.c
, we would enter the following in the bash terminal:Barring any errors in compiling, this should produce an executable binary file called
a.out
. This is just the default name for a file that has been compiled. It is an abbreviated form of name “assembler output”, as the last stage of the compilation process.To run an executable binary file, type a
./
followed by the name of the file, like so:To give the compiler a target file name for the executable, you have to provide the
-o
flag followed by the target name:This will create another executable binary named
loops
, which can be executed by:Acknowledgments and Extra Materials: USNA Intro to C Programming, Declaration of Variables in C, Decision and Branching Concepts - Booleans, C++ Reference - Scope