C Basics
C Basics
C Basics
Variables and Output
#include <stdio.h>
2
Demo
Variables and Output
#include <stdio.h>
4
Basic C Data Types
The C programming language provides five basic data types:
● int
● float
● double
● char
● _Bool (this is a bit strange, but there is a reason)
5
Basic C Data Types
The C programming language provides five basic data types:
● int
● float
● double
● char
● _Bool (this is a bit strange, but there is a reason)
○ You can use the identifier bool instead if you include the header file stdbool.h
6
Storage Sizes and Ranges
● Every value, whether it’s a character, integer, or floating-point
number, has a range of values associated with it.
Code Portability?
Notice that long and pointer data types are different on different
processors (and maybe compilers).
Type Specifiers
● There are several type specifiers that can be used to modify a type:
long, long long, short, unsigned, signed
10
Working with Arithmetic Expressions
In C, just as in virtually all #include <stdio.h>
12
The for Statement
The general format of the for statement is:
int main(void) {
int triangleNumber = 0;
for (int n = 1; n <= 200; n = n + 1) {
triangleNumber = triangleNumber + n;
}
printf("The 200th triangular number is %i\n",
triangleNumber);
}
Example Code 13
The for Statement
The general format of the for statement is:
int main(void) {
int triangleNumber = 0;
for (int n = 1; n <= 200; n++) {
triangleNumber += n;
}
printf("The 200th triangular number is %i\n",
triangleNumber);
}
Example Code 14
Relational Operators
All relational operators available in the C language
15
for Loop Variants
There are a few common for loop variants that are helpful to know
about:
Multiple Expressions
Omitting Fields
16
The while Statement
General Form Init_expressio
n
17
The while Statement
General Form
loop_condition
18
The while Statement
General Form
loop_expressio
n
19
The do Statement
General Form
The first argument to scanf is a format string - the same is we use for
printf!
The second is the variable we want to read the value into. In this case,
number.
Do not worry about the & operator preceding the variable at this point. 24
We will cover the details when we talk about pointers later.
Getting help in Linux
● All commands have manual pages, including scanf
https://en.wikipedia.org/wiki/Man_page
The if Statement
General Form
Take a look at
this code example that
records grades and
computes the average.
switch ( expression ) {
case value_1:
statement ...
break;
case value_2:
statement ...
break;
case value_n:
statement ...
break;
default:
statement ...
A program to evaluate simple expressions
break; of the form: value operator value
} 27
The switch Statement
General Form
switch ( string ) {
case “string1”:
statement ...
Can we do this?
break;
case “string2”:
statement ...
break;
case “string3”:
statement ...
break;
default:
statement ...
A program to evaluate simple expressions
break; of the form: value operator value
} 28
The Conditional Operator
Perhaps the most unusual operator
General Form:
in the C language (and others such
as Java) is one called the
conditional operator.
$ gcc prog1.c
$
32
Running Your Executable Program
$ ./a.out
Programming is
fun.
$
33
Compiling a Basic C Program - part 2
$ gcc prog2.c -o
prog2
$
34
Running Your Executable Program - part 2
$ ./prog2
Programming is
fun.
$
35
gcc: the C compiler
● gcc hello.c
○ Compiles hello.c program and produces an executable named a.out
○ Run the program by typing ./a.out
● gcc hello.c -o hello.out
○ -o flag is used to indicate the name of the executable
○ Compiles hello.c program and produces an executable named hello.out
○ Run the program by typing ./hello.out (a more usual name would be just hello)
● -g flag allows us to use the gdb debugger more effectively
○ gcc -g hello.c -o hello.out
gdb Basics
gdb: debugging C programs
● Run using gdb ./hello.out
○ Program needs to have been compiled with -g flag (otherwise you miss variable
names, etc.)
● b n - inserts a breakpoint at line number n. (ex: b 10)
● r - runs program until a breakpoint is found
● c - continue halted program until next breakpoint
● n - execute next line of code
● p [VARIABLE] - print the value of a variable (ex: p counter)
● q - quit gdb