This is the 10th day of my participation in the August Text Challenge.More challenges in August
directory
- An introduction to sizeof
- Sizeof function
- Guess you like it
C/C++ learning Directory >> C language basics
An introduction to sizeof
In C, the char string is also an important data type. We can use strlen to get the length of a string, and sizeof to get the length of a string.
String: Usually use a pair of double quotation marks""
A bracketed string of characters to represent a string constant, which defaults to escape characters'\ 0'
At the end, string constants are not modifiable;
sizeof
The function scans the entire string until it hits the first string terminator'\ 0'
, and then returns the counter value (length includes'\ 0'
);
/* * Description: * [in] s: string * * Return value: Returns the length of string s, note that the returned length includes \0 */ int sizeof(char *s);Copy the code
Note:strlen
The returned length is not included'\ 0'
, so when getting the string length,strlen
The function returns a value less than one byte;
Sizeof function
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language sizeof function // @time :2021/06/02 08:00 // @motto: a thousand miles without a step, a small stream without a river or sea, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include "stdafx.h" #include<stdlib.h> #include<stdio.h> void main() { char p[] = "www.codersrc.com"; Printf (" string: %s Length: %d\n", p, sizeof(p)); char p1[] = "www"; Printf (" string: %s Length: %d\n", p1, sizeof(p1)); char p2[] = "0123456789"; Printf (" string: %s Length: %d\n", p2, sizeof(p2)); system("pause"); } Output: string: www.codersrc.com Length: 17 String: WWW length: 4 String: 0123456789 Length: 11 Press any key to continue..Copy the code
Guess you like it
- Install Visual Studio
- Install the Visual Studio plug-in, Visual Assist
- Visual Studio 2008 uninstall
- Visual Studio 2003/2015 Uninstall
- Set the Visual Studio font/background/line number
- C format controller/placeholder
- C language logic operator
- C language ternary operator
- C language comma expression
- C (++ I/I ++)
- C language for loop
- C language break and continue
- C while loop
- C does while and while loops
- C switch statement
- C language GOto statement
- C char Character string
- C strlen function
- C sizeof
C language sizeof function
This article is published by the blog – Ape Say Programming Ape Say programming!