Posted: . At: 10:46 AM. This was 4 years ago. Post ID: 14617
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



Very useful C code for printing a set of strings in random order.


This is a simple program that prints a few strings one after another in a loop, but it is randomized. This is very interesting. This goes to show it is very easy to do something like this.

strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
 
char *unbase64(unsigned char *input, int length);
 
const char* strings[] = {"This is string number one.","This is string number two.","This is string number three.","This is string number four."};
 
int main (void) {
 
	int lifetime;
	int k;
	srand((unsigned)time(NULL));
	for (lifetime = 1; lifetime < 80; lifetime++) {
		k = rand() % 4;
		printf("%s\n", strings[k]);
	}
	return 0;
}

This is a simpler example of a loop, this is how to do a for loop in c.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdlib.h"
#include "stdio.h"
 
int main (void) {
 
    int lifetime;
    for (lifetime = 1; lifetime < 80; lifetime++) {
        printf("Wear Mask.\n");
        printf("Stay 6ft apart.\n");
        printf("Wash Hands\n");
        printf("Get tested.\n\n");
    }
    return 0;
}

Below is an example of very bad code, this is not even pseudocode, it is just bad. The example above will actually work.

An example of useless code.
An example of useless code. This would not even work properly.

But this is what is found out in the wild. But these examples should really help you out when you are starting out in programming and want to know how to create a simple for loop. This is not very hard at all.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.