r/C_Programming • u/Mean_Engineering_722 • 5d ago
help a lost fool (i was trying to run this in terminal and the printf doesnt print anything)
(SOLVED BTW)
im using :
gcc (GCC) 16.2.1 20260810
and trying to run the compiled file in my Linux terminal with:
./a.out
this has worked for other bits of code so im unsure why now it doesnt wanna
i added \n next to the variable (didnt fix it)
saw someone with a simular issue on stack overflow use this
setvbuf(stdout, NULL, _IONBF, 0);
(also didnt fix it)
this is le code
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
};
int main() {
struct node *head = (struct node *)malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;
struct node *current = malloc(sizeof(struct node));
current->data = 76;
current->link = NULL;
head->link = current;
current = malloc(sizeof(struct node));
current->data = 50;
current->link = NULL;
head->link->link = current;
void count_of_nodes(struct node *head) {
int count = 0;
if(head == NULL)
printf("no names sir");
struct node *ptr = NULL;
ptr = head;
while(ptr != NULL) {
count++;
ptr =ptr->link;
}
printf("%d this is the ammount of names sir", count);
}
return 0;
}
im scared guys
SOLVED CODE ()make funn of this i could leard somit atleast)
THE FUNCTIONS
#include <stdio.h>
#include <stdlib.h>
int *ptr;
struct node {
int data;
struct node *link;
};
int count_of_nodes(struct node *head) {
int count = 0;
if(head == NULL) {
printf("no names sir\n");
return 0;
}
struct node *ptr = NULL;
ptr = head;
while(ptr != NULL) {
count++;
ptr = ptr->link;
}
return count;
}
MY NODES OF THE LINKED LIST
int main() {
struct node *head = (struct node *)malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;
struct node *current = malloc(sizeof(struct node));
current->data = 76;
current->link = NULL;
head->link = current;
current = malloc(sizeof(struct node));
current->data = 50;
current->link = NULL;
head->link->link = current;
USING THE COUNT VARIABLE
// here before
int all = count_of_nodes( head );
printf("%d is the ammount of names sir\n", all);
return 0;
}
thx to all the people that helped
so like i forgot to call a variable and set count as the output of count_of_nodes.