C language initial, simple algorithm and procedure;

Source: Internet
Author: User
Tags gcd strlen


Want to learn C language again, and share the results on this blog every day. To see a statement, write more reliable than do; In addition to the IT industry, blogs always seem to be important to measure a programmer's foundation; I am neither a programmer nor a solid foundation. But I really want to insist, today is a beginning, see how long can hold on. Based on the basic understanding, some features of the following C are summed up: rich data types and operators, portability, and direct access to the underlying hardware (addresses can be accessed).

The first problem is that all leap years between 2000 and 2500 are exported. The program is very simple, only a bit easy to get wrong----what is a leap year. Baidu Encyclopedia gives the definition and set the reason for the leap year four years a leap, a century does not leap, 400 years again leap. The reason for setting a leap year is because according to 24 hours a day, 365 days a year, every four years will be a day (slightly smaller than a day), so four years a leap, and four years a leap every 400 years will be more than 3rd, so also stipulates hundred years not leap, 400 year again leap. The algorithm flow chart is as follows:


The corresponding C program is too simple, not to repeat;

The second problem is to find the M,n gcd, the same algorithm is very important (I have been to take the short division of the calculation, I have not seen this algorithm, this algorithm faster convergence speed)

(The algorithm is called Euclidean or Euclidean method, the specific proof method see classic problem-Euclid GCD) source code also omitted; are all simple operator applications. In addition, how to calculate LCM, for example, the given number is m,n, the GCD obtained by the above algorithm is P, then the LCM number is mn/p;

Input and output of character data: Putchar () and GetChar () can only output and enter one character, and how to enter a string. Using loops

while (C=getchar ()). = '/n '). Or define an array of strings, implemented with gets (), about which you can output all substrings of a given string from the topic "(http://blog.csdn.net/ns_code/article/details/21043665# Comments) "One of the problems in the application. My code for this inscription is as follows:

#include <stdio.h>
void Main ()
{
	char a[100];
	int num,i,j,k;
	num=0;
	/*while ((A[num]=getchar ())!= ' \ n ')
	{
		num=num+1;
	} *
	for (num=0; (A[num]=getchar ())!= ' \ n '; num++)
	
	printf ("aaa%d\n", num);
	for (i=0;i<num;i++) for
		(j=i;j<num;j++)
		{
			k=i;
			while (k<=j)
			{
				printf ("%c", A[k]);
				k=k+1;
			}
			printf ("\ n");
		}
	


Format input and output: 7.2f% refers to seven columns of two decimal places;

Operator Precedence: Assignment < relationship < arithmetic;

Swith statement; Once a case is executed, the next case is automatically performed unless the break is added;

Break out the current statement with a break in a circular statement and a cwith statement;

In a looping statement, continue out of the current loop

Fabs (t) means the absolute value of t;

String handler function: Puts;gets;strcat (STR1,STR2) strings 2 to the string 1, strcpy (STR1,STR2) copies the string 3 to 1, and the string cannot be assigned through a simple STR1=STR2 assignment ; the corresponding strncpy (str1,str2,n) copies the first n characters from 2 to 1, strcmp (STR1,STR2), such as STR1>STR2, the function value is a positive number, is less than a negative number, or equal to zero. Strlen the length of the test string; strlwr and STRUPR correspond to each other, turning the string into lowercase or uppercase respectively;

The one-way value transfer form of function, the change of parameter value can not change the value of the actual parameter, but with the pointer variable as the function parameter, it is easy to complete the modification of multiple variables.


The pointer's understanding: The pointer is like an address, the address can be found by the object he refers to, we can apply the pointer to modify the object referred to. Where the pointer and array names are associated, for example, to define an array: int a "10", then a represents the first address of the array element; We define the pointer variable int*p, then P=&a "0" and p=a are equivalent; that is, a is a pointer, but a constant pointer, and cannot continue to be assigned in the program. In addition, * (P+i) and a "I" are unconditionally equivalent


The relationship between multidimensional arrays and pointers: for a "I" "J":

A meaning: 0 header Address

a[0],* (a+0), *a meaning: 0 rows 0 Column address

A+1,&A[1], * (a+1) 1 header address;

a[1]+2,* (a+1) +2 1 rows of 2 column elements address

* (a[1]+2), * (* (a+1) +2) 1 rows 2 Column element values


Relationship between sizeof () and strlen ():

1, sizeof () is the operation algorithm, strlen () is a function;

2, sizeof () to calculate the byte value of the variable; strlen generally ask for the length of the string (excluding the end of the ' "", but the packet ' \ \ ' t ', and so on, pay attention to the ' \ \ ' \ddd ' \xhh ' and other characters)

Here are a few simple programs that can tell the relationship between them



#include 
#include 

 
int main (void)
{
	char s[]= "010\010\\010\n";
	printf ("%d\n%d", sizeof (s), strlen (s));//The result shown is 10 (' \101 ' refers to the character with the ASICII code
	//101; ' \ \ ' represents the backslash character, including the end of the string ' "') and 9
}




Note the difference between the previous program//
#include 
#include 

 
int main (void)
{
	char *s= "010\010\\010\n";
	printf ("%d\n%d", sizeof (s), strlen (s));//The result shown is 4 (including the end of the string "") and 9
}



 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.