JP SS 3 jezyk C


CSE1301 Sem 2-2003 11/26/2015
Basic Structure of a C Program
Example: Hello World
Algorithm:
C Program:
#include
int main()
{
output  Hello World!
printf( Hello World! );
return 0;
}
Basic Structure of a C Program (cont)
Example: Hello world
C Program:
#include
int main()
Includes standard
{
input/output library of
printf( Hello World! );
procedures.
Read:  Hash-include
return 0;
}
Lecture 3: C Primitives 1 1
CSE1301 Sem 2-2003 11/26/2015
Basic Structure of a C Program
Example: Hello World
C Program:
#include
int main()
{
Curly braces mark the
printf( Hello World );
beginning and end of a
return 0;
block of instructions.
}
Basic Structure of a C Program
Example: Hello World
C Program:
#include
Instruction (function call)
int main()
{
to output  Hello World
printf( Hello World );
return 0;
}
Lecture 3: C Primitives 1 2
CSE1301 Sem 2-2003 11/26/2015
Basic Structure of a C Program
Example: Hello World
 Statements (lines of
instructions) always end
C Program:
with a semi-colon (;)
#include
int main()
{
printf( Hello World );
return 0;
}
Values and Variables
" Basic Types:
 Integers
 Floating point numbers
 Characters
 Character Strings
Lecture 3: C Primitives 1 3
CSE1301 Sem 2-2003 11/26/2015
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.
Lecture 3: C Primitives 1 4
CSE1301 Sem 2-2003 11/26/2015
Basic Types: character string
" Character Strings (a string of char-s)
" Examples:
  Hi there!
  Line 1\nLine 2\nLine 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)
Lecture 3: C Primitives 1 5
CSE1301 Sem 2-2003 11/26/2015
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
Lecture 3: C Primitives 1 6
CSE1301 Sem 2-2003 11/26/2015
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
Lecture 3: C Primitives 1 7
CSE1301 Sem 2-2003 11/26/2015
Example -- Find the square root (cont)
#include
#include
/* 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.
**
*/
Lecture 3: C Primitives 1 8
CSE1301 Sem 2-2003 11/26/2015
Comments (cont)
" Comments do not  nest
/* Comments start with a  /*
and end with a  */
but they don t nest! */
Components of a C Program
Lecture 3: C Primitives 1 9
CSE1301 Sem 2-2003 11/26/2015
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)
Lecture 3: C Primitives 1 10
CSE1301 Sem 2-2003 11/26/2015
Character Representation
" The ASCII values range from 0 to 127
 value 0: special character  \0
(a.k.a. NUL character)
 value 127: special character
 other special characters:
 \n  \t  \   \\ etc.
 various  extended sets from 128 to 255
Lecture 3: C Primitives 1 11
CSE1301 Sem 2-2003 11/26/2015
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
Lecture 3: C Primitives 1 12
CSE1301 Sem 2-2003 11/26/2015
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;
Lecture 3: C Primitives 1 13
CSE1301 Sem 2-2003 11/26/2015
Variable Declaration: Examples
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;
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)
Lecture 3: C Primitives 1 14
CSE1301 Sem 2-2003 11/26/2015
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: =
= ;
not to be
confused
with ==
Lecture 3: C Primitives 1 15
CSE1301 Sem 2-2003 11/26/2015
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
integer = 33.33;
/* Do various assignment
character = 33.33;
statements */
floatingPoint = 33.33;
int main()
{ integer = floatingPoint;
int integer;
floatingPoint = integer;
char character;
float floatingPoint;
return 0;
}
integer = 33;
character = 33;
floatingPoint = 33;
integer = 'A';
character = 'A';
floatingPoint = 'A';
various.c
Lecture 3: C Primitives 1 16
CSE1301 Sem 2-2003 11/26/2015
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
#include
/* Converts an angle in degrees to
radians. */
 Global
const float PI = 3.1415926;
constant variable
int main()
{
float angleInDegs;
float angleInRads;
printf("Enter angle in degrees: ");
 Local variables
scanf("%f", &angleInDegs);
angleInRads = PI/180*angleInDegs;
printf("%f\n", angleInRads);
return 0;
}
more on this later...
Lecture 3: C Primitives 1 17
CSE1301 Sem 2-2003 11/26/2015
Input/Output
" scanf()
Example:
scanf( %d , &x);
" printf()
Example:
printf( The value of x is %d\n , x);
" #include
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
Lecture 3: C Primitives 1 18
CSE1301 Sem 2-2003 11/26/2015
Type int as Boolean
" In C, integers are used as Booleans
" Integer value 0 is false.
" Any non-zero integer value is true.
Functions
Lecture 3: C Primitives 1 19
CSE1301 Sem 2-2003 11/26/2015
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
Lecture 3: C Primitives 1 20
CSE1301 Sem 2-2003 11/26/2015
Example: hello1.c
#include
/*
* Print a simple greeting.
*/
void sayHello ( void )
Function {
printf( Hello World!\n );
definition
}
/*
* Call a function which
* prints a simple greeting.
*/
int main(void)
{
sayHello();
Function call
return 0;
}
Example: hello1.c #include
/*
Function name
* Print a simple greeting.
*/
void sayHello ( void )
{
Function body printf( Hello World!\n );
}
/*
* Call a function which
* prints a simple greeting.
*/
int main(void)
{
sayHello();
return 0;
}
Lecture 3: C Primitives 1 21
CSE1301 Sem 2-2003 11/26/2015
Example: hello1.c #include
/*
Return type
* Print a simple greeting.
*/
void sayHello ( void )
{
printf( Hello World!\n );
}
/*
* Call a function which
Formal
* prints a simple greeting.
Parameter List
*/
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.
Lecture 3: C Primitives 1 22
CSE1301 Sem 2-2003 11/26/2015
Parameters (cont.)
" If a function does not take parameters,
declare its formal argument list void.
void sayHello ( void )
Declaration:
{
printf( Hello World!\n );
}
sayHello();
Function call:
Return Values
" Values are returned by copying a value
specified after the return keyword
Lecture 3: C Primitives 1 23
CSE1301 Sem 2-2003 11/26/2015
Example: max.c
Return type
/* Returns the larger of two
numbers. */
int max (int a, int b)
{
int result;
if (a > b)
{
result = a;
}
else
{
result = b;
}
return result;
}
Return Values (cont.)
" If a function does not return a value, declare
its return type void.
void sayHello ( void )
Declaration:
{
printf( Hello World!\n );
}
sayHello();
Function call:
Lecture 3: C Primitives 1 24
CSE1301 Sem 2-2003 11/26/2015
Pointers
Memory Address of a Variable
char ch =  A ;
ch:
 A
0x2000
The memory
The value of the
address of the
variable ch
variable ch
Lecture 3: C Primitives 1 25
CSE1301 Sem 2-2003 11/26/2015
The & Operator
" Gives the memory address of an object
char ch =  A ;
0x2000
 A
&ch
yields the value 0x2000
" Also known as the  address operator
Example:
char ch;
printf( %p , &ch);
 conversion specifier for
printing a memory address
Lecture 3: C Primitives 1 26
CSE1301 Sem 2-2003 11/26/2015
Pointers
A variable which can store
the memory address of
another variable
0x3A15
0x2000
chPtr
0x1FFE 0x1FFF 0x2000 0x2001 0x2002
etc
 B
ch
Pointers
" A pointer is a variable
" Contains a memory address
" Points to a specific data type
" Pointer variables are usually named varPtr
Lecture 3: C Primitives 1 27
CSE1301 Sem 2-2003 11/26/2015
Example:
char* cPtr;
cPtr:
0x2004
Can store an address of
variables of type char
" We say cPtr is a pointer to char
Pointers and the & Operator
Example:
char c =  A ;
char *cPtr;
Assigns the
cPtr = &c;
address of c to cPtr
c: cPtr:
A 0x2000
0x2000 0x2004
Lecture 3: C Primitives 1 28
CSE1301 Sem 2-2003 11/26/2015
Notes on Pointers
" We can have pointers to any data type
int* numPtr;
Example:
float* xPtr;
" The * can be anywhere between the
type and the variable
int *numPtr;
Example:
float * xPtr;
Notes on Pointers (cont)
" You can assign the address of a variable to a
 compatible pointer using the & operator
int aNumber;
int *numPtr;
Example:
numPtr = &aNumber;
" You can print the address stored in a pointer
using the %p conversion specifier
printf( %p , numPtr);
Example:
Lecture 3: C Primitives 1 29
CSE1301 Sem 2-2003 11/26/2015
Notes on Pointers (cont)
Beware of pointers
int *numPtr;
which are not
initialized!
???
numPtr
Notes on Pointers (cont)
" When declaring a pointer, it is a good
idea to always initialize it to NULL
(a special pointer constant)
int *numPtr = NULL;
NULL
numPtr
Lecture 3: C Primitives 1 30
CSE1301 Sem 2-2003 11/26/2015
Pointers and the *ð Operator
Example:
char c =  A ;
char *cPtr = NULL;
Changes the value of
the variable which cPtr
cPtr = &c;
points to
*cPtr =  B ;
cPtr:
c:
NULL
B
A 0x2000
0x2000 0x2004
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);
Lecture 3: C Primitives 1 31


Wyszukiwarka

Podobne podstrony:
JP SS 3 jezyk C
JP SS 3 jezyk C
JP SS 6 Klasy i obiekty
JP SS 2 algorytmy i podstawy programowania
JP SS 5 podstawy Java
JP SS 4 start Java
JP SS 5 podstawy Java (3)
JP SS Interfejs graficzny
JP SS 1 podstawy JP
JP SS 4 wprowadzenie Java
JP SS 1 podstawy JP
JP SS 5 podstawy Java
JP SS 7 Klasy i obiekty
JP SS 6 BlueJ
JP SS 4 start Java
JP SS 8 Instrukcje, pętle
JP SS 7 Typy i operacje
JP SS 9 Tablice IO wyjÄ…tki

więcej podobnych podstron