Questions tagged [string.h]

A header file from the C standard library that defines various functions for interacting with C strings.

In C a string is a null-terminated set of contiguous char values, often referred to via a pointer. The string.h header defines many common methods for interacting with such strings. These methods are guaranteed to be portable as a part of the standard library, but may have other limitations or security concerns.

138 questions
21
votes
6 answers

or ?

Which is the best way to include the standard header string.h in a C++ project? Using the [dot]h at the end, like this: #include or just writing #include Or, maybe, using another way that I don't know? Thanks!
Overflowh
  • 1,052
  • 6
  • 18
  • 39
12
votes
3 answers

What is the difference between memcpy() and strncpy() given the latter can easily be a substitute for the former?

What is the significant difference between memcpy() and strncpy()? I ask this because we can easily alter strncpy() to copy any type of data we want, not just characters, simply by casting the first two non-char* arguments to char* and altering the…
Thokchom
  • 1,544
  • 3
  • 14
  • 30
11
votes
2 answers

strcmp() return different values for same string comparisons

char s1[] = "0"; char s2[] = "9"; printf("%d\n", strcmp(s1, s2)); // Prints -9 printf("%d\n", strcmp("0", "9")); // Prints -1 Why do strcmp returns different values when it receives the same parameters ? Those values are still legal since…
Bilow
  • 1,808
  • 14
  • 24
7
votes
3 answers

Are strupr() and strlwr() in string.h part of the ANSI standard?

I was looking for this on internet and in every place with the functions of string.h these two are not mentioned. Is because what? They aren't in every compiler?
exsnake
  • 1,500
  • 2
  • 15
  • 36
7
votes
5 answers

Why copy function arguments into local variables?

What is the reason for strlcpy.c to copy the arguments into local variables: size_t strlcpy(char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; /* Copy as many bytes as will fit */ if (n…
Micha Wiedenmann
  • 17,330
  • 20
  • 79
  • 123
5
votes
1 answer

What do each of the str-function abbreviations/acronyms mean?

The C standard library string.h contains several functions to manipulate strings, all of which start with str and end with an abbreviation. Some of these abbreviations are obvious: strlen string length strcpy string copy strcmp string compare Some…
MD XF
  • 7,062
  • 7
  • 34
  • 64
5
votes
6 answers

strcat in c program is not working

#include #include void main() { char *str1="hello"; char *str2="world"; strcat(str2,str1); printf("%s",str2); } If I run this program, I'm getting run time program termination. Please help me. If I use this: char…
user3815049
4
votes
2 answers

including C/C++ headers in Xcode 4

I've been using a C++ library without problems on projects built with Xcode 3, but I'm now getting build problems on projects built with Xcode 4. Drop the library into the Xcode 4 project and it builds fine, but as soon as I #include it, I get a…
Eric
  • 12,878
  • 13
  • 78
  • 119
4
votes
7 answers

Why strlen function works without #include?

Quick question: strlen[char*] works perfectly regardless whether I #include or not All I get from compiler is a warning about implicit declaration, but functionally it works as intended. Why is that?
James Raitsev
  • 82,013
  • 132
  • 311
  • 454
4
votes
1 answer

how to use strxfrm in C language?

I ask a question to know the usage of "strxfrm" in C. I know the function is to transform a string according to current locale configuration. but I don't know what "transform" is, and how this function transforms. For example, I tried a code like…
4
votes
1 answer

How to inline string.h function on linux?

I want to optimize some code such that all the functions in string.h will be inlined. I'm on x86_64. I've tried -O3, -minline-all-stringops and when I do "nm a.out" it shows it is calling the glibc version. Checking with gcc -S, I see the…
tz1
  • 41
  • 3
4
votes
0 answers

definition for __ THROW __nonnull

I see the definition of memmove() and memcpy() which both contain the line __THROW __nonnull ((1, 2)); where can I find this defined ?
vaibhav.pnd
  • 135
  • 1
  • 7
4
votes
4 answers

Find the length of string array with strlen()

I have been trying to find the length of string which has an array of chars with strlen() function but it is not working. The code I am using is something like this: string s[]={"a","b","c"}; int len = strlen(s.c_str()); It produces the following…
DeadCoder
  • 87
  • 1
  • 2
  • 9
3
votes
2 answers

Function to Split a String into Letters and Digits in C

I'm pretty new to C, and I'm trying to write a function that takes a user input RAM size in B, kB, mB, or gB, and determines the address length. My test program is as follows: int bitLength(char input[6]) { char nums[4]; char letters[2]; …
hobbsdevon
  • 33
  • 3
3
votes
1 answer

Trying to read a two numbers from a text file

I am trying to read two numbers from a text file. I used strtok, and the delimiter is " ". The text file (named data.txt) file has only two numbers, the line is "3 5". But the program crashes... any help is appreciated. #include #include…
smiili
  • 63
  • 5
1
2 3
9 10