Skip to content
44 changes: 44 additions & 0 deletions helloworld/H11_E1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//1. Given three integer variables, x = 512, y = 1024, and z = 2048, write a program
//to print out their left values as well as their right values.

#include <stdio.h>

main() {
int x = 512;
int y = 1024;
int z = 2048;

printf("x: address=%p, content=%d\n", &x, x);
printf("y: address=%p, content=%d\n", &y, y);
printf("z: address=%p, content=%d\n", &z, z);


//2. Write a program to update the value of the double variable flt_num from 123.45
//to 543.21 by using a double pointer.

double flt_num = 123.45;
double *ptr_flt_num = &flt_num;
*ptr_flt_num = 543.21;
printf("flt_num =%f\n", flt_num);


//3. Given a character variable ch and ch = 'A', write a program to update the value of
//ch to decimal 66 by using a pointer.

char ch = 'A';
char *ptr_ch = &ch;
*ptr_ch = 66;
printf("ch returns %c\n", ch);

//4. Given that x=5 and y=6, write a program to calculate the multiplication of the two
//integers and print out the result, which is saved in x, all in the way of indirection
//(that is, using pointers).

x = 5;
y = 6;
int *ptrx = &x;
int *ptry = &y;
*ptrx *= *ptry;
printf("x=%d\n", x);
return 0;
}
83 changes: 83 additions & 0 deletions helloworld/H12_E1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*1. Given this character array:
char array_ch[5] = {'A', 'B', 'C', 'D', 'E'};
write a program to display each element of the array on the screen.*/
#include <stdio.h>

main() {
char array_ch[5] = {'A', 'B', 'C', 'D', 'E'};
int i;
for (i=0; i<5; i++){
printf("%c", array_ch[i]);
}
printf("\n");

/*2. Rewrite the program in Exercise 1, but this time use a for loop to initialize the
character array with 'a', 'b', 'c', 'd', and 'e', and then print out the value of
each element in the array.*/

for (i=0; i<5; i++){
array_ch[i] = i + 97;
printf("%c",array_ch[i]);
}


/*3. Given this two-dimensional unsized array:
char list_ch[][2] = {
'1', 'a',
'2', 'b',
'3', 'c',
'4', 'd',
'5', 'e',
'6', 'f'};
write a program to measure the total bytes taken by the array, and then print out all
elements of the array.*/

char list_ch[][2] = {
'1', 'a',
'2', 'b',
'3', 'c',
'4', 'd',
'5', 'e',
'6', 'f'};
int total_byte = sizeof(list_ch);
int number_of_elements = total_byte / sizeof(char);
int imax = number_of_elements / 2;
for (i=0; i<imax; i++){
printf("\n");
for ( int j=0; j<2; j++){
printf("%c", list_ch[i][j]);
}
}
printf("\n");

/*4. Rewrite the program in Listing 12.5. This time put a string of characters, I like
C!, on the screen.*/

char array_ch_new[10] = {'I',' ','l','i','k','e',' ','C','!','\0'};
for (i=0; array_ch_new[i]; i++){
// for (i=0; i < 9; i++){
printf("%c", array_ch_new[i]);
}
printf("\n");

/*5. Given the following array:
double list_data[6] = {
1.12345,
2.12345,
3.12345,
4.12345,
5.12345};
use the two equivalent ways taught in this lesson to measure the total memory
space taken by the array, and then display the results on the screen.*/
double list_data[6] = {
1.12345,
2.12345,
3.12345,
4.12345,
5.12345};
total_byte = sizeof(list_data);
printf("The total memory space taken by the array by method I.: %d\n", total_byte);
total_byte = sizeof(double) * 6;
printf("The total memory space taken by the array by method II.: %d\n", total_byte);
return 0;
}
12 changes: 12 additions & 0 deletions helloworld/H12_E4_separated.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

main() {
int i;
char array_ch_new[9] = {'I',' ','l','i','k','e',' ','C','!'};
// for (i=0; array_ch_new[i]; i++){
for (i=0; i<9; i++){
printf("%c", array_ch_new[i]);
}
printf("\n");
return 0;
}
55 changes: 55 additions & 0 deletions helloworld/H13_E1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//1. Given a character array in the following statement,
//char str1[] = "This is Exercise 1.";
//write a program to copy the string from str1 to another array, called str2.
#include <stdio.h>
#include <string.h>
main() {
setbuf(stdout, NULL);
char str1[] = "This is Exercise 1.";
char str2[strlen(str1)];
strcpy (str2, str1);
printf("%s\n", str2);


//2. Write a program to measure the length of a string by evaluating the elements in a
//character array one by one until you reach the null character. To prove you get the
//right result, you can use the strlen() function to measure the same string again.

char array_ch_new[10] = {'I',' ','l','i','k','e',' ','C','!','\0'};
int i;
int length = 0;
for (i=0; array_ch_new[i]; i++) {
length ++;
}
printf("The lenght of string is %d\n", length);
length = strlen(array_ch_new);
printf("The lenght of string is %d\n", length);

//3. Rewrite the program in Listing 13.4. This time, convert all uppercase characters to
//their lowercase counterparts.


char str[80];
int delt = 'a' - 'A';
printf("Enter a string less than 80 characters:\n");
gets(str);
i = 0;
while (str[i]) {
if ((str[i] >= 'A') && (str[i] <= 'Z'))
str[i] += delt; /* convert to lower case */
++i;
}
printf("The entered string is (in lowercase):\n");
puts(str);


//4. Write a program that uses the scanf() function to read in two integers entered by
//the user, adds the two integers, and then prints out the sum on the screen.

int x = 0;
int y = 0;
printf("Enter two integer:\n");
scanf("%d %d", &x, &y);
printf("The sum of the numbers is %d", x + y);
return 0;
}
33 changes: 33 additions & 0 deletions helloworld/H14_E1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//1. Given the following:
//• a.) An int variable with block scope and temporary storage
//• b) A constant character variable with block scope
//• c) A float local variable with permanent storage
//• d) A register int variable
//• e) A char pointer initialized with a null character
//write declarations for all of them.

//a)

{
int x;
}

//b)

{
const char ch;
}

//c)

{
static float number;
}

//d)

register int i;

// e)

char *ptr_ch = 0;
36 changes: 36 additions & 0 deletions helloworld/H14_E2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//2. Rewrite the program in Listing 14.2. This time, pass the int variable x and the
//float variable y as arguments to the function_1() function. What do you get on
//your screen after running the program?

#include <stdio.h>

int x = 1234; /* program scope */
double y = 1.234567; /* program scope */

void function_1(int x, double y) {
printf("From function_1:\n x=%d, y=%f\n", x, y);
}

main() {
int x = 4321; /* block scope 1*/
function_1(x, y);
printf("Within the main block:\n x=%d, y=%f\n", x, y);
/* a nested block */
{
double y = 7.654321; /* block scope 2 */
function_1(x, y);
printf("Within the nested block:\n x=%d, y=%f\n", x, y);
}
return 0;
/* Answer:
From function_1:
x=4321, y=1.234567
Within the main block:
x=4321, y=1.234567
From function_1:
x=4321, y=7.654321
Within the nested block:
x=4321, y=7.654321
*/

}
21 changes: 21 additions & 0 deletions helloworld/H14_E3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//3. Compile and run the following program. What do you get on the screen, and why?
#include <stdio.h>
int main() {
int i;
for (i=0; i<5; i++){
int x = 0;
static int y = 0;
printf("x=%d, y=%d\n", x++, y++);
}
return 0;
/*
The screen:
x=0, y=0
x=0, y=1
x=0, y=2
x=0, y=3
x=0, y=4

x is set to 0 every time the program enters the for loop,
y has a permanent sorage, so the value saved in y is kept.*/
}
24 changes: 24 additions & 0 deletions helloworld/H14_E4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//4. Rewrite the add_two() function in Listing 14.3 to print out the previous result of
//the addition, as well as the counter value.

#include <stdio.h>
/* the add_two function */
int add_two(int x, int y) {
static int counter = 1;
static int prev_sum = 0;
printf("This is the function call of %d,\n", counter++);
printf("The previous result was %d,\n", prev_sum);
prev_sum = x + y;
return (x + y);
}
/* the main function */
main() {
int i, j;

for (i=0, j=5; i<5; i++, j--)
printf("the addition of %d and %d is %d.\n\n", i, j, add_two(i, j));
return 0;

//Azért ha őszinte vagyok, ezt a static dolgot nem értem.

}