Conversions, Strings and printf

This is a discussion on Conversions, Strings and printf within the C Programming forums, part of the General Programming Boards category; Consider the following code: #include <stdio.h> int main() { int i=8, j=9; char m[]=" \"file\""; char d = 'x'; printf("%c%s\n%d\n", ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    Question Conversions, Strings and printf

    Consider the following code:

    #include <stdio.h>

    int main() {
    int i=8, j=9;
    char m[]=" \"file\"";
    char d = 'x';
    printf("%c%s\n%d\n", d,m,i%j);
    return 0;
    }

    which prints:

    x "file"
    8


    There are two parts of this code that I am unsure of. One is "i%j" argument in the printf function. Exactly what does this call for (well obviously it ends up as 8 but why)? How do you read this to yourself?

    My second question is with regards to the string " \"file\"". Why are the "\" characters omitted in the output?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The % sign used in the i % j statement is a modulous function... It's the remainder of i/j

    The \ don't appear in "file" because they are C escape characters... In a string literal \" causes the quotation mark to be printed, where it would normally terminate the string.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,507
    << !! Posting Code? Read this First !! >>
    Read it, understand it, post readable code next time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need urgent help with structs and strings
    By superfly in forum C Programming
    Replies: 9
    Last Post: 06-07-2010, 02:15 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  4. malloc with arrays of strings
    By Lib in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 10:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21