mxmora@sri-unix.SRI.COM (Matt Mora) (02/09/91)
I am a proud new owner of THINK C 4.0.2. Is there any goodies out there that would make programming in this environment even more fun? I'm looking for any INIT's,FKEYS, source code, headers,gotcha's or any general information I should know about. I have a beginning C programming question. This question came up in my C class and the instructor didn't now the anwser. Why do you have to use a double percent sign in a string literal to have it print a percent sign when all logic would indicate that a backslash percent sign should work? Example: Anytime you want to include a special character you preceede it with a back slash. printf("this will beep\a"); printf("this will tab\tthis part over"); printf("you are %d years old.\n",age); So you would think that since the percent sign is the mask in a string literal that if a backslash was before it, the compiler would do the right thing. printf("this won't work %d\%percent",intrate); This wll actually print out a pointer from who knows where. printf("this does work %d %%percent",intrate) Just curious Thanks Matt -- ___________________________________________________________ Matthew Mora | my Mac Matt_Mora@QM.SRI.COM SRI International | my SUN mxmora@unix.sri.com ___________________________________________________________
ech@cbnewsk.att.com (ned.horvath) (02/09/91)
From article <21005@sri-unix.SRI.COM>, by mxmora@sri-unix.SRI.COM (Matt Mora): > ...Why do you have to use > a double percent sign in a string literal to have it print a percent sign > when all logic would indicate that a backslash percent sign should work? > > Example: > Anytime you want to include a special character you preceede it with > a back slash. > > printf("this will beep\a"); > > printf("this will tab\tthis part over"); > > printf("you are %d years old.\n",age); > > So you would think that since the percent sign is the mask in a string literal > that if a backslash was before it, the compiler would do the right thing. > > printf("this won't work %d\%percent",intrate); > > This wll actually print out a pointer from who knows where. > > printf("this does work %d %%percent",intrate) The compiler does the right thing: \n, \t and the rest are special to the compiler, which builds the appropriate control character into the string constant. \% isn't special to the compiler, so it just puts % into the format string. % IS important to printf, which scans the format string for goodies (printf never sees a \ unless you code \\, OK?). Hope that helps... =Ned Horvath= ehorvath@attmail.com
hairston@henry.ECE.CMU.EDU (David Hairston) (02/09/91)
[mxmora@sri-unix.sri.com (Matt Mora) writes:] [] I have a beginning C programming question. This question came up in my [] C class and the instructor didn't now the anwser. Why do you have to use [] a double percent sign in a string literal to have it print a percent sign [] when all logic would indicate that a backslash percent sign should work? my guess, the '%' is interpreted before the '\' has a chance to cancel it. thus, the only way to escape '%''s is with an "operator" of like precedence. -dave- hairston@henry.ece.cmu.edu
vd09+@andrew.cmu.edu (Vincent M. Del Vecchio) (02/09/91)
mxmora@sri-unix.SRI.COM (Matt Mora) writes: > I have a beginning C programming question. This question came up in my > C class and the instructor didn't now the anwser. Why do you have to use > a double percent sign in a string literal to have it print a percent sign > when all logic would indicate that a backslash percent sign should work? > A subtle difference. The thing you need to know is that \a, \b, \n, \\, etc., get interpreted by the compiler. "foo\n" is represented as 4 characters after compilation. I'm not sure what K&R says about "\%", for example, or other combinations of "\<char>" which aren't specifically covered. I imagine that it would either be compiled to { '\\', '%' } or just { '%' } but I'm not sure which. The important point is that the backslashes are simply a device to represent otherwise unrepresentable characters to the compiler. The % operands, on the other hand, have no significance to the compiler and are perfectly legitimate parts of strings. For example, you can char a[10] = "%"; char *b = "d"; int i = 12; printf(strcat(a,b),i); and you will get "12". On the other hand, you can't do char a[10] = "\"; char *b = "n"; printf(strcat(a,b)); since a "\n" is actually a single character (here the compiler would complain that the string constant "\" is unterminated). The % operands only have special meaning to printf and scanf. When printf sees a "%" in your format string, it looks at the next couple of characters to see what kind of data you are sending it. printf could have been written to accept "\\%" (2 characters) as an instruction to print a single percent, but that would involve making '\\' a special character as well, and within printf, '\\' is not a special character. Also, then you would have to give it "\\\\%" to get it to print \% on your screen. Also, printf follows the more general convention that when you have a sort of escape character (like '\\' at the compiler level, or '%' at the printf level), doubling it removes any special meaning the character has. Hope this answers your question...
Bruce.Hoult@bbs.actrix.gen.nz (02/10/91)
Matt Mora writes: >Why do you have to use a double percent sign in a string literal to have >it print a percent sign when all logic would indicate that a backslash >percent sign should work? Unfortunately, that's the wrong logic :-) >So you would think that since the percent sign is the mask in a string >literal that if a backslash was before it, the compiler would do the >right thing. It does. :-) When you use \% the compiler does exactly the right thing -- it puts a % into the compiled format string, just the same as typing a % by itself does. The problem is that the format string isn't interpreted by the compiler, but by the runtime library, which sees only the %, not the \%. -- Bruce.Hoult@bbs.actrix.gen.nz Twisted pair: +64 4 772 116 BIX: brucehoult Last Resort: PO Box 4145 Wellington, NZ "And they shall beat their swords into plowshares, for if you hit a man with a plowshare, he's going to know he's been hit."
schorsch@oxy.edu (Brent William Schorsch) (02/17/91)
I have Think C v.4.0... can/should I upgrade?? is it going to cost me a lot of $$? is it worth it? thanks, -Brent (schorsch&oxy.edu) PS - thats upgrade to 4.0.2 or whatever the newest version is...