C 101: Foreach loop macro 
[acool@localhost ~]$ 
[acool@localhost ~]$ date
Mon Apr 3 11:17:50 AM PDT 2023
[acool@localhost ~]$
[acool@localhost ~]$ cat c-foreach-macro.c
#include <stdio.h>

#define foreach(item, array) \
for(int keep = 1, \
count = 0,\
size = sizeof (array) / sizeof *(array); \
keep && count != size; \
keep = !keep, count++) \
for(item = (array) + count; keep; keep = !keep)

int main()
{
int mynumbers[] = {5,20,35,62,8,74,89};

char mygreeting[] = {"Hello world!"};

float myfloats[] = {1.6,6.9,5.8,43.5,1.1,0.9};

foreach(int *v, mynumbers) {
printf("value: %d\n", *v);
}

foreach(char *v, mygreeting) {
printf("value: %c\n", *v);
}

foreach(float *v, myfloats) {
printf("value: %.2f\n", *v);
}

return 0;
}
[acool@localhost ~]$
[acool@localhost ~]$ gcc c-foreach-macro.c -o c-foreach-macro
[acool@localhost ~]$
[acool@localhost ~]$ ./c-foreach-macro
value: 5
value: 20
value: 35
value: 62
value: 8
value: 74
value: 89
value: H
value: e
value: l
value: l
value: o
value:
value: w
value: o
value: r
value: l
value: d
value: !
value:
value: 1.60
value: 6.90
value: 5.80
value: 43.50
value: 1.10
value: 0.90
[acool@localhost ~]$


Bonus - dumping output from preprocessor.
[acool@localhost ~]$ 
[acool@localhost ~]$ gcc c-foreach-macro.c -E -o c-macro.preprocessed
[acool@localhost ~]$
[acool@localhost ~]$ cat c-macro.preprocessed
...
int main()
{
int mynumbers[] = {5,20,35,62,8,74,89};

char mygreeting[] = {"Hello world!"};

float myfloats[] = {1.6,6.9,5.8,43.5,1.1,0.9};

for(int keep = 1, count = 0, size = sizeof (mynumbers) / sizeof *(mynumbers); keep && count != size; keep = !keep, count++) for(int *v = (mynumbers) + count; keep; keep = !keep) {
printf("value: %d\n", *v);
}

for(int keep = 1, count = 0, size = sizeof (mygreeting) / sizeof *(mygreeting); keep && count != size; keep = !keep, count++) for(char *v = (mygreeting) + count; keep; keep = !keep) {
printf("value: %c\n", *v);
}

for(int keep = 1, count = 0, size = sizeof (myfloats) / sizeof *(myfloats); keep && count != size; keep = !keep, count++) for(float *v = (myfloats) + count; keep; keep = !keep) {
printf("value: %.2f\n", *v);
}

return 0;
}
[acool@localhost ~]$


[ view entry ] ( 183 views )   |  print article
C 101: Creating Child Processes With A Loop 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

void child( int seconds );

int children = 15;

int main(void)
{
printf("Parent ID %d, Main ID %d \n", getppid(), getpid());

for(int i=0;i<children;i++)
{
if(fork()==0)
{
child(children-i); // pass seconds to sleep
}
else
{
wait(NULL);
}
}
}

void child(int seconds)
{
printf("Parent ID %d, Child ID %d, Sleeping %d seconds.\n", getppid(), getpid(), seconds);
sleep(seconds);
exit(0);
}

[acool@localhost C-Exercises]$ date
Sun Oct 23 04:14:24 PM PDT 2022
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$ gcc parent_children.c
[acool@localhost C-Exercises]$ ./a.out
Parent ID 4298, Main ID 10357
Parent ID 10357, Child ID 10358, Sleeping 15 seconds.
Parent ID 10357, Child ID 10394, Sleeping 14 seconds.
Parent ID 10357, Child ID 10436, Sleeping 13 seconds.
Parent ID 10357, Child ID 10475, Sleeping 12 seconds.
Parent ID 10357, Child ID 10510, Sleeping 11 seconds.
Parent ID 10357, Child ID 10532, Sleeping 10 seconds.
Parent ID 10357, Child ID 10546, Sleeping 9 seconds.
Parent ID 10357, Child ID 10558, Sleeping 8 seconds.
Parent ID 10357, Child ID 10581, Sleeping 7 seconds.
Parent ID 10357, Child ID 10598, Sleeping 6 seconds.
Parent ID 10357, Child ID 10607, Sleeping 5 seconds.
Parent ID 10357, Child ID 10613, Sleeping 4 seconds.
Parent ID 10357, Child ID 10647, Sleeping 3 seconds.
Parent ID 10357, Child ID 10732, Sleeping 2 seconds.
Parent ID 10357, Child ID 10815, Sleeping 1 seconds.
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$ ps -A |grep a.out
10357 pts/0 00:00:00 a.out
10394 pts/0 00:00:00 a.out
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$ pstree -p 10357
a.out(10357)───a.out(10436)
[acool@localhost C-Exercises]$

Slightly different version, in this version we don't wait() for each child individually.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

void child( int seconds );

int children = 15;

int main(void)
{
printf("Parent ID %d, Main ID %d \n", getppid(), getpid());

for(int i=0;i<children;i++)
{
if(fork()==0)
{
child(children-i); // pass seconds to sleep
}
}

while (wait(NULL) > 0); // wait for all children to finish
}

void child(int seconds)
{
printf("Parent ID %d, Child ID %d, Sleeping %d seconds.\n", getppid(), getpid(), seconds);
sleep(seconds);
exit(0);
}

[acool@localhost C-Exercises]$ 
[acool@localhost C-Exercises]$ gcc parent_children.c
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$ ./a.out
Parent ID 4298, Main ID 14322
Parent ID 14322, Child ID 14323, Sleeping 15 seconds.
Parent ID 14322, Child ID 14324, Sleeping 14 seconds.
Parent ID 14322, Child ID 14325, Sleeping 13 seconds.
Parent ID 14322, Child ID 14326, Sleeping 12 seconds.
Parent ID 14322, Child ID 14327, Sleeping 11 seconds.
Parent ID 14322, Child ID 14328, Sleeping 10 seconds.
Parent ID 14322, Child ID 14329, Sleeping 9 seconds.
Parent ID 14322, Child ID 14330, Sleeping 8 seconds.
Parent ID 14322, Child ID 14331, Sleeping 7 seconds.
Parent ID 14322, Child ID 14332, Sleeping 6 seconds.
Parent ID 14322, Child ID 14333, Sleeping 5 seconds.
Parent ID 14322, Child ID 14334, Sleeping 4 seconds.
Parent ID 14322, Child ID 14335, Sleeping 3 seconds.
Parent ID 14322, Child ID 14336, Sleeping 2 seconds.
Parent ID 14322, Child ID 14337, Sleeping 1 seconds.
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$ pstree -p 14322
a.out(14322)─┬─a.out(14323)
├─a.out(14324)
├─a.out(14325)
├─a.out(14326)
├─a.out(14327)
├─a.out(14328)
├─a.out(14329)
├─a.out(14330)
├─a.out(14331)
├─a.out(14332)
├─a.out(14333)
├─a.out(14334)
└─a.out(14335)
[acool@localhost C-Exercises]$ ps -A |grep a.out
14322 pts/0 00:00:00 a.out
14323 pts/0 00:00:00 a.out
14324 pts/0 00:00:00 a.out
14325 pts/0 00:00:00 a.out
14326 pts/0 00:00:00 a.out
14327 pts/0 00:00:00 a.out
14328 pts/0 00:00:00 a.out
14329 pts/0 00:00:00 a.out
14330 pts/0 00:00:00 a.out
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$ pstree -p 14322
a.out(14322)─┬─a.out(14323)
├─a.out(14324)
├─a.out(14325)
├─a.out(14326)
├─a.out(14327)
└─a.out(14328)
[acool@localhost C-Exercises]$
[acool@localhost C-Exercises]$


[ view entry ] ( 221 views )   |  print article
C101: Printing Emojis 
#include <stdio.h>
#include <wchar.h>
#include <locale.h>

// multibyte array, each character is 4 bytes long
wchar_t mystring[] = L"Angel Cool á 🤪 😁 ა";

// [acool@localhost other]$ gcc -Wall -ggdb emoji-multibyte-array.c -o emoji-multibyte-array
int main()
{
setlocale(LC_ALL,"");

// print total number of characters in string
wprintf(L"wcslen of wchar_t mystring[]: %ld\n", wcslen(mystring));

// output string
wprintf(L"%ls\n",mystring);

// print one character
wprintf(L"Emoji : %lc\n", mystring[13]);

// print all
for (int j = 0; j < wcslen(mystring); ++j)
{
wprintf(L"%lc,", mystring[j]);
}

wprintf(L"\n");

// size in bytes of string, it also includes 4 bytes for the null (\0) characters at the end it seems
wprintf(L"Size in bytes of mystring[]: %d\n", sizeof(mystring));

// print size of wchar_t data type
wprintf(L"wchar_t is %d bytes long!\n", sizeof(wchar_t));

return 0;
}

[acool@localhost other]$ date
Sat Oct 22 12:05:24 PM PDT 2022
[acool@localhost other]$ gcc emoji-multibyte-array.c
[acool@localhost other]$ ./a.out
wcslen of wchar_t mystring[]: 18
Angel Cool á 🤪 😁 ა
Emoji : 🤪
A,n,g,e,l, ,C,o,o,l, ,á, ,🤪, ,😁, ,ა,
Size in bytes of mystring[]: 76
wchar_t is 4 bytes long!
[acool@localhost other]$


[ view entry ] ( 190 views )   |  print article
C 101: Preventing Zombie Processes  
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
pid_t pid;
int status;

pid = fork();

if(pid<0)
{
printf("Error: fork() returned %u.\n", pid);
return 1;
}

if(pid == 0)
{
printf("Child: PID is %u. Parent's PID is %u\n", getpid(), getppid());
sleep(10);
puts("Child: about to exit.\n");
return 33;
}
else
{
printf("Parent: PID is %u. Child's PID is %u\n", getpid(), pid);

while((pid=waitpid(-1,&status,WNOHANG)) == 0)
{
printf("Parent: No child has terminated.");
printf("Parent: Going to sleep for 1 second.\n");
sleep(1);
}
}

printf("Parent: child with PID %u ", pid);

if(WIFEXITED(status)!=0)
{
printf("exited with status %u\n",WIFEXITED(status));
}
else
{
printf("exited abnormally\n");
}

return 0;
}

Preventing Zombie Processes Using waitpid()
Listing 19.5 -Teach Yourself C For Linux Programming
[acool@localhost C-practice]$ gcc -Wall fork.c
[acool@localhost C-practice]$
[acool@localhost C-practice]$ ./a.out
Parent: PID is 57544. Child's PID is 57545
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Child: PID is 57545. Parent's PID is 57544
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Parent: No child has terminated.Parent: Going to sleep for 1 second.
Child: about to exit.

Parent: child with PID 57545 exited with status 1
[acool@localhost C-practice]$
[acool@localhost C-practice]$


[ view entry ] ( 218 views )   |  print article
C 101: Creating Structures 
Method 1:
// define it as a template
struct person {
char first_name[50];
char last_name[50];
};

// create two instances of person
struct person Person1, Person2;

// assign values to Person1
strcpy(Person1.first_name, "Angel");
strcpy(Person1.last_name, "Cool");

Method 2:
// define it and create two instances of it
struct car {
char make[50];
char color[50];
} Car1, Car2;

// assign values to Car1
strcpy(Car1.make, "Chevy");
strcpy(Car1.color, "red");

Method 3:
// use 'typedef' to create it
typedef struct {
char first_name[50];
char last_name[50];
} person;

// create two instances of person (no 'struct' keyword needed)
person Person1, Person2;

Method 1 example
[acool@localhost C-practice]$ 
[acool@localhost C-practice]$ cat method_1.c
#include <stdio.h>
#include <string.h>

void main(){
struct person {
char first_name[50];
char last_name[50];
};

// create two instances of person
struct person Person1, Person2;

// assign values to Person1
strcpy(Person1.first_name, "Angel");
strcpy(Person1.last_name, "Cool");

// print assigned values
printf("Hello %s %s!\n", Person1.first_name, Person1.last_name);
}
[acool@localhost C-practice]$ gcc method_1.c -o method_1
[acool@localhost C-practice]$ ./method_1
Hello Angel Cool!
[acool@localhost C-practice]$

Method 2 example
[acool@localhost C-practice]$ 
[acool@localhost C-practice]$ cat method_2.c
#include <stdio.h>
#include <string.h>

void main(){
// define it and create two instances of it
struct car {
char make[50];
char color[50];
} Car1, Car2;

// assign values to Car1
strcpy(Car1.make, "Chevy");
strcpy(Car1.color, "red");

// print assigned values
printf("My %s %s.\n",Car1.color, Car1.make);
}
[acool@localhost C-practice]$
[acool@localhost C-practice]$ gcc method_2.c -o method_2
[acool@localhost C-practice]$ ./method_2
My red Chevy.
[acool@localhost C-practice]$

Method 3 example
[acool@localhost C-practice]$ 
[acool@localhost C-practice]$ cat method_3.c
#include <stdio.h>
#include <string.h>

void main(){
// use 'typedef' to create it
typedef struct {
char first_name[50];
char last_name[50];
} person;

// create two instances of person (no 'struct' keyword needed)
person Person1, Person2;

// assign values to Person1
strcpy(Person1.first_name, "Andres Manuel");
strcpy(Person1.last_name, "Lopez-Obrador");

printf("Viva la 4T! Viva %s %s!\n", Person1.first_name, Person1.last_name);
}
[acool@localhost C-practice]$
[acool@localhost C-practice]$ gcc method_3.c -o method_3
[acool@localhost C-practice]$ ./method_3
Viva la 4T! Viva Andres Manuel Lopez-Obrador!
[acool@localhost C-practice]$
[acool@localhost C-practice]$


Another example:
[acool@localhost ~]$ 
[acool@localhost ~]$
[acool@localhost ~]$ date
Wed Apr 5 02:18:30 PM PDT 2023
[acool@localhost ~]$
[acool@localhost ~]$ cat books.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Books {
char title[50];
char author[50];
char subject[100];
int id;
};

typedef struct {
char title[50];
char author[50];
char subject[100];
int id;
} eBooks;

void printdetails(eBooks *myEbook, struct Books *myBook)
{
printf( "myBook id : %d\n", myBook->id);
printf( "myBook title : %s\n", myBook->title);
printf( "myBook author : %s\n", myBook->author);
printf( "myBook subject : %s\n", myBook->subject);

printf("====================================\n");

printf( "myEbook id : %d\n", myEbook->id);
printf( "myEbook title : %s\n", myEbook->title);
printf( "myEbook author : %s\n", myEbook->author);
printf( "myEbook subject : %s\n", myEbook->subject);
}

int main()
{
// Pay attention to this! ...two different ways to declare it.
struct Books *myBook = (struct Books *) malloc(sizeof(struct Books));
eBooks *myEbook = (eBooks *) malloc(sizeof(eBooks));

strcpy( myBook->title, "C Programming");
strcpy( myBook->author, "Angel Cool");
strcpy( myBook->subject, "Programming C Structs Examples");
myBook->id = 1;

strcpy( myEbook->title, "C Programming (eBook)");
strcpy( myEbook->author, "Angel Cool (eBook)");
strcpy( myEbook->subject, "Programming C Structs Examples (eBook)");
myEbook->id = 2;

printdetails(myEbook,myBook);

return 0;
}
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ gcc books.c -o books
[acool@localhost ~]$
[acool@localhost ~]$ ./books
myBook id : 1
myBook title : C Programming
myBook author : Angel Cool
myBook subject : Programming C Structs Examples
====================================
myEbook id : 2
myEbook title : C Programming (eBook)
myEbook author : Angel Cool (eBook)
myEbook subject : Programming C Structs Examples (eBook)
[acool@localhost ~]$
[acool@localhost ~]$ file books
books: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=ea1090588d4c761da3060066a7d7a67b7db9caa6, for GNU/Linux 3.2.0, not stripped
[acool@localhost ~]$
[acool@localhost ~]$


[ view entry ] ( 218 views )   |  print article

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |


2024 By Angel Cool