473,545 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Char const for backslash

In trying to replace character literals for their char constant, I am
having difficulty printing the char constant for backslash. It instead
prints the char literal. How do I resovle this?
#include <stdio.h>

/*A program that reads keyboard input and reproduces it on the monitor
with some modifications.
It displays all newline character occurences as \n, tabs as \t and
backslashes as \\.

Basically, it replaces some of the characters with their character
constants.

*/

void main()
{
int c;

while ((c=getchar()) != EOF)
{

if ((char)c=='\n')
{
printf("\\n");
putchar(c);
}
else if ((char)c=='\t')
{
printf("\\t");
}
else if ((char)c=='\\')
{
printf("\\");
}
else
{
putchar(c);
}

} //end of while loop

} // end of main
I want that when I type a backslash, the output must be replaced with
\\, the character constant for backslash.
Nov 14 '05 #1
3 5739
Sathyaish <Vi************ ****@yahoo.com> scribbled the following:
In trying to replace character literals for their char constant, I am
having difficulty printing the char constant for backslash. It instead
prints the char literal. How do I resovle this?


Try printf("\\\\").

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Normal is what everyone else is, and you're not."
- Dr. Tolian Soran
Nov 14 '05 #2
Vi************* ***@yahoo.com (Sathyaish) writes:
In trying to replace character literals for their char constant, I am
having difficulty printing the char constant for backslash. It instead
prints the char literal. How do I resovle this?
#include <stdio.h>

/*A program that reads keyboard input and reproduces it on the monitor
with some modifications.
It displays all newline character occurences as \n, tabs as \t and
backslashes as \\.

Basically, it replaces some of the characters with their character
constants.

*/

void main()
int main (void)
{
int c;

while ((c=getchar()) != EOF)
{

if ((char)c=='\n')
Why do you cast `c' to `char'? It's completely unnecessary. Before it is
compared to the `int' constant '\n', the result of `(char)c' is promoted
to `int' anyway.
else if ((char)c=='\\')
{
printf("\\");


\\ in a string or character literal represents a single \, so "\\" is a
string literal consisting of a single backslash character. You want
"\\\\".

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #3
Thank you very much, Joona and Martin. You're so right.

Regards,
Sathyaish Chakravarthy.

Martin Dickopp <ex************ ****@zero-based.org> wrote in message news:<cu******* ******@zero-based.org>...
Vi************* ***@yahoo.com (Sathyaish) writes:
In trying to replace character literals for their char constant, I am
having difficulty printing the char constant for backslash. It instead
prints the char literal. How do I resovle this?
#include <stdio.h>

/*A program that reads keyboard input and reproduces it on the monitor
with some modifications.
It displays all newline character occurences as \n, tabs as \t and
backslashes as \\.

Basically, it replaces some of the characters with their character
constants.

*/

void main()


int main (void)
{
int c;

while ((c=getchar()) != EOF)
{

if ((char)c=='\n')


Why do you cast `c' to `char'? It's completely unnecessary. Before it is
compared to the `int' constant '\n', the result of `(char)c' is promoted
to `int' anyway.
else if ((char)c=='\\')
{
printf("\\");


\\ in a string or character literal represents a single \, so "\\" is a
string literal consisting of a single backslash character. You want
"\\\\".

Martin

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3896
by: electric sheep | last post by:
Hi, can somebody explain the following syntax to me. This is straight from a gnu info file: int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/"; I think the idea at play here is whether the pointer is constant or not ?
13
19337
by: Vijay Kumar R. Zanvar | last post by:
Hello, I have few questions. They are: 1. Is "const char * const *p;" a valid construct? 2. How do I align a given structure, say, at 32-byte boundary? 3. Then, how do I assert that a given object is aligned, say, at 32-byte boundary?
2
1568
by: Pinnacle | last post by:
Can anyone explain what is "const char* const*" mean??
2
6475
by: s88 | last post by:
Hi all: I saw the code likes... 7 #include <stdio.h> 8 int main(void){ 9 const char *const green = "\033[0;40;32m"; 10 const char *const normal = "\033[0m"; 11 printf("%sHello World%s\n", green, normal); 12 return 0; 13 }
24
2299
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other compilers like GCC though. Many thanks! - Kevin
1
1709
by: Kip | last post by:
Greetings, Could anyone explain to me how the parameter type works here? I am also interested in the technical details of what is actually happening on the stack. I have never seen a "char * const &szFoo". I usually just use "char *pszFoo" whenever I want to pass a string, or maybe "const char *pszFoo" if I know that I won't modify it....
7
8503
by: Eric | last post by:
Hi For this code, int getopt (int argc, char *const argv, const char *opts) what does the "char *const argv" mean? Does it equal to "char **const argv"? Or "char *const *argv"? Which is the const? Thanks
7
14673
by: xgngli | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of the const pointer (i.e. t)? I got a segmentation fault in the last for loop. Thanks in advance! #include <iostream> using namespace std;
1
2663
by: Philip.J.Yoon | last post by:
Hello, I have two functions: void function1(char* const names); void function2(const char** names) { //do other stuff
2
2015
by: itsolution | last post by:
Hi Guys, When a function has following arg type, update_data(const char *const * update_list,...) exactly what object type update_list can take? For example, I can call update_aaa() using following variale as an arg:
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.