How to convert int to string in C? [duplicate] - c

This question already has answers here:
How can I convert an int to a string in C?
(10 answers)
Closed 7 years ago.
I'm trying to convert an integer to a string in C but the current code doesn't make it.
I'm not seeking to display it in the screen, so all the functions printf, sprintf... are irrelevant.
int X = 15;
char *T;
T = (char*)X;
// Expected result : "15"
Can anyone help please ?
Thanks.

Not displaying it to screen doesn't invalidate functions like sprintf() since they literally "print to string".
int X = 15;
char buffer[10];
memset(&buffer, 0, sizeof(buffer)); // zero out the buffer
sprintf(buffer, "%d", X);
// Expected result : "15"
printf("contents of buffer: %s\n", buffer);

sprintf will print to a string, not the screen.
It's exactly what you're looking for.

Related

How does C programs know the size of a string given a pointer to char? [duplicate]

This question already has answers here:
Does printf terminate every string with null character?
(3 answers)
What's the rationale for null terminated strings?
(20 answers)
Closed 24 days ago.
I would like to understand why this code works:
void open_image(const char *filename)
{
char *ImageName = filename;
printf("%s", ImageName);
}
const char image_name[] = "image.jpg";
open_image(image_name);
it prints "image.jpg" as wanted, but I don't know how the program knows the length of the string.
The program knows the size of the string image_name as it is computed during compilation.
But in the open_image function, how does the printf function knows the length of this string as it is only given the pointer ImageName created at runtime?
Thank you.
In C a string (array of char) is always terminated by the "terminator" character '\0', so you can get the length of the array by checking each character until you find it.
Here's a simple solution:
int i = 0;
char ch;
do
{
ch = arr[i];
i++;
} while(ch != '\0');
printf("String length: %d\n", i-1); // This will give the string length
printf("Total length of the array: %d\n", i); // This will give the array full length, including the terminator character.
Probably the best way to get a char array length is to import the string.h library which exposes the strlen() function.
However, it's always useful to learn how things actually work at low-level, especially when learning C). :-)

How to print a char repeatedly using format specifier instead of loop? [duplicate]

This question already has answers here:
How to repeat a char using printf?
(12 answers)
Closed 2 years ago.
I want to print a char '*' repeatedly where I give the no. of times the asterisk should be repeated.
Example: count = 20 and I want to print ******************** using printf() and format specifiers.
There is certainly no way to achieve that using only format specifier. You could hide your loop in a macro maybe but you'll definitely need a loop somewhere.
You cannot do that with standard format specifiers provided by printf(). However, there's a hacky solution (assuming the maximum padding length is reasonable), if you are willing to waste some space to store a filler string in your program.
#include <stdio.h>
int main(void) {
const char *fill = "********************"; // 20 chars
printf("%.*s\n", 10, fill);
printf("%.*s\n", 15, fill);
int n = 20;
printf("%.*s\n", n, fill);
return 0;
}
This works using .* to provide the maximum length of the string to print as first parameter.
Output:
**********
***************
********************
NOTE: you will only get up to strlen(fill) characters of padding (20 in the above example), anything more and printf will stop at the \0 terminator of fill.
I hope this is what you are looking for:
#include <stdio.h>
int main()
{
printf("%.*s", 20, "********************************");;
return 0;
}

Can anyone explain me the working of this C code? [duplicate]

This question already has an answer here:
What is the meaning of "%-*.*s" in a printf format string?
(1 answer)
Closed 3 years ago.
I don't know how this code is working?
#include<stdio.h>
int main()
{
char *s = "PRO coder";
int n = 7;
printf("%.*s", n, s);
return 0;
}
The result I am getting is "PRO cod"
printf format string %.*s takes two arguments, * for the number and finally s for a string, so it prints the first 7 characters of the string pointer s. In general, anytime there is a number, you may use * instead to read it as an argument.
%7s would print seven characters or more if the string were longer, while %.7s would print up to seven characters. So sometimes one would write "%*.*s", 7, 7, s to print exactly 7 characters.

How concatenate string and variable to a string in C [duplicate]

This question already has answers here:
How do I concatenate const/literal strings in C?
(17 answers)
Closed 4 years ago.
I need to concatenate a string and a variable to fill another string:
#include <stdio.h>
int main(){
char *te1;
char *te2;
char *tabela[2];
te1 = "text 1";
te2 = "text 2";
tabela[0] = "text 0, " + te1 + ", " + te2;
printf("%s\n", tabela[0]);
return 0;
}
expected result:
text 0, text 1, text 2
Ooof! You need to read up on C, pointers and arrays.
Firstly, you need to allocate some space, either using malloc and assign to a pointer or char buffer[200]
Next be aware that you cannot add strings like other languages. You need to either strcat or one of its variants or sprintf(buffer, "text 0 %s, %s",te1, te2);
There's some clues but with no ill intent meant, you really do need to ready up and understand some fundamentals.

How do i change an int variable to a string variable in c? [duplicate]

This question already has answers here:
Converting int to string in C
(14 answers)
Closed 8 years ago.
You read the question I need to be able to scan a int value and print it back out as a string variable
If you just want to print it back, you can use the "%d" specifier when using printf. If you want to convert the integer value to a character string for other purposes, you can use itoa.
YOu should use sprintf instead of itoa as it iota is not a starndard
int aInt;
scanf("%d", &aInt)
char str[50];
sprintf(str, "%d", aInt);
detail use of sprintf
You can use the sprintf function to do it.
int a;
// [log(2^63 - 1)] + 1 = 19 where
// [x] is the greatest integer <= x
// +1 for the terminating null byte
char s[19+1];
// read an integer
scanf("%d", &a);
// store the integer value as a string in s
sprintf(s, "%d", a);

Resources