473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 5731
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.helsinki.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
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 =...
13
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...
2
by: Pinnacle | last post by:
Can anyone explain what is "const char* const*" mean??
2
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 ...
24
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...
1
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 *...
7
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...
7
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...
1
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
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() ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.