Hi everyone. I tried binging and googling this one and I found answers in forums that I don't think applied to my problem. When I try running this code
// Printing of characters & strings
#include <stdio.h>
void main()
{
char x = "A";
char name = "Eritrea";
printf("output of characters\n\n");
printf("%c\n", x);
printf("%3c\n", x);
printf("\n");
printf("output of strings\n\n");
printf("output of strings\n\n");
printf("%s\n", name);
printf("%20s\n", name);
printf("%.5s\n", name);
printf("%-10.2s\n", name);
}
I get this error message on both line 7 and 8. error #2168: Operands of '=' have incompatible types 'char' and 'char *'. The strange thing is this is the way they had it in the book I am studying ('C' Programming for Beginners). I am using Pelles C version 6.5.. If anyone can tell me what I am doing wrong I would be greatful. Thanks for the help.
try
#include <stdio.h>
int main()
{
char *x = "A";
char *name = "Eritrea";
/*code*/
return 0;
}
It worked but it didn't give me the desired output. The output was
output of characters
τ
τ
output of strings
output of strings
Eritrea
Eritrea
Eritr
Er
*** Process returned 11 ***
Press any key to continue...
and the desired output was
output of characters
A
A
output of strings
output of strings
Eritrea
Eritrea
Eritr
Er
*** Process returned 11 ***
Press any key to continue...
I don't know how many the process would've returned in that case. here is the code again
// Printing of characters & strings
#include <stdio.h>
void main()
{
char *x = "A";
char *name = "Eritrea";
printf("output of characters\n\n");
printf("%c\n", x);
printf("%3c\n", x);
printf("\n");
printf("output of strings\n\n");
printf("output of strings\n\n");
printf("%s\n", name);
printf("%20s\n", name);
printf("%.5s\n", name);
printf("%-10.2s\n", name);
}
Once again thanks for the help. How can I fix this?
I don't understand what are yow try to do ?
This program was really just meant do demonstrate the printing of strings and characters after assigning each to a variable so I was just looking to get the same output that was printed in the book refered to above.
After doing a bit of research I think strings are of type char* and single characters are of type char (correct me if I'm wrong) so after changing my code to
// Printing of characters & strings
#include <stdio.h>
void main()
{
char x;
char *name;
x = 'A';
name = "Eritrea";
printf("output of characters\n\n");
printf("%c\n", x);
printf("%3c\n", x);
printf("\n");
printf("output of strings\n\n");
printf("output of strings\n\n");
printf("%s\n", name);
printf("%20s\n", name);
printf("%.5s\n", name);
printf("%-10.2s\n", name);
}
I got the desired output refered to above.
I am just a beginner so if there is a better way to do it or if there is anything wrong, please let me know. It worked just fine when I ran it though. Thanks for the help.
You could have used
char x = 'A';
the problem was most likely the double quotes...
Single quote.... 1 character
Double quote ... string of characters with a 0 at the end.
#include <stdio.h>
#include <string.h>
int main()
{
char plus = '+';
char minus ='\x2d';
char *one = "One";
char *two = "Two";
char three[6] = "Three";
/*if you want to print a single character*/
printf( "\n %c char \n", plus );
printf( "\n %c char \n", minus );
/*if you want to print a single character from string*/
printf( "\n %c char from string \n", three[2] );
/*if you want to print all characters from string*/
for( int i=0; i < strlen(one); i++)
{
printf( "\nindex = %d char = %c \n", i, one );
}
/*if you want to print a string*/
printf( "\n %s \n", one);
/*and, (more complex) output*/
printf( "\n %s %c %s = %s ;\n", one, plus, two, three);
printf( "\n Three %c 3 = %.2f ;\n", minus, 0.0);
return 0;
}