Exforsys
+ Reply to Thread
Results 1 to 6 of 6

beginner question in C ?

This is a discussion on beginner question in C ? within the C and C++ forums, part of the Programming Talk category; Hello, I am learning C through the book "The C Programming Language", 2nd edition, Kernighan and Ritchie I am on ...

  1. #1
    tolgao is offline Junior Member Array
    Join Date
    Jun 2011
    Answers
    1

    beginner question in C ?

    Hello,

    I am learning C through the book
    "The C Programming Language", 2nd edition, Kernighan and Ritchie


    I am on exercise, 1-10. I did not understand the below answer in this exercise.

    Question:
    Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way.

    Answer:

    int main()
    {
    int c, d;

    while ( (c=getchar()) != EOF) {
    d = 0;
    if (c == '\\') {
    putchar('\\');
    putchar('\\');
    d = 1;
    }
    if (c == '\t') {
    putchar('\\');
    putchar('t');
    d = 1;
    }
    if (c == '\b') {
    putchar('\\');
    putchar('b');
    d = 1;
    }
    if (d == 0)
    putchar(c);
    }
    return 0;
    }
    Why do you use two putchar statements:

    for example :

    putchar('\\');
    putchar('t');
    why do you need putchar( ' \\ ') ? What does it do ?

    also what is the purpose of variable d ?

    Also, Is there another way to write this code ?


    Thanks,
    Tolga


  2. #2
    Koorakula Bala Manikanta is offline Junior Member Array
    Join Date
    Aug 2011
    Location
    vijayawada
    Answers
    1
    hiiiiiiiii dude,

    we use // in the above program,because in output we can see the tab=\t symbol in the form tab and space=\s....

    see the out put :


    mani kanta bala

    mani\skanta\tbala

    (instead of space and tab ,\s \t)

    you try this

    printf(" \Hello World \\ backslash" );

    the output : Hello World \ backslash


  3. #3
    guptamahesh is offline Junior Member Array
    Join Date
    Aug 2011
    Answers
    2
    they are literaaly ambiguious you shud refer "let us c" book by yashwant karnetkar....if u dont have i have an ebokk...let me know


  4. #4
    poojadreams is offline Junior Member Array
    Join Date
    Aug 2011
    Location
    Hyderabad
    Answers
    22
    yashwant karnetkar book is very useful for basics. It is clearly explained. Even newbie can understand it. You will get basics very clear.


  5. #5
    kevinm is offline Junior Member Array
    Join Date
    Aug 2011
    Answers
    5
    I used to refer yashwant karnetkar. Nice book


  6. #6
    venkatjaya is offline Junior Member Array
    Join Date
    Jan 2012
    Answers
    8

    Cool

    Welcome to the home page for the Northampton College 2002-3 course for C Programming for Beginners.

    The course runs for 30 weeks on Wednesday evenings from 6pm-9pm in the ITAS building (rear car park by the management buildings - just pass the bicycle stands, up the stairs, and to your left), and is aimed at the complete novice to programming.

    You will learn the fundamentals behind how to design, write, test and document computer programs written in the C programming language - a language which has been used extensively throughout commercial and educational institutions for a variety of purposes for nearly thirty years, and is still in widespread use today. Many languages have their roots in C (such as C++, C#, and Java) so learning C is a good basis for learning many modern languages.

    Students can opt to take a City & Guilds examination at an appropriate point in the course in order to obtain a recognized qualification.







    ....................................................

    Last edited by admin; 02-13-2012 at 08:47 AM.

    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...