Basic Data types in C Τι πρέπει να ξέρετε ήδη
και αν όχι, χρειάζεται να το μάθετε
• int
• char
• float
• double
Variable declaration in C:
int x, y, z; Example:
Variable assignment:
x = 35; Example:
type variables / instances
value
Boolean
•Has two values, true and false.
•In C we use integers as Booleans.
•0 represents false.
•Any !=0 integer represents true.
•Έχετε δοκιμάσει x==-1;
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
Strings : array of characters
char name[30]; Example:
Unlike other arrays,
strings require an end-of-string character : ‘\0’
max length
including ‘\0’
Multidimensional Arrays
•All the elements are of the same type.
•An element: array2D[row][column]
array2D:
#include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
Example 1:
#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
#include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
Function definition
Function name #include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
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;
}
}
#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
#include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
Example 1
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
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;
}
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;
}
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;
}
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
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)
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
Struct (δομή)
•Members may have different types.
structure.member1
•structs are also known as “records” (εγγραφές) and members as
“fields”(πεδία)
Structure Name
member3
member4
member2
member1 (μέλος)
typedef
•Gives a new name to a type that has already
been defined.
•E.g.
typedef int Boolean;
•Saves typing struct SomeRec everywhere.
#include <stdio.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
Example 3:
Macro substitution
#include <stdio.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
#include <stdio.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
Structure declaration
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;
Data type
New type name
#include <stdio.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
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);
}
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);
}
#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]);
}
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]);
}