info about strings
This commit is contained in:
parent
0ed9ce82e6
commit
0ac77260b3
|
@ -0,0 +1,38 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
char *text, c;
|
||||
int i = 0;
|
||||
// this returns as a pointer
|
||||
text = (char*) malloc(1 * sizeof(char));
|
||||
|
||||
printf("%p\n", text);
|
||||
|
||||
// the size function will print out 8 bytes as it is
|
||||
// calculating the size of the pointer, not the actual
|
||||
// value itself. to do that, you gotta dereference it first
|
||||
size_t size = sizeof(*text);
|
||||
printf("%d\n", *text);
|
||||
|
||||
printf("Size in bytes: %d bytes\n", (int)size);
|
||||
|
||||
while(c = getc(stdin), c != '\n') {
|
||||
text[i] = c;
|
||||
i++;
|
||||
|
||||
printf("===========================================\n");
|
||||
printf("Num of characters: %d\n", i);
|
||||
printf("Current char: %c\n", text[i]);
|
||||
printf("Address of current char: %p\n", &text[i]);
|
||||
printf("===========================================\n\n");
|
||||
char* deez = realloc(text, i * sizeof(char));
|
||||
//printf("%s", deez);
|
||||
}
|
||||
|
||||
printf("The Text: %s\n\n", text);
|
||||
|
||||
// size_t size = sizeof(*text);
|
||||
// printf("Size in bytes: %d bytes\n", (int)size);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define LOWER 0
|
||||
#define STEP 20
|
||||
#define END 200
|
||||
|
||||
int main() {
|
||||
float fahr, celsius;
|
||||
|
||||
printf("Fahrenheit\tCelsius\n");
|
||||
for(fahr = LOWER; fahr <= END; fahr += STEP) {
|
||||
celsius = (5.0/9.0) * (fahr - 32.0);
|
||||
printf("%3.1f\t\t%3.1f\n", fahr, celsius);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define LOWER 0
|
||||
#define STEP 20
|
||||
#define END 200
|
||||
|
||||
int main() {
|
||||
float fahr, celsius;
|
||||
|
||||
printf("Fahrenheit\tCelsius\n");
|
||||
for(fahr = LOWER; fahr <= END; fahr += STEP) {
|
||||
celsius = (5.0/9.0) * (fahr - 32.0);
|
||||
printf("%3.1f\t\t%3.1f\n", fahr, celsius);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
|
||||
// declaring and defining the 14 digit numbers
|
||||
int digits[] = {3, 4, 2, 1, 5, 6, 8, 3, 1, 5, 1, 5, 6, 5};
|
||||
|
||||
// declaring the variable for the total of the first, third, fifth elements and so on...
|
||||
int sum_odd;
|
||||
|
||||
printf("=====================================\n");
|
||||
printf("First set of digits\n");
|
||||
printf("=====================================\n");
|
||||
// loop through the first, third, fifth element and so on
|
||||
for(size_t i = 0; i < (sizeof(digits) / sizeof(digits[0])); i += 2) {
|
||||
// multiplying the value of each element by 2
|
||||
digits[i] = digits[i] * 2;
|
||||
printf("Element %d: %d\n", ((int)i + 1), digits[i]);
|
||||
|
||||
// adding the value of the current element to the old sum_even value
|
||||
sum_odd += digits[i];
|
||||
}
|
||||
|
||||
// declaring the variable for second, forth, sixth elements and so on...
|
||||
int sum_even;
|
||||
|
||||
printf("\n");
|
||||
printf("=====================================\n");
|
||||
printf("Second set of digits\n");
|
||||
printf("=====================================\n");
|
||||
// loop through the second, forth, sixth digits and so on
|
||||
for(size_t j = 1; j < (sizeof(digits) / sizeof(digits[0])); j += 2) {
|
||||
// multiplying the value of each element by 2
|
||||
digits[j] = digits[j] * 2;
|
||||
printf("Element %d: %d\n", ((int)j + 1), digits[j]);
|
||||
|
||||
// adding the value of the current element to the old sum_even value
|
||||
sum_even += digits[j];
|
||||
}
|
||||
|
||||
// add the sum of sum_odd and sum_even together
|
||||
int total = sum_odd + sum_even;
|
||||
// print out the value
|
||||
printf("\n");
|
||||
printf("=====================================\n");
|
||||
printf("Total: %d\n", total);
|
||||
printf("=====================================\n");
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue