Download Floats

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

ALGOL 68 wikipedia , lookup

Reserved word wikipedia , lookup

Name mangling wikipedia , lookup

Simplex algorithm wikipedia , lookup

C++ wikipedia , lookup

Standard ML wikipedia , lookup

Corecursion wikipedia , lookup

Go (programming language) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

C syntax wikipedia , lookup

Transcript
Numbers in ‘C’
Two general categories:

Integers
 Unsigned – all positive integers
 Signed – positive and negative integers

Floats
1
Constants in C
•
•
There are 3 basic types of constants in C.
An integer constant is an integer-valued
number.We will concern here solely with
decimal constants like 0, 1, 743, 5280, 32767 or
-764.
• A floating point constant is a base-10 number
than contains either a decimal point or an
exponent or both like 0., 1., 0.2, 50.0, 12.3,12.667, 2E-8 or 0.006e-3.
• A character constant is a single character
enclosed in apostrophes like 'a', 'x', '9', or '?'.
Preprocessor directives
•
Preprocessor: A system program that
modifies the C program prior to compilation.
•
Preprocessor directive: An instruction to
the preprocessor. Begins with #.
•
Library: A collection of functions, symbols
and values.
•
2 kinds of preprocessor directives: includes
and defines.
Preprocessor directives


#include <stdio.h>: stdio.h, which stands for "standard
input/output header", is the header in the C standard library
that contains macro definitions, constants, and declarations
of functions and types used for various standard input and
output operations.
The #include <stdio.h> directive must be included on top of
every C program.
#define PI 3.1416: this is a constant macro definition. It
associates a name to a value for the duration of the program
In this case it associates the symbol PI to the value 3.1416. It
is an optional directive.
Comments
Comments are lines of code that are
ignored by the compiler. They are
placed for the programmer's benefit.
 /* this is a comment */
 /* this is
another comment, it can be spread
over multiple lines */

Instructions
•
•
•
•
•
•
Instructions in C are terminated by a semicolon (;)
Line changes and tabs are not important to
the C compiler.
a=3; b=4; c=5;
are the same as
a=3;
b=4;
c=5;
Skeleton of a program










#include <stdio.h>
/* optional additional includes */
/* optional constant macros */
int
main (void)
{
/* optional declarative statements */
/* one or more executable statements */
return (0);
}
/* PROGRAM # 1 */
#include <stdio.h>
/* This is my first C program. */
/* It’ll print a message to the display. */
int main (void ){
printf(“Welcome to the C programming language
course!\n”);
printf("This is our very first C program.\n");
printf(“We wish you a very pleasant experience with
C.”);
return (0);
}
8

1st line:
course!
Welcome to the C programming language

2nd line:
This is our very first C program.

3rd line:
We wish you a very pleasant experience with C.
9
Variables
In programming, we often need to have
places to store data. These receptacles are
called variables. They are called that
because they can change values.
 All variables must be declared at the top of
the program. There are three basic types of
variables in C:




int: for integer (whole numbers).
double (or float): for real (floating point numbers).
char: for characters.
Declaring Variables in ‘C’
All variables in ‘C’ must be declared.
Compiler should know
1. The variable name
2. Type of the variable
Why?
In order to allocate enough memory to it , before the variable
can be used.
What are data types?

char

int, long

float, double, long double

void
11
How to declare?
/*declare variable of type character.*/  This Denotes a comment in C
char
a_character;
char
letter;
/*declare variable of type integer.*/
int
an_integer;
int
number;
/*declare variable of type float.*/
float
floating_point_number;
float
average;
The following is also valid:
int
age, number, mark;
12
Where to Declare?
Declaration MUST happen at the top of the program,
that is, the very first thing that we do, MUST be declaring
the variables.
13
Identifiers
All variables must have names. There
are strict rules for variable names.
These rules will apply to function names
later so we will call these names
identifiers.
 A declaration is done with the type
followed by the identifier ;.
 Ex:
 int lifespan;
 double mass;
 char letter;

Hard rules for identifiers
Rule #1: An identifier must not be a
reserved word. Reserved words are used
by C exclusively. Here are a few: double,
char, int, do, float, if, return, sizeof,
void,while, typedef, struct, switch, for,
else.
 See the complete list in the Documents
section of the course website.
 Rule #2: An identifier must contain only
letters, digits or underscores. Abc8 is valid,
Abc-8 is not. _xyz is valid, atom number
is not.

Hard rules for identifiers

Rule #3: An identifier must never begin
with a digit. U238 and _765 are valid,
7abc and 67_q are not.
Soft rules for identifiers
Rule #4: An identifier should not be a
standard identifier. A standard identifier is a
name used by C but is not a reserved
word.
printf, scanf are examples of standard
identifiers.
 Rule #5: All-capital names should be
used only for constant macros. Variables
and function should never use capital
letters. Never mix upper-case and lowercase letters in a name.

17
How to select names for variables, functions, etc. in ‘C’?
o
o
Use the set of rules for picking up VALID names in C
Use meaningful and descriptive names so we need less comments.
Rules For Picking up Names
1. Names in ‘C’ are Case Sensitive,
o i.e., ‘C’ makes distinction between upper and lower-case letters.
o e.g.,
int number;
int Number;
/* number and Number are two different integer variables.*/
2. User defined names cannot be the same as C keywords.
o i.e., names such as for, while, if are NOT valid user defined names.
o e.g.,
int for;
/* invalid variable name. for is C keyword. */
18
Rules For Picking up Names
3. Every name must start with
o A letter of alphabet (upper, lower)
o Underscore ( _ )
o e.g.,
int age;
float Salary;
double _code;
4. The remainder of the name may be
o Upper or lower case letters of alphabets
o Decimal digits (0-9)
o Underscore
19
A collection of keywords, variables, operators, expressions,
statements, different data types, etc. that together performs
one or more than one task.
Therefore, we need to know the followings:
o C Keywords
o If
o For
o While
o
C Different data types
o Integer
o Floating Point
o Character
o Void




int
float
char
void
20
o
Valid C Variables
o int month;
o float average;
o double PI;
o char name;
o
C Operators
o Arithmetic
o Assignment and Compound Assignment
o Sizeof
o Relational
o Logical
o Conditional
o Increment/Decrement
o
C Statements
C Expressions,
A valid sequence of operands and operators to calculate a value,
usually expressing a math, logical calculation, …
o
21
‘C’ Keywords
Terms with pre-defined meaning in ‘C’.
We must use the keywords within their intended meanings.
Keyword definition cannot be changed.
22
Example:
case
sizeof
for
do
char
double
long
return
static
void
int
float
if
else
switch
struct
enum
while
used as part of switch
gives the size of the variable in byte
used for looping purposes
used as part of do-while
a data type
a data type
a data type
used to return a value from a function
used in combination with data types
a data type
a data type
a data type
used for conditional purposes
used a part of if
used for selection purposes
used to define a data structure
used to define user defined data type
used for looping purposes
23
Numbers in C in a table
Data Type
Purpose
Bytes
Range
int
integer
2
-32,768 to +32,767
long
long integer
4
-2,147,483,648 to
+2,147,483,647
unsigned
unsigned integer
2
0 to 65,535
unsigned
long
unsigned long
integer
4
0 to 4,294,967,295
float
floating point
4
3.4E+/-38
double
double float
8
1.7E+/-308
long double
long double float
16
1.7E+/-4932
24
Escape Sequences
Are used to format the print out.
(\) symbol is referred to as the escape character
AND is used to signify a escape sequence.
Sequence
Purpose
\n
New line
\t
Tab
\"
To print a double quote
\\
To print a backslash
Note On the Use of Different Data Types
Use the data type that conserves memory and still
accomplishes the desired purpose.
For example, depending on your machine, you
may need 1 byte for characters, 2 bytes for
integers and 4 bytes for float.
You also know that integers are in fact a subset of
float & as such you may decide to use float type
for all numerical values. Although this is
mathematically correct however you would not
have an efficient memory management here.
You could have used 2-byte for integers but you
used 4-byte instead!
26
Placeholders in I/O Statements
 Placeholders
(or conversion specifiers) will
substitute the value of the variable inside the
output string (printf).
 You must absolutely match the placeholder with
the variable type.
 %lf: long floating point (double).
 %d: decimal (int).
 %c: character (char).
 \n: a special character meaning that a new line
will be inserted.
27
Integer placeholders
%d is the default integer placeholder. When
used
it will simply display the value as is without any
padding. To add padding, to have columns for
example, we need formatted placeholders.
 %nd will reserve n places to display the number.
Justification will be to the right. The negative
sign takes one place.
 If the value is 17 and %4d is used, then it will
display 2 spaces followed by 17 on the screen.
__17

28
Integer placeholders
The number is always displayed in its
entirety, even when the format is too
narrow. With -1234 and a %3d
placeholder, you would see -1234,
therefore using 5 spaces instead of the 3
requested.
 A negative number change the
justification to the left of the field. With a
value of -1234 and a %-8d placeholder,
you will get -1234___ .
3 trailing blanks

29

Make yourself ready for the lab!
30