1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Basic C programming 1

Discussion in 'C' started by vivekgupta, Jun 11, 2012.

  1. vivekgupta

    vivekgupta New Member

    Joined:
    Jun 10, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    (1). #include<stdio.h>
    int main()
    {
    printf("The single quote \' is this ");
    }


    (2). #include<stdio.h>
    int main()
    {
    printf("The single quote ' is this ");
    }

    Questions

    (a)same output so why we take \' as escape sequence . It does not do anything special??



    (b) Why did these characters call "escape sequences" . ?
    Help plzz
     
  2. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    This is pretty simple to explain so here I go...

    your trying to print to the console

    "The single quote ' is this "

    take note that its surrounded by double quotes " ". the ' is a single quote so you do not need to escape it unless it's surrounded by single quotes. printf requires double quotes so there is no need to worry about escaping a single quote unless your enclosing something in between them. Your also not formatting your text for out put so there shouldn't be any way of knowing the out put.

    printf() takes formatting and text or numbers.

    it should be like this

    (1).
    Code:
     #include<stdio.h>
    int main()
    {
    printf("%s", "The single quote \' is this ");
    }

    (2).
    Code:
    #include<stdio.h>
    int main()
    {
    printf("%s","The single quote ' is this ");
    }
    %s means string but you can also use %d for decimals, %c for character, and %f for float. Make sure formating is the first parameter to printf() followed by a comma and the out put data.

    Code:
    #include<stdio.h>
    
    void main()
    {
    
    printf("%s","This is a string \n"); // string
    
    printf("%f \n", 12.45); // float
    
    printf("%d \n",56778); // decimal
    
    char i = scanf("i");
    }
    the above code will print out the values with a newline after each print. That is how you use printf().
     
  3. vivekgupta

    vivekgupta New Member

    Joined:
    Jun 10, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    one more question :P

    Why dont we use '&' in the case of strings in function scanf ??

    e.g. :-


    scanf("%s",date);

    here date is a character arraymeans string.


    Plz help
     
  4. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    using & next to a variable's name passes the value by reference. You don't need to do that if it's already a pointer. Try doing some simple pointer and reference output and see what you get.
     

Share This Page