Failed to guess the correct output [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Basically I am a Java programmer, and not very well know about pointers in C.
so,
#include<stdio.h>
int main( ){
char*______Time______ = "world";
printf("%s",_____Time_____);
return 0;
}
I guess the output here should be: world ?
Is something spooky here which I should know?
Thanks for any help.

This should print "world", yes.
It looks a bit like it's trying to play with the GCC built-in preprocessor symbol __TIME__, but of course it's spelled wrong to do that.

I expect to see world, but your shell might then see that the last command ended without a newline and it might add something to signify that before starting its prompt on a fresh line.

and not very well know about pointers in C
A pointer is a variable which points to a specific address in the memory.
In this case, it points to the first letter of "world", which is then printed by printf() all until NUL (automatically inserted at the end of strings).
So, answering your question: yes, the output will be "world".

Related

C char array storing a variable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I would like to store 2 variables into char array, and print our the first one as shown below.
const char *a[2];
a[0] = getCapital(bufferStore); //"Australia"
a[1] = getCurrencyCode(bufferStore); "9876.00"
printf("%s", a[0]);
However, I did not get any output. The code of getCapital and getCurrencyCode should be redundant here. The main thing I want to find out is how I can print out "Australia". I'm new to C language and pointers are really hard to understand, and my assignment is due in 2 hours. Any help will be greatly appreciated!
The file stdout, which is what printf writes to, is by default line buffered. That means everything you write to it is buffered, i.e. stored in memory, and is flushed (and actually printed) when you print a newline.

returning a buffer in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a couple of questions.
I need to write a program(winapi) that will create a buffer of a fixed size, then append strings to it and returns it.
1. Is it even possible for "main" to return a buffer?
2. How should I create, append string and return it?
I am not new to C, but I have very little experience with buffers and strings handling.
Thank you!
Is it even possible for "main" to return a buffer?
No and you shouldn't. What should main() return in C and C++?
How should I create, append string and return it?
char aString[FIXED_SIZE];
memset(aString, 0, sizeof aString);
strcpy(aString, "This is a string");
main in c only returns an integer, which is used to indicate the success or failure of the program.
See What should main() return in C and C++?
What you might want to consider is writing to stdout (What does it mean to write to stdout in C?) or direct to a file.

Loop thorough **variable in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a pointer of pointers, and I want a loop to go thorough them and store its value into something else. Is there any way to do that?
e.g:
char **variable;
Now I want to read that into another variable:
char **variable2
i thought of doing something like this:
for(i = 0;i <LENGTH_OF_VARIABLE-1;i++){
variable2[i] = variable[i+1]
}
But that is not possible in c, right?
Now you might ask why not variable2 = variable? well variable2 should store only parts of the variable, not all of them.
EDIT: Variable's size is not known, and its dynamic(read from the command line). AND no it doesn't contain '\0' at the end. Cause its processed to remove such a character and then passed to a function that I am implementing.
If you already putting anything to your **variable, does that mean that you have allocated memory correctly?
I think it will better for you to revise and understand how simple one dimensional array works, after understanding that, move to double arrays. Then take a look how pointers work and learn how to allocate memory. After understanding this steps i have mentioned above, take a look at double pointers and allocation of memory in case of double pointers.
here you go.

Printf its variations and its prototype understanding [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can anybody explain the variations in printf syntax..i am confused in some of it
like
printf(5+"hello my friend");//i have problem this addition of 5
printf("hello""my""friend");//i have problem with double apostrophe
what kind of printf prototype do these follows ?
Is this have anything to do with dynamic linking?
Can anybody show some other weird printfs and explain them.
A string in C is accessed via a pointer to a char (see H2CO3's comment for a more precise definition). If you add 5 to a pointer to a char, you start the string 5 characters later. So 5+"hello my friend" points to " my friend", skipping "hello".
When a C compiler sees two strings with nothing (except possibly whitespace) in between, it treats them as a single string. This makes it easier to break long strings in multiple lines.
So "hello""my""friend" compiles into exactly the same thing as "hellomyfriend" or
"hello"
"my"
"friend"
None of these has anything to do with printf, and a lot to do with strings.

Is printf("%d",*(++(*a))); Undefined [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is printf("%d",*(++(*a))); Undefined? Here a is pointer to a pointer to integer.
I do not have anymore code other than this.It is an extension of This question which had created lots of confusions.Just want to know what is happening in this print.Does it depend on Architechture(32 Vs 64) or compiler versions.
Hoping answers will be descriptive and clear.
If you break it down, it does this:
Take the value of what a is pointing at: *a
Increment by one ++(*a)
Dereference that *(++(*a))
So, if the value+1 of what is stored at a is a valid pointer, this will work. Otherwise, the result is undefined and will most likely result in a runtime error.
Yes, your code is correct and even if cryptic can make some (little) sense as in:
void print_next(int **a) {
printf("%d\n",*(++(*a)));
}
int arr[] = {1,5,6,3,5,6};
int *p = arr;
while (p<arr+6)
print_next(&p);
If your question is specifically about *(++(*a)) expression, then there's nothing undefined here (assuming all pointers involved are valid). There are no attempts to perform multiple modifications of the same object. There are no independent reads of any of the modified objects. End of story.
Basically, there's nothing to explain here, since the code is perfectly fine in a rather straightforward manner. There's really no room for anything more "descriptive and clear" than that.
If this is not sufficiently clear, you have to explain what exactly looks suspicious to you in this expression.

Resources