A
An
Hello,
I'm new to C and now I encountered the following problem.
When I compile the program hulp.c (the code is below) with the command
gcc -O -o hulp hulp.c,
running the program with input 1 2 3 4 1 2 3 4 1 2 3 4 1 2
gives output
12341234123412
00041234123412.
I don't understand how it is possible that the value of
macht[0],macht[1]and macht[2] don't remain 1 2 3.
It gets even more mysterious to me, since when I compile things using
gcc -o hulp hulp.c
running the program with the same input as above now gives the
expected output:
12341234123412
12341234123412.
How is this possible? What is the influence of the -O in the compiler
command? In the manual I read this has to do with optimization, but
what does it do here?
If I replace the type 'char' of 'inleeslg' to 'int', with the same
input as above BOTH compilers give the expected output:
12341234123412
12341234123412.
How can this be explained?
I really hope someone can help me out...
Here below one can find the code.
The first line of the file "facortest" is 2 1 1 2 3 4 5 6 7 8 9 10 11
0 .
Thank you in advance,
An
*********************************************************
#include <stdio.h>
main()
{FILE *fp;
short i;
char inleeslg;
char macht[14];
long pbas;
char e;
fp=fopen("Factortest","r");
if ((fp=fopen("Factortest","r"))==NULL) printf("Kan datafile niet
openen \n");
else{
printf("Type 14 small numbers:\n");
for(i=0;i<14;i++)
{scanf("%d",&macht);
}
/*Printing the input */
for(i=0;i<14;i++)
{printf("%d",macht);
}
printf("\n");
fscanf(fp,"%d %d %d",&pbas,&e,&inleeslg);
/*Printing the input again */
for(i=0;i<14;i++)
{printf("%d",macht);
}
printf("\n");
}
fclose(fp);
}
******************************************************************
I'm new to C and now I encountered the following problem.
When I compile the program hulp.c (the code is below) with the command
gcc -O -o hulp hulp.c,
running the program with input 1 2 3 4 1 2 3 4 1 2 3 4 1 2
gives output
12341234123412
00041234123412.
I don't understand how it is possible that the value of
macht[0],macht[1]and macht[2] don't remain 1 2 3.
It gets even more mysterious to me, since when I compile things using
gcc -o hulp hulp.c
running the program with the same input as above now gives the
expected output:
12341234123412
12341234123412.
How is this possible? What is the influence of the -O in the compiler
command? In the manual I read this has to do with optimization, but
what does it do here?
If I replace the type 'char' of 'inleeslg' to 'int', with the same
input as above BOTH compilers give the expected output:
12341234123412
12341234123412.
How can this be explained?
I really hope someone can help me out...
Here below one can find the code.
The first line of the file "facortest" is 2 1 1 2 3 4 5 6 7 8 9 10 11
0 .
Thank you in advance,
An
*********************************************************
#include <stdio.h>
main()
{FILE *fp;
short i;
char inleeslg;
char macht[14];
long pbas;
char e;
fp=fopen("Factortest","r");
if ((fp=fopen("Factortest","r"))==NULL) printf("Kan datafile niet
openen \n");
else{
printf("Type 14 small numbers:\n");
for(i=0;i<14;i++)
{scanf("%d",&macht);
}
/*Printing the input */
for(i=0;i<14;i++)
{printf("%d",macht);
}
printf("\n");
fscanf(fp,"%d %d %d",&pbas,&e,&inleeslg);
/*Printing the input again */
for(i=0;i<14;i++)
{printf("%d",macht);
}
printf("\n");
}
fclose(fp);
}
******************************************************************