0

I want to print my output in which only x coordinate is printed out.

      1.  printf("sphere{\n");
      2.  printf("\t<%g %g %g>,\n", V3ARGS(ell->v));
      3.  printf("//<\n %g \n// %g %g>\n }", V3ARGS(ell->a));

In third line of code, after 1st variable I put '//' because its a sign of comments i.e. I am printing 2nd and 3rd variable as comment in output file. Is there any way in which I can print out only 'x' value i.e. 1st variable?

user3717474
  • 47
  • 2
  • 8
  • 1
    Welcome to Stack Overflow. Please read the [About] page soon. Since you've not told us anything about what `V3ARGS` is (it is probably a macro, and one that expands to multiple comma-separated values), and not told us anything about the structure type of the pointer variable `ell` (beyond it has elements `v` and `a`, but we don't know what type that is), this is insoluble. You don't mention which software package you're using, even. Without this auxilliary information, your question is unanswerable. – Jonathan Leffler Aug 12 '14 at 00:54
  • Note that `//` only marks the start of a comment in C source code, and only when it appears outside of a string (or character) literal. – Jonathan Leffler Aug 14 '14 at 03:27

2 Answers2

1

Here is some code that could be representative of what you're using, plus macros to extract one argument at a time:

#include <stdio.h>

#define V3ARGS(v)   (v).x, (v).y, (v).z
#define ARG_1(x, y, z)   (x)
#define ARG_2(x, y, z)   (y)
#define ARG_3(x, y, z)   (z)
#define ARG1(v)   ARG_1(v)
#define ARG2(v)   ARG_2(v)
#define ARG3(v)   ARG_3(v)


typedef struct Point_3D
{
    double x;
    double y;
    double z;
} Point_3D;;

typedef struct DoublePoint
{
    Point_3D    a;
    Point_3D    v;
} DoublePoint;

int main(void)
{
    DoublePoint d = { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 } };
    DoublePoint *ell = &d;
    printf("sphere{\n");
    printf("\t<%g %g %g>,\n", V3ARGS(ell->v));
    printf("//<\n %g \n// %g %g>\n }\n", V3ARGS(ell->a));
    printf("a.x = %g\n", ARG1(V3ARGS(ell->a)));
    printf("a.y = %g\n", ARG2(V3ARGS(ell->a)));
    printf("a.z = %g\n", ARG3(V3ARGS(ell->a)));
    printf("v.x = %g\n", ARG1(V3ARGS(ell->v)));
    printf("v.y = %g\n", ARG2(V3ARGS(ell->v)));
    printf("v.z = %g\n", ARG3(V3ARGS(ell->v)));
    return 0;
}

The trick here is the two levels of macro, ARG1 and ARG_1. Note that ARG1() takes a single argument, expected to be an invocation of V3ARGS, and then calls ARG_1(). This trick expands the macro argument into the three comma-separated arguments expected by ARG_1(), which then 'returns' just the first of those arguments.

The output from the code shown (where the first output is the code you showed in the question with a newline at the end of the output):

sphere{
    <4.4 5.5 6.6>,
//<
 1.1 
// 2.2 3.3>
 }
a.x = 1.1
a.y = 2.2
a.z = 3.3
v.x = 4.4
v.y = 5.5
v.z = 6.6
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

As @JonathanLeffler pointed out, whatever your V3ARGS(...) is, it's probably a #define macro that resolves to a comma list like x, y, z. This means you are essentially asking what format string only prints x if you said:

printf("some format string here", x, y, z);

You're in luck, as it so happens that it is legal to pass fewer format specifiers than you pass arguments:

Passing too many arguments to printf

So if you only want the first argument, then only use one specifier:

printf("%g", x, y, z);

It gets trickier if you wanted just the second, or just the third. Because there's no generalized way to skip values; the format specifiers are consumed in order (though there is a POSIX extension to access by number):

Is there a "null" printf code that doesn't print anything, used to skip a parameter?

So if you got into a case like this and wanted to just print z, and couldn't modify the macro, this should work in ANSI C99:

typedef struct {
   double x,
   double y,
   double z
} Coordinate;

Coordinate temp;
temp = (Coordinate){ V3ARGS(ell->a) };

printf("%g", temp.z);

That takes advantage of the fact that structure fields can be initialized by a comma delimited list in that style of assignment.

But you might be better served by looking at the definition of V3ARGS. Because it may well be doing something like:

#define V3ARGS(foo) (foo).x, (foo).y, (foo).z

In which case you could just say something like:

printf("%g", ell->a.z);
Community
  • 1
  • 1
  • Macro is defined as #define V3ARGS(a) (a)[X], (a)[Y], (a)[Z] #define VPRINT(a, b) \ fprintf(stderr, "%s (%g, %g, %g)\n", a, V3ARGS(b)); So I edit my file as printf("sphere{\n"); printf("\t<%g %g %g>,\n", V3ARGS(ell->v)); printf("//(\n %g \n//)\n }", V3ARGS(a)[X]); but it gives error as In function ‘primitive_func’: /home/gurwinder/brlcad/src/conv/g-xxx.c:283:36: error: ‘a’ undeclared (first use in this function) /home/g-xxx.c:283:36: note: each undeclared identifier is reported only once for each function it appears in – user3717474 Aug 12 '14 at 02:56
  • 1
    You should paste the macro into the actual question and format it appropriately. – Retired Ninja Aug 12 '14 at 03:06
  • 1
    @user3717474: You should indeed paste the code as code into the question, preferably as an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)) or SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) — two names (and links) for the same basic idea. Note that my answer also contains an MCVE (or SSCCE). – Jonathan Leffler Aug 12 '14 at 03:09
  • Note that if you have the compiler set to fussy (and it is GCC), then it will warn you about unused arguments passed to `printf()` et al. The code in my answer has the merit of compiling under stringent warning options (with GCC 4.9.1 on Mac OS X 10.9.4): `gcc -O3 -g -std=c11 -Wall -Wextra -Werror x3.c -o x3`. – Jonathan Leffler Aug 12 '14 at 03:13