Professional Documents
Culture Documents
1. Introduction of C:
On compiling
C is what is called a compiled language. This means that once you write your C
program, you must run it through a C compiler to turn your program into an
executable that the computer can run (execute). The C program is the human-
readable form, while the executable that comes out of the compiler is the
machine-readable and executable form. What this means is that to write and run
a C program, you must have access to a C compiler.
You can use Turbo C or Turbo C++ Compilers for this purpose.
2. History of C
3. Features of C
• Small size
• Extensive use of function calls
• Loose typing -- unlike PASCAL
• Structured language
• Low level (Bitwise) programming readily available
• Pointer implementation - extensive use of pointers for memory, array,
structures and functions.
C has now become a widely used professional language for various reasons.
4. C Program Structure
A C program basically has the following form:
Documentation Section
Link Section
Definition Section
Global Declaration Section
Main() Function Section
Declaration Part
Executable Part
}
Subprogram Section
Function 1
Function 2
…..
……..
Function n
1. Documentation Section:
This section consists of a set of comments lines giving the name of the program,
the author and other details which the programmer would like to use later.
2. Link Section:
This section provides instruction to the complier to link functions from the system
library.
3. Definition section:
4. Global Section:
Here those variables are declared which are used in more than one function.
Every program must have one main () function section It contains declaration
part and executable part.
6. Subprogram Section:
It contains all the user-defined functions that are called in the main function.
#include <stdio.h>
int main()
{
printf("This is output from my first program!\n");
return 0;
}
This C program starts with #include <stdio.h>. This line includes the "standard
I/O library" into your program. The standard I/O library lets you read input from
the keyboard (called "standard in"), write output to the screen (called "standard
out"), process text files stored on the disk, and so on. It is an extremely useful
library. C has a large number of standard libraries like stdio, including string, time
and math libraries.
The line int main() declares the main function. Every C program must have a
function named main somewhere in the code.. At run time, program execution
starts at the first line of the main function.
• In C, the { and } symbols mark the beginning and end of a block of code.
In this case, the block of code making up the main function contains two
lines.
• The printf statement in C allows you to send output to standard out (for
us, the screen). The portion in quotes is called the format string and
describes how the data is to be formatted when printed. The format string
can contain string literals such as "This is output from my first program!,"
symbols for carriage returns (\n), and operators as placeholders for
variables.
• The return 0; line causes the function to return an error code of 0 (no
error) to the shell that started execution.
5. Variables
This line says, "I want to create a space called b that is able to hold one integer
value." A variable has a name (in this case, b) and a type (in this case, int, an
integer). You can store a value in b by saying something like:
b = 5;
6. Data Types:
ANSI C Keywords:
auto break double int struct
break else long switch case
enum register typedef char extern
return union const float short
unsigned continue for signed void
default goto size of volatile do
if static while
Identifiers refers to the names of variables, functions and arrays. These are user-
defined names and consist of a sequence of letters and digits, with a letter as a
first character. Both uppercase and lowercase letters are permitted. The
underscore character is also permitted.
8. Constants:
ANSI C allows you to declare constants. When you declare a constant it is a bit
like a variable declaration except the value cannot be changed.
int const a = 1;
const int a =2;
Note:
• You can declare the const before or after the type. Choose one and stick to it.
• It is usual to initialize a const with a value as it cannot get a value any other
way.
You frequently see const declaration in function parameters. This says simply
that the function is not going to change the value of the parameter.
Integer Constants:
An integer constant refers to a sequence of digits. These are of
three types
a. Decimal
b. Octal
c. Hexadecimal
Real Constants:
These quantities are represented by numbers containing fractional
parts like 17.548. Such numbers are called real constants.
Single Char Constants:
These contains a single character enclosed within a pair of single
quote marks.
Example: ‘5’ ‘X’
String Constants:
A string constant is a sequence of characters enclosed in double
quotes. The characters may be letters, numbers, special characters
and blank space. Examples:
“Hello!” “1987” “WELL DONE”
Constants Meaning
‘\a’ Alert
‘\b’ Backspace
‘\f’ Form feed
‘\n’ New line
‘\r’ Carriage return
‘\v’ Vertical tab
‘\’’ Single quote
‘\”’ Double quote
‘\?’ Question mark
‘\\’ Backslash
Operator Meaning
* Multiplication
/ Division
% Modulo Division
Integer arithmetic:
When both the operands in a single arithmetic expression are integers, the
expression is called Integer expression, and the operation is called integer
arithmetic...
Integer arithmetic always yields an integer value.
Examples
Let a = 14, b = 4
a – b = 10
a + b = 18
a * b = 56
a / b = 3 (decimal part truncated)
a % b = 2 (remainder of division)
Real arithmetic:
An arithmetic operation involving only real operands is called real arithmetic.
The result is an approximation of correct value.
Examples:
6.0 / 7.0 = 0.857143
-2.0 / 3.0 = - 0.666667
• There are other operators available. The two arithmetic operators that are
used frequently are ++ and --. You can place these in front or on the back of
variables. ++ specifies increment, the -- specifies decrement. If the operator is
placed in front, it is prefix if it is placed behind, it is postfix. Prefix means,
increment before any operations are performed, postfix is increment
afterwards. These are important considerations when using these operators.
• C allows *= += /= -= operators. For example:
b. Relational Operators:
You probably are familiar with < and the > relational operators from mathematics.
The same principles apply in C when you are comparing two objects.
• There are six possibilities in C: <, <=, >, >=, !=, and ==. The first four a
self-explanatory, the != stands for "not equals to" and == is "equivalent to".
• Here we can point out the difference between syntax and semantics. a = b
is different from a == b. Most C compilers will allow both statements to be
used in conditionals like if, but they have two completely different
meanings. Make sure your assignment operators are where you want
them to be and your relational where you want relational comparisons!
c. Logical Operators:
• Logical operators simulate boolean algebra in C.
• A sampling of Logical/Boolean Operators: &&, ||, &, |, and ^.
Operator Meaning
|| logical OR
! logical NOT
• For example, && is used to compare two objects with AND: x != 0 && y !=
0
• Expressions involving logical operators undergo Short-Circuit
Evaluation. Take the above example into consideration. If x! = 0 evaluates
to false; the whole statement is false regardless of the outcome of y! = 0.
This can be a good thing or a bad thing depending on the context.
d. Conditional Operators:
A ternary operator pair “?:” is available in C to construct conditional expressions
of the form
exp1 ? exp2 : exp3
Working:
The exp1 is evaluated first; if it is nonzero (true) then the exp2 is evaluated and
becomes the value of the expression. If exp1 is false, exp3 is evaluated and its
value becomes the value of the expression.
Example:
a = 10;
b = 15;
x = (a>b)?a:b;
is equivalent to
if (a > b)
x = a;
else
b = b;
e. Bitwise Operators
These operators are used for testing the bits, or shifting them right or left. Bitwise
operators may not be applied to float or double.
Operator Meaning
| Bitwise OR
~ 1’s complement
^ Bitwise exclusive OR
(a + b) * c or a + (b * c)
All operators have a priority, and high priority operators are evaluated before
lower priority ones. Operators of the same priority are evaluated from left to right,
so that
a-b-c
is evaluated as
(a-b)-c
From high priority to low priority the order for all C operators is:
Operators Associativity
( [ - . Left to right
! - ++ -{- + * & (type-cast) sizeof Right to left
(in the above line, +, - and * are the unary forms)
* / % Left to right
+ - Left to right
<< >> Left to right
< <= > >= Left to right
== != Left to right
& Left to right
^ Left to right
| Left to right
&& Left to right
|| Left to right
?: Left to right
= += -= *= /= %= &= ^= |= <<= >>= Right to left
, Left to right
Thus
If the operands are of different types, the ‘lower; types is automatically converted
to the ‘higher’ type before the operation proceeds. The result is higher type.
The final result of an expression is converted to the type of the variable on the
left of the assignment sign before assigning the value to it. However, the
following changes are introduced during final assignment.
Casting a Value:
There are instances when we want to force a type conversion in a way that is
different from automatic conversion.
Example
ratio = female_number/male_number
(type-name) expression
1. Reading a Character:
getchar()
syntax is:
variable_name=getchar();
Example
char name;
name = getchar();
getch()
getche()
syntax
putchar(variable_name);
Example:
answer=’Y’;
putchar(answer);
printf()
terminated by 0
f double/float format -m.ddd...
e,E " Scientific Format
-1.23e002
g,G " e or f whichever
is most compact
% - print % character
So: printf("%-2.3f\n",17.23478);
The output on the screen is:
17.235
and:
printf("VAT=17.5%%\n");
...outputs:
VAT=17.5%
Example:
#include <stdio.h>
int main()
{
int a, b, c;
a = 5;
b = 7;
c = a + b;
printf("%d + %d = %d\n", a, b, c);
return 0;
}
The computer adds the value in a (5) to the value in b (7) to form the
result 12, and then places that new value (12) into the variable c. The
variable c is assigned the value 12. For this reason, the = in this line is
called "the assignment operator."
• The printf statement then prints the line "5 + 7 = 12." The %d
placeholders in the printf statement act as placeholders for values. There
are three %d placeholders, and at the end of the printf line there are the
three variable names: a, b and c. C matches up the first %d with a and
substitutes 5 there. It matches the second %d with b and substitutes 7. It
matches the third %d with c and substitutes 12. Then it prints the
completed line to the screen: 5 + 7 = 12. The +, the = and the spacing are
a part of the format line and get embedded automatically between the %d
operators as specified by the programmer.
Scanf()
int scanf(char *format, args....) -- reads from stdin and puts input in address of
variables specified in args list. Returns number of chars read.
scanf(``%d'',&i);
We can just give the name of an array or string to scanf since this corresponds to
the start address of the array/string.
char string[80];
scanf(``%s'',string);
The program will read in an integer value that the user enters on the keyboard
(%d is for integers, as is printf, so b must be declared as an int) and place that
value into b.
You MUST put & in front of the variable used in scanf. It is easy to forget the &
sign, and when you forget it your program will almost always crash when you run
it.
If we want to use mathematical functions like sin, cos , etc we need to include
math.h
string.h
graphics.h
math.h
stdio.h
conio.h
time.h
Unit 3
1. if statements
The if statement has the same function as other languages. It has three
basic forms:
a). Simple if
if (test expression)
{
statement
}
if (test expression)
{
statement1
}
else
{
statement2
}
if(test condition1)
if(test condition2)
{ statement-1 }
else
{ statement-2 }
else
{ statement-3 }
statement x
statement1
else if (expression)
statement2
else if
statement3……….
main()
if (x>0)
{ z=w; ........ }
else
{ z=y; ........ }
Examples:
2. The ?: operator
A ternary operator pair “?:” is available in C to construct conditional
expressions of the form
exp1 ? exp2 : exp3
Working:
The exp1 is evaluated first; if it is nonzero (true) then the exp2 is evaluated and
becomes the value of the expression. If exp1 is false, exp3 is evaluated and its
value becomes the value of the expression.
Example:
a = 10;
b = 15;
x = (a>b)?a:b;
is equivalent to
if (a > b)
x = a;
else
b = b;
3. goto statement
goto statement can transfer the control to any place in a program, it is useful to
provide branching within a loop. Another important use of goto is to exit from
deeply nested loops when an error occurs.
But it is good practice to avoid goto statements. When goto is used, many
computers generate less efficient code, morever the logic becomes more
complicated. In case any goto statement is absolutely necessary, it must be
documented.
4. Switch Statement
switch (expression) {
case item1:
statement1;
break;
case item2:
statement2;
break;
case itemn:
statementn;
break;
default:
statement;
break;
}
In each case the value of itemi must be a constant, variables are not allowed.
The break is needed if you want to terminate the switch after execution of one
choice. Otherwise the next case would get evaluated.
We can also have null statements by just including a ; or let the switch statement
fall through by omitting any statements (see e.g. below).
For example:-
switch (letter)
{
case `A':
case `E':
case `I':
case `O':
case `U':
numberofvowels++;
break;
case ` ':
numberofspaces++;
break;
default:
numberofconstants++;
break;
}
In the above example if the value of letter is `A', `E', `I', `O' or `U' then
numberofvowels is incremented.
5. for loop
For example:
int x;
main()
{
for (x=3;x>0;x-)
{
printf("x=%d\n",x);
}
}
...outputs:
x=3
x=2
x=1
All the following are legal for statements in C. The practical application of such
statements is not important here, we are just trying to illustrate peculiar features
of C for that may be useful:-
In the third example the loop will continue to iterate until z becomes 0;
6. while loop
while (expression)
statement
For example:
int x=3;
main()
while (x>0)
printf("x=%d\n",x);
x--;
...outputs:
x=3
x=2
x=1
...to the screen.
Because the while loop can accept expressions, not just conditions, the following
are all legal:-
while (x--);
while (x=x+1);
while (x+=5);
Using this type of expression, only when the result of x--, x=x+1, or x+=5,
evaluates to 0 will the while condition fail and the loop be exited.
We can go further still and perform complete operations within the while
expression:
NOTE: This type of operation is used a lot in C and not just with character
reading!! .
7. do-while loop
do
statement;
while (expression);
For example:
int x=3;
main()
{
do
{
printf("x=%d\n",x--);
}
while (x>0);
}
..outputs:-
x=3
x=2
x=1
NOTE: The postfix x-- operator which uses the current value of x while printing
and then decrements x.
while (a < b)
{
printf("%d\n", a);
a = a + 1;
}
This causes the two lines within the braces to be executed repeatedly until a is
greater than or equal to b.
do
printf("%d\n", a);
a = a + 1;
The for loop in C is simply a shorthand way of expressing a while statement. For
example, suppose you have the following code in C:
x=1;
while (x<10)
Note that the while loop contains an initialization step (x=1), a test step (x<10),
and an increment step (x++). The for loop lets you put all three parts onto one
line, but you can put anything into those three parts. For example, suppose you
have the following loop:
a=1;
b=6;
while (a < b)
a++;
printf("%d\n",a);
}
You can place this into a for statement as well:
It is slightly confusing, but it is possible. The comma operator lets you separate
several different statements in the initialization and increment sections of the for
loop (but not in the test section).
Consider the following example where we read in integer values and process
them according to the following conditions. If the value we have read is negative,
we wish to print an error message and abandon the loop. If the value read is
great than 100, we wish to ignore it and continue to the next value in the data. If
the value is zero, we wish to terminate the loop.
if (value < 0)
printf(``Illegal value\n'');
break;
printf(``Invalid value\n'');
continue;
....;
....;
BEWARE: In C Array subscripts start at 0 and end one less than the array size.
For example, in the above case valid subscripts range from 0 to 49. This is a BIG
difference between C and other languages and does require a bit of practice to
get in the right frame of mind.
thirdnumber=listofnumbers[2];
listofnumbers[5]=100;
int tableofnumbers[50][50];
int bigD[50][50][40][30]......[50];
anumber=tableofnumbers[2][3];
tableofnumbers[25][16]=100;
int a, b, c, d, e;
int a[5];
The five separate integers inside this array are accessed by an index. All arrays
start at index zero and go to n-1 in C. Thus, int a[5]; contains five elements. For
example:
int a[5];
a[0] = 12;
a[1] = 9;
a[2] = 14;
a[3] = 5;
a[4] = 1;
One of the nice things about array indexing is that you can use a loop to
manipulate the index. For example, the following code initializes all of the values
in the array to 0:
int a[5];
int i;
The following code initializes the values in the array sequentially and then prints
them out:
#include <stdio.h>
int main()
{
int a[5];
int i;
More on Arrays
Variable Types
An int is a 4-byte integer value. A float is a 4-byte floating point value. A char is a
1-byte single character (like "a" or "3"). A string is declared as an array of
characters.
10.Strings:
char name[50];
C has no string handling facilities built in and so the following are all illegal:
char firstname[50],lastname[50],fullname[100];
firstname= "Arnold"; /* Illegal */
lastname= "Schwarznegger"; /* Illegal */
fullname= "Mr"+firstname +lastname; /* Illegal */
However, there is a special library of string handling routines which we will come
across later.
In order to allow variable length strings the \0 character is used to indicate the
end of a string.
String constants are often used in making the output of code intelligible using
printf ;
printf("Hello, world\n");
printf("The value of a is: %f\n", a);
String constants can be associated with variables. C provides the char type
variable, which can contain one character--1 byte--at a time. A character string is
stored in an array of character type, one ASCII character per location. Never
forget that, since strings are conventionally terminated by the null character ``\0'',
we require one extra storage location in the array!
C does not provide any operator which manipulate entire strings at once. Strings
are manipulated either via pointers or via special routines available from the
standard string library string.h. Using character pointers is relatively easy since
the name of an array is a just a pointer to its first element.
(s and t are char*, cs and ct are const char*, c is an char converted to type int, and n is
an int.)
void main()
/* initialize string */
strcpy(line,"hello, I am a string;");
}
Unit 3
• Why should we make functions in our programs when we can just do it all
under main? Think for a minute about high-end stereo systems. These
stereo systems do not come in an all-in-one package, but rather come in
separate components: pre-amplifier, amplifier, equalizer, receiver, cd
player, tape deck, and speakers. The same concept applies to
programming. Your programs become modularized and much more
readable if they are broken down into components.
• This type of programming is known as top-down programming, because
we first analyze what needs to be broken down into components.
Functions allow us to create top-down modular programs.
• Each function consists of a name, a return type, and a possible parameter
list. This abstract definition of a function is known as it's interface. Here
are some sample function interfaces:
• char *strdup(char *s)
• int add_two_ints(int x, int y)
• void useless(void)
The first function header takes in a pointer to a string and outputs a char
pointer. The second header takes in two integers and returns an int. The
For example:
• int add_two_ints(int x, int y)
• A function can return a single value to its caller in a statement using the
keyword return. The return value must be the same type as the return type
Most languages allow you to create functions of some sort. Functions let you
chop up a long program into named sections so that the sections can be
reused throughout the program. Functions accept parameters and return a
result. C functions can accept an unlimited number of parameters. In general,
C does not care in what order you put your functions in the program, so long
as a the function name is known to the compiler before it is called.
A function may simply perform a task without returning any value, in which
case it has the following layout:
...statements...
}
Library functions:
Libraries are very important in C because the C language supports only the
most basic features that it needs. C does not even contain I/O functions to
read from the keyboard and write to the screen. Anything that extends
beyond the basic language must be written by a programmer. The resulting
chunks of code are often placed in libraries to make them easily reusable.
We have seen the standard I/O, or stdio, library already: Standard libraries
exist for standard I/O, math functions, string handling, time manipulation,
and so on. You can use libraries in your own programs to split up your
programs into modules. This makes them easier to understand, test, and
debug, and also makes it possible to reuse code from other programs that
you write.
3. Function Arguments
C functions can accept parameters of any type. For example:
int fact(int i)
{
int j,k;
j=1;
for (k=2; k<=i; k++)
j=j*k;
return j;
}
int add(i,j)
int i;
int j;
{
return i+j;
}
4. Return values
If you do not want to return a value you must use the return type void and
miss out the return statement:
void squares()
{ int loop;
for (loop=1;loop<10;loop++);
printf("%d\n",loop*loop);
}
main()
{
squares();
}
5. Nesting of function
C permits nesting of functions freely. main can call function1, which calls
function2, which calls function3, ………and so on.
Example:
main()
{
int a,b,c;
float ratio();
scanf(“%d %d %d”, &a, &b,&c);
printf(“%f”\n”, ratio(a,b,c));
}
float ratio(x,y,z)
int x,y,z;
{
if(difference(y,z))
return(x/(y-z));
else
return(0,0);
}
difference(p,q)
int p,q;
{
if(p!=q)
return 1;
else
return 0;
}
6. Recursion
factorial(int n)
{
int fact;
if(n==1)
return;
else
fact=n*factorial(n-1);
return fact;
}
7. Calling of function
The call to a function in C simply entails referencing its name with the
appropriate arguments. The C compiler checks for compatibility between the
arguments in the calling sequence and the definition of the function.
Arguments are always passed by value in C function calls. This means that
local ``copies'' of the values of the arguments are passed to the routines. Any
change made to the arguments internally in the function are made only to the
local copies of the arguments. In order to change (or define) an argument in
the argument list, this argument must be passed as an address, thereby
forcing C to change the ``real'' argument in the calling routine.
As an example, consider exchanging two numbers between variables. First
let's illustrate what happen if the variables are passed by value:
Run this code and observe that a and b are NOT exchanged! Only the copies
of the arguments are exchanged. The RIGHT way to do this is of course to
use pointers:
#include < stdio.h>
void exchange ( int *a, int *b );
void main()
{ /* RIGHT CODE */
int a, b;
a = 5;
b = 7;
printf("From main: a = %d, b = %d\n", a, b);
exchange(&a, &b);
printf("Back in main: ");
printf("a = %d, b = %d\n", a, b);
}
• You use regular variables if the function does not change the values of
those arguments
• You MUST use pointers if the function changes the values of those
arguments
If the function returns no value, You can call it with the following statement:
function_name();
You must include () in the call. If you do not, the function is not called, even
though it will compile correctly on many systems.
Here the declaration float list[] tells C that list is an array of float.
Local variables:
These are the variables which are declared inside the functions. There scope
is local to that function. They do not exist outside that function.
Example:
int sum(int a, int b)
{
int add;
add=a+b;
return add;
}
Global variables:
These are the variables which are declared outside the functions. Scope is in
all functions.
Example
int add;
void sum(int a, int b)
{ add= a+b; }
Here add is global variable
C. Type qualifiers include the keywords: const and volatile. The const qualifier
places the assigned variable in the constant data area of memory which
volatile is used less frequently and tells the compiler that this value can be
E. The auto keyword places the specified variable into the stack area of
F. The extern keyword makes the specified variable access the variable of the
same name from some other file. This is very useful for sharing variables
in modular programs.
variable in the fast register memory located directly on the CPU. Most
compilers these days (like gcc) are so smart that suggesting registers
with global variables. static can also be used with functions, making those
• Structures, or structs, are very useful in creating data structures larger and
more complex than the ones we have discussed so far. We will take a
cursory look at some more complex ones in the next section.
struct student {
char *first;
char *last;
char SSN[9];
float gpa;
char **classes;
};
struct student student_a, student_b;
struct {
char *first;
char *last;
char SSN[10];
float gpa;
char **classes;
} student_a, student_b;
As you can see, the tag immediately after struct is optional. But in the
second case, if you wanted to declare another struct later, you couldn't.
struct student_t {
char *first;
char *last;
char SSN[10];
float gpa;
char **classes;
} student, *pstudent;
• There is an easier way to define structs or you could "alias" types you
create. For example:
typedef struct {
char *first;
char *last;
char SSN[9];
float gpa;
char **classes;
} student;
student student_a;
Now we get rid of those silly struct tags. You can use typedef for non-
structs:
2. Accessing structure members
But how do we access fields inside of the structure? C has a special operator
for this called "member of" operator denoted by . (period). For example, to
assign the SSN of student_a:
strcpy(student_a.SSN, "111223333\0");
3. Nested structures
Refer to unions
4. Array of structures
agun arniesguns[1000];
This gives arniesguns a 1000 guns. This may be used in the following way:
arniesguns[50].calibre=100;
itscalibre=arniesguns[0].calibre;
assigns the calibre of Arnie's first gun to itscalibre.
5. Structure and Function
C supports the passing of structure values as argument to functions. There are
three methods by which the values of structure can be transferred from one
fuction to other.
Method1:
To pass each member of the structure as an actual argument of the function call.
The actual arguments are then treated independently like ordinary variables.
Method 2:
The second method involves passing copy of the entire structure to the called
function. Since the function is working on a copy of the structure, any changes to
structure members within the function are not reflected in the original structure. It
is therefore necessary for the function to return the entire structure back to the
calling function.
Method 3:
The third approach employs a concept called pointers to pass the structure as an
argument. In this case, the address location of the structure is passed to the
called function. The function can access indirectly the entire structure and work
on it.
Note:
a. The called function must be declared for its type, appropriate to the data
type it is expected to return. For example, if it is returning a copy of entire
structure, then it must be declared as struct with an appropriate tag name.
b. The structure variable used as the actual argument and the corresponding
formal argument in the called function must be of the same struct type.
e. The called function must be declared in the calling function for its type, if it
is placed after the calling function.
6. Union
A union is a variable which may hold (at different times) objects of different sizes
and types. C uses the union statement to create unions, for example:
union number
{
short shortnumber;
long longnumber;
double floatnumber;
} anumber
defines a union called number and an instance of it called anumber. number is a
union tag and acts in the same way as a tag for a structure.
printf("%ld\ n",anumber.longnumber);
When the C compiler is allocating memory for unions it will always reserve
enough room for the largest member (in the above example this is 8 bytes for the
double).
In order that the program can keep track of the type of union variable being used
at a given time it is common to have a structure (with union embedded in it) and
a variable which flags the union type:
An example is:
typedef struct
{ int maxpassengers;
} jet;
typedef struct
{ int liftcapacity;
} helicopter;
typedef struct
{ int maxpayload;
} cargoplane;
typedef union
{ jet jetu;
helicopter helicopteru;
cargoplane cargoplaneu;
} aircraft;
typedef struct
{ aircrafttype kind;
int speed;
aircraft description;
} an_aircraft;
This example defines a base union aircraft which may either be jet, helicopter, or
cargoplane.
• Unions are declared in the same fashion as structs, but have a fundamental
difference. Only one item within the union can be used at any time, because
the memory allocated for each item inside the union is in a shared memory
location. Why you ask? An example first:
struct conditions {
float temp;
union feels_like {
float wind_chill;
float heat_index;
}
} today;
As you know, wind_chill is only calculated when it is "cold" and heat_index
when it is "hot". There is no need for both. So when you specify the temp
in today, feels_like only has one value, either a float for wind_chill or a
• Types inside of unions are unrestricted, you can even use structs within
unions.