what is the difference between....... Watch

Circuitgram
Badges: 0
Rep:
?
#1
Report Thread starter 7 years ago
#1
any one can help by saying differences between string constants and character constants.. in C........i am confuse......
0
reply
Dragon
Badges: 0
Rep:
?
#2
Report 7 years ago
#2
A character constant is expressed with single quotes ('a'), while a string constant is double quotes ("a"). A string is actually a null-terminated array of characters. So "a" is a two-element array of characters, the first element is the character 'a', the second is the null character.
1
reply
Circuitgram
Badges: 0
Rep:
?
#3
Report Thread starter 7 years ago
#3
i understand the differences between string constant and character constants.
After that i have written some code but could not find out the error.........so.......any one please help me........
#include <stdio.h>
#include <conio.h>
int main()
{
char a,b,c[5];
a='A';
b='a';
c[]="ascii";
printf("ASCII value of A=> %d and of a=> %d",a,b);
printf("Character value of a=> %c and of b=> %c",a,b);
printf("The string c => %s",c[]);
_getch();
}
0
reply
Planto
Badges: 16
Rep:
?
#4
Report 7 years ago
#4
Code:
c[]="ascii"            // You shouldn't have the [] here.

c="ascii"                        // This still isn't valid, because you can't assign string literals in this way.

strcpy(c, "ascii")            // Almost there, but c is a 5 char array and "ascii" requires 6 (including the null char '\0')
- Include String.h
- Make c a 6-element array.
- Use strcpy.

Alternatively, you can declare c as a char pointer and assign a string literal directly, but this will then be treated as static, read-only data.

Edit: For future reference, it generally helps (a lot) if you also post the error messages you are getting.
0
reply
jayron
Badges: 0
Rep:
?
#5
Report 7 years ago
#5
A character constant consits of up to four character enclosed in either single or double quotes.A string constant looks loke a character constant only longer.It is treated as a concatenation of maximum size constants for the conditions.
1
reply
Planto
Badges: 16
Rep:
?
#6
Report 7 years ago
#6
Bumping two-month-old threads with incorrect (and frankly nonsensical) information. Excellent.
2
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

The new Gillette ad. Is it:

Man-hating bullsh*t (140)
46.51%
Pro-humanity (161)
53.49%

Watched Threads

View All