int and char

#include<stdio.h>
#include<conio.h>

main()
{
char c='amsdlsn';
printf("%c",c);
getch();
}

output:
n

Q.1why is it that whatever i take the value of c, it has to be the last letter of the string?
Q.2the code works even if i replace char by int and %c by %d.why is it so?
1. I don't understand your question.
2. All characters have an integer value.
http://www.asciitable.com/
Q.1 see my ques is
if char c='sjfbksj' output is j
if cahr c='azhsb' output is b
the last letter is always the output..y is it so?

Q.2 so u mean int c='sgdrg' is a valid expression?
Also
if int c='a' output is 97
if int c='ab' output is 24930
why is it so?
vgoel38 wrote:
Q.1 see my ques is
if char c='sjfbksj' output is j
if cahr c='azhsb' output is b
the last letter is always the output..y is it so?


Because a char can only hold 1 char obviously - so if you
try to give it a more than one char like you are trying to do - you will
only get the last one.

Q.2 so u mean int c='sgdrg' is a valid expression?
Also
if int c='a' output is 97
if int c='ab' output is 24930
why is it so?


in your case an int is 4 bytes long.
so if you say
int c = 'ABCDEFG' - then the compiler will take the last 4
chars and put them into the integer.
So c will be (in hex) 0x44454647 - which is (decimal) 1145390663

BUT the compiler will give you a warning if you try to fit too big a value
into a particular variable. The warning will be something like:
warning: character constant too long for its type


Can you see what is going on here???
q1..yeah but why is it that last digit is output and not any other?
q2..i coudn't understand following line of yours

then the compiler will take the last 4 chars
So c will be (in hex) 0x44454647
Topic archived. No new replies allowed.