// Basic demonstration of nested for loops.
#include <stdio.h>
int main(int argc, char* argv[])
{
// Outer loop will run until i equals 3
// (three times if starting at 0)
for (int i=0; i<3; ++i)
{
printf("\ti = %d\n", i);
// Nested loop will until j equals 6
// (three times if starting at 9)
for (int j=9; j>6; --j)
printf("\t\tj = %d\n", j);
}
return 0;
}
==150== Memcheck, a memory error detector
==150== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward
et al.
==150== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==150== Command: ./nested
==150==
i = 0
j = 9
j = 8
j = 7
i = 1
j = 9
j = 8
j = 7
i = 2
j = 9
j = 8
j = 7
==150==
==150== HEAP SUMMARY:
==150== in use at exit: 0 bytes in 0 blocks
==150== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==150==
==150== All heap blocks were freed -- no leaks are possible
==150==
==150== For lists of detected and suppressed errors, rerun with:
-s
==150== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
View my playlist of Commodore 64 games videos. They are all several minutes in length, and show enough of each game to serve as a preview for those who are looking for retro games to try.
https://github.com/pereiradaniel/beginning_c/blob/master/ch2/init.c // init.c // Daniele Grech Pereira, 02 November 2022 // Demonstration of variables before and after initialization. #include <stdio.h> #define LENGTH(a) sizeof(a) / sizeof(a[0]) void display(char* string, int array[], int size); int main(int argc, char* argv[]) { int num[10] = {1,2,3,4,5,6,7,8,9,10}; // Simultaneously declare and initialize a variable. int num2[10]= {1,2,3}; // First three are specified, the rest set to 0. int num3[10]= {0}; // Easy way to initialize all array values with 0. int arr[10]; // Declare an array of ints without initializing. // Printing the uninitialized array int arr[] will display garbage and generate warnings // from valgrind. display("Printing int num[] which has been initialized with data:", num, LENGTH(num)); display("Printing int num2[] which has been initialized with...
The char type in C is an integer which gets decoded as a character. You can print the char's numerical type. https://github.com/pereiradaniel/beginning_c/blob/master/ch2/char.c // Chars // Daniel Pereira, 16 November 2022. // Display a char and it's numerical value. #include <stdio.h> int main(int argv, char* argc[]) { char c = 0; printf("Enter a character: \n"); scanf(" %c", &c); printf("The character: %c\nIt's numerical value: %d\n", c, c); return 0; } https://github.com/pereiradaniel/beginning_c/blob/master/ch2/char.txt ==87== Memcheck, a memory error detector ==87== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==87== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==87== Command: ./char ==87== Enter a character: X The character: X It's numerical value: 88 ==87== ==87== HEAP SUMMARY: ==87== in use at exit: 0 bytes in 0 blocks ==87== total heap usage: 2 al...