Initial commit

This commit is contained in:
2021-10-01 20:24:05 +02:00
commit 860c025165
194 changed files with 4846 additions and 0 deletions

9
kapitel5/agg.c Normal file
View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int main(void)
{
enum mangd { par = 2, dussin = 12, tjog = 20,
skock = 60 };
printf("Kalle beställer %d ägg\n", dussin);
return 0;
}

View File

@@ -0,0 +1,8 @@
#include <stdio.h>
int main(void)
{
printf("9 / 2 = %d\n", 9/2);
printf("9 / 2 = %f\n", (float)9/2);
return 0;
}

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int main(void)
{
int x = 50;
int y = 9;
printf("%d / %d = %f\n", x, y, x/(float)y);
return 0;
}

20
kapitel5/aritmetik1.c Normal file
View File

@@ -0,0 +1,20 @@
#include <stdio.h>
int main(void)
{
int a = 5;
float b = 7.5;
int c;
printf("Heltalet: %d\n", a);
printf("Flyttalet: %f\n", b);
/* Aritmetik med heltal och flyttal */
printf("%d + %f = %f\n", a, b, (a+b));
/* Se upp, här skalas decimaltecknet bort! */
c = (a+b);
printf("%d + %f = %d\n", a, b, c);
return 0;
}

8
kapitel5/aritmetik2.c Normal file
View File

@@ -0,0 +1,8 @@
#include <stdio.h>
int main(void)
{
printf("9 / 2 = %d\n", 9/2);
printf("9 / 2 = %f\n", 9.0/2);
return 0;
}

10
kapitel5/bytes.c Normal file
View File

@@ -0,0 +1,10 @@
#include <stdio.h>
int main(void)
{
printf("En char: %lu byte\n", sizeof(char));
printf("En vanlig int: %lu byte\n", sizeof(int));
printf("En long int: %lu byte\n", sizeof(long int));
printf("En short int: %lu byte\n", sizeof(short int));
return 0;
}

14
kapitel5/bytes_cast.c Normal file
View File

@@ -0,0 +1,14 @@
#include <stdio.h>
int main(void)
{
printf("En char: %lu byte\n",
(unsigned long int)sizeof(char));
printf("En vanlig int: %lu byte\n",
(unsigned long int)sizeof(int));
printf("En long int: %lu byte\n",
(unsigned long int)sizeof(long int));
printf("En short int: %lu byte\n",
(unsigned long int)sizeof(short int));
return 0;
}

10
kapitel5/bytes_zu.c Normal file
View File

@@ -0,0 +1,10 @@
#include <stdio.h>
int main(void)
{
printf("En char: %zu byte\n", sizeof(char));
printf("En vanlig int: %zu byte\n", sizeof(int));
printf("En long int: %zu byte\n", sizeof(long int));
printf("En short int: %zu byte\n", sizeof(short int));
return 0;
}

22
kapitel5/casting-ex1.c Normal file
View File

@@ -0,0 +1,22 @@
#include <stdio.h>
int main(void)
{
char c = 'A';
float x = 3.14;
float y = 3.999;
int z = 45;
long int lx = 922337203685477580;
long int ly = 345;
int ii = 429496729;
printf("Casta float %f till int: %d\n", x, (int)x);
printf("Casta float %f till int: %d\n", y, (int)y);
printf("Casta int %d till float: %f\n", z, (float)z);
printf("Casta char %c till int: %d\n", c, (int)c);
printf("Casta long int %li till int: %d\n", lx, (int)lx);
printf("Casta long int %li till int: %d\n", ly, (int)ly);
printf("Casta int %d till short int: %d\n", ii, (short int)ii);
return 0;
}

14
kapitel5/charint.c Normal file
View File

@@ -0,0 +1,14 @@
#include <stdio.h>
int main(void)
{
char tecken = 'A';
int x = 66;
int annat_tecken = 'C';
printf("%c = %d\n", tecken, tecken);
printf("%c = %d\n", x, x);
printf("%c = %d\n", annat_tecken, annat_tecken);
return 0;
}

9
kapitel5/const-int.c Normal file
View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int main(void)
{
const int a = 10;
a = 15;
printf("a = %d\n", a);
return 0;
}

9
kapitel5/flt.c Normal file
View File

@@ -0,0 +1,9 @@
#include <stdio.h>
#include <float.h>
int main(void)
{
printf("Minsta möjliga float: %e\n", FLT_MIN);
printf("Största möjliga float: %e\n", FLT_MAX);
return 0;
}

11
kapitel5/fmodtest.c Normal file
View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#include <math.h>
int main(void)
{
float taljare = 19.5;
float namnare = 4.3;
printf("fmod(%0.1f, %0.1f) = %0.1f\n", taljare, namnare,
fmod(taljare,namnare));
return 0;
}

27
kapitel5/if-else-ex1.c Normal file
View File

@@ -0,0 +1,27 @@
#include <stdio.h>
int main(void)
{
int a = 5;
int b = 10;
if (a < b)
{
printf ("%d är mindre än %d\n", a, b);
printf ("Uttrycket (%d < %d) ger talet: %d\n", a, b, (a<b));
}
else /* Körs om (a < b) INTE är sant */
{
printf ("%d är större än %d\n", a, b);
}
/* Fristående if-satser */
if (22)
printf("22 är 'sant'\n");
if (0)
printf("0 är däremot 'inte sant', och denna rad "
"körs aldrig\n");
if (-2)
printf("Även negativa tal är 'sanna'\n");
return 0;
}

15
kapitel5/int-falt.c Normal file
View File

@@ -0,0 +1,15 @@
#include <stdio.h>
int main(void)
{
int a[8] = {1, 56, 39, 12, 9};
printf("Objekt 0 = %d\n", a[0]);
printf("Objekt 1 = %d\n", a[1]);
printf("Objekt 2 = %d\n", a[2]);
printf("Objekt 3 = %d\n", a[3]);
printf("Objekt 4 = %d\n", a[4]);
printf("Objekt 5 = %d\n", a[5]);
printf("Objekt 6 = %d\n", a[6]);
printf("Objekt 7 = %d\n", a[7]);
return 0;
}

10
kapitel5/konstant-tal.c Normal file
View File

@@ -0,0 +1,10 @@
#include <stdio.h>
#define TAL 5
int main(void)
{
#define TAL 1
printf("Talet är: %d\n", TAL);
return 0;
}

11
kapitel5/konstanter.c Normal file
View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#define ETT_TAL 15
int main(void)
{
const int annat_tal = 25;
printf ("%d\n", ETT_TAL);
printf ("%d\n", annat_tal);
return 0;
}

24
kapitel5/lager_v1.c Normal file
View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include <string.h>
int main(void)
{
struct artikel
{
char namn[50];
int antal;
float pris;
};
struct artikel lager;
strcpy(lager.namn, "Skruv");
lager.antal = 12;
lager.pris = 1.5;
printf("Följande artiklar finns i lager\n\n");
printf("Namn: %s\tAntal på lager: %d\tPris: %.2f\n",
lager.namn, lager.antal, lager.pris);
return 0;
}

38
kapitel5/lager_v2.c Normal file
View File

@@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#define ARTIKLAR 3
int main(void)
{
struct artikel
{
char namn[50];
int antal;
float pris;
};
struct artikel lager[ARTIKLAR];
int i;
strcpy(lager[0].namn, "Skruv");
lager[0].antal = 12;
lager[0].pris = 1.5;
strcpy(lager[1].namn, "Hammare");
lager[1].antal = 2;
lager[1].pris = 159;
strcpy(lager[2].namn, "Skruvmejsel");
lager[2].antal = 5;
lager[2].pris = 79.90;
printf("Följande artiklar finns i lager\n");
printf("-------------------------------\n\n");
for (i = 0; i<ARTIKLAR; i++)
{
printf("Namn: %-10s\tAntal på lager: %d\tPris: %.2f\n",
lager[i].namn, lager[i].antal, lager[i].pris);
}
return 0;
}

38
kapitel5/lager_v3.c Normal file
View File

@@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#define ARTIKLAR 3
int main(void)
{
typedef struct artikel
{
char namn[50];
int antal;
float pris;
}ART;
ART lager[ARTIKLAR];
int i;
strcpy(lager[0].namn, "Skruv");
lager[0].antal = 12;
lager[0].pris = 1.5;
strcpy(lager[1].namn, "Hammare");
lager[1].antal = 2;
lager[1].pris = 159;
strcpy(lager[2].namn, "Skruvmejsel");
lager[2].antal = 5;
lager[2].pris = 79.90;
printf("Följande artiklar finns i lager\n");
printf("-------------------------------\n\n");
for (i = 0; i<ARTIKLAR; i++)
{
printf("Namn: %-10s\tAntal på lager: %d\tPris: %.2f\n",
lager[i].namn, lager[i].antal, lager[i].pris);
}
return 0;
}

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int main(void)
{
printf("%d\n", 7+8*3);
printf("%d\n", (7+8)*3);
return 0;
}

9
kapitel5/max_int.c Normal file
View File

@@ -0,0 +1,9 @@
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("Maximal signed int: %d\n", INT_MAX);
printf("Maximal unsigned int: %u\n", UINT_MAX);
return 0;
}

12
kapitel5/modulo-ex1.c Normal file
View File

@@ -0,0 +1,12 @@
#include <stdio.h>
int main(void)
{
printf("%d / %d = %d\n", 3, 2, 3/2);
printf("%d %% %d = %d\n\n", 3, 2, 3%2);
printf("%d / %d = %d\n", 13, 4, 13/4);
printf("%d %% %d = %d\n", 13, 4, 13%4);
return 0;
}

28
kapitel5/struct-test.c Normal file
View File

@@ -0,0 +1,28 @@
#include <stdio.h>
int main(void)
{
struct test1
{
short int a; /* 2 bytes */
/* 2 bytes utfyllnad (2+2=4) */
int b; /* 4 bytes (4) */
char c; /* 1 byte */
char d; /* 1 byte */
/* 2 bytes utfyllnad (1+1+2=4) */
};
struct test2
{
int b; /* 4 bytes (4) */
char c; /* 1 bytes */
char d; /* 1 bytes */
short int a; /* 2 bytes (1+1+2=4) */
};
printf("Storleken av test1: %lu bytes\n",
(long unsigned int)sizeof(struct test1));
printf("Storleken av test2: %lu bytes\n",
(long unsigned int)sizeof(struct test2));
return 0;
}

27
kapitel5/type-demo.c Normal file
View File

@@ -0,0 +1,27 @@
#include <stdio.h>
#include <string.h>
int main(void)
{
typedef char text[50]; /* Nytt alias 'text' för char[50] */
typedef char tiny; /* Nytt alias 'tiny' för char */
text namn;
text adress;
text ort = "Ankeborg";
tiny knattarna = 3;
printf("Vad heter du? ");
fgets(namn, sizeof(text), stdin);
printf("Vilken gatuadress bor du på? ");
fgets(adress, sizeof(text), stdin);
namn[strcspn(namn, "\n")] = '\0';
adress[strcspn(adress, "\n")] = '\0';
printf("En 'text' är %lu tecken stor.\n",
(unsigned long int)sizeof(text));
printf("Hej %s, från %s %s.\n", namn, adress, ort);
printf("Knattarna är %d st.\n", knattarna);
return 0;
}

23
kapitel5/uniontest.c Normal file
View File

@@ -0,0 +1,23 @@
#include <stdio.h>
#include <string.h>
int main(void)
{
union myUnion /* Samma syntax som för struct */
{
int x;
float y;
char z[20];
} unionTest; /* Skapar en variabel unionTest ur myUnion */
printf("Storleken är %lu bytes\n",
(unsigned long int)sizeof(unionTest));
/* Endast unionTest.z nedan kommer innehålla ett riktigt värde */
unionTest.x = 500;
unionTest.y = 3.14;
strcpy(unionTest.z, "Hej hopp!");
printf("x = %d\ny = %f\nz = %s\n", unionTest.x, unionTest.y,
unionTest.z);
return 0;
}

25
kapitel5/uniontest_v2.c Normal file
View File

@@ -0,0 +1,25 @@
#include <stdio.h>
#include <string.h>
int main(void)
{
union myUnion /* Samma syntax som för struct */
{
int x;
float y;
char z[20];
} unionTest; /* Skapar en variabel unionTest ur myUnion */
printf("Storleken är %lu bytes\n",
(long unsigned int)sizeof(unionTest));
/* Här arbetar vi med en medlem i taget, helt korrekt */
unionTest.x = 500;
printf("x = %d\n", unionTest.x);
unionTest.y = 3.14;
printf("y = %f\n", unionTest.y);
strcpy(unionTest.z, "Hej hopp!");
printf("z = %s\n", unionTest.z);
return 0;
}

13
kapitel5/unsignedtest.c Normal file
View File

@@ -0,0 +1,13 @@
#include <stdio.h>
#include <unistd.h>
int main(void)
{
unsigned int i;
for (i = 10; i >= 0; i--)
{
printf("%u\n", i);
sleep(1);
}
return 0;
}

20
kapitel5/veckodagar.c Normal file
View File

@@ -0,0 +1,20 @@
#include <stdio.h>
int main(void)
{
int idag;
enum veckodag { mandag = 1, tisdag, onsdag,
torsdag, fredag, lordag, sondag };
idag = onsdag;
if ( idag == lordag || idag == sondag )
{
printf("Idag är det helg\n");
}
else
{
printf("Idag är det vardag\n");
}
return 0;
}