Ses6 BasicC PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

COMPUTER PROGRAMMING 1

Session 6: Basic C

Iván Amaya
CONTENTS
 VARIABLES

 BASIC DATA TYPES

 LOCAL VS. GLOBAL VARIABLES

 READING DATA FROM USER

 BASIC MATH OPERATIONS

 EXAMPLES

 FURTHER READING
COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 2
VARIABLES
A variable is a space in memory for storing values

C requires each variable to be declared before using it

C requires us to tell it how much memory to reserve for each variable

Memory allocation is linked to the data type of each variable

We need a way to “identify” which variable we want to address

Valid identifiers (i.e. names) contain letters, digits and underscores (i.e. ‘_’)

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 3


VARIABLES
Care must be taken regarding variables:

Proper type must be used (e.g. float as int)

Identifiers are case sensitive (i.e. var != Var != vAr != vaR != VAR)

Identifier cannot begin with digits (i.e. 1a is not a valid id)

Select meaningful names (e.g. do not use var1, var2, var3, …)

Identifiers should begin by lowercase letter (e.g. age and not Age)

Separate multiple words (e.g. brute_salary or bruteSalary)


COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 4
BASIC DATA TYPES
Everything in C has an associated data type (even empty)

unsigned can be used to shift range up (i.e. starts at zero)

char: -128 to 127


int: -32 768 to 32 767
long: -2 147 483 648 to 2 147 483 647
float: 1.2E-38 to 3.4E+38 (6 decimal places)
double: 2.3E-308 to 1.7E+308 (15 decimal places)
void: represents empty
COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 5
LOCAL VS. GLOBAL VARIABLES
Each variable in C has a scope (i.e. reach): local or global

Local variables are known only to the function they are declared in

Global variables are known to everyone (declare outside functions)

Care must be taken with global variables to avoid overwriting

Be wary of repeating variable names, esp. with local and global

Local variable takes precedence over global

This means you can have a global var and a local var
COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 6
READING DATA FROM USER
scanf(format, &variable_name)

Included in stdio.h

Takes two parameters: format and variable_name

format tells C the type of data being read (e.g. ‘%d’)

variable_name must be preceded by ampersand sign (i.e. ‘&’)

variable_name should be of an appropriate data type (e.g. ‘int’)

1+ variables can be read at once (e.g. scanf(“%d%d”, &v1, &v2)


COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 7
BASIC MATH OPERATIONS
Basic math operations can be used directly in C:

Addition (+), subtraction (–), multiplication (*),

division (/) and remainder (%)

Care must be taken with precedence:

*, /, and % come first. + and – come later

Operations are carried out from left to right

Parenthesis can be used to change precedence of operations (inner first)


COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 8
EXAMPLES

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 9


EXAMPLES

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 10


EXAMPLES

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 11


EXAMPLES

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 12


EXAMPLES

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 13


EXAMPLES

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 14


EXAMPLES
1. You are tasked with developing a function for cyphering. Said cypher is
based on a shifting procedure, i.e. you take the code of each letter and
shifts it up by a given constant. Since the user requires knowing the
encrypted word, the program must print it. Limit your solution to a single
character. Hint: remember that each letter maps to an ASCII character.

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 15


EXAMPLES
2. Think again about the Asteroids program that was in the exam. Write a
program that reads the required values (i.e. radius and center of each circle,
plus power-up location) and calculates the distance between them. This
program will serve as a building block for the whole Asteroids logic.

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 16


EXAMPLES
3. Basic C programs do not have a graphic interface. Thus, every figure we
want to show must be printed via text. Develop a program for printing the
following figures:

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 17


EXAMPLES
4. Think again on the algorithm for digit separation. Write the code for
reading a four digit number and separating it into each digit. Print these
digits separated by a tab stop.

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 18


FURTHER READING
Look up and read information regarding the usage of:

Conditionals (if statement)

Loops (while, do-while, for)

Multiple conditional (switch)

COMPUTER PROGRAMMING – SESSION 6 IVÁN MAURICIO AMAYA CONTRERAS 19

You might also like