LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2012, 10:08 PM   #1
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
2-3 questions .


what is the difference among

printf("gcchen");
&
printf("hello""NevemTeve");
&
printf("pan64","NevemTeve");


and

"its my thinking that , printf works on base address" if i am correct than please say ::: yes/no !
now i tell you why i am thinking this , because printf(2+"hello");
so here printf() is fetching the base address of "hello" string and adding "+2" means 8 bytes in it !
and the output is :: llo

Last edited by tushar_pandey; 07-25-2012 at 10:15 PM.
 
Old 07-25-2012, 10:24 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
What language are you working in?
 
Old 07-25-2012, 10:31 PM   #3
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
i am working in c ,
 
Old 07-25-2012, 10:36 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Is this legal syntax in C?:
Code:
printf(2+"hello")
 
Old 07-25-2012, 10:42 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I think the C printf format is:
Code:
printf("<format string>", "<string to print>")
OR
printf("<format string>", <variable>)
None of your examples match that format
 
Old 07-25-2012, 10:42 PM   #6
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
@pixellany , please watch this

#include<stdio.h>

int main()
{

printf(2+"hello");

printf("\n");
return 0 ;
}

daa@daa-Aspire-5740:~/c$ ./a.out
llo
 
Old 07-25-2012, 10:48 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Interesting.....do you have a reference that discusses that syntax and why it would be used?

It does appear that it takes the base address of the string and adds 2 to it......Have you tried 1+, 3+, etc.?
 
Old 07-25-2012, 11:02 PM   #8
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
Interesting.....do you have a reference that discusses that syntax and why it would be used?
i am solving a question , which is present in my book "test your c skills , yahswant kanitkar" . but there is no solution for it .

and if i find the meaning of " printf("hello") , the working behind this statement , than it is easy !

Last edited by tushar_pandey; 07-25-2012 at 11:04 PM.
 
Old 07-25-2012, 11:20 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
> printf("gcchen");

Perfectly legal

> printf("hello""NevemTeve");

Perfectly legal, the two string literal concatenated by the compiler.

> printf("pan64","NevemTeve");

Perfectly legal, but compilation warning may arise: there is no %-sequences in format string, so the second argument is ignored.

> printf(2+"hello");

Perfectly legal, can be written as: printf("hello"+2);
or printf("llo");

or:
const char hellostr[]="hello";
printf (hellostr+2);

or
const char hellostr[]="hello";
printf (&hellostr[2]);
 
Old 07-25-2012, 11:37 PM   #10
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
@nevemTeve

Sorry , but how can you say that printf("gcchen"); is perfectly legal , and if it is perfectly legal than why this statement does not need the format specifier !

because in

> printf("pan64","NevemTeve");

Perfectly legal, but compilation warning may arise: there is no %-sequences in format string, so the second argument is ignored.


this statement it needs format specifier .

Last edited by tushar_pandey; 07-25-2012 at 11:38 PM.
 
Old 07-26-2012, 12:12 AM   #11
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
@nevem teve

Sir , "Sorry , but how can you say that printf("gcchen"); is perfectly legal , and if it is perfectly legal than why this statement does not need the format specifier !" // this line is clear , after reading this syntax "String that contains the text to be written to stdout . It can optionally contain embedded format tags that are substituted by the values specified in subsequent argument(s) and formatted as requested." from "http://v2.cplusplus.com/reference/cl...cstdio/printf/"

but why this is illegal .... printf("hello","nevemteve");

Last edited by tushar_pandey; 07-26-2012 at 12:15 AM.
 
Old 07-26-2012, 01:51 AM   #12
SIG_SEGV
Member
 
Registered: Jul 2012
Location: Banglore, INDIA
Distribution: Fedora-Core
Posts: 70

Rep: Reputation: 11
Hello tusshar,
check out this pgm.... find out printf's working..........


#include <stdio.h>
int main ()
{
printf ("\ngcchen");
printf ("\ngcc%xhen"); // will get junk value o/p for %x here
printf("\npan64","NevemTeve");
printf("\npan64%s","NevemTeve");
}
 
1 members found this post helpful.
Old 07-26-2012, 03:09 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,909

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Quote:
Originally Posted by tushar_pandey View Post
but why this is illegal .... printf("hello","nevemteve");
No, it is not illegal, it works, just the second argument ("nevemteve") will not be used at all, will be ignored.
You can also try: printf("hello", 1,2,54,7, "fe","ignored", "some more", "anything") and any number of arguments are legal, but will be ignored.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
New Linux user with a few questions - Networking and Drivers questions Techsniffer Linux - Newbie 9 04-04-2012 12:21 PM
Make questions and ARCH questions for gentoo/calculate linus72 Calculate 5 04-09-2010 12:53 AM
basic questions on hostname and domain name + related postfix questions Moebius Linux - Newbie 7 09-04-2007 11:50 AM
LXer: Questions Answered, Questions Posed - What Happened? LXer Syndicated Linux News 0 05-14-2007 02:46 PM
window manager questions and/or theme questions t3gah Linux - Software 2 02-27-2005 04:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration