This question already has answers here:
Octal representation inside a string in C
(2 answers)
Closed 5 years ago.
#include<stdio.h>
#include<string.h>
int main() {
char a[100]="1234\0567"; //Answer: 6
int len=strlen(a);
printf("%d",len);
}
This code prints 6 as the answer. Shouldn't it print 4 since strlen returns the count until the null character is encountered?
However when a space is included between \0 and 5. It returns 4 as the answer
char a[100]="1234\0 567"; //Answer: 4
Am I missing something?
You first version has the octal number \056 not the \0 character
Edit:
in the similar situations use \000 octal sequence instead:
/* from your example*/ char a[100]="1234\000567";
The \056 is a single character, with the octal ASCII code (56)8, decimal - (46)10.
Related
This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Using printf with a non-null terminated string
(6 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
char as[4];
*as='0';
*(as+1)='1';
*(as+2)='2';
*(as+3)='3';
printf("%s",as);
return 0;
}
The output i got is : 0123.
In the above program i declared an array of size 4 -> char as[4];
and in that i stored 4 chars 0,1,2,3.
Normally if we declare a char array, '\0' will be stored at last (i.e. as[3]='\0');
but i stored '3' in it. how it did not generate error?.
There is absolutely nothing stopping you from using an array of char as an array of char. There could be any number of reasons to want this.
However, C strings are null-terminated by definition. Using the %s specifier in printf tells it to expect a null-terminated string, so your program will (probably) not work correctly unless you give it such a string.
This question already has answers here:
How can I convert an int to a string in C?
(10 answers)
Closed 3 years ago.
Please read till the end.
I have a function that loops through a character array until it finds the '\0' end character in a character array.
I want to convert the integer
int number = 128;
To a character array
char data[] = ""; // data[] = "128" , contains '\0'
I have found several ways suggested (memcpy, itoa) but I want a conversion that adds the '\0' automatically at the end.
itoa() automatically adds a null terminator.
See: http://www.cplusplus.com/reference/cstdlib/itoa/
This question already has answers here:
Getting wrong string length
(3 answers)
Closed 4 years ago.
I have this piece of code:
char* input = malloc(sizeof(char)*100);
scanf("%s", input); //let's say that the number of chars in "%s" is 5
How do I calculate how many chars I typed in (5)? I tried by playing around with sizeof(), but couldn't find a solution.
Edit (better explanation): the input variable can host up to 100 chars, but let's say I type in the terminal 'abcde': then it hosts only 5 chars, the other 95 are not taken. I want to calculate that '5'.
You have to find the null terminator.
int i = 0;
while(input[i] != 0) {
++i;
}
//i marks the spot
But yeah, strlen() does a better job, since it has some improved/optimized searching, since it uses word(16/32/64? bit) compare and stuff.
This question already has answers here:
When initializing a char array, is the remaining space zero filled or uninitialized?
(2 answers)
Closed 9 years ago.
I was declaring a char array like this-
char str[16]= "The world is";
I know that its 13th character will be '\0'. But I am curious about rest of the character values till 16 in str. Are all of them assigned to '\0'. I searched it but could not find a good explanation. So I thought another fastest way is to ask here.:)
The rest will contain zero. What you are actually doing is initializing your array as follows:
char str[16]= {'T','h','e',' ','w','o','r','l','d',' ','i','s','\0'};
If you initialize an array in C that you have given a length, with something that is shorter, C will fill the rest with 0.
Example: this will give an array filled with zero's
int str[16]= {0};
so what you wrote is equivalent to:
char str[16]= {'T','h','e',' ','w','o','r','l','d',' ','i','s','\0',0,0,0};
Note that 0 == '\0' (both 0000...)
This question already has answers here:
Printing leading 0's in C
(11 answers)
Print numbers sequentially using printf with filling zeroes
(8 answers)
Closed 9 years ago.
I was trying to print a integer in c but those starting with zeroes causing me problem.
For example if no. is 01234 it is printing like 1234 instead of 01234.please tell how to do it in C
My problem is that there are 2 integers and I want to know whether first integer is in the starting of second or not.
for ex-
123 and
12345 "yes" because 123(first integer) is in the beginning of second integer(12345)
but in case
123 and
012345
it should print "no" because 123 in not in the beginnig of 0123345 but in c trailing zeroes get deleted and my program is printing "yes"
please tell what to do (note-no.of digits can vary in range of integer and 2nd integer is either equal or greater then 1st integer)
int i = 1234;
printf("%08d", i); // zero-pad to 8 places.
printf documentation
Working example
my suggestion would be , If you are taking the value from STDIN from the user.
Then if you want to use that value for printing purpose, then you need to store that integer with leading zeros into a string rather than an integer. Because leading zero has no meaning if you are storing that string value in an integer.
so %s in printf with retain the number of zeroes that user has enetered in that way.
#include <stdio.h>
#include <string.h>
#define N_DIGITS 64
main()
{
char a[N_DIGITS], b[N_DIGITS];
scanf("%s%s", a, b);
if(strncmp(a, b, strlen(a))==0)
puts("yes");
else
puts("no");
return 0;
}
Note that the length of digits is limited by value N_DIGITS.
The result is as below;
$ ./a.out <<<"123 12345"
yes
$ ./a.out <<<"123 012345"
no