Basic knowledge of C language

one,

About sizeof and strlen. Sizeof() is used to calculate the space occupied by a type or a variable in memory. For example, integers are divided into short, int, and long integers, occupying 2, 2/4, and 4 bytes respectively. The specific number of bytes occupied by int is related to the compilation system. We usually use char arrays when we input strings:

Char s[10];

Scanf(“%s”,s);

The input string may be a mixture of Chinese and English. English is a letter, and a letter is a char, occupying a byte. A Chinese character occupies two bytes, these two bytes are consecutive, and the highest bit of each byte is 1. So you can judge how many Chinese characters are in a string based on this.

L=strlen(s); //strlen is used here, just the length of the string (in bytes), excluding /0
For(num=i=0;i<l;i++)
{
If(s[i]<0) //In the case of signed type, the highest bit 1 represents a negative number
Num++;
Printf("Total %d Chinese characters", num/2); //Here we need to divide by 2, because the two bytes whose highest bit is 1 is a Chinese character
	//#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
	char a[] = { 'h', 'e', 'l', 'l', 'o' ,'\0'};
	char b[] = "hello";//Define the string
	char c[] = { 'h', 'e', 'l', 'l', 'o' };
	char *p = b;
	cout << a << endl;
	cout << b<<endl;//The output is hello
	cout << c << endl;
	char *pchar = c;
	//The following statement implements the output of hello with a pointer.
	for (size_t i = 0; i < 5; i++)
	{
		cout << *pchar;
		pchar++;
	}
	cout << "\n" << endl;
	cout << *p;//The pointer points to the first address, so the output is h
	string str = "hello world";
	string *p1 = &str; //Note that the address operator & must be added
	cout<< str << "," << *p1; //输出的是  hello world,hello world
	getchar();
	return 1;
}

Char arrays can store strings, and the array name is the address of the string. Both Scanf and cout operate on addresses, so the result of cout<<b is a string.

When the Char array is stored by letters, if you want to output a string, you need to add the string end identifier \0, otherwise the output is garbled. Or you can use the pointer to output letters one by one

String is actually a class, not a data type. String's pointer points to the string object, not the first character.

The two are interchangeable.

l Speaking of classes, in fact, a class is a constructed data type, that is, a new data type reconstructed from an already defined data type. In addition to classes, there are array types, structure types, enumeration types, and union types.

The structure type in C language is basically the same as the class type in C++, but the default qualifiers are different. C language defaults to public, and C++ defaults to private.

l The trouble with C++ lies in the way variables are called. You can use variable names, you can use pointers, and you can use references. The program is actually composed of functions, including multiple sub-functions and a main function. The transfer of parameters in the function is generally passed by value, which means that a copy of the variable will be created, and the value of this copy is equal to the value of the parameter. In this way, changing the value of the copy in the function does not change the value of the original variable.

l For reference types, add an & in front of the variable when defining:

Int a;

Int&b=a;&c=a; //b,c are two references to a

A reference variable is another name for a variable that does not allocate new memory space, so changing the value of the variable will change the value of all reference variables.

l When it comes to functions and variables, there is another concept that is global variables and local variables. The criterion for judging is whether the definition of the variable is placed outside the function or inside the function. However, the difference between global variables and local variables is not so simple. The space allocated by global variables is located in the "heap" in the memory space. The access speed of the heap is slow and the space is large; the space for local variables is located in the "stack" in the memory, and the stack stores Take fast, small space. And global variables are defined with default initial values, such as the default initial value of integer global variables is 0.

l Global variables can be accessed in all functions, so if the parameter is passed by reference, it will cause changes in the function and affect other functions, so global variables are not often used. And there is the advantage of large space in the heap, so when we want to define a large array, we can use static variables.

l Static variables are modified with the static qualifier. Even if the definition is inside the function body, it still exists when the program exits, and the original value remains unchanged. A feature of static variables is that they are only defined once, and are only defined when the function is called for the first time, so you can use this feature to record the number of times the function is called: define static int count=0 in the function body;

Count++; static variables are also initialized to 0 by default

l Initialization of the array. How to not assign an initial value to any element in the array at the same time when defining, then the value of each element in the array is undefined. Note the simultaneous and any here. At the same time, it is emphasized that it can only be continuously assigned from the beginning, and any emphasis is that the remaining unassigned values ​​will be initialized to 0.


#include<stdio.h>
#include<iostream>
using namespace std;
intmain()
{
	int i, j;
	cout << "Case:1" << endl;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j <= i; j++)//How to write less than or equal?
		{
			cout << "*";
		}
		cout << endl;

	}

	cout << "\n";

	cout << "Case:2" << endl;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4; j++)
		{
			if (j >= i)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}

	cout << "\n";
	cout << "Case:3" << endl;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j <=3+i; j++)
		{
			if (j >=3- i)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}

	getchar();
	return 1;
}
	#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

intmain()
{
	char a[10], b[10];
	char c, d;
	scanf("%s", a);
	printf("The string stored in a is: %s\n", a);

	c = getchar();
	printf("The characters stored in c are: %c ", c);

	gets(b);
	printf("The string stored in b is: %s\n", b);

	d = getchar();
	printf("The characters stored in d are: %c", d);
	getchar();

	return 0;
}

l There will always be test cases in the computer-based exam, and test cases are stored in files, which requires reading data from the files.

While(scanf(“%d%d”,&a,&b)!=EOF)

{

}

EOF is actually a macro definition, equal to -1, which means End Of File. And scanf will return -1 when it reads the end of the file. At this time, it will jump out of the loop. When Scanf reads data successfully, it will return the number of successfully read data

l To read the data in the loop body and display it, there are formatted input and output functions scanf("<formatted string>", <address table>); printf("<formatted string>", <parameter table >); there are also unformatted input and output functions gets(character array name or pointer); puts(). The latter takes up less memory and can read spaces until terminated by a carriage return. Scanf stops when it encounters a space, and appends the terminator \0 after it. puts() will automatically convert '\0' to '\n' for output when outputting a string, that is, the puts method will automatically wrap the line after outputting the string.

Scanf automatically adds \0 to stop input when encountering spaces, tabs, and carriage returns, but the spaces and carriage returns after that are still in the buffer and will be read into the following getchar. To avoid this problem, you can insert a getchar() between the two inputs to eat the carriage return. Or write one more %*c during scanf, read the last carriage return, but do not assign it to any variable.

gets: accepts all characters entered before the enter key and replaces '\n' with '\0'. The enter key does not stay in the input buffer

https://blog.csdn.net/xingjiarong/article/details/47282817

The scanf function reports error C4996: 'scanf', because VS considers the C standard functions to be unsafe. You can add a macro definition according to the prompt https://blog.csdn.net/jh0703/article/details/47820875

l Gets do not need to consider EOF

Char str[10];

While(gets(str))





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326065956&siteId=291194637