Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Basic C programming 1 (http://www.go4expert.com/forums/basic-c-programming-1-t28565/)

vivekgupta 11Jun2012 15:17

Basic C programming 1
 
(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

pein87 12Jun2012 00:06

Re: Basic C programming 1
 
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: C

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



(2).
Code: C

#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: C

#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().

vivekgupta 12Jun2012 02:19

Re: Basic C programming 1
 
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

pein87 13Jun2012 22:25

Re: Basic C programming 1
 
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.


All times are GMT +5.5. The time now is 16:06.