why it is not giving any error?

P

prasi

hi all,
I have executed the following program, I was expecting some errors in
the program but it didn't give any ERRORS!!!!!!!!!!!!!!!!!!!!!!!!!!!
please can anybody explain me ?
#include<stdio.h>
int main()
{
char a=97;

"hello everyone"+printf("value of a=%c\n",a);
return 0;
}

Thanks in advance
Prasanna K P
 
D

David G. Hong

#include said:
int main()
{
char a=97;

You have assigned a decimal value of 97 to char variable 'a'. This
infact is legal in C as characters use ascii conversion. Not too sure
about why, but if you type in google, "ascii code" you will see
decimals numbers assigned to each character.
 
A

August Karlstrom

prasi said:
hi all,
I have executed the following program, I was expecting some errors in
the program but it didn't give any ERRORS!!!!!!!!!!!!!!!!!!!!!!!!!!!
please can anybody explain me ?
#include<stdio.h>
int main()
{
char a=97;

"hello everyone"+printf("value of a=%c\n",a);
return 0;
}

Adding an integer to a character pointer is a well defined operation (It
has no side effect in your example though.) For instance:

char *s = "xyz";

printf("%c\n", *(s + 2));

will output 'z'.


August
 
C

Chris Dollin

prasi said:
hi all,
I have executed the following program, I was expecting some errors in
the program but it didn't give any ERRORS!!!!!!!!!!!!!!!!!!!!!!!!!!!
please can anybody explain me ?
#include<stdio.h>
int main()
{
char a=97;

"hello everyone"+printf("value of a=%c\n",a);
return 0;
}

There's nothing wrong with the code.

It's /stupid/, but there's no obligation on a compiler to detect
stupid code.

97 is legal as a character value (ill-advised, but legal).

Adding integers to pointers is legal - C would be pretty useless if
it were not - and ignoring calculated values is also legal and
often useful.

You might get some messages if you poked your compiler options a
bit harder.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
You have assigned a decimal value of 97 to char variable 'a'. This
infact is legal in C

True so far.
as characters use ascii conversion.

Untrue.

The C standard -does not_ specify which characterset is used, and standard C has
been implemented in other charactersets other than ASCII.

On the platforms that I use,assigns the variable a the value associated with the SOLIDUS ('/') character.
Needless to say, the characterset is _not_ ASCII.

[snip]


- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDQSxoagVFX4UWr64RAilkAJ9JbGQrOAzGt2JhdU6mMzf0WMeeQQCfU2T6
p3reCWQNcscJ1atN1kKGqkw=
=7X04
-----END PGP SIGNATURE-----
 
P

pete

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


True so far.


Untrue.

It's legal because CHAR_MAX is guaranteed to be at least 127,
and CHAR_MIN is guaranteed to be no greater than zero.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
It's legal because CHAR_MAX is guaranteed to be at least 127,
and CHAR_MIN is guaranteed to be no greater than zero.

Which is entirely irrelevant to this thread.

The issue is whether or not "characters use ascii conversion" is a valid
statement, not whether 97 is a value between CHAR_MIN and CHAR_MAX.

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDQUoiagVFX4UWr64RAv5oAJ9ReVwkOzQ0DL1EtiZ26rRdO4oqfQCgj4MA
BppF/jnyGH+ABHvOMYJY+5I=
=eZ5q
-----END PGP SIGNATURE-----
 
P

pete

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Which is entirely irrelevant to this thread.

No. The subject line is:
why it is not giving any error?

The reason why
char a=97;
is not giving any error, was attempted by David G. Hong.

I gave the correct reason.

ASCII is irrelevant to OP's question.
 
M

Martin Ambuhl

prasi said:
hi all,
I have executed the following program, I was expecting some errors in
the program but it didn't give any ERRORS!!!!!!!!!!!!!!!!!!!!!!!!!!!
please can anybody explain me ?

No one can explain you. Let's get back to your code. What errors were
you expecting? Why? You would learn more from examining your false
expectations. Asking us to explain all the errors *not* in this code is
silly: that list would be inexhaustible.

And what makes you think wanton repetition of exclamation marks
accomplishes anything?
 
M

Martin Ambuhl

David said:
You have assigned a decimal value of 97 to char variable 'a'. This
infact is legal in C as characters use ascii conversion.

It is legal to assign to a char any value in the range CHAR_MIN to
CHAR_MAX. This has nothing to do with the character sets used.
There is nothing that specifies that C use ASCII (note caps). The word
'conversion' is a bit off the mark as well.
 
C

Christopher Benson-Manica

Martin Ambuhl said:
It is legal to assign to a char any value in the range CHAR_MIN to
CHAR_MAX.

Does it matter whether the char in question is signed or unsigned?
 
F

Flash Gordon

Christopher said:
Does it matter whether the char in question is signed or unsigned?

Not for the truth of Martin's statement, nor for the assignment of 97.
 
M

Martin Ambuhl

Christopher said:
Does it matter whether the char in question is signed or unsigned?

a char is in the range CHAR_MIN to CHAR_MAX,
a signed char is in the range SCHAR_MIN (-127 or less) to SCHAR_MAX
(+127 or more)
an unsigned char is in the range 0 to UCHAR_MAX (+255 or more)

The OP's code used 97, which is always in range for a char, signed char,
or unsigned char. He initialized a char with this value, so only
CHAR_MIN and CHAR_MAX are relevant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top