You are on page 1of 18

C Programming

Absolute Beginners Guide


By
Greg Perry and Dean Miller
Introduction
C is a compact general purpose language
Developed by Dennis MacAlistair Ritchie for the
Unix operating system (1972)
Was named C as it succeeded an earlier program
named B
Its flexibility and portability made it very popular
and was formalized in 1989 by the American
National Standards Institute (ANSI)
The most recent version of ANSI C was formally
adopted in 2011.

Developed by J. Woodcock; revised by M.


2
Anderson
Why Study C?
Many new languages are derived from C
Because it is more compact it is easier to learn
It is versatile and efficient and allows
maximum control

Developed by J. Woodcock; revised by M.


3
Anderson
Compilers
The book recommends:
Code::Blocks because it offers versions for Macs,
Linux and Windows operating systems
(www.codeblocks.org)
Open source: refers to free software available to all
that can be altered or improved.
Cross-platform: means that software can run on
different operating systems
IDE: integrated development environment; you can
write, edit and debug programs without having to
switch software
Compilers
What we are using:
Thinclient.spcompsci.org
Has a compiler and text editor
Gedit editor (go to Applications then Accessories)
Save program as sample.c
Has the Terminal: runs the program
Find it under Applications then Accessories)
Commands to run programs
To compile program: gcc name.c o name
To run program: ./name
Compiling Process
Preprocessing the processor substitutes all preprocessor directives with the
library code.
Produces a .i file.

Translating the compiler translates the high-level code statements in the .i


file into low-level Assembly language instructions.
Produces a .s file.

Assembling the assembler converts the Assembly language instructions in the .s


file into machine code.
Produces a .o file.

Linking the linker combines one or more binary (.o files) into a single executable
file.

To see these intermediary files compile with the save-temps option:


gcc hello.c save-temps o hello

Developed by J. Woodcock; revised by M.


6
Anderson
The main() Function
Most important part of a C program; required; a C program
will contain more than one or more functions
Functions are routines that perform tasks
Some functions are part of C or are created
Functions must contain lower case letters
First place the computer begins when running a program
{} start and end a function

A function is distinguished from a command by parentheses;


sometimes () are omitted
Ex. main(), calcIt(), printf(), strlen()
Data Types
Four basic data types in C:

Data type Description Example


char A single byte, capable of B
storing just one character.

int An integer whole number. 153


typically 2 or 4 bytes

float A floating-point number 0.123456


correct to six decimal places
typically half the size of a double

double A floating-point number 0.0123456789


correct to ten decimal places.
typically is 8 bytes

Developed by J. Woodcock; revised by M.


8
Anderson
Preprocessor Directives
Dont cause anything to happen at runtime; they work
during the compiling of your program
Most common: #include and #define
#include: 2 formats #include <filename> or #include
filename
#include is a merge file command; the #include
statement is replaced with the contents of the
filename specified after
Ex. #include<stdio.h> : means standard Input/Output and
this corresponds with the printf() function
Ex. #include<string.h>: corresponds to the strcpy()
function
Printf() function
Produces output on the screen
Format: printf(controlString [, data]);
controlString must be in quotes
Every C command and function needs a
semicolon (;) after it to let C know that the
line is finished except main() function
C does not automatically move the cursor
down to the next line, need to use an escape
sequence
C Language Escape Sequences

Sequence Significance Sequence Significance


\n newline \\ backslash
\t tab \? question mark
\v vertical tab \ single quote
\b backspace \ double quote
\r carriage \xhh hexadecimal
return number
\f form feed \000 octal number
\a audible alert \0 null character

Developed by J. Woodcock; revised by M.


11
Anderson
Displaying Variable Values
The printf() function will be used to display the value stored in a variable.

When displaying the value in a variable, a format specifier/conversion characters


is needed:
Specifier Description Example
%d An integer whole number 153
-32768 to 35768
%ld A long integer -2^31-2^31
%f A floating-point number 0.123456
%c A single character b
%s A string of characters Hello World
%p A memory address 0x003F
The arguments to the printf() function, will be a string and a variable name
separated by a comma. The string will contain the format specifier in the location
in the string where the value of the variable is desired:
int num = 5;
printf(The value in the variable is %d. \n, num);
printf(%d is the value in the variable. \n, num);
Developed by J. Woodcock; revised by M.
12
Anderson
Displaying Variable Values
The format specifier/conversion characters can be modified to display a desired number
of spaces, can be right or left aligned, display leading zeros, or a desired number of
decimal spaces.

%7d will print out an integer with a minimum of 7 spaces.

%07d will print out an integer with a minimum of 7 spaces and leading spaces will
contain 0s.

By default the number will be printed right aligned empty spaces will be to the
left of the number.

%.2f will print out a floating-point number with 2 decimal spaces to the right of the
decimal point. The .2 is referred to as a precision specifier.

%-10.3f will display a floating-point number in 10 spaces, with 3 decimal places, and
left-aligned. The minus sign specifies that the number be left aligned.
Developed by J. Woodcock; revised by M.
13
Anderson
Creating Variables
A variable is a box located in computer memory that holds a number or a character.
To create a variable it must be declared. A variable declaration has the
following syntax:
data_type variable_name;

A variable can be any name provided that it adheres to the following naming
convention:
Naming rule Example
Cannot contain any of the C keywords while
Cannot contain arithmetic operators a+b*c
Cannot contain non-alphanumeric characters %$#@!.,;:?/
Cannot contain any spaces number one
Cannot start with a number 2bad

Can contain numbers elsewhere good2


Can contain mixed Case UPdown
Can contain underscores is_ok

Multiple variables may be declared on one line as a comma-separated list:


data_type variable1, variable2, variable3;
Developed by J. Woodcock; revised by M.
14
Anderson
Variable declarations and initialization

Variables declarations must be made before they are used, and should be made
before any executable code in the function or in the file.

Initialization is when a value is first assigned to a variable; the equal sign is the
assignment operator (=).

Examples:
int num1, num2; /* Declares two integer variables. */
char letter; /* Declares a character variable. */
float decimal = 7.5; /* Declares and initializes a
floating-point variable */
num1 = 100; /* Initializes the integer variables. */
num2 = 200;
letter= B; /* Initializes the character variable */

Developed by J. Woodcock; revised by M.


15
Anderson
C Language Keywords

auto break case char


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

Developed by J. Woodcock; revised by M.


16
Anderson
Some C Programming Syntax
Single characters enclose in single quotes
Strings (a group of multiple characters) go inside
quotation marks
Clarify code with comments
For multi-line comments, begin them with /* and
C considers everything after that a comment until
it encounters a closing */
For single line comments, use //
Use whitespace and line breaks to make
programs easy to read
Assignment
Program the following pages and send a print
screen of the results.
Pages 21, 26, 35, 38, 46-47.

You might also like