returning a buffer in c [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 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.

Related

C char arrays for user input [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I rarely log on and am very new to the C language, therefore I apologize if this is a duplicate question or if this is a silly query.
I'm currently learning C and am hitting a wall with strings. I understand that char arrays are used in place of strings in the language. My question is, is there a better way than to assign an arbitrary value when declaring a char[] for user input(i.e setting the size of the array to one value when the user might enter more or less than that amount of characters)?
If you're on a POSIX system (basically anything that's not Windows), you can use getline(3) to do this. It will automatically allocate a buffer of the right size for you. Otherwise, you'll have to guess a length, allocate that, then read the input up to that length, and if you guessed wrong, use realloc to increase your guess and try again.

How to read multiple lines in string until specified character 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 6 years ago.
Improve this question
Need to read everything until say *** comes up:
Input:
Hey there
how are
you
***
Output:
Hey there
how are
you
Would have used scanf("%[^***]s) but can't read all lines at once.
Only having basic C knowledge
The way I would do this is read one line at a time (with a funcion such as fgets instead of scanf) and then see if the line that you last read is equal to ***. You can use use strcmp to do that but you can also do it by hand if you are not allowed to use strcmp for some reason.

How to use string comparison with if else? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible in c programming language to check string with if else, and if it is, can someone please explain how, with some basic exampel. I would like to check if string matches a certain word. Thanks very much. For example if(n==0) instead of 0 to be a word.
To check for the contents of a string [whether is matches another], you cannot use == operator. You need to use strcmp() function with if condition.
check the man page here
If the strings match, it will return 0, otherwise, it will return non-zero. However, this is case-sensitive. ["Hello" and "hello" are not same]
Suppose, you want to check whether your string strcontains Hello or not. A pseudo-code will look like
if (! strcmp(str, "Hello"))
printf("String contains Hello\n");
else
printf("No Hello!!\n");

What does "end-of-file" mean 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'm trying to learn C, and am having difficulty understanding what end-of-file means, in terms of a statement like "if fgets() attempts to read past the end of file".
I understand that one can mark an EOF by pressing certain key combos, and that char '\0' represents an EOF, but there must be something basic that I'm not understanding regarding my question, and I hope someone can help explain it to me>
A file is a finite sequence of bytes, just like a book is a finite sequence of words. Eventually you reach the end and there's nothing more to read.
A null character does not represent the end of a file, by the way — you're probably confusing that with the null character used to mark the end of a string in memory in C/C++. That's unrelated to files.

Failed to guess the correct output [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
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".

Resources