Error compiling basic C program with arrays and strings

sossego

Retired from the forums
The resulting error is :
Code:
$ cc ex9.c -o ex9.o
ex9.c:46:3: error: expected ')'
                another[0], another[1],
                ^
ex9.c:45:8: note: to match this '('
        printf("another each: %c %c %c %c\n"
              ^
1 error generated.

The entire code is:
Code:
#include <stdio.h>

int main(int argc, char *argv[])
{

	int numbers[4] = {0};
	char name[4] = {'a'};

	//first print them out raw
	printf("numbers: %d %d %d %d\n",
		numbers[0], numbers[1],
		numbers[2], numbers[3]);

	printf("name each: %c %c %c %c\n",
		name[0], name[1],
		name[2], name[3]);

	printf("name: %s\n", name);

	//setup numbers
	numbers[0] = 1;
	numbers[1] = 2;
	numbers[2] = 3;
	numbers[3] = 4;

	//setup name
	name[0] = 'J';
	name[1] = 'e';
	name[2] = 'd';
	name[3] = '\0';

	//print out initialized
	printf("name each : %c %c %c %c\n",
		name[0], name[1],
		name[2], name[3]);

	//see the name like a string
	printf("name: %s\n", name);

	//another way to use name
	char *another = "Jed";

	printf("another: %s\n", another);

	printf("another each: %c %c %c %c\n"
		another[0], another[1],
		another[2], another[3]);

	return 0;
}

The compilers used were /usr/local/bin/clang and /usr/bin/cc with the error occurring both times.

My question is : Where is the fault in the code?
 
Compare this printf from your code:
Code:
   printf("name each : %c %c %c %c\n",
      name[0], name[1],
      name[2], name[3]);
with this printf, also from your code:
Code:
   printf("another each: %c %c %c %c\n"
      another[0], another[1],
      another[2], another[3]);
and you will note the missing comma on the second printf code snippet.
 
I once stared at a line of FORTRAN code for an hour wondering why it wasn't being executed only to finally realize it was commented out. :)
 
Back
Top