Вы находитесь на странице: 1из 99

C Programming

Introduction

High-level Computer Languages


and Their Inventors

FORTRAN
BASIC
Pascal
C
C++
Java
C#

John W. Backus, 1954


George Kemeny and Tom Kurtz, 1964
Nicolas Wirth
Dennis M. Ritchie
Bjarne Stroustrup
James Gosling
Anders Hejlsberg

Programming Language
Popularity
http://langpop.com/

TIOBE Programming Community


Index for August, 2010
http://www.tiobe.com/index.php/content/paperinfo/t
pci/index.html

10 Reasons to Learn C
http://iel.ucdavis.edu/publication/WhyC.htm
l
1.

C is one of foundations for modern information technology and


computer science.
2. C is the most commonly used programming language in industry.
3. C is a standardized programming language with international
standards.
4. Writing computer programs is essential to solving complex
science and engineering problems.
5. Computer programming is becoming a necessary skill for many
professions.
6. Computer programming can develop student's critical thinking
capabilities.
7. C is one of the most commonly used programming languages in
colleges and universities.
8. C is the language of choice for programming embedded and
mechatronic systems with hardware interface.
9. C excels as a model programming language.
10. Once you have learned C, you can pick up other languages
without much difficulty by yourself because all other modern
5
languages borrowed heavily from C.

History of C
C
Invented by Ritchie based on B, a simplified version of
BCPL.
Used to develop Unix operating system and Unix
commands.
Most system software such as OS are written in C or C++.
Replacement for assembly language for hardware
interface.
By late 1970's C had evolved to K & R C.

C Standards
1st C standard created in 1989 by ANSI, ratified by ISO in
1990.
It is called C89. Some call it C90.
2nd C standard was ratified in 1999, called C99.
Numerical extensions such as complex numbers, variable length
arrays, and IEEE floating-point arithmetic are major enhancement
in C99. C99 will be pointed out whenever features in C99 only are
presented.

The current C standard, which has been informally named


C11, was ratified in 2011. (Our compiler supports some,

Some Resource Links


Deitel & Deitel provide a C Programming Resource Center that
has links to multiple resources, including the C99 standard.
http://www.deitel.com/ResourceCenters/Programming/C/tabid/199/De
fault.aspx

Other useful sites include:


The ANSI C Standard Library:
http://www.csse.uwa.edu.au/programming/ansic-library.html
The C Library Reference Guide (by Eric Huss), which provides
prototypes and information about most C functions:
http://www.acm.uiuc.edu/webmonkeys/book/c_guide /

Phases of Compilation-Based
Execution of a C Program
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute
8

Phases of Compilation-Based
Execution of a C Program -Edit
This is accomplished with
an editor program.

Software packages for the C/C++ integrated program


development environments such as Eclipse and
Microsoft Visual Studio have editors that are
integrated into the programming environment.
C programs are typed in with the editor, corrections
are made if necessary, and the program is stored on a
secondary storage device such as a hard disk.
C program file names should end with the .c
extension.
9

Phases of Compilation-Based
Execution of a C Program -Preprocessing

In a C system, a preprocessor executes automatically


before the compilers translation phase begins.

The C preprocessor obeys special commands called


preprocessor directives, which indicate that certain
manipulations are to be performed on the program before
compilation.

10

Phases of Compilation-Based
Execution of a C Program -Preprocessing
These manipulations
usually consist of including other files in
the file to be compiled, performing various text replacements,
defining various macros, etc..

References:
In Deitel & Deitel, they are discussed in Chapter 13, p517.

Examples include:
#include <stdio.h>
#define <identifier>

<replacement text>

11

Phases of Compilation-Based
Execution of a C Program -Preprocessing

In Step 3, the compiler translates the C source code (after


the preprocessing steps have been done) into object code
and stores it on the disk.
Object code is essentially machine code, a series of 1s and 0s that
the CPU can execute.

However, object code is incomplete and not yet executable.


C programs typically contain references to functions defined
elsewhere, such as in the standard libraries or in the private libraries
of groups of programmers working on a particular project.
The object code produced by the C compiler typically contains
holes due to these missing parts.

Those holes are resolved by the linker.


12

Phases of Compilation-Based
Execution of a C Program -Linking
A linker links the object code with the code for
the missing functions to produce an executable
image (with no missing pieces).
During linking, multiple object files might be
combined into one executable.

13

Phases of Compilation-Based
Execution of a C Program
Loading
and
Executing
The next phase is called loading.

Before a program can be executed, the program must


first be placed in memory.
This is done by the loader, which takes the executable
image from disk and transfers it to memory.
Additional components from shared libraries that
support the program are also loaded.
Finally, the computer, under the control of its CPU,
executes the program one instruction at a time.
14

15

16

From High-Level Program to Executable


File
(from C++)

17

A Simple C Program

/* File: hello.c
Print Hello, world on the screen */
#include <stdio.h>
int main()
{
printf(Hello, world\n");
return 0;
} // end function main()

18

A Simple C Program

/* File: hello.c
Print Hello, world on the screen */
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf(Hello, world\n");
system (pause);
return 0;
} // end function main()

19

/* File: hello.c
Print Hello, world on the screen */
#include <stdio.h>
int main()
{
printf(Hello, world\n");
return 0;
}

Comments
Text surrounded by /* and */ is ignored by
computer
Used to describe program
#include <stdio.h>
Preprocessing directive - tells computer to
load contents of a header file
<stdio.h> allows standard input/output

20

/* File: hello.c
Print Hello, world on the screen */
#include <stdio.h>
int main()
{
printf(Hello, world\n");
return 0;
}

int main()
C programs contain one or more functions,
exactly one of which must be main
Parenthesis used to indicate a function
int means that main "returns" an integer
value
Braces indicate a block
The bodies of all functions must be
contained in braces
We will enforce this style with braces.
21

/* File: hello.c
Print Hello, world on the screen */
#include <stdio.h>
int main()
{
printf(Hello, world\n");
return 0;
}

printf( hello, world\n" );


Instructs computer to perform an action
Specifically, prints string of characters
within quotes
Entire line called a statement
All statements must end with a
semicolon.
\ - escape character
Indicates that printf should do
something out of the ordinary.
\n is the newline character.

22

/* File: hello.c
Print Hello, world on the screen */
#include <stdio.h>
int main()
{
printf(Hello, world\n");
return 0;
}

return 0;
A way to exit a function.
return 0, in this case, means that the program
terminated normally.
The returned value of 0 is displayed in the exit status
in ChIDE.

Right brace }
Indicates end of main has been reached.
23

Structure of a C Program

24

Preprocessor
Directives
Global
Declarations
variables
function prototypes
int main ()
{
Local declarations
Statements
}
Other functions...
void xxx()
{
}
void xxx()
{
}

25

Preprocessor
Directives
Global
Declarations
variables
Other functions...
void xxx()
{
}
void xxx()
{
}

int main ()
{
Local declarations
Statements
}
26

27

28

Some Common Errors


(even with simple programs )
Its helpful to get to know your compiler and the errors
it produces under various conditions.
Missing statement terminators
Invalid preprocessor directives
Missing program block identifiers
Invalid escape sequences (possible warning)
Invalid comment blocks
Missing function prototypes
29

Data Types in C
Unlike Java, C contains only four standard data types: void,
char, integer types, and floating point types.
As we will see, the size of these data types is machine and system
dependent.
Since we sometimes need to know the size of a data type at
runtime, C provides an operator, sizeof(), that will return the
size of any data type in bytes.
Example:
/* x will contain 4 on most systems */
int x = sizeof (int);

30

Data Types in C
Unlike Java, C contains only four standard data types: void,
char, integer types, and floating point types.
void: When used as a return value from a function, indicates
that nothing is returned.
char:
Usually a single byte.
Usually represents a value from the ASCII table.
Can also be interpreted as a small signed integer (between -128 and
127).
Declared using the char keyword.
Character literals always in single quotes.
31

ASCII Table

32

Data Types in C
int: Integer data type. Comes in different flavors,
depending on the modifiers (short or long, signed or
unsigned):

33

Data Types in C
int:

Integer data type.

Note that unsigned integers can store positive numbers


approximately twice as large as the signed integer of the
same type.
ANSI standard requires that
sizeof (short int) <= sizeof (int) <= sizeof (long int)

34

Data Types in C
float: Floating point data type.
Comes in three sizes: float, double, long double.

ANSI standard enforces:


sizeof (float) <= sizeof (double) <= sizeof (long double)
35

Data Types in C -- Summary

36

Predefined Constants for


Data Types in C
Because the size (and therefore the max/min values) of the
standard data types can vary so much between systems, C
provides two ways of dealing it:
the sizeof() operator which can be used dynamically to determine
the size of any data type in bytes.
Predefined constants that define the limits on values.

Most constants are defined in the <limits.h> header file.


INT_MIN
SHRT_MIN
CHAR_MIN

INT_MAX
SHRT_MAX
CHAR_MAX

UINT_MAX
USHRT_MAX
UCHAR_MAX

These provide the minimum and maximum values on specific


systems for most data types.
37

Predefined Constants for


Data Types in C
The corresponding values for the floating point data types
are defined in <float.h>
FLT_MIN
DBL_MIN

FLT_MAX
DBL_MAX

38

Valid Identifiers

A valid identifier in C can consist of


letters (both lower and upper case)
underscores (_)
digits

Notes:
The first character must not be a digit.
In the ANSI C standard, an identifier can be of any length, but the
compiler will only look at the first 31 characters.
An identifier cannot duplicate a reserved word.

Further Notes:
Note that, in contrast to Java, $ is not a valid character in a C
identifier.
Like Java and C++, C is case sensitive.
Use of leading underscores is discouraged for general use.
39

Variable Declarations
Variables are named locations in memory that contain
a value.
Variable declarations are made in C just like in Java.
<data type> <variable name>;
int
numOfStudents;
float numHoursWorked;

And of course, variables can be declared and initialized


in the same statement:
int
numOfStudents = 55;
float numHoursWorked = 35.5F;

Later we will see that we can define and declare our


own data types.
In the earlier ANSI C standard, all declarations in a
function must precede any executable statements.
This was changed in C99, which allows variables to be declared
anywhere inside a function.
If you check, youll find that Visual C++ 2010 and earlier does
40
not implement C99 in this regard, but VS 2013 does.

Variable Declarations
Some good rules for variable names:
Use either capitalization or underscores to create multi-word
identifiers.
Abbreviations should be long enough to clearly indicate what is
being abbreviated.
Avoid the use of generic names.
Dont create several variable names that differ only by one or
two characters.

41

Data Types in C
Differences with Java
Note that there is no boolean data type in C.
Boolean evaluations are done through integers (a 0 represents false, a
non-zero value represents true).

There is no String class in C (or its equivalent).


Strings in C are represented as arrays of characters terminated by a
null (\0).

C has pointers!! This is not a data type per se, but can be
applied to any data type, even ones that we create.
This is a complex subject that we will discuss in more detail shortly.

42

Constants
Constants are values that cannot be changed during
the execution of a program.
Well consider three types of constants:
literals
#define preprocessor directives
Use of the const declaration on a variable.

43

Literals
As we know, a variable is a named storage location in
the computers memory.
A literal, on the other hand, is a constant value that is
hard coded into the program.
It is not associated with a named location in memory.
The value does not change.

Character literals are enclosed in single quotes


a, \n, \0 (NULL)
char ch = a;
(Note that the escape characters, like \n or \, are considered
single characters (even though it takes two characters to write them).)
44

Literals
Integer literals are numbers without decimal values.
5, 236, -7
int x = 5;

By default, an integer literal is considered a(n) ?.

Floating point literals are numbers with decimal


values.
3.14159
float pi = 3.14159; // Compiler warning
float pi = 3.14159F; // No compiler warning

By default, a floating point literal is considered a(n) ?.

45

Literals
Integer literals are numbers without decimal values.
5, 236, -7
int x = 5;

By default, an integer literal is considered an int.

Floating point literals are numbers with decimal


values.
3.14159
float pi = 3.14159; // Compiler warning
float pi = 3.14159F; // No compiler warning

By default, a floating point literal is considered a double.

46

Literals
String literals are character strings in double quotes.
Hello World
h

There is no string (or String) class in C.


Strings in C are arrays of characters terminated by a NULL.
They are operated on by functions that receive the string as an input
parameter.
Example: There is no string concatenation operator in C (+).
To concatenate two strings:
use the strcat (char *, char *) function in <string.h>.
juxtaposition of strings will work with string literals: printf (Hi there);
will print Hi there
47

Literals
String Declarations
Strings can be declared with statements like the following:
char str[] = Hello;
char *str = Hello;
With declarations like this, C creates a character array as
follows:
H E

O \0

str

The variable str is a char* that points to the first element in the array.
The \0 is automatically appended in a declaration.

We will consider strings in much more detail later.


48

Preprocessor Constants
#define
#define constants are macros that replace the
<identifier> with the text that follows:
Model:

#define <identifier> <replacement text>

Exam:

#define SALES_TAX_RATE 0.0825

The preprocessor performs a simple text replacement


throughout the file before it is sent to the compiler.
No variable is created, no memory is allocated.

49

Preprocessor Constants
#define
The header file <math.h> contains many predefined
constants that we can use (e.g., M_PI, M_E, ...).
However, to use them in Visual C++, we must include
the following preprocessor directive in our files
before we include <math.h>:
#define _USE_MATH_DEFINES

50

Variable Constants
The final way of declaring a constant is by declaring a variable
constant:

const int X = 25;


const float PI = 3.14159F;
This declaration does create a variable in memory, but the const
modifier tells C that its value cannot be changed.

Notes:
The modifier const comes before the data type.
The variable must be initialized to be used effectively. If not, its value
will be whatever is in memory when the declaration is executed.
Note: this is different from C++, which requires const variables to be
initialized when they are declared. In C, no compile error if not initialized.

Once created, the value cannot be changed.


51

Formatted Input and


Output
Well introduce our primary input and output
statements:
printf()
scanf()

52

Formatted Input and Output


printf()
Function printf()
Precisely formatted output can be accomplished using the output function
printf().

The printf() function is defined in <stdio.h>.

The printf() function has the following format:


printf( format-control-string, arguments );

Format-control-string:
Always enclosed in double quotation marks.
Uses format specifications to describe output format. Each specification begins with
a percent sign (%) and ends with a conversion specifier.
Escape sequences can also be embedded in the format string.

arguments
are matched with the conversion specifiers in the format-control-string in both type
and number. The matching is done left-to-right.
53

Formatted Input and Output


printf()
Conversion Specifiers
Follow the template:
%<flag><min width><.precision><size>conversion-code

Elements that are in angle brackets (< >) are optional.

Conversion code

There are about 30 of these, but we are only concerned with a few.
See chart next page.

54

Formatted Input and Output


printf()
Some common printf() conversion codes
Argument Type

Conversion Code in the


Format-Control-String

char

%c

signed char, short, int

%d

unsigned char, short, int

%u

octal number

%o

hexadecimal number

%x, %X

float

%f

double

%f or %lf

string

%s

pointer

%p
55

Formatted Input and Output


printf()
printf() conversion codes
Conversion Code

Data Type

%c

Char

%s

String

%d

Signed Integer

%u

Unsigned integer

%f

Float

%lf

Double

%Lf

Long Double

%p

Pointer value

%x, %X

Hexadecimal value
(letters)

%o

Octal Value

%e, %E

Scientific notation

56

Formatted Input and Output


printf()
Conversion Specifiers
Follow the template:
%<flag><min width><.precision><size>conversion-code

<Size>

Consists of the letters h (short), l (long), or L (long).


Only applies to short int, long int, long long int, and long double
data types.

57

Formatted Input and Output


printf()
Conversion Specifiers
Follow the template:
%<flag><min width><.precision><size>conversion-code

<min width>

Used to specify the minimum number of positions in the output. (Very useful when
printing in columns.)
Default is right justified within the field specified (use minus sign flag to left justify).
printf() will override the width specified if the output is larger.
int x = 25;

25

printf (%5d, x);


printf (%-5d, x);

25

printf (%05d, x);

00025
58

Formatted Input and Output


printf()
Conversion Specifiers
Follow the template:
%<flag><min width><.precision><size>conversion-code

<.precision>

Used only with floating point data types.


Used to specify the precision (number of decimal places) in the output.
float x = 25.3567F;
printf (%f, x);
printf (%9.2f, x);
printf (%-9.2f, x);

25.356701
25.36
25.36

59

Formatted Input and Output


printf()
Conversion Specifiers
Follow the template:
%<flag><min width><.precision><size>conversion-code

<flag>

Most common flags are +, -, 0.


+ causes a plus sign to be prepended to the output if the number is positive (wont be
prepended to a negative number).
- causes the output to be left justified in the field.
0 causes leading 0s to be prepended to the output (if the fields size will allow it).
int x = 25;

+25

printf (%+d, x);


printf (%-9d, x);

25

printf (%09d, x);

000000025
60

Formatted Input and Output


printf()
Escape sequences

61

62

63

Formatted Input and Output


scanf()

scanf() is the standard input function for C.

Very precise input formatting can be accomplished with


scanf().

Function scanf() has the following input formatting capabilities


(not exclusive):

Inputting all types of data.


Inputting specific characters from an input stream.
Skipping specific characters in the input stream.
Inputting only those characters we specify.
Skipping only those characters we specify.
Specifying the number of characters to be input.
64

Formatted Input and Output


scanf()
Like printf(), the first argument in a call to scanf() is a formatting
string which can contain conversion specifiers. However, the list of
subsequent arguments is now a list of addresses, rather than variables or
constants.
int i; float x;
scanf (%d%f, &i, &x);

65

Formatted Input and Output


scanf()
The conversion specifiers in scanf() are very similar (with a
few exceptions) to those used in printf().

66

Formatted Input and Output


scanf()

67

Formatted Input and Output


scanf()

68

Formatted Input and Output


scanf()
Note that conversion specifier %i is capable of
inputting decimal, octal and hexadecimal
integers with one specifier.
Which one is inputted depends on the format
of the input:
a leading 0x indicates a hex number.
a leading 0 indicates an octal number.
no prefix indicates a decimal number.
69

70

Formatted Input and Output


scanf()
When inputting floating-point numbers, any of the
floating-point conversion specifiers e, E, f, g or G
can be used.
The following program reads three floating-point
numbers, one with each of the three types of
floating conversion specifiers, and displays all three
numbers with conversion specifier f.
The program output confirms the fact that floatingpoint values are imprecisethis is highlighted by
the third value printed.
71

72

Formatted Input and Output


scanf()
Characters and strings are input using the conversion specifiers
c and s, respectively.
The next program prompts the user to enter a string.
The program then inputs the first character of the string with %c
and stores it in the character variable x, then inputs the
remainder of the string with %s and stores it in character array
y.
Note that the %s conversion specifier automatically appends a
NULL to the end of the string.
It is up to the programmer to make sure there is enough room in the
character array for all of the characters, including the NULL!!
73

Note: The name of a string is considered a char*. Thus, no address


operator is required in a scanf() that reads a string.
Note:
The string is read from the input; a NULL (\0) is
automatically appended.

74

Formatted Input and Output


scanf()
A sequence of characters can be input using a scan set.
A scan set is a set of characters enclosed in square brackets,
[], and preceded by a percent sign in the format control
string.
A scan set scans the characters in the input stream, looking
only for those characters that match characters contained in
the scan set.
Each time a character is matched, it is stored in the scan sets
corresponding argumenta pointer to a character array.
The scan set stops inputting characters when a character that
is not contained in the scan set is encountered.
75

Formatted Input and Output


scanf()
If the first character in the input stream does not
match a character in the scan set, only the null
character is stored in the array.
The next program uses the scan set [aeiou] to
scan the input stream for vowels.
Notice that the first seven letters of the input are
read.
The eighth letter (h) is not in the scan set and
therefore the scanning is terminated.
76

77

Formatted Input and Output


scanf()
The scan set can also be used to scan for characters not
contained in the scan set by using an inverted scan set.
To create an inverted scan set, place a caret (^) in the square
brackets before the scan characters.
This causes characters not appearing in the scan set to be
stored.
When a character contained in the inverted scan set is
encountered, input scanning is terminated.
The next program uses the inverted scan set [^aeiou] to
search for consonantsmore properly to search for
nonvowels.
78

79

Formatted Input and Output


scanf()
A field width can be used in a scanf conversion
specifier to limit the number of characters that are read
from the input stream.
Note that tokens are separated by whitespace. When the
width is specified, scanf() will read until (a) a whitespace
character is encountered, or (b) the maximum number of
characters have been entered.
Figure 9.23 inputs a series of consecutive digits as a twodigit integer and an integer consisting of the remaining
digits in the input stream.
80

81

Formatted Input and Output


scanf()
Often its necessary to skip certain characters in the input
stream.
For example, a date could be entered as
11-10-1999

Each number in the date needs to be stored, but the dashes


that separate the numbers can be discarded.
To eliminate unnecessary characters, include them in the
format control string of scanf (white-space characters
such as space, newline and tabskip all leading whitespace).
82

Formatted Input and Output


scanf()
For example, to skip the dashes in the input, use the
statement
scanf( "%d-%d-%d", &month, &day, &year );

Although, this scanf does eliminate the dashes in the


preceding input, its possible that the date could be
entered as
10/11/1999

In this case, the preceding scanf would not eliminate


the unnecessary characters.
For this reason, scanf provides the assignment
suppression character *.
83

Formatted Input and Output


scanf()
The assignment suppression character enables scanf to read any
type of data from the input and discard it without assigning it to a
variable.
The next program uses the assignment suppression character in
the %c conversion specifier to indicate that a character appearing
in the input stream should be read and discarded.
Only the month, day and year are stored.
The values of the variables are printed to demonstrate that they
are in fact input correctly.
The argument lists for each scanf call do not contain variables
for the conversion specifiers that use the assignment suppression
character.
The corresponding characters are simply discarded.
84

85

Formatted Input and Output


scanf() Summary Notes
With the exception of the character format code (%c),
scanf() ignores leading whitespace.
If scanf() encounters an invalid character when it is trying
to convert the input to the stored data type, the scanning
stops.
A common example of this problem: encountering a nonnumeric character when reading a number.

Any character in the format string other than a whitespace


or a conversion specifier must be matched by the user
during input.
If the input stream does not match the characters in the format
string, scanf() stops.
86

Conversion Specifiers
printf() and scanf()

87

Data Types in C
Pointers
Pointers are variables whose values are memory
addresses (Deitel, 277).
Normally, a variable directly contains a specific value.
A pointer, on the other hand, contains an address of a variable
that contains a specific value.

In this sense, a variable name directly references a value,


and a pointer indirectly references a value.
Referencing a value through a pointer is called
indirection.

88

Data Types in C
Pointers
Pointers, like all variables, must be defined before they can
be used.
We define a pointer by using an asterisk before the variable
name. Thus, the definition:
int *countPtr, count;

specifies that variable countPtr is of type int * (i.e., a


pointer to an integer).
Note that the * only applies to countPtr in this definition. The
variable count is still just a plain integer.

When * is used in this manner in a definition, it indicates


that the variable being defined is a pointer.
Pointers can be defined to point to objects of any type.
89

Data Types in C
Pointers
Pointers are variables whose values are memory
addresses.
int count;
int *countPtr = &count:

90

Data Types in C
Pointers
The &, or address operator, is a unary operator that returns
the address of its operand.
For example, assuming the definitions
int y = 5;
int *yPtr;

the statement
yPtr = &y;

assigns the address of the variable y to pointer variable


yPtr.
Variable yPtr is then said to point to y.
The next slide shows a schematic representation of memory
after the preceding assignment is executed.
91

Data Types in C
Pointers
Again, assuming the definitions
int y = 5;
int *yPtr;

the statement
yPtr = &y;

assigns the address of the variable y to pointer variable


yPtr (Deitel).

92

Data Types in C
Pointers
Lets assume that the integer variable y is stored at
location 600000, and the pointer variable yPtr is
stored at location 500000.
Then the result of the statement:
yPtr = &y;

is:

93

Data Types in C
Pointers
When the unary * operator, commonly referred to as
the indirection operator or dereferencing operator, is
applied to a variable outside of a declaration, it
returns the value of the object to which its operand
(i.e., a pointer) points.
For example, the statement
printf( "%d", *yPtr );
prints the value of variable y, namely 5.
Using * in this manner is called dereferencing a
pointer.
94

Data Types in C
Pointers
The next figure demonstrates the pointer operators & and *.
The printf conversion specifier %p outputs the memory
location as a hexadecimal integer on most platforms.
Notice that the address of a and the value of aPtr are
identical in the output, thus confirming that the address of a
is indeed assigned to the pointer variable aPtr (line 11).
The & and * operators are complements of one another
when theyre both applied consecutively to aPtr in either
order (line 21), the same result is printed.

95

96

Data Types in C
Pointers

97

98

Data Types in C
Pointers -- Summary
Pointers are declared using the asterisk (*) in a
declaration:
int *xPtr;
float *fltPtr;

Pointers are dereferenced using the asterisk (*) on


an existing variable in an expression:
printf (%d, *xPtr);

The ampersand (&) is used to return the address of


an existing variable:
int z;
xPtr = &z;
99

Вам также может понравиться