473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Printf Problem

Hi.I could not understand the following printf statement.Thanks for any
help.

int a=3,b=5;
printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);

I think a[i] equals to*[a+i].But I could not understand the use of &
here.Bye.
Regards,
Eric

Aug 20 '06 #1
4 1864
Ico
di**********@yahoo.com wrote:
Hi.I could not understand the following printf statement.Thanks for any
help.

int a=3,b=5;
printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);
Neither does my compiler. If you post any code, please make sure it
compiles. I will assume you missed some quotes and ment :

printf(&a["Ya!Hello!how is this?%s"],&b["junk/super"]);

This is an old trick, not to be used in real-life code. Due to the way
arrays an pointers work in the C language, the following two lines
have the same effect:

"hello"[2]
2["hello"]

Both expressions return 'l'. If I recall correctly, the clc faq has a
detailed explanation why this is the case.
I think a[i] equals to*[a+i].But I could not understand the use of &
In your example, the expression

&a["Ya!Hello!how is this?%s"]

is the same as

&"Ya!Hello!how is this?%s"[a]

A was set to 3 before, so this expression returns the address of the
string+3, which happens to be "Hello!how is this?%s"

The same happens with the second expression, which is used as an
argument for the %s directive in the printf format string.
--
:wq
^X^Cy^K^X^C^C^C^C
Aug 20 '06 #2
please make sure that the code is compiled properly
syntax for printf
printf (" variables" , " control sting " );
if you want to print an address of a variable use %i or % u

thanx
http://eecsatri.blogspot.com

di**********@yahoo.com wrote:
Hi.I could not understand the following printf statement.Thanks for any
help.

int a=3,b=5;
printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);

I think a[i] equals to*[a+i].But I could not understand the use of &
here.Bye.
Regards,
Eric
Aug 20 '06 #3
di**********@yahoo.com writes:
Hi.I could not understand the following printf statement.Thanks for any
help.

int a=3,b=5;
printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);

I think a[i] equals to*[a+i].But I could not understand the use of &
here.
No, a[i] means *(a+i), which is equivalent to *(i+a), which is
equivalent to i[a].

You should never use this trick in real code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 20 '06 #4
steve <ad********@gmail.comwrote:
please make sure that the code is compiled properly
Good advice.
syntax for printf
printf (" variables" , " control sting " );
It's difficult to tell what you meant by this, and difficult to
believe that you in fact know that the prototype of printf is

int printf( const char * format, ... );
if you want to print an address of a variable use %i or % u
Wrong. The format specifier for printing an address (which must be
cast to void * if it is not already of that type) is %p.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul E Johnson | last post by:
Dear friends in C: I'm completely baffled by this problem I have with printf statements and conditional operators in C. I'm using gcc-3.3. I have a project where the rarely used Union type...
18
by: Manohar S | last post by:
It is a problem related to printf, and buffering to the printf statements Look through this program. main() { int pid; int loop,max=3; for(loop = 0; loop <max;loop++) { pid = fork();
3
by: buzzdee | last post by:
hi, i just wanted to print out some unsigned long int values in hexadecimal, printing out one value works, but not byte by byte. anybody has a suggestinon what my problem is? this is my...
9
by: Eric Lilja | last post by:
Hello, I have two code snippets I want you to look at. My program compiles without warnings (warning level set to max, gcc 3.4.3) with either snippet but the latter one causes a segfault at...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
5
by: diadia | last post by:
#include <sys/types.h> #include <sys/stat.h> #include "ctype.h" #include <stdio.h> int estimateLen(struct stat buf, FILE *fp) { size_t _size = buf.st_size / 4;
19
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
0
by: sabya123 | last post by:
I have got a problem using printf and wprintf. Can I use them simultaneously in a single program? My problem is that, If I am using both of them in a program only the first one is being taken....
19
by: RedDevilDan | last post by:
I am working on a Memory Footprint Reduction project. I came across an idea to disable all printf statements so that less memory is required. In addition, when there is no single printf statement,...
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.