Question in C

M

Mark Storkamp

Mateusz_madi said:
Hi why when i write:
printf("%c", \11); i get S??

That's odd, I get "error: stray '\' in program"

How about posting a copy of a complete compilable program.
 
C

Coos Haak

Op Wed, 29 Dec 2010 09:42:11 -0800 (PST) schreef Mateusz_madi:
Hi why when i write:
printf("%c", \11); i get S??

You don't, the program will not compile because of a stray '\'.
 
K

Kenny McCormack

Op Wed, 29 Dec 2010 09:42:11 -0800 (PST) schreef Mateusz_madi:


You don't, the program will not compile because of a stray '\'.

Interesting that you say that...

On my system, I get S, just like the OP.

--
One of the best lines I've heard lately:

Obama could cure cancer tomorrow, and the Republicans would be
complaining that he had ruined the pharmaceutical business.

(Heard on Stephanie Miller = but the sad thing is that there is an awful lot
of direct truth in it. We've constructed an economy in which eliminating
cancer would be a horrible disaster. There are many other such examples.)
 
M

Mateusz_madi

That's the whole code:

----------------------
#include<stdio.h>

char input[]="SSSWILTECH1\1\11W\1WALLMP1";

main()
{
int i,c;


for( i=2; (c=input)!='\0'; i++){
switch(c){
case 'a' : putchar('i'); continue;
case '1' : break;
case 1 : while( (c=input[++i])!='\1' && c!='\0');
case 9 : putchar('S');
case 'E' : case 'L' : continue;
default : putchar(c); continue;
}
putchar(' ');
}
putchar('\n');
}
------------------------
Additionally i have question how: /*case 'E' : case 'L' : continue; */
works, and what is the difference between /* case 1 */ and /* case '1'
*/

Regards
 
R

Ralf Damaschke

[...] Coos Haak said:
Op Wed, 29 Dec 2010 09:42:11 -0800 (PST) schreef Mateusz_madi:
Hi why when i write:
printf("%c", \11); i get S??

You don't, the program will not compile because of a stray '\'.

Interesting that you say that...

On my system, I get S, just like the OP.

OK, then I suggest you to study your compiler documentation for
the meaning of the diagnostic message "S". You apparently know
little about C, but believe me: the statement above requires a
diagnostic message and those have to be documented by the
implementation.

-- Ralf
 
K

Kenny McCormack

[email protected] (Kenny McCormack) said:
[...] Coos Haak said:
Op Wed, 29 Dec 2010 09:42:11 -0800 (PST) schreef Mateusz_madi:

Hi why when i write:
printf("%c", \11); i get S??

You don't, the program will not compile because of a stray '\'.

Interesting that you say that...

On my system, I get S, just like the OP.

OK, then I suggest you to study your compiler documentation for
the meaning of the diagnostic message "S". You apparently know
little about C, but believe me: the statement above requires a
diagnostic message and those have to be documented by the
implementation.

-- Ralf

Gotcha!

--
One of the best lines I've heard lately:

Obama could cure cancer tomorrow, and the Republicans would be
complaining that he had ruined the pharmaceutical business.

(Heard on Stephanie Miller = but the sad thing is that there is an awful lot
of direct truth in it. We've constructed an economy in which eliminating
cancer would be a horrible disaster. There are many other such examples.)
 
M

Mateusz_madi

Ok, wrong question asked, why: printf("%d", '\11'); gives 9 ?? What
this \ means ?
 
B

Ben Pfaff

Mateusz_madi said:
Ok, wrong question asked, why: printf("%d", '\11'); gives 9 ?? What
this \ means ?

In this case, it introduces an octal constant. Octal 11 is
decimal 9.
 
M

Mateusz_madi

HA! That mae sense, so when i put in the test "something...\12" values
\xx will mean the same as 012??
Regards
 
J

Joachim Schmitz

Mateusz_madi said:
Ok, wrong question asked, why: printf("%d", '\11'); gives 9 ?? What
this \ means ?

'\nnn' is number in octal. Octal 11 is 9 decimal.

Bye, Jojo
 
B

BartC

Mateusz_madi said:
Ok, wrong question asked, why: printf("%d", '\11'); gives 9 ?? What
this \ means ?

Probably '\11' specifies a character constant with the given numeric code
\11, where 11 is octal notation (why octal, I don't know; maybe everyone
used octal in the 70s).

And 11 in octal is 9.

I don't know how you managed to get S out of it.
 
J

Joachim Schmitz

BartC said:
Probably '\11' specifies a character constant with the given numeric
code \11, where 11 is octal notation (why octal, I don't know; maybe
everyone used octal in the 70s).

And 11 in octal is 9.

I don't know how you managed to get S out of it.

simple:

case 9: putchar('S');

(taken from the code the OP posted elsethread)

bye, Jojo
 
S

Seebs

That's the whole code:

Okay, the question you wrote then is probably the worst-asked question
I've ever seen on Usenet. What you wrote is totally, completely,
and utterly unlike the actual code in every way.
char input[]="SSSWILTECH1\1\11W\1WALLMP1";

Okay, so, you have a \11 here.
case 9 : putchar('S');

Look, you have something that does a putchar('S')!

I think you should consider the possibility that, if you're getting an 'S'
you don't expect, you might want to look closely at the code leading to
it. It says "case 9". Hmm.
Additionally i have question how: /*case 'E' : case 'L' : continue; */
works,

Cases fall through, so either case 'E' or case 'L' will hit the continue
statement, which jumps back to the top of the loop.
and what is the difference between /* case 1 */ and /* case '1'
*/

1 is the integer value 1. '1' is the integer value corresponding to the
numeral 1 in your current character set.

Computers store characters as numbers using some sort of encoding. On
particularly common encoding is ASCII, which provides definitions for
the letters and numbers used in English, plus some punctuation, stored
as values from 0 to 127.

A string in C is just a series of such numbers, terminated by one with
the value 0. In ASCII, the numeral 1 has the value 49, so "case '1'" will
match the numeric value 49... and if you have a string "1", the first
character of that string has the numeric value 49.

To answer the rest:

In strings, backslashes introduce an "escape sequence". This allows you
to insert special characters in the string that would otherwise be hard
to type. You can use \" to insert a double quote in a string, for instance.
Of particular interest are the hexadecimal and octal escape sequences:

\045 => character whose octal value is 045
\x45 => character whose hexadecimal value is 0x45

0x45 is 4 16s plus a 5, or 69 decimal, and corresponds to 'E' in ASCII.
045 is 4 8s plus a 5, or 37 decimal, and corresponds to '%' in ASCII.

If you have a \ followed by numbers between 0 and 7 inclusive, it
is an "octal escape". So \1 is the value 1, and \11 is the value
(one eight plus one one) = 9.

So the \11 in your string becomes a character with the numerical value 9,
which isn't a printable character, but which 'case 9:' picks up.

-s
 
S

Seebs

OK, then I suggest you to study your compiler documentation for
the meaning of the diagnostic message "S". You apparently know
little about C, but believe me: the statement above requires a
diagnostic message and those have to be documented by the
implementation.

You're talking to "Kenny". Kenny is a liar. He has stated in the past
that there is *nothing* important in this life other than social status.
Because he's a moron, he resents the tendency of this newsgroup to respect
people who know what they're talking about and correct errors. As a result,
he reacts to any correction of errors by declaring them to be "wrong" in
some way.

Thing is, he does this *totally* without regard to facts, which he regards
as unimportant. Since he saw a post "correcting" the OP, he declared by
fiat that the correction was wrong and the OP's description was correct,
so he posted claiming this had happened.

If you interpret this as a claim about the real world and its behaviors, you
have already missed the point of anything Kenny wrote. What he was writing
would have been expressed, by a more competent writer, as:

It offends me that you are telling someone that there is something
wrong with code rather than helping him by answering his question.
Since I am a total fucking moron whose head is so far up his ass
that I wear Klein bottle sweaters, it has not occurred to me that
it is possible that the code is actually totally buggered, and I'm
going to assume the program works just fine and you're talking
about how it could theoretically fail to work on some machine no
one's ever built, and post supporting the OP's account.

.... Of course, someone self-aware enough to write that wouldn't.

-s
 
S

Seebs

Ok, wrong question asked, why: printf("%d", '\11'); gives 9 ?? What
this \ means ?

This is the right question, though totally unrelated to what you said before.

I have also already answered it in another post.

-s
 
K

Keith Thompson

Mateusz_madi said:
Ok, wrong question asked, why: printf("%d", '\11'); gives 9 ?? What
this \ means ?

Look up "character constants" in your C textbook.

Another good resource is the comp.lang.c FAQ, <http://www.c-faq.com/>.
The FAQ is *not* a good way to learn C from scratch, but it's great
for clearing up the inevitable misunderstandings you'll run into as
you learn.
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top