Programming in C
C Basics
Variables and Output
#include <stdio.h>
int main (void) {
int sum;
sum = 50 + 25;
printf("The sum of 50 and 25 is %i\n",
sum);
return 0;
}
The sum of 50 and 25 is 75
2
Demo
Variables and Output
#include <stdio.h>
int main (void) {
int sum;
sum = 50 + 25;
printf("The sum of 50 and 25 is %i\n",
sum);
return 0;
}
The sum of 50 and 25 is 75
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.
● This range has to do with the amount of storage that is allocated to
store a particular type of data.
● In general, that amount is not defined in the language. It typically
depends on the computer you’re running, and is, therefore, called
implementation- or machine-dependent.
● For example, an integer might take up 32 bits on your computer, or
perhaps it might be stored in 64 bits. 7
Data types in C (for gcc)
C Data Type Typical 32-bit Intel IA 32 x86-64
char 1 1 1
short 2 2 2
int 4 4 4
long 4 4 8
long long 8 8 8
float 4 4 4
double 8 8 8
long double 8 10/12 10/16
pointer 4 4 8
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
long int factorial;
● This declares the variable factorial to be a long integer variable.
As with floats and doubles, the particular accuracy of a long
variable depends on your particular computer system. On many
systems, an int and a long int both have the same range and either
can be used to store integer values up to 32-bits wide (231 – 1, or
2,147,483,647).
9
Basic Data Types
10
Working with Arithmetic Expressions
In C, just as in virtually all #include <stdio.h>
programming languages, the plus int main (void) {
sign (+) is used to add two values, int a = 100;
int b = 2;
the minus sign (–) is used to subtract
int c = 25;
two values, the asterisk (*) is used to int d = 4;
multiply two values, and the slash (/) printf(“%i\n”, a - b);
printf(“%i\n”, b * c);
is used to divide two values. printf(“%i\n”, a / c);
printf(“%i\n”, a + b * c);
These operators are known as binary printf(“%i\n”, a + b * c + d);
arithmetic operators because they return 0;
}
operate on two values or terms.
11
Triangle Numbers
12
The for Statement
The general format of the for statement is:
for (init_expression; loop_condition; loop_expression)
program_statement
Here is a simple example:
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:
for (init_expression; loop_condition; loop_expression)
program_statement
Here is a simple example:
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
C loops can also use the
following two statements
inside the loop body:
● break - get out of loop
● continue - start from the
top 20
Printf: Aligning Output
Producing readable program
output is important, so it is good
to make sure that our output is
aligned.
Consider this program that
generates a table of triangular
numbers.
It does not generate aligned
output.
How do we fix this? 21
Printf: Aligning Output
How do we fix this?
We use a field specifier in the
format string of the printf
function.
for(int n=1; n<=10; ++n) {
triangularNumber += n;
printf (“ %2i %i\n”, n,
triangularNumber);}
Take a look at this program to
see how we do this to produce
the output on the right. 22
Printf: Aligning Output
How do we fix this?
We use a field specifier in the
format string of the printf
function.
float target = 1000;
for(int n=1; n<=10; ++n) {
printf (“ %2i %.2f\n”, n,
target/n);}
Take a look at this program to
see how we do this to produce
the output on the right. 23
Program Input
Reading input from a user in a C program is pretty easy:
We use the scanf function. (Take a look at this code example)
The scanf function is also included in the stdio library.
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.
C also supports if-else,
nested if statements, and 26
The switch Statement
General Form
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.
It is referred to as a ternary Example:
operator, that is, it takes three
operands.
It is also referred to as an “if
expression” and is very common in
functional languages.
29
gcc Basics
A Basic C Program
This is a basic
example of a C #include <stdio.h>
program.
This program int main (void) {
produces the printf(“Programming is fun.
output string
\n”);
Programming is
fun to the
return 0;
terminal. } prog1.c
What is “\n”?
31
Compiling a Basic C Program
$ 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