




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Preamble: #define and typedef. □ C allows us to define our own constants and type names to help make code more readable. #define TERMS 3. #define FALL 0.
Typology: Slides
1 / 36
This page cannot be seen from the preview
Don't miss anything!





























CSSE 120—Rose Hulman Institute of Technology
C allows us to define our own constants and typenames to help make code more readable #define
TERMS 3
#define
FALL 0
#define
WINTER 1
#define
SPRING 2
typedef int
coinValue;
coinValue quarter = 25, dime
= 10;
How could we make our own bool type?
Q1-Q
For more info, see Kochan,p. 299-303 (#define),p. 325-327 (typedef)
struct <optional_tag_name> {
<type_1> <fieldname_1> ;<type_2> <fieldname_2> ;
<type_n> <fieldname_n> ; };
This says that each variable of this
struct
type has
all these fields, with the specified types
But structs are best declared in conjunction with typedef
, as on on next slide…
Declare the type:
typedef struct
{
int
year;
double
gpa;
}
Student;
Make and print a student's info:
Student
myStudent;
myStudent.gpa
=
3.4;
myStudent.year
=
2010;)
printf(
"[%s
%d
%4.2lf]\n“
,s.year,s.gpa);
Q
before main
int main(void) {
Point
myPoint;
myPoint.x =
3;
myPoint.y =
4;
printf("myPoint.x = %d
myPoint.y =
%d\n“
,myPoint.x,myPoint.y);
return EXIT_SUCCESS;
}
Student juan;juan.year
= 2008;
juan.gpa = 3.2; Student
makeStudent(int
year, double
gpa)
{
Student
stu;
stu.year
=
year;
stu.gpa
= gpa;
return
stu;
}
Shorter: Student juan = {2008, 3.2}; (Only allowed when declaring and initializing
variable together in a single statement.)
typedef
struct
{
char
*name;
int
year;
double
gpa;
}
Student;
Point makePoint(int
xx,
int
yy)
It receives two int parameters and returns a Point
Call
makePoint
Store the result into a new Point called myPoint
the values of
x
and
y
Grouping code into separate files for the purposes oforganization, reusability, and extensibility
Header files^
.h file extension
Other .c files will #include your header file
For publically available functions, types, #defines, etc.
Source files^
.c file extension
The actually C code implementations of functions, etc.
Needs to #include .h files to use functions that are notwritten in this file
The
.c
and
.h
file with the same name are called
collectively a
module
Our example:^
PointOperations.c
PointOperations.h
Let’s create this module together in Eclipse^
Right-click src folder
New
Header File
Call the file PointOperations.h
Right-click src folder
New
Source file
Call the file PointOperations.c
main.c
and
PointOperations.c
need to know about
PointOperations.h
Add #includes into both files, like this:^
#include “PointOperations.h”
Additionally
main.c
needs to know about the
makePoint
function (currently only in private
.c
file)
Add this function prototype to
PointOperations.h
Point
makePoint(int xx, int yy);
The compiler automatically knows that theimplementation of the function is within the .c file ofthis module
Any .c file that #includes “PointOperations.h” cannow call that function (it’s publically available)
makePoint(int xx, int yy)
Point result;result.x = xx;result.y = yy; return result;
}
<stdio.h>
#include
<stdlib.h>
#include
"PointOperations.h"
int main(void) {
Point
myPoint = makePoint(3,5);
printf("myPoint.x = %d
myPoint.y =
%d\n",myPoint.x,myPoint.y);return EXIT_SUCCESS;
}