You are on page 1of 20

C Programming

Introduction
ME 325
Spring, 2007
Overview
• Control and escape Codes

• Variable Types and Declaration

• Number System

• Other Data Types


Backslash Format Control Code /
Escape sequences.
C Meaning Code Meaning
ode
\b Backspace \f Form Feed

\n New Line \r Carriage Return

\t Horizontal Tab \” Double Quote

\’ Single Quote \0 Null, string


terminator
\\ Back Slash \v Vertical Tab
The \\ and \” is needed as these characters are use in parsing
the format string
Variable Introduction
• Variables are the names that refer to sections of
memory into which data can be stored.
• Imagine that memory is a series of different size
boxes. The box size is memory storage area required
in bytes.
• In order to use a box to store data, the box must be
given a name, this process is known as declaration.
– It helps if you give a box a meaningful name that relates to
the type of information en it is easier to find the data.
• The boxes must be of the correct size for the data
type you are going to put into it.
– An integer number such as 2 requires a smaller box than a
floating point number such as 1.9
• Data is placed into a box by assigning the data to
the box.
• By using he name of the box you can retrieve the
Variables and Data Types
• The amount of memory space
necessary to store a variable is also
referred to as a data type.
– A data type provides two valuable pieces
of information to the compiler: what
amount of space the variable will use, what
kind of information will be allowed in that
space.
• After declaring a variable, the compiler
reserves a space in memory for that
variable:
Variable Naming Conventions
• Variable names must start with a letter
or underscore. Then letters, numbers
and the underscore _ can be used.
• Spaces are NOT allowed.
• Do NOT use any of the C reserved
keywords.
• Case is significant, num is a different
variable to Num
• Only the first 31 characters are
significant.
Binary System
• Base or radix (2)
• Numbers are represented by only two
digits - 0 and 1 (2n where n is the
position of the number from right to
left).

1001=1(23)+0(22)+0(21)+1(20)=8+0+0+1
=9
Decimal System
• Base (radix) 10
• The numeric system that we have
always used.
Hexadecimal  System
• Base (radix) 16 (multiple of 4 and 8 Nibble
and byte)
• While the decimal system uses 10 digits
(they are all numeric), the hexadecimal
system uses sixteen (16) symbols to
represent a number.
• After counting from 0 to 9, the system uses
letters until it gets 16 different values. The
letters used are a, b, c, d, e, and f, or their
uppercase equivalents.
• The hexadecimal system counts as follows:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f
– f=15, then the next number would be 10=16,
A Byte
• A byte is a group or eight consecutive
bits. The bits are counted from right to
left starting at 0. The most right bit is
bit 0;
• Each four bits are called Nibble.
(hexadecimal)
C Numeric Types
Variable Type Keyword Range Storag
e Bytes
Character char -127 to 127 1
Unsigned unsigned char 0 to 255 1
Character
Integer int -32,768 to 32,767 2
Unsigned Integer unsigned int 0 to 65,535 2
Short Integer short -32,768 to 32,767 2
Unsigned Short unsigned short 0 to 65,535 2
Integer
Long Integer long -2,147,483,648 to 4
2,147,483,647
Unsigned Long unsigned long 0 to 4,294,967,2954 4
Integer  
Single precision float 1.2E-38 to 3.4E38, approx. 4
floating point range precision = 7 digits.
Double precision double 2.2E-308 to 1.8E308, approx. 8
floating point range precision = 19 digits
Variable Declaration
Examples
• The main variable types that you will use
most are char, int, float.

• Single declarations
– int age;
float amountOfMoney;
char initial;
• Multiple declarations
– You can repeat the declaration with a different
variable on each line or same line separated by a
coma
int age;
int houseNumber;
Or
int age, houseNumber, quantity;
float distance, rateOfDiscount;
char firstInitial, secondInitial;
Variable Scope
• The scope of a variable is
– The area of the program where that
variable is valid, i.e. the parts of the
program that have access to that variable.
This is determined by the location of the
declaration.

– The life span of that variable, i.e. the


length of time that the variable remains in
memory.
Where do you Declare
Variables
• Variables can be declared

– Prior to the start of the main( ) function.


– Within the main function, after the opening
{
– Within a function (later) , after the opening
{
– Within a block of code, after the {
• Global or external variable
• Local variable
Assigning a value into a
Variable
• Variable Declaration and
Initializing
– int age = 21, houseNumber = 0;
– float amountOfMoney = 0.0;
– char firstInitial = 'S', secondInitial = 72;

• Inside body of program


– age=22; this will overwrite the initialized
value of 21 by 22.
Incrementing and decrementing
a variable
• num = num + 1;
– Increment the existing value of num by 1
• num = num - 1;
– Decrement the existing value of num by 1
Shortcuts
Operato Shorthand Equivalent Result (num=7)
r
++ ++num num = num+1 8

num++

-- --num Num = num-1 7

num--

+= num +=3 num = num+3 10

-= num -=3 num = num-3 4

*= num *=2 num = 2*num 14

/= num /=3 num = num/3 2

%= num %=3 num = num%3 1 (Remainder)


Other Data Types
• Data structures
– A data structure is a group of data elements
grouped together under one name. These data
elements, known as members, can have different
types and different lengths. Data structures are
declared in C using the following syntax:
struct structure_name
{
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
} object_names;

here structure_name is a name for the structure type,


object_name can be a set of valid identifiers for objects
that have the type of this structure. Within braces { }
there is a list with the data members, each one is
specified with a type and a valid identifier as its name.
Struct Example
• struct product
{
int weight;
float price;
};

struct product apple;


apple.weight=1;
apple.price=50.0;
Enumerated Types
• Enumerated types contain a list of
constants that can be addressed in
integer values.

• We can declare types and variables as


follows.
enum days {mon, tues, ..., sun}
week;
enum days week1, week2;