CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
1
Basic Structure of a C Program
output “Hello World!”
Algorithm:
#include <stdio.h>
int main()
{
printf(“Hello World!”);
return 0;
}
C Program:
Example: Hello World
Basic Structure of a C Program (cont)
#include <stdio.h>
int main()
{
printf(“Hello World!”);
return 0;
}
C Program
:
Example: Hello world
Includes
standard
input/output library
of
procedures.
Read: “Hash-include”
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
2
Basic Structure of a C Program
#include <stdio.h>
int main()
{
printf(“Hello World”);
return 0;
}
C Program:
Curly braces mark the
beginning
and
end
of a
block of instructions.
Example: Hello World
Basic Structure of a C Program
#include <stdio.h>
int main()
{
printf(“Hello World”);
return 0;
}
C Program:
Instruction (
function call
)
to output “Hello World”
Example: Hello World
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
3
Basic Structure of a C Program
#include <stdio.h>
int main()
{
printf(“Hello World”)
;
return 0
;
}
C Program:
“Statements” (lines of
instructions) always end
with a
semi-colon
(
;)
Example: Hello World
Values and Variables
• Basic Types:
– Integers
– Floating point numbers
– Characters
– Character Strings
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
4
Basic Types: int and float
• Integers (
int
)
0 1 1000 -1 -10 666
• Floating point numbers (
float
)
1.0 .1 1.0e-1 1e1
Basic Types: char
• Characters (
char
)
‟a‟ ‟z‟ ‟A‟ ‟Z‟ ‟?‟ ‟@‟ ‟0‟ ‟9‟
- Special Characters: preceded by \
‟\n‟ ‟\t‟ ‟\0‟ ‟
\‟
‟ ‟
\\
‟ etc.
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
5
Basic Types: character string
• Character Strings (a string of
char
-s)
• Examples:
– ”Hi there!”
– ”Line 1
\n
Line 2
\n
Line 3”
– ””
– ”
\”
\”
”
Arithmetic Expressions
• take arithmetic (numerical) values and
• return an arithmetic (numerical) value
• Are composed using the following operators:
+
(addition)
-
(subtraction)
*
(multiplication)
/
(division or quotient)
%
(modulus or remainder)
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
6
More on precedence
• *, /, %
are at the same level of precedence
• +, - are at the same level of precedence
• For operators at the same “level”, left-to-right
ordering is applied.
2 + 3 – 1 = (2 + 3) – 1 = 4
2 – 3 + 1 = (2 – 3) + 1 = 0
2 * 3 / 4 = (2 * 3) / 4 = 6 / 4
2 / 3 * 4 = (2 / 3) * 4 = 0 / 4
int-s and float-s
• float is a “communicable” type
• Example:
1 + 2 * 3 -
4.0
/ 5
= 1 + (2 * 3) - (
4.0 / 5
)
= 1 + 6 -
0.8
=
6.2
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
7
Increment and decrement
operators
• ++ is the increment operator
i++;
is equivalent to
i = i + 1;
• -- is the decrement operator
j--;
is equivalent to
j = j - 1;
Function Calls
• Tell the computer to execute a series of C
commands and (maybe) return a value
– In algorithm-speak: An invocation of a named
sequence of instructions
• Example: printf, scanf, sqrt
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
8
Example -- Find the square root (cont)
#include <stdio.h>
#include <math.h>
/* Find square root */
int main()
{
/* declare variables */
float x,myResult;
/*
output ”Enter a number: " */
printf(“Enter a number\n”);
/* input x */
scanf(“%f”,&x);
/* set myResult to result of squareRoot(x) */
myResult=sqrt(x);
/* output myResult */
printf(“Result is %f\n”,myResult);
return 0;
}
Comments
• Essential for documenting programs
• Run from a
/*
to the next
*/
• Examples:
/*
THIS IS A COMMENT
*/
/*
So is
this
*/
/*
** ...and this.
**
*/
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
9
Comments (cont)
• Comments do not “nest”
/*
Comments start with a “/*”
and end with a “
*/
”
but they don‟t nest! */
Components of a C Program
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
10
Topics
• Type
• Variables
• Keywords and Identifiers
• Assignments
• Constant Variables
Type
• Built-in types:
char
,
int
,
float
• Type
modifiers
:
long
,
short
,
const
• User-defined types (arrays and records)
• What about “strings”?
– Strings are arrays of
char
(discussed later)
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
11
Character Representation
• The ASCII values range from
0
to
127
– value 0: special character
‟\0‟
(a.k.a. NUL character)
– value 127: special character <DEL>
– other special characters:
‟\n‟ ‟\t‟ ‟\‟‟ ‟\\‟
etc.
– various “extended” sets from 128 to 255
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
12
Variable
• Is a logical
name
for a container
– (an actual piece of computer memory for
values)
• Has a type associated with it
– tells the computer how to interpret the bits
• Must be declared before use:
int i;
float result;
int i=0;
char initial=
‟
K
‟
;
Variable Declaration: Examples
int myID;
myID
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
13
Variable Declaration: Examples
int myID;
char myInitial =
‟
J
‟
;
Single “forward quotes” or
apostrophe (
’
) rather than
“back quotes” (‘)
Variable Declaration: Examples
float commission = 0.05;
short int myHeight = 183; /* cm */
long int mySalary = 100000000000000000000;
long float chanceOfADate = 3e-500;
double chanceOfA2ndDate = 1.5e-500;
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
14
float
commission = 0.05;
short int
myHeight = 183; /* cm */
long int
mySalary = 100000000000000000000;
long float
chanceOfADate = 3e-500;
double
chance_of_a_2nd_date = 1.5e-500;
Variable Declaration: Examples
Keyword
• ...has a special meaning in C
• ...is “case-sensitive”
• ...cannot be used as variable names
• Examples:
int, char, long, main, float,
double, const, while, for, if,
else, return, break, case,
switch, default, typedef,
struct, etc. (see D&D 2/e Appendix A or
D&D 3/e page 545, Figure 15.4)
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
15
Identifier
• ...is a series of characters consisting of
letters, digits and underscores (
_
)
• ...cannot begin with a digit
• ...must not be a keyword
• ...is “case-sensitive”
• Examples:
sUmoFA, x1, y2, _my_ID_, Main
(careful!)
Assignment
• Puts a specified value into a specified
variable
• Assignment operator:
=
<variable name> = <expression> ;
not to be
confused
with
==
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
16
Assignment: Examples
float x = 2.5 ;
char ch ;
int number ;
ch = ‟\n‟ ;
number = 4 + 5 ;
/* current value of number is 9. */
number = number * 2;
/* current value of number is now 18. */
#include <stdio.h>
/* Do various assignment
statements */
int main()
{
int integer;
char character;
float floatingPoint;
integer = 33;
character = 33;
floatingPoint = 33;
integer = 'A';
character = 'A';
floatingPoint = 'A';
integer = 33.33;
character = 33.33;
floatingPoint = 33.33;
integer = floatingPoint;
floatingPoint = integer;
return 0;
}
various.c
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
17
Constant Variables
• ...are variables that don’t vary
• ...may not be assigned to.
• ...must be initialized
const float Pi = 3.14159;
const int classSize = 100;
Example: Constants
“Global”
constant variable
“Local” variables
more on this later...
#include <stdio.h>
/* Converts an angle in degrees to
radians. */
const float PI = 3.1415926;
int main()
{
float angleInDegs;
float angleInRads;
printf("Enter angle in degrees: ");
scanf("%f", &angleInDegs);
angleInRads = PI/180*angleInDegs;
printf("%f\n", angleInRads);
return 0;
}
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
18
•scanf()
Example:
scanf(“%d”, &x);
•printf()
Example:
printf(“The value of x is %d\n”, x);
•#include <stdio.h>
Input/Output
Boolean
• A special “type” with only two values:
– false
and
true
.
• Used to implement conditions
– for selection and looping in an algorithm
• Boolean expressions
– represent statements which are either strictly
true or strictly false
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
19
Type int as Boolean
• In C, integers are used as Booleans
• Integer value
0
is
false
.
• Any
non-zero
integer value is
true
.
Functions
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
20
User-Defined Functions
• Create your own functions, similar to
printf()
or
sqrt()
• Recall a procedure in an algorithm - a
named collection of instructions
– InviteToParty
– RingUp
– MakeToParty
• A function implements the procedure or
function parts of an algorithm.
Writing User-defined Functions
• Need to specify:
– the
name
of the function
– its
parameters
– what it
returns
– block
of statements to be carried out when the
function is called
• The block of statements is called the
“
function body
”
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
21
Example: hello1.c
Function
definition
Function call
#include <stdio.h>
/*
* Print a simple greeting.
*/
void sayHello ( void )
{
printf(“Hello World!\n”);
}
/*
* Call a function which
* prints a simple greeting.
*/
int main(void)
{
sayHello();
return 0;
}
Example: hello1.c
Function name
Function body
#include <stdio.h>
/*
* Print a simple greeting.
*/
void sayHello ( void )
{
printf(“Hello World!\n”);
}
/*
* Call a function which
* prints a simple greeting.
*/
int main(void)
{
sayHello();
return 0;
}
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
22
Example: hello1.c
Return type
Formal
Parameter List
#include <stdio.h>
/*
* Print a simple greeting.
*/
void sayHello ( void )
{
printf(“Hello World!\n”);
}
/*
* Call a function which
* prints a simple greeting.
*/
int main(void)
{
sayHello();
return 0;
}
Parameters (cont.)
• Parameters are passed by
copying
the value
of the actual parameters to the formal
parameters.
• Changes to formal parameters do not affect
the value of the actual parameters.
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
23
Parameters (cont.)
• If a function does not take parameters,
declare its formal argument list
void
.
void sayHello (
void
)
{
printf(“Hello World!\n”);
}
sayHello();
Function call:
Declaration:
Return Values
• Values are returned by copying a value
specified after the
return
keyword
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
24
/* Returns the larger of two
numbers. */
int
max (int a, int b)
{
int result;
if (a > b)
{
result = a;
}
else
{
result = b;
}
return result;
}
Example: max.c
Return type
Return Values (cont.)
• If a function does not return a value, declare
its return type
void
.
void
sayHello ( void )
{
printf(“Hello World!\n”);
}
sayHello();
Function call:
Declaration:
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
25
Pointers
char ch = ‟A‟;
‟A‟
0x2000
ch:
Memory Address of a Variable
The
value
of the
variable ch
The
memory
address
of the
variable ch
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
26
The
&
Operator
• Gives the memory address of an object
• Also known as the “
address operator
”
&ch
yields the value 0x2000
char ch = ‟A‟;
‟A‟
0x2000
“
conversion specifier
” for
printing a memory address
char ch;
printf(“%p”, &ch);
Example:
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
27
Pointers
ch
0x1FFF
0x2000
0x2001
0x2002
0x1FFE
etc
„B‟
0x2000
chPtr
0x3A15
A variable which can store
the
memory address
of
another variable
Pointers
• A pointer is a
variable
• Contains a
memory address
• Points to a specific
data type
• Pointer variables are usually named
varPtr
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
28
cPtr:
char* cPtr;
Example:
• We say cPtr is a
pointer
to char
0x2004
Can store an
address
of
variables of type
char
Pointers and the
&
Operator
Example:
A
c:
0x2000
char c = ‟A‟;
char *cPtr;
cPtr:
0x2004
cPtr = &c;
0x2000
Assigns the
address of
c
to
cPtr
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
29
Notes on Pointers
int* numPtr;
float* xPtr;
Example:
• We can have pointers to any data type
int *numPtr;
float * xPtr;
Example:
• The
*
can be anywhere between the
type and the variable
Notes on Pointers (cont)
• You can print the address stored in a pointer
using the
%p
conversion specifier
printf(“%p”, numPtr);
Example:
• You can assign the address of a variable to a
“
compatible
” pointer using the
&
operator
int aNumber;
int *numPtr;
numPtr =
&
aNumber;
Example:
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
30
Notes on Pointers (cont)
int *numPtr;
Beware of pointers
which are not
initialized!
???
numPtr
Notes on Pointers (cont)
int *numPtr = NULL;
NULL
numPtr
•
When declaring a pointer, it is a good
idea to always initialize it to
NULL
(a special pointer constant)
CSE1301 Sem 2-2003
11/26/2015
Lecture 3: C Primitives 1
31
A
c:
0x2000
B
Pointers and the
Operator
Example:
char c = ‟A‟;
char *cPtr = NULL;
cPtr = &c;
*cPtr = ‟B‟;
Changes the value of
the variable which
cPtr
points to
cPtr:
0x2004
NULL
0x2000
Pointers and Function Arguments
• Change the value of an actual parameter
variable
• scanf
demystified
char ch;
int numx;
float numy;
scanf(“%c %d %f”,
&
ch,
&
numx,
&
numy);