Open In App

strlen() function in c

Last Updated : 29 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The strlen() function in C calculates the length of a given string. The strlen() function is defined in string.h header file. It doesn’t count the null character ‘\0’.

Syntax of C strlen()

The syntax of strlen() function in C is as follows:

size_t strlen(const char* str);

Parameters

The strlen() function only takes a single parameter.

  • str: It represents the string variable whose length we have to find.

Return Value

  • This function returns the integral length of the string passed.
strlen in c

C strlen() Function

Example of C strlen()

The below programs illustrate the strlen() function in C:

C




// c program to demonstrate
// example of strlen() function.
#include <stdio.h>
#include <string.h>
 
int main()
{
    // defining string
    char str[] = "GeeksforGeeks";
 
    // getting length of str using strlen()
    int length = strlen(str);
    printf("Length of string is : %d", length);
 
    return 0;
}


Output

Length of string is : 13

Important Points about strlen()

The following points should be kept in mind while using strlen():

  • strlen() does not count the NULL character ‘\0’.
  • The time complexity of strlen() is O(n), where n is the number of characters in the string.
  • Its return type is size_t ( which is generally unsigned int ).


Similar Reads

Difference between strlen() and sizeof() for string in C
sizeof() Sizeof operator is a compile time unary operator which can be used to compute the size of its operand. The result of sizeof is of unsigned integral type which is usually denoted by size_t.sizeof can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as
2 min read
Write your own strlen() for a long string padded with '\0's
Given a big character array that represents a string such that all trailing non-string characters are filled with '\0' at the end, i.e, all leading characters are string characters, all trailing are '\0' and no garbage characters. Write an efficient function to find length of such a string. Examples: Input: str[] = {'g', 'e', 'e', 'k', '\0', '\0',
3 min read
C Function Arguments and Function Return Values
Prerequisite: Functions in C A function in C can be called either with arguments or without arguments. These functions may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values. Hence the function prototype of a function
6 min read
Write a one line C function to round floating point numbers
Algorithm: roundNo(num) 1. If num is positive then add 0.5. 2. Else subtract 0.5. 3. Type cast the result to int and return. Example: num = 1.67, (int) num + 0.5 = (int)2.17 = 2 num = -1.67, (int) num - 0.5 = -(int)2.17 = -2 Implementation: /* Program for rounding floating point numbers */ # include&lt;stdio.h&gt; int roundNo(float num) { return nu
1 min read
Does C support function overloading?
First of all, what is function overloading? Function overloading is a feature of a programming language that allows one to have many functions with same name but with different signatures. This feature is present in most of the Object Oriented Languages such as C++ and Java. But C doesn't support this feature not because of OOP, but rather because
2 min read
How can I return multiple values from a function?
We all know that a function in C can return only one value. So how do we achieve the purpose of returning multiple values. Well, first take a look at the declaration of a function. int foo(int arg1, int arg2); So we can notice here that our interface to the function is through arguments and return value only. (Unless we talk about modifying the glo
2 min read
How to declare a pointer to a function?
While a pointer to a variable or an object is used to access them indirectly, a pointer to a function is used to invoke a function indirectly. Well, we assume that you know what it means by a pointer in C. So how do we create a pointer to an integer in C? Huh..it is pretty simple... int *ptrInteger; /*We have put a * operator between int and ptrInt
2 min read
Can We Call an Undeclared Function in C++?
Calling an undeclared function is a poor style in C (See this) and illegal in C++and so is passing arguments to a function using a declaration that doesn't list argument types.If we call an undeclared function in C and compile it, it works without any error. But, if we call an undeclared function in C++, it doesn't compile and generates errors. In
2 min read
What is evaluation order of function parameters in C?
It is compiler dependent in C. It is never safe to depend on the order of evaluation of side effects. For example, a function call like below may very well behave differently from one compiler to another: void func (int, int); int i = 2; func (i++, i++); There is no guarantee (in either the C or the C++ standard language definitions) that the incre
1 min read
Can We Use Function on Left Side of an Expression in C and C++?
In C, it is not possible to have function names on the left side of an expression, but it's possible in C++. How can we use the function on the left side of an expression in C++? In C++, only the functions which return some reference variables can be used on the left side of an expression. The reference works in a similar way to pointers, so whenev
1 min read
C++ | Function Overloading and Default Arguments | Question 5
Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++. (A) Inheritance (B) Polymorphism (C) Encapsulation (D) None of the above Answer: (B) Explanation: Both of the features allow one function name to work for different parameter. Quiz of this Question
1 min read
C++ | Function Overloading and Default Arguments | Question 2
Output? #include&lt;iostream&gt; using namespace std; int fun(int x = 0, int y = 0, int z) { return (x + y + z); } int main() { cout &lt;&lt; fun(10); return 0; } (A) 10 (B) 0 (C) 20 (D) Compiler Error Answer: (D) Explanation: All default arguments must be the rightmost arguments. The following program works fine and produces 10 as output. #include
1 min read
C++ | Function Overloading and Default Arguments | Question 3
Which of the following overloaded functions are NOT allowed in C++? 1) Function declarations that differ only in the return type int fun(int x, int y); void fun(int x, int y); 2) Functions that differ only by static keyword in return type int fun(int x, int y); static int fun(int x, int y); 3)Parameter declarations that differ only in a pointer * v
1 min read
C++ | Function Overloading and Default Arguments | Question 4
Predict the output of following C++ program. include&lt;iostream&gt; using namespace std; class Test { protected: int x; public: Test (int i):x(i) { } void fun() const { cout &lt;&lt; &quot;fun() const &quot; &lt;&lt; endl; } void fun() { cout &lt;&lt; &quot;fun() &quot; &lt;&lt; endl; } }; int main() { Test t1 (10); const Test t2 (20); t1.fun(); t
1 min read
C++ | Function Overloading and Default Arguments | Question 5
Output of following program? #include &lt;iostream&gt; using namespace std; int fun(int=0, int = 0); int main() { cout &lt;&lt; fun(5); return 0; } int fun(int x, int y) { return (x+y); } (A) Compiler Error (B) 5 (C) 0 (D) 10 Answer: (B) Explanation: The statement "int fun(int=0, int=0)" is declaration of a function that takes two arguments with de
1 min read
strtoul() function in C/C++
The strtoul() function in C/C++ which converts the initial part of the string in str to an unsigned long int value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0. This function discard any white space characters until the first non-whitespace character is found, then takes as many characters as poss
3 min read
wcscspn() function in C/C++
The wcscspn() function in C/C++ searches the first occurrence of a wide character of string_2 in the given wide string_1. It returns the number of wide characters before the first occurrence of that wide character . The search includes the terminating null wide characters. Therefore, the function will return the length of string_1 if none of the ch
2 min read
C Library Function - difftime()
The difftime() is a C Library function that returns the difference in time, in seconds(i.e. ending time - starting time). It takes two parameters of type time_t and computes the time difference in seconds. The difftime() function is defined inside the &lt;time.h&gt; header file. Syntax The syntax of difftime() function is as follows: double difftim
1 min read
Count the number of objects using Static member function
Prerequisite : Static variables , Static Functions Write a program to design a class having static member function named showcount() which has the property of displaying the number of objects created of the class. Explanation: In this program we are simply explaining the approach of static member function. We can define class members and member fun
2 min read
C program to copy string without using strcpy() function
strcpy is a C standard library function that copies a string from one location to another. It is defined in the string.h header file. We can use the inbuilt strcpy() function to copy one string to another but here, this program copies the content of one string to another manually without using strcpy() function. Methods to Copy String without using
3 min read
mbtowc function in C
Convert multibyte sequence to wide character. The multibyte character pointed by pmb is converted to a value of type wchar_t and stored at the location pointed by pwc. The function returns the length in bytes of the multibyte character. mbtowc has its own internal shift state, which is altered as necessary only by calls to this function. A call to
2 min read
isxdigit() function in C Language
isxdigit() function in C programming language checks that whether the given character is hexadecimal or not. isxdigit() function is defined in ctype.h header file. Hexadecimal equivalent of Decimal Numbers: Hexadecimal: 0 1 2 3 4 5 6 7 8 9 A B C D E F Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Syntax: char isxdigit( char x); Examples: Input : A
2 min read
isupper() function in C Language
isupper() function in C programming checks whether the given character is upper case or not. isupper() function is defined in ctype.h header file. Syntax : int isupper ( int x ); Examples: Input: A Output: Entered character is uppercase character Input: a Output: Entered character is not uppercase character Input: 1 Output: Entered character is not
2 min read
isalnum() function in C Language
isalnum() function in C programming language checks whether the given character is alphanumeric or not. isalnum() function defined in ctype.h header file. Alphanumeric: A character that is either a letter or a number. Syntax: int isalnum(int x); Examples: Input : 1 Output : Entered character is alphanumeric Input : A Output : Entered character is a
2 min read
wcstof function in C library
The wcstof() functions convert the initial portion of the wide-character string pointed to by str to a float point value. The str parameter points to a sequence of characters that can be interpreted as a numeric floating-point value. These functions stop reading the string at the first character that it cannot recognize as part of a number i.e. if
2 min read
pieslice() function in C
pieslice() draws and fills a pie slice with center at (x, y) and given radius r. The slice travels from s_angle to e_angle which are starting and ending angles for the pie slice. The angles for pie-slice are given in degrees and are measured counterclockwise. Syntax : void pieslice(int x, int y, int s_angle, int e_angle, int r); where, (x, y) is ce
2 min read
arc function in C
The header file graphics.h contains arc() function which draws an arc with center at (x, y) and given radius. start_angle is the starting point of angle and end_angle is the ending point of the angle. The value of the angle can vary from 0 to 360 degree. Syntax : void arc(int x, int y, int start_angle, int end_angle, int radius); where, (x, y) is t
2 min read
settextstyle function in C
The header file graphics.h contains settextstyle() function which is used to change the way in which text appears. Using it we can modify the size of text, change direction of text and change the font of text. Syntax : void settextstyle(int font, int direction, int font_size); where, font argument specifies the font of text, Direction can be HORIZ_
2 min read
grapherrormsg() function in C
The header file graphics.h contains grapherrormsg() function which returns an error message string. Syntax : char *grapherrormsg( int errorcode ); where, errorcode: code for the respective error Illustration of the grapherrormsg() : In the below program, gd = DETECT is not written and thus program must throw an error. Below is the implementation of
1 min read
textwidth() function in C
The header file graphics.h contains textwidth () function which returns the width of input string in pixels. Syntax : int textwidth(char *string); Example : Input : string = "Hello Geek ! Have a good day." Output : Below is the implementation of textwidth() function. // C Implementation for textwidth() #include &lt;graphics.h&gt; #include &lt;stdio
1 min read
Article Tags :