Moved cond.c to folder LC21

This commit is contained in:
Louise Brown 2023-09-26 17:05:15 +01:00
parent fefda4652a
commit 3ed2a4276c
4 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <assert.h>
int main()
{
int x = 7;
/* Some big code in between and let's say x
       is accidentally changed to 9 */
x = 9;
// Programmer assumes x to be 7 in rest of the code
assert(x==7);
/* Rest of the code */
return 0;
}

View File

@ -0,0 +1,45 @@
#include<stdio.h>
#define BLOCKSIZE (8)
int main(void)
{
int i = 0;
int limit = 19; /* could be anything */
int blocklimit;
/* The limit may not be divisible by BLOCKSIZE,
* go as near as we can first, then tidy up.
*/
blocklimit = (limit / BLOCKSIZE) * BLOCKSIZE;
/* unroll the loop in blocks of 8 */
while( i < blocklimit )
{
printf("process(%d)\n", i);
printf("process(%d)\n", i+1);
printf("process(%d)\n", i+2);
printf("process(%d)\n", i+3);
printf("process(%d)\n", i+4);
printf("process(%d)\n", i+5);
printf("process(%d)\n", i+6);
printf("process(%d)\n", i+7);
/* update the counter */
i += 8;
}
if( i < limit )
{
/* Jump into the case at the place that will allow
* us to finish off the appropriate number of items. */
switch( limit - i )
{
case 7 : printf("process(%d)\n", i); i++;
case 6 : printf("process(%d)\n", i); i++;
case 5 : printf("process(%d)\n", i); i++;
case 4 : printf("process(%d)\n", i); i++;
case 3 : printf("process(%d)\n", i); i++;
case 2 : printf("process(%d)\n", i); i++;
case 1 : printf("process(%d)\n", i);
}
}
}

View File

@ -0,0 +1,11 @@
# define NDEBUG
#include <stdio.h>
# include <assert.h>
int main()
{
int x = 7;
assert (x==5);
printf("x = %d\n", x);
return 0;
}