This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have been doing A LOT of exercises and examples to get the hang of C, its finally starting to sink in slowly, but surely. Though I'm having one small problem with this and can't amend it for the life of me:
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR * argv[]) {
int i, grades[] = {98, 87, 92, 79, 85};
for(i = 0; i <= 4; ++i) {
printf("Element %d is %d\n"
i, grades[i]);
}
i = getchar();
return 0;
}
The problem is the i it is expecting a bracket, I don't know why and any amendment I have tried has failed, I know its a simple problem but it has me stuck.
Any help would be greatly appreciated.
The problem is that you're missing a comma that separates arguments in your call to printf:
printf("Element %d is %d\n", i, grades[i]);
// ^
// This one
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have to write a program in c language and this is the code : The problem is that when I try to compile it it says : syntax error before return .Where is my error?
#include <stdio.h>
int main (void)
{
char i,c2,j;
int c=4;
i=j=3;
while (++i <=c)
{
int j=1;
printf("\n Nr1=%c Nr2=%d",64+i,c2);
} do;
return 1;
}
Remove the do from your code. Just while (++i <= c) { /* ... */ };
You are redeclaring j inside the while loop. Remove int j=1;
and remove the do; at the end of your while
There is no such thing as while ... do loop in C. There are while loops or do ... while loops.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
i'm beginner in C programming language, i have piece of code written in C language on linux Platform my code is :
#include <stdio.h>
int main(int argc, char* argv[]){
printf("Count Of Args %d \n",argc);
int i = 0;
while(i < argc){
printf("%s \n",argv[i]);
printf("loop N: %d \n",i);
i++;
}
return 0;
}
While loop does not working and i don't know why... please show me where is problem? thanks.
Works perfectly:
$ ./program abc def
Count Of Args 3
./program
loop N: 0
abc
loop N: 1
def
loop N: 2
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
int main() {
int i;
int four_ints[4];
char* c;
for(i=0; i<4; i++) four_ints[i] = 18;
c = (char*)four_ints;
for(i=0; i<4; i++) c[i] = 24;
printf("%x\n", four_ints[2]);
}
So if I print like that it will simply print 12.
However if I change it to printf("%x\n", four_ints[11])
It suddenly prints 28ac90
Why would it do that?
In the second statement printf("%x\n", four_ints[11]) you access a position of the array that was not reserved for your program (int four_ints[4]). That is you have no guarantees of what is stored on a not reserved portion of memory.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
i am trying to compare array elements and idea is to collect all dissimilar element to other temp[] array. I think it is going somewhere wrong ...unable to understand...please help me
#include <stdio.h>
#include <stdlib.h>
#define SIZE 30
int my_arr[SIZE] = {10,20,45,63,89,20,15,12,89,24,12,10,89,25,64,39,37,64,95,
27,23,58,97,23,18,56,94,76,32,11
};
int main()
{
int i,j,temp_arr[100];
for(i=0;i<SIZE;i++)
{
for(j=0+i; j<SIZE; j++)
{
if(*(my_arr+i)!=*(my_arr+j))
*(my_arr+i) = temp_arr[i];
}
}
return 0;
}
Here:
*(my_arr+i) = temp_arr[i];
temp_arr[j] is not initialized and you are assigning it to my_arr[i]. You description sounds like you want:
temp_arr[i] = *(my_arr+i);
But then you will end up having holes in the temp_arr. So perhaps you need another index for counting items in the temp_arr.
Something like:
int tmp_cnt = 0;
for(i=0;i<SIZE;i++) {
for(j=0+i; j<SIZE; j++) {
if(*(my_arr+i)!=*(my_arr+j))
temp_arr[tmp_cnt++] = *(my_arr+i);
}
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Ok this is a simple C code but for some unknown reason the program refuse to compile and give segmentation fault 11 Please help me
#include <stdio.h>
typedef struct {
int P_answer[9];
int number;
} BOX;
int main()
{
BOX Matrix[8][8];
int i,j;
int k;
for(i=0;i<9;i++){
for(j=0;j<9;j++){
Matrix[i][j].number=0;
Matrix[i][j].P_answer[0]=1;
Matrix[i][j].P_answer[1]=2;
Matrix[i][j].P_answer[2]=3;
Matrix[i][j].P_answer[3]=4;
Matrix[i][j].P_answer[4]=5;
Matrix[i][j].P_answer[5]=6;
Matrix[i][j].P_answer[6]=7;
Matrix[i][j].P_answer[7]=8;
Matrix[i][j].P_answer[8]=9;
}
}
}
Matrix is an 8-by-8 array; each of your loops goes through 9 iterations.
The indexes of an array go from 0 to (size-1).
In your for-loops you go from 0 to size.
That's the reason of your segmentation fault.