How can I view a number of user groups? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can I view a number of user groups?
I mean the implementation in C.
I wanted to use the getgrouplist() function, but I want it to take the number of groups automatically.

Here's an off the cuff program which seems to work on my macOS 10.14.1 system, which seems to be quite behind the times:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int res = 0;
int ng = 100;
int gs[ng];
res = getgrouplist("hacksaw", 20, gs, &ng);
printf("num_grps: %d\nres: %d\n", ng, res);
}
Here's one which works on Ubuntu 16.04:
#include <stdio.h>
#include <grp.h>
int main(void)
{
int res = 0;
int ng = 100;
gid_t gs[ng];
res = getgrouplist("hacksaw", 20, gs, &ng);
printf("num_grps: %d\nres: %d\n", ng, res);
}
The size 100 was chosen arbitrarily to provide maybe enough space.

Related

warning: no description found for function main using betty compliant syntax [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm learning the programming language C and using the Betty coding style of writing C github.com/holbertonschool/betty),
I have have been getting this syntax warning.
#include <stdio.h>
int main(void)
{
int a;
printf("\n Enter: ");
scanf("%d", &a);
return (0);
}
total: 0 errors, 1 warnings, 8 lines checked
c:2: warning: no description found for function main
After including the library, you ought to include a description of the program, like so:
#include <stdio.h>
/**
* main - Entry point
*
* Description: 'the program's description'
*
* Return: Always 0 (Success)
*/
int main(void)
{
Code goes here
}

Generating the same sequence of random number in a loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
A for loop to be exact. I've tried using srand() to seed the loop but it doesn't seem to be working. Could someone clarify the seeding part? Anyways, could someone point out my errors?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int a,b,c,d,loop1=0,loop2=1,count=0;
srand(1);
while(loop1!=1)
{
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;
printf("%d %d %d %d\n",a,b,c,d);
count+=1;
if (count>3)
{
break;
}
}
return 0;
}
The output should look like this:
1 2 3 4
1 2 3 4
1 2 3 4
You need to set the seed at the beginning of the loop if you want to have the same sequence in each iteration:
while(loop1!=1) {
srand(1);
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;
printf("%d %d %d %d\n",a,b,c,d);
count+=1;
if (count>3) {
break;
}
}

Format for tokenizing "..." strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Lets say that I have file which contains the data
Data1 "X1 Y1 Z1"
Data2 "X2 Y2 Z2"
Data3 "X3 Y3 Z3"
Generally, how would I scan the file and make my program count "X1 Y1 Z1" as a single token?
#include <stdio.h>
int main(){
FILE *fp = fopen("data.txt", "r");
char data_name[16];
char data_string[32];
while(2==fscanf(fp, "%15s \"%31[^\"]\"", data_name, data_string)){
printf("%s, %s\n", data_name, data_string);
}
fclose(fp);
return 0;
}

what is the fault with c here? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Please excuse me for asking this, because I know the code I'm gonna give you is wrong. Being a newbie I am not able to find the fault. Please help me correct the question and give a solution as well. Again I'm sorry to bother with this simple problem. Tomorrow is my exm in C so i'm kinda desperate. :(
Q: What will be the output of the program?
First let me show you how I find the code first:
#include<stdio.h>
int funct l(int n){
if (n>3)
return int funct(n-3)));
}
main() {
int n= 10;
printf("%d", funct l (n));
}
Then I thought i'd correct it. Then I cleaned up the code as far as i can. Then the code came to this:
#include<stdio.h>
int funct(int n){
if (n>3){
return funct(n-3);
}
}
main() {
int n= 10;
printf("%d", funct(n));
}
still it doesn't give proper answer (though I don't know what it'll show). It is either 1 or 2 and process returned 1 (0*1) is showing at the last line.
Please help me out!
Your funct function doesn't always return a value. This means that it could return anything. Try this:
int funct(int n) {
if (n > 3)
return funct(n - 3);
return n;
}
Here is the call stack when n = 10
funct(n = 10)
funct(n = 7)
funct(n = 4)
funct(n = 1)
return 1
return 1
return 1
return 1
Here is the call stack when n = 11
funct(n = 11)
funct(n = 8)
funct(n = 5)
funct(n = 2)
return 2
return 2
return 2
return 2

About tail recursion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Recently I am reading Mastering Algorithms with c, and in this book I have 1 exercise that I am not able to implement with c.
Tn = 1 if n=1 ,
Tn = 2T(n/2) + n if n > 1
Anyone can help me? I'll appreciate it a lot.
I have tried.
#include <stdio.h>
int test(int n) {
if (n == 1)
return 1;
else if( n > 1 )
return test(n / 2) * 2 + n;
}
int testtail(int n, int running_result) {
if (n == 1)
return running_result;
else
**return testtail(n / 2, ???? );** // How can I implement the second param
}
I am sorry guys! I am not a native English speaker! Maybe I made some mistakes in grammer! I should apologize for this!

Resources