Introducing C

Hello World

#include <stdio.h>
void main(){
	printf("Hello World!\n");
Introducing C

Hello World

#include <stdio.h>
void main(){
	printf("Hello World!\n");

#include statement

Includes other source code into your program. One use is to tell your program about libraries you are using. We are using the Standard Input/Output library right now: stdio. The file stdio.h is the header that defines the Standard I/O Library
Introducing C

Hello World

#include <stdio.h>
void main(){
	printf("Hello World!\n");

The main function

main() is the starting point for a C program. It's where the computer begin execution. Note the syntax: the code is contained in curly braces.
Introducing C

Hello World

#include <stdio.h>
void main(){
	printf("Hello World!\n");

A printf function call

printf() is a library function that prints. It's the main way we will get output from out C programs.
Introducing C

Hello World

#include <stdio.h>
void main(){
	printf("Hello World!\n");

printf statement in more detail

A function call(What we are doing) Always a name followed by ( optional arguments) and ;
A string - Content between " and " is our way of telling the computer about text we want to print.
\n means "newline".
C statements end with a;
Introducing C

Escape characters

Characters that are hard to express
\n 	newline
\t 	tab
\'	print a single quote
\\ print a backslash
many others
Introducing C

Variable Declarations

Here is how C deals with memory

Imagine the system memory as a nice, flat stretch of beach

You want a variable, you need to dig a hole in the sand and dump the value in

How big a hole?

The holes in C are very specialized. Once dug, they can only hold one type of thing. Our hole they can only hold one type of thing. Our hole might hold integers, floating point values, or characters, for example

Introducing C

Declare variable before use

When you declare a variable, you are telling the compiler the kind of value the variable may hold (its type)

You cannot change the type of value a variable can hold once declared (well, pretty much anyway)

In fact, everything needs a type in C and it must be declared before use!

Introducing C

Common types, "regular" C

int: an integer, usually 4 bytes

float: float, usually 4 bytes

double: float, usually 8 bytes

char: single char, value in single quotes

Introducing C

Must declare before use

Every variable must be declared before it can be used (its type must be indicated)


<variable_type> <variable_name> [=<initial_value>];

Example: int length;

Introducing C

Rules for Variable Names

Must begin with a letter

Any combination of letters, digits and underscore

Cannot match with a C keyword

E.g. int int; int long;

Introducing C

Some C keywords

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Introducing C

printf: a new feature

You can use printf to output variables as well as strings. Put a "descriptor" in the string you print:

int numLots = 10;
double totalArea = 100;
printf("There are %d lots\n", numLots);
printf("The %d lots have a total area of %f\n", numLots, totalArea);
Introducing C

Many descriptors

%d or %idecimal (integer)
%efloating point exponent
%ffloating point decimal
%uunsigned integer
Introducing C

Full Format string

The format string contains a set of format descriptor that describe how an object is to be printed

start specificationFlagsWidthPrecisionsize modifier(h for short int)Conversion type(d for decimal)
Introducing C


An expression is written in pretty standard mathematical notation:

wins + losses;
width * height;
area / width;
x * y * z - q;
M_PI * 2;
printf("We played %d games\n", wins + losses);

We can put expressions where we have been putting variables

Introducing C

Types determine results

For integers: +, -, *, / all yield integers. Thus division can lead to truncation (2/3 has value 0). % gives the remainder

For floats: +, -, *, / all word as advertised. No remainder

Introducing C


An assignment puts a value in a variable. It is of the form:

variable = expression;

Do everything on the right hand side, get a value. Dump the value in to the variable.

double area, circumference;
area = width * height;
circumference = radius * 2 * M_PI;
Introducing C

Expression and Assignment Examples

double volume, diameter, hypot;
int game;
volume = radius * radius * M_PI;
diameter = radius * 2;
games = wins + losses;
hypot = sqrt(near * near * pow(far, 2));
Introducing C


printf("Please enter the yards of pipe used: ");
scanf("%f", &yardOfPipe);

scanf is an input routine

Useful for reading in string input and doing conversion to the correct type, all at one

Systax is "kind the like" printf

Note the use of ther & operator!!!

Introducing C

Basic form

To understand input, it is probably better to start with an example.

scanf("%d %f", &myInt, &myFloat);

... is waiting for input of the exact form: 25 3.14159

Introducing C

The &

scanf("%d %f", &myInt, &myFloat);

Hard to explain at the moment, but any variable that gets read in needs that ampersand character.

It is the address of the variable

more on that later

Introducing C

scanf example

printf("Input the radius");
scanf("%lf", &radius);
printf("Input the height");
scanf("%lf", &height);
printf("Input the number of cylinders");
scanf("%d", &numCylinder);