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 am working on a program plot.c to plot a function f(t). I can't use nested For loops unfortunately which make this harder for me.For example a function like : f(t)=t^2-4t+5.
The values of t will be between two values specified as low and high in the program. For each value of t, i want to store an asterisk in the element of a string (i.e. an array of chars) corresponding to the function value f(t), while all leading elements before the asterisk are blank.
This is of course assuming that the f(t) values are rounded to integers.In terms of the array in C, its size could be a variable. For instance:
int m=3*6;
char ex[m];
Here is what the outputs are supposed to look like :
If you have integer values, you could abuse printf format specifications.
If you want to print a * preceeded by n spaces, you could use something like:
printf("%*s\n", n, "*");
Bear in mind tha tif this is for a class assignment, you also need to be able to explain why this works, but the relevant section should be in the man page for printf.
Related
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.
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
In C, is it possible to force an unsigned char to print as a hex, not with %x, but in the little square box that displays the four hex numbers, but only takes up one character, for example 轩?
No, it is not possible.
This is not related to C, but to the font your terminal uses. C outputs characters as bytes; your terminal is responsible for choosing how to display them as pixels. (You might as well ask how to make the text italic.)
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
I'm trying to take user input using the getline() function. I store the input and point to it with a char *pointer.
Now I want to split the string at the white space, if there is any, but I can't change a string literal. So my idea was to transfer a copy of the input to a char array so I could then play around with it. The only issue is I don't know the size of the users input yet so I can't specify the size of the array I want.
Any ideas how I can get around this, I'm probably missing something, I'm new to C from a Java background.
Many Thanks!
You read the line, figure out its size, then make a copy of that size.
If you store a user input with some function getline (there is no such a function in C Standard) then you can split it into tokens by using standard C function strtok declared in header <string.h> If you do not want to change the original string then you can write the required function yourself by means of searching blank and non-blank characters in the string.
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
Why isn't the following possible?
printf("%d", "%d", 2, 4);
This would be a useful feature. Is it due to technical imitation or design reasons?
How would printf() know where the formatting strings ended and the actual values began? You're not including any such information, which is the entire point of the formatting string: describing the variable number of arguments so the code inside printf() knows how many arguments to process.
And in what way is that better than
printf("%d%d", 2, 4);
?
Also, the printed result ("24", without newline) would be pretty hard to interpret in any way, so you'd might as well add spacing, which will help make the formatting string more readable:
printf("%d %d", 2, 4);
That will print "2 4" (again, without newline).
Note that in C, there's no way for a variable-arguments function (printf() in this case) to somehow determine the amount (or types!) of its argument(s). It has to know, or be able to compute on its own based on some of the arguments (or some other state).
Also, if wanted to print "%s", I wonder how you imagine
printf("%s", "%s");
should work?
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.