It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Could someone help me in printing the following data in C, exact format specifier and procedure to print.
union
{
char c[8];
short s[4];
long l[2];
void * vfp[2];
} info;
When i try using printf("%s\n", info.c); and printf("%s\n",info.s). I got some garbage values.
Also i need help in printing those pointers.
Thanks for the support.
You need to understand that the usage semantics of a union. You can only read the type that you stored inside a particular object instance of union. And you can only store one type in a union at a particular instance in time.
So either you stored s or c, it cannot be both. If your intention is to store both the types then what you need is a structure and not a union.
First of all you're using a union. the memory is shared between those 4 arrays. I'm not sure you want that.
Second, the list of format specifiers is here. %s is for strings. %p is for pointers, and %ld can be used for a signed long.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
For example, I have a file, which says
char,5
int,6
Reading the above file, is it possible to declare 2 variable array in the code? So in future
if i add a new line it will automatically declare?
No, not in C.
You will need to write a script which reads this file and writes the c program.
In short, what you need is a C Source Code Generator.
Sure, just code exactly what you want. You can start with a structure that can hold either a character or an integer (with some boolean or integer to indicate which). Then you can allocate an array of them of any size.
When you read the first line, create an array of 5 such structures. Set their type variable to "char".
When you read the second lien, increase the size by 6. Set those six new ones to be integers.
And so on.
You can use an enum to track the type of each entry in the array. You can use a struct to hold the integer value, character value, (or just re-use the integer value) and type. You can make helper functions like isInteger, setIntegerValue, getCharacterValue, and so on.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the meaning of "f" in C's printf?
The f in printf stands for formatted, its used for printing with formatted output.
As others have noted, the trailing f indicates formatted output (or formatted input for functions in the scanf family).
However, I'll add that the distinction matters because it's important for callers to know that the string is expected to have format-specifier semantics. For example, do not do this:
char* s = get_some_user_input();
printf(s); // WRONG. Instead use: printf("%s", s) or fputs(stdout, s)
If s happens to contain % characters, printing it directly with printf can cause it to access non-existent arguments, leading to undefined behavior (and this is a cause for some security vulnerabilities). Keep this naming convention in mind if you ever define your own printf-like variadic functions.
If I'm not mistaken, printf stands for "Print formatted data to stdout".
printf allows for formatting, while print doesnt. Also, print doesn't exist in C. I don't even know what printg is.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to get the size of a file from the commandline in C using argv. I'm not too familiar with file i/o in C, so any pointers would be greatly appreciated. Thanks.
You've not stated the platform, but your C program is given an argument list when it is started, and the file names are strings. The POSIX function you'd probably use is stat(); it takes a pointer to a struct stat and will put the file's size into the st_size member of the structure.
The answer may be different on Windows; the POSIX subsystem will provide a stat() workalike (probably named _stat()), but there'll also be a native interface.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I just started in C programming and I know that "%d" defines that it will be a number value, as in:
int x = 9;
printf("X = %d", x);
getchar();
return 0;
but what are the other variable specifiers for C? (to define strings, and do they change for float, double, long, etc.?)
There's a good summary of the specifiers available at http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders .
Your question as phrased is meaningless. The %d is a format specifier, and has nothing to do with variable declarations.
If you were to google 'printf c', you would find many, many sites that answer your question.
You may want to pick up a copy of Kernighan and Ritchie's The C Programming Language. It short but fully packed. Working through this book is well worth the time.
But as to your question, you're asking about Format specifiers for string literals. You'll probably also want to look at escape characters soon (e.g. \n for new lines). Fortunately, it's all in K&R (the above book), specifically on page 153 (2nd edition).
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
ok guys,
this is a very good question in C code (not c++):
void *pName=function();
i'd like within "function()" to get the name of the pointer "pName", of course without passing it as an argument string to the same function.
and if you are really so good in C you can provide me the type of pointer, i mean for example:
char *pName=function();
i'd like to get name and type , so "pName" and "char" within the function.
thank you :)
This is not possible. The best you could do is write a macro (shudder) that passes the type and name as arguments to the function without you having to write them a second time.