basic data types in c“λώσσα c... · array of instances of structs assignment member of an...

31
Basic Data types in C Τι πρέπει να ξέρετε ήδη και αν όχι, χρειάζεται να το μάθετε int char float double

Upload: others

Post on 20-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Basic Data types in C Τι πρέπει να ξέρετε ήδη

και αν όχι, χρειάζεται να το μάθετε

• int

• char

• float

• double

Page 2: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Variable declaration in C:

int x, y, z; Example:

Variable assignment:

x = 35; Example:

type variables / instances

value

Page 3: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Boolean

•Has two values, true and false.

•In C we use integers as Booleans.

•0 represents false.

•Any !=0 integer represents true.

•Έχετε δοκιμάσει x==-1;

Page 4: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Arrays (1D)

•All the elements are of the same type.

•An element: array1D[index]

•In C, the first element has index 0 (zero).

array1D:

0 1 N - 1

Page 5: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Strings : array of characters

char name[30]; Example:

Unlike other arrays,

strings require an end-of-string character : ‘\0’

max length

including ‘\0’

Page 6: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Multidimensional Arrays

•All the elements are of the same type.

•An element: array2D[row][column]

array2D:

Page 7: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

#include <stdio.h>

int leapYear(int year)

{

if ((year % 4 == 0 && year % 100 != 0))

|| (year % 400 == 0) )

{

return 1;

}

else

{

return 0;

}

}

Example 1:

Page 8: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

#include <stdio.h>

int leapYear(int year)

{

if ((year % 4 == 0 && year % 100 != 0))

|| (year % 400 == 0) )

{

return 1;

}

else

{

return 0;

}

}

File inclusion header

Page 9: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

#include <stdio.h>

int leapYear(int year)

{

if ((year % 4 == 0 && year % 100 != 0))

|| (year % 400 == 0) )

{

return 1;

}

else

{

return 0;

}

}

Function definition

Page 10: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Function name #include <stdio.h>

int leapYear(int year)

{

if ((year % 4 == 0 && year % 100 != 0))

|| (year % 400 == 0) )

{

return 1;

}

else

{

return 0;

}

}

Page 11: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Must be compatible with the function’s return type

Function return type

#include <stdio.h>

int leapYear(int year)

{

if ((year % 4 == 0 && year % 100 != 0))

|| (year % 400 == 0) )

{

return 1;

}

else

{

return 0;

}

}

Page 12: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

#include <stdio.h>

int leapYear(int year)

{

if ((year % 4 == 0 && year % 100 != 0))

|| (year % 400 == 0) )

{

return 1;

}

else

{

return 0;

}

}

Parameter type

Function parameter

Page 13: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

#include <stdio.h>

int leapYear(int year)

{

if ((year % 4 == 0 && year % 100 != 0))

|| (year % 400 == 0) )

{

return 1;

}

else

{

return 0;

}

}

Example 1

Page 14: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

int dayTable[2][13] = {

{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},

{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

};

int dayOfYear(int year, int month, int day)

{

int i;

int isLeap = leapYear(year);

for (i = 1; i < month; i++) {

day += dayTable[isLeap][i];

}

return day;

}

Example 2

Page 15: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Global variable

Local variable

int dayTable[2][13] = {

{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},

{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

};

int dayOfYear(int year, int month, int day)

{

int i;

int isLeap = leapYear(year);

for (i = 1; i < month; i++) {

day += dayTable[isLeap][i];

}

return day;

}

Page 16: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

2-dimensional array of int

int dayTable[2][13] = {

{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},

{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

};

int dayOfYear(int year, int month, int day)

{

int i;

int isLeap = leapYear(year);

for (i = 1; i < month; i++) {

day += dayTable[isLeap][i];

}

return day;

}

Page 17: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

int dayTable[2][13] = {

{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},

{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

};

int dayOfYear(int year, int month, int day)

{

int i;

int isLeap = leapYear(year);

for (i = 1; i < month; i++) {

day += dayTable[isLeap][i];

}

return day;

}

Page 18: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

int dayTable[2][13] = {

{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},

{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

};

int dayOfYear(int year, int month, int day)

{

int i;

int isLeap = leapYear(year);

for (i = 1; i < month; i++) {

day += dayTable[isLeap][i];

}

return day;

}

Example 2

Page 19: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

main()

{

int year, month, day;

printf(“Enter year, month and day: ”);

scanf(“%d %d %d”, &year, &month, &day);

day = dayOfYear(year, month, day);

printf(“\nDay of Year = %d\n”, day);

}

Example 2 (main)

Page 20: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

main()

{

int year, month, day;

printf(“Enter year, month and day: ”);

scanf(“%d %d %d”, &year, &month, &day);

day = dayOfYear(year, month, day);

printf(“\nDay of Year = %d\n”, day);

}

Function call

Page 21: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Struct (δομή)

•Members may have different types.

structure.member1

•structs are also known as “records” (εγγραφές) and members as

“fields”(πεδία)

Structure Name

member3

member4

member2

member1 (μέλος)

Page 22: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

typedef

•Gives a new name to a type that has already

been defined.

•E.g.

typedef int Boolean;

•Saves typing struct SomeRec everywhere.

Page 23: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

#include <stdio.h>

#define MAXNAME 80

struct StudentRec

{

char name[MAXNAME];

int mark;

};

typedef struct StudentRec Student;

Example 3:

Page 24: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Macro substitution

#include <stdio.h>

#define MAXNAME 80

struct StudentRec

{

char name[MAXNAME];

int mark;

};

typedef struct StudentRec Student;

Page 25: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

#include <stdio.h>

#define MAXNAME 80

struct StudentRec

{

char name[MAXNAME];

int mark;

};

typedef struct StudentRec Student;

Structure declaration

Page 26: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Structure name / tag

Members

Don’t forget this!

#include <stdio.h>

#define MAXNAME 80

struct StudentRec

{

char name[MAXNAME];

int mark;

};

typedef struct StudentRec Student;

Page 27: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Data type

New type name

#include <stdio.h>

#define MAXNAME 80

struct StudentRec

{

char name[MAXNAME];

int mark;

};

typedef struct StudentRec Student;

Page 28: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Student readStudent(void)

{ Student next;

scanf(“%s %d”, next.name, &next.mark);

return next;

}

/* next.name without & */

void printStudent(const Student student)

{

printf(“%s %d\n”, student.name, student.mark);

}

Page 29: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

An instance of the struct

A member of a struct variable “Package”

Student readStudent(void)

{ Student next;

scanf(“%s %d”, next.name, &next.mark);

return next;

}

void printStudent(const Student student)

{

printf(“%s %d\n”, student.name, student.mark);

}

Page 30: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

#define MAXCLASS 100

main()

{

Student class[MAXCLASS];

int n, i, best = 0;

printf(“Enter number of students: ”);

scanf(“%d”, &n);

for (i = 0; i < n; i++) {

class[i] = readStudent();

if (class[best].mark < class[i].mark) {

best = i;

}

}

printf(“Best student is: ”);

printStudent(class[best]);

}

Page 31: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best

Array of instances of structs

Assignment

Member of an array element

#define MAXCLASS 100

main()

{

Student class[MAXCLASS];

int n, i, best = 0;

printf(“Enter number of students: ”);

scanf(“%d”, &n);

for (i = 0; i < n; i++) {

class[i] = readStudent();

if (class[best].mark < class[i].mark) {

best = i;

}

}

printf(“Best student is: ”);

printStudent(class[best]);

}