C: Interpret gcc output and numbres
I have written a C program which produces the following output, when running gcc on a Linux VM on Windows:
Error message:
However, the number of "{ and }" should be correct, so that I do not understand why the compiler complains about this. Also I am using k and random after declaring them, how can I get rid of the warnings?
Thank you for your help!
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
# include <getopt.h>
#include <stdbool.h>
int main(int argc, char ** argv)
{
int opt=0;
//Parameter:
//boolean random:
// typdef enum {false, true} bool;
bool random=false;
//Zahl zu der gezaehlt werden soll: k
int k=10;
//Zahl der Kindprozesse, die erzeugt werden sollen: N
int n=1;
//Parsieren der Kommandozeilenparameter:
while ((opt = getopt(argc, argv, "nt:")) != -1)
{
switch (opt)
{
case 'k':
k = atoi(optarg);
break;
case 'n':
n = atoi(optarg);
break;
case 'r':
random=true;
default: /* '?' */
printf("%s", "No valid parameters.");
exit(EXIT_FAILURE);
}
}
//Erzeugen von n Kindprozessen:
int zaehlerprozesse=0;
while(zaehlerprozesse<n)
{
fork();
zaehlerprozesse++;
}
if(//TODO list)
{
//Kindprozess liegt vor
int zaehler=1;
char ausgabe[256]= {0};
int zaehlen=k;
//Umgehen mit random:
if(random==true)
{
srand((unsigned) time(&t));
int help=rand()%(k*0.5);
//0 oder 1 um zu bestimmen, ob addieren oder substrahieren:
int luck=rand()%1;
if(luck==0)
{
zaehlen=zaehlen-help;
}
else
{
zaehlen=zaehlen+help;
}
}
while(zaehler<=zaehlen)
{
int pid=getpid();
int ppid=getppid();
sprintf(ausgabe, "%d %c %d %c %dn", pid,' ', ppid,' ',zaehler);
write(STDOUT_FILENO, ausgabe, strlen(ausgabe));
sleep(1);
zaehler++;
}
exit((getpid()+k)%100);
}
else
{
//Elternprozess liegt vor
time_t curtime;
time(&curtime);
printf("Start: %s", ctime(&curtime));
}
int exitcode=0;
//TODO bestimmen wie man auf alle aus der Liste wartet:
wait(&exitcode);
//exitcode to String casten:
char str[24];
sprintf(str, "Exit-Code: %dn",WEXITSTATUS(exitcode));
//Ausgabe des Exitcodes:
write(STDOUT_FILENO, str, strlen(str));
time_t curtime;
time(&curtime);
printf("Ende: %sn", ctime(&curtime));
return 0;
}
c
add a comment |
I have written a C program which produces the following output, when running gcc on a Linux VM on Windows:
Error message:
However, the number of "{ and }" should be correct, so that I do not understand why the compiler complains about this. Also I am using k and random after declaring them, how can I get rid of the warnings?
Thank you for your help!
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
# include <getopt.h>
#include <stdbool.h>
int main(int argc, char ** argv)
{
int opt=0;
//Parameter:
//boolean random:
// typdef enum {false, true} bool;
bool random=false;
//Zahl zu der gezaehlt werden soll: k
int k=10;
//Zahl der Kindprozesse, die erzeugt werden sollen: N
int n=1;
//Parsieren der Kommandozeilenparameter:
while ((opt = getopt(argc, argv, "nt:")) != -1)
{
switch (opt)
{
case 'k':
k = atoi(optarg);
break;
case 'n':
n = atoi(optarg);
break;
case 'r':
random=true;
default: /* '?' */
printf("%s", "No valid parameters.");
exit(EXIT_FAILURE);
}
}
//Erzeugen von n Kindprozessen:
int zaehlerprozesse=0;
while(zaehlerprozesse<n)
{
fork();
zaehlerprozesse++;
}
if(//TODO list)
{
//Kindprozess liegt vor
int zaehler=1;
char ausgabe[256]= {0};
int zaehlen=k;
//Umgehen mit random:
if(random==true)
{
srand((unsigned) time(&t));
int help=rand()%(k*0.5);
//0 oder 1 um zu bestimmen, ob addieren oder substrahieren:
int luck=rand()%1;
if(luck==0)
{
zaehlen=zaehlen-help;
}
else
{
zaehlen=zaehlen+help;
}
}
while(zaehler<=zaehlen)
{
int pid=getpid();
int ppid=getppid();
sprintf(ausgabe, "%d %c %d %c %dn", pid,' ', ppid,' ',zaehler);
write(STDOUT_FILENO, ausgabe, strlen(ausgabe));
sleep(1);
zaehler++;
}
exit((getpid()+k)%100);
}
else
{
//Elternprozess liegt vor
time_t curtime;
time(&curtime);
printf("Start: %s", ctime(&curtime));
}
int exitcode=0;
//TODO bestimmen wie man auf alle aus der Liste wartet:
wait(&exitcode);
//exitcode to String casten:
char str[24];
sprintf(str, "Exit-Code: %dn",WEXITSTATUS(exitcode));
//Ausgabe des Exitcodes:
write(STDOUT_FILENO, str, strlen(str));
time_t curtime;
time(&curtime);
printf("Ende: %sn", ctime(&curtime));
return 0;
}
c
1
Please post a Minimal, Complete, and Verifiable example. In fact, just the act of trying to make an MCVE (i.e. removing one line or block at a time while making sure this bug still happens, will most likely reveal to you where the syntax error is.
– David Grayson
Nov 22 '18 at 7:58
if(//TODO list)
Did you forget to replace some todo?
– Mathieu
Nov 22 '18 at 7:59
2
Why do you post text as image? Please don't post text as image.
– Kamil Cuk
Nov 22 '18 at 8:00
1
The error says the exact line where it is. It says what it is (not about parens, about what’s missing before that paren). Syntax highlighting shows what’s happening. There’s even a comment saying something’s missing. And MCVE doesn’t help much when fixing syntax errors, looking at the code does.
– Sami Kuhmonen
Nov 22 '18 at 8:01
add a comment |
I have written a C program which produces the following output, when running gcc on a Linux VM on Windows:
Error message:
However, the number of "{ and }" should be correct, so that I do not understand why the compiler complains about this. Also I am using k and random after declaring them, how can I get rid of the warnings?
Thank you for your help!
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
# include <getopt.h>
#include <stdbool.h>
int main(int argc, char ** argv)
{
int opt=0;
//Parameter:
//boolean random:
// typdef enum {false, true} bool;
bool random=false;
//Zahl zu der gezaehlt werden soll: k
int k=10;
//Zahl der Kindprozesse, die erzeugt werden sollen: N
int n=1;
//Parsieren der Kommandozeilenparameter:
while ((opt = getopt(argc, argv, "nt:")) != -1)
{
switch (opt)
{
case 'k':
k = atoi(optarg);
break;
case 'n':
n = atoi(optarg);
break;
case 'r':
random=true;
default: /* '?' */
printf("%s", "No valid parameters.");
exit(EXIT_FAILURE);
}
}
//Erzeugen von n Kindprozessen:
int zaehlerprozesse=0;
while(zaehlerprozesse<n)
{
fork();
zaehlerprozesse++;
}
if(//TODO list)
{
//Kindprozess liegt vor
int zaehler=1;
char ausgabe[256]= {0};
int zaehlen=k;
//Umgehen mit random:
if(random==true)
{
srand((unsigned) time(&t));
int help=rand()%(k*0.5);
//0 oder 1 um zu bestimmen, ob addieren oder substrahieren:
int luck=rand()%1;
if(luck==0)
{
zaehlen=zaehlen-help;
}
else
{
zaehlen=zaehlen+help;
}
}
while(zaehler<=zaehlen)
{
int pid=getpid();
int ppid=getppid();
sprintf(ausgabe, "%d %c %d %c %dn", pid,' ', ppid,' ',zaehler);
write(STDOUT_FILENO, ausgabe, strlen(ausgabe));
sleep(1);
zaehler++;
}
exit((getpid()+k)%100);
}
else
{
//Elternprozess liegt vor
time_t curtime;
time(&curtime);
printf("Start: %s", ctime(&curtime));
}
int exitcode=0;
//TODO bestimmen wie man auf alle aus der Liste wartet:
wait(&exitcode);
//exitcode to String casten:
char str[24];
sprintf(str, "Exit-Code: %dn",WEXITSTATUS(exitcode));
//Ausgabe des Exitcodes:
write(STDOUT_FILENO, str, strlen(str));
time_t curtime;
time(&curtime);
printf("Ende: %sn", ctime(&curtime));
return 0;
}
c
I have written a C program which produces the following output, when running gcc on a Linux VM on Windows:
Error message:
However, the number of "{ and }" should be correct, so that I do not understand why the compiler complains about this. Also I am using k and random after declaring them, how can I get rid of the warnings?
Thank you for your help!
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
# include <getopt.h>
#include <stdbool.h>
int main(int argc, char ** argv)
{
int opt=0;
//Parameter:
//boolean random:
// typdef enum {false, true} bool;
bool random=false;
//Zahl zu der gezaehlt werden soll: k
int k=10;
//Zahl der Kindprozesse, die erzeugt werden sollen: N
int n=1;
//Parsieren der Kommandozeilenparameter:
while ((opt = getopt(argc, argv, "nt:")) != -1)
{
switch (opt)
{
case 'k':
k = atoi(optarg);
break;
case 'n':
n = atoi(optarg);
break;
case 'r':
random=true;
default: /* '?' */
printf("%s", "No valid parameters.");
exit(EXIT_FAILURE);
}
}
//Erzeugen von n Kindprozessen:
int zaehlerprozesse=0;
while(zaehlerprozesse<n)
{
fork();
zaehlerprozesse++;
}
if(//TODO list)
{
//Kindprozess liegt vor
int zaehler=1;
char ausgabe[256]= {0};
int zaehlen=k;
//Umgehen mit random:
if(random==true)
{
srand((unsigned) time(&t));
int help=rand()%(k*0.5);
//0 oder 1 um zu bestimmen, ob addieren oder substrahieren:
int luck=rand()%1;
if(luck==0)
{
zaehlen=zaehlen-help;
}
else
{
zaehlen=zaehlen+help;
}
}
while(zaehler<=zaehlen)
{
int pid=getpid();
int ppid=getppid();
sprintf(ausgabe, "%d %c %d %c %dn", pid,' ', ppid,' ',zaehler);
write(STDOUT_FILENO, ausgabe, strlen(ausgabe));
sleep(1);
zaehler++;
}
exit((getpid()+k)%100);
}
else
{
//Elternprozess liegt vor
time_t curtime;
time(&curtime);
printf("Start: %s", ctime(&curtime));
}
int exitcode=0;
//TODO bestimmen wie man auf alle aus der Liste wartet:
wait(&exitcode);
//exitcode to String casten:
char str[24];
sprintf(str, "Exit-Code: %dn",WEXITSTATUS(exitcode));
//Ausgabe des Exitcodes:
write(STDOUT_FILENO, str, strlen(str));
time_t curtime;
time(&curtime);
printf("Ende: %sn", ctime(&curtime));
return 0;
}
c
c
edited Nov 22 '18 at 8:47
P.W
12.5k3844
12.5k3844
asked Nov 22 '18 at 7:55
SQLLearnerSQLLearner
85
85
1
Please post a Minimal, Complete, and Verifiable example. In fact, just the act of trying to make an MCVE (i.e. removing one line or block at a time while making sure this bug still happens, will most likely reveal to you where the syntax error is.
– David Grayson
Nov 22 '18 at 7:58
if(//TODO list)
Did you forget to replace some todo?
– Mathieu
Nov 22 '18 at 7:59
2
Why do you post text as image? Please don't post text as image.
– Kamil Cuk
Nov 22 '18 at 8:00
1
The error says the exact line where it is. It says what it is (not about parens, about what’s missing before that paren). Syntax highlighting shows what’s happening. There’s even a comment saying something’s missing. And MCVE doesn’t help much when fixing syntax errors, looking at the code does.
– Sami Kuhmonen
Nov 22 '18 at 8:01
add a comment |
1
Please post a Minimal, Complete, and Verifiable example. In fact, just the act of trying to make an MCVE (i.e. removing one line or block at a time while making sure this bug still happens, will most likely reveal to you where the syntax error is.
– David Grayson
Nov 22 '18 at 7:58
if(//TODO list)
Did you forget to replace some todo?
– Mathieu
Nov 22 '18 at 7:59
2
Why do you post text as image? Please don't post text as image.
– Kamil Cuk
Nov 22 '18 at 8:00
1
The error says the exact line where it is. It says what it is (not about parens, about what’s missing before that paren). Syntax highlighting shows what’s happening. There’s even a comment saying something’s missing. And MCVE doesn’t help much when fixing syntax errors, looking at the code does.
– Sami Kuhmonen
Nov 22 '18 at 8:01
1
1
Please post a Minimal, Complete, and Verifiable example. In fact, just the act of trying to make an MCVE (i.e. removing one line or block at a time while making sure this bug still happens, will most likely reveal to you where the syntax error is.
– David Grayson
Nov 22 '18 at 7:58
Please post a Minimal, Complete, and Verifiable example. In fact, just the act of trying to make an MCVE (i.e. removing one line or block at a time while making sure this bug still happens, will most likely reveal to you where the syntax error is.
– David Grayson
Nov 22 '18 at 7:58
if(//TODO list)
Did you forget to replace some todo?– Mathieu
Nov 22 '18 at 7:59
if(//TODO list)
Did you forget to replace some todo?– Mathieu
Nov 22 '18 at 7:59
2
2
Why do you post text as image? Please don't post text as image.
– Kamil Cuk
Nov 22 '18 at 8:00
Why do you post text as image? Please don't post text as image.
– Kamil Cuk
Nov 22 '18 at 8:00
1
1
The error says the exact line where it is. It says what it is (not about parens, about what’s missing before that paren). Syntax highlighting shows what’s happening. There’s even a comment saying something’s missing. And MCVE doesn’t help much when fixing syntax errors, looking at the code does.
– Sami Kuhmonen
Nov 22 '18 at 8:01
The error says the exact line where it is. It says what it is (not about parens, about what’s missing before that paren). Syntax highlighting shows what’s happening. There’s even a comment saying something’s missing. And MCVE doesn’t help much when fixing syntax errors, looking at the code does.
– Sami Kuhmonen
Nov 22 '18 at 8:01
add a comment |
1 Answer
1
active
oldest
votes
I can see three issues in the code.
if(//TODO list)
Because of the comment marker//
the closing parenthesis)
for theif
is missing and also there is no condition for it.
srand((unsigned) time(&t));
t
is undeclared here.
int help=rand()%(k*0.5);
. You are using adouble
(k*0.5
) as an operand of%
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426202%2fc-interpret-gcc-output-and-numbres%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I can see three issues in the code.
if(//TODO list)
Because of the comment marker//
the closing parenthesis)
for theif
is missing and also there is no condition for it.
srand((unsigned) time(&t));
t
is undeclared here.
int help=rand()%(k*0.5);
. You are using adouble
(k*0.5
) as an operand of%
add a comment |
I can see three issues in the code.
if(//TODO list)
Because of the comment marker//
the closing parenthesis)
for theif
is missing and also there is no condition for it.
srand((unsigned) time(&t));
t
is undeclared here.
int help=rand()%(k*0.5);
. You are using adouble
(k*0.5
) as an operand of%
add a comment |
I can see three issues in the code.
if(//TODO list)
Because of the comment marker//
the closing parenthesis)
for theif
is missing and also there is no condition for it.
srand((unsigned) time(&t));
t
is undeclared here.
int help=rand()%(k*0.5);
. You are using adouble
(k*0.5
) as an operand of%
I can see three issues in the code.
if(//TODO list)
Because of the comment marker//
the closing parenthesis)
for theif
is missing and also there is no condition for it.
srand((unsigned) time(&t));
t
is undeclared here.
int help=rand()%(k*0.5);
. You are using adouble
(k*0.5
) as an operand of%
answered Nov 22 '18 at 8:02
P.WP.W
12.5k3844
12.5k3844
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426202%2fc-interpret-gcc-output-and-numbres%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please post a Minimal, Complete, and Verifiable example. In fact, just the act of trying to make an MCVE (i.e. removing one line or block at a time while making sure this bug still happens, will most likely reveal to you where the syntax error is.
– David Grayson
Nov 22 '18 at 7:58
if(//TODO list)
Did you forget to replace some todo?– Mathieu
Nov 22 '18 at 7:59
2
Why do you post text as image? Please don't post text as image.
– Kamil Cuk
Nov 22 '18 at 8:00
1
The error says the exact line where it is. It says what it is (not about parens, about what’s missing before that paren). Syntax highlighting shows what’s happening. There’s even a comment saying something’s missing. And MCVE doesn’t help much when fixing syntax errors, looking at the code does.
– Sami Kuhmonen
Nov 22 '18 at 8:01