STRUCTS, TYPEDEF, #DEFINE, AND USING C MODULES, Slides of Software Engineering

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

2022/2023

Uploaded on 03/01/2023

koss
koss 🇺🇸

4.8

(16)

242 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSSE 120—Rose Hulman Institute of Technology
STRUCTS, TYPEDEF, #DEFINE,
AND USING C MODULES
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download STRUCTS, TYPEDEF, #DEFINE, AND USING C MODULES and more Slides Software Engineering in PDF only on Docsity!

CSSE 120—Rose Hulman Institute of Technology

STRUCTS, TYPEDEF, #DEFINE,AND USING C MODULES

Preamble: #define and typedef 

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 syntax 

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);

Example: Student struct type

Q

Together let’s make a Point type Type this in after the #includes but

before main

typedef

struct

int

x;

int

y;

Point;

Together let’s make a Point Type

this

in

within

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;

}

Initializing a struct

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;

makePoint



Write code for makePoint:



Point makePoint(int

xx,

int

yy)



It receives two int parameters and returns a Point



From within the

main

function:



Call

makePoint



Store the result into a new Point called myPoint 

print

the values of

x

and

y

C Modules 

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

Making Modules 

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

Adding the wiring 

main.c

and

PointOperations.c

need to know about

PointOperations.h



Add #includes into both files, like this:^ 

#include “PointOperations.h”

Function prototypes in the .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)

PointOperations.c #include "PointOperations.h" Point

makePoint(int xx, int yy)

Point result;result.x = xx;result.y = yy; return result;

}

main.c #include

<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;

}