The second day of c Programming and the second day of c Programming

Source: Internet
Author: User

The second day of c Programming and the second day of c Programming

A function is also called a method. It is a code block that implements a function or completes a task.

# Include <stdio. h>
Void show ()
{
Printf ("I like c language ");
}
Int main ()
{
Show ();
Return 0;
}

 

The show () above is a user-defined function, and the int of int main () requires that the value returned by the main function be an integer. Therefore, return 0 is used in the last row of the function.

Void indicates that the function is allowed to return NULL values, that is, the function cannot return values, that is, return cannot be returned at the end.

One program has only one primary function. Other functions are called by the primary function.

 

# Include <stdio. h>
Int add (int x, int y)
{
Return x + y;
}

Int main ()
{
Int;
A = add (1, 2 );
Printf ("% d \ n", );
Return 0;
}

Int x and int y are two parameters passed to the add function. The system allocates memory for x and y only when the add function is called, once the function is executed, the system recovers the memory.

The execution process of the above program: first, execute the main function, call the add function in the main function, return the value of x + y, return to the main function, assign a value to a, print the value of a to the screen

 

# Include <stdio. h>
Int add (int x, int y)
{
Return x + y;
}

Int main ()
{
Int x;
X = 0;
Printf ("% d \ n", add (1 + 2, x ));
Return 0;
}

X and y in () of the add function are form parameters, and their values are uncertain.

The main function transmits two actual values 1 + 2 and x to the add function. x is a local variable and defines its value as 0, the local variable is only valid in this function.

Therefore, parameters are classified into form parameters and actual parameters. During function calling, real parameters and form parameters occupy different memory spaces.

Note that the data types of the actual and formal parameters must be consistent.

 

# Include <stdio. h>
Int x = 567;
Void show ()
{
Printf ("% d \ n", x );
}

Int main ()
{
Show ();
Printf ("% d \ n", x );
Return 0;
}

In the above program, x is a global variable (defined externally by all functions), which can be shared by all functions in the program and occupy the memory during execution of the program, the memory is released after the program is executed.

When a local variable has the same name as a global variable, the local variable overwrites the global variable.

 

If a function needs to modify the global variables, add a comment before the function that modifies the global variables.

 

# Include <stdio. h>
Int main ()
{
Printf ("% d + % d \ t = % d \ n", 15, 16, 15 + 16 );
Printf ("% d + % d \ t = % d \ n", 15, 16, 15-16 );
Printf ("% d + % d \ t = % d \ n", 15, 16, 15*16 );
Printf ("% d + % d \ t = % f \ n", 15/16, (float );
Return 0;
}

Analyze the first line of printf:

The first % d represents 15, and so on. The last % d represents the value of 15 + 16, and % d Represents the output of an integer.

% F Represents the output of a floating point number (which contains the fractional part). Here it represents the value of 15/16. If there is no float before 15/16, the output will also be an integer ,() is the conversion operator, and float in () is the type of forced conversion

 

 

# Include <stdio. h>
Int main ()
{
Int;
Scanf ("% d", & );
Printf ("% d", );
Return 0;
}

The scanf ("% d", & a) function prompts the user to input and save the input value as an integer, the scanf function replaces the value entered by the user with the value of the original variable a. scanf is the input function.

Next, printf will output the value of a to the screen. printf is the output function.

 

# Include <stdio. h>
Int main ()
{
Putchar ('A ');
Return 0;
}

The putchar function outputs a character to the display. ''indicates that only one character can be included.

Putchar can also output the value of a variable, for example:

 

# Include <stdio. h>
Int main ()
{
Char;
A = 'a ';
Putchar ();
Return 0;
}

Char indicates that variable a is a character type.

 

# Include <stdio. h>
Int main ()
{
Char c;
C = getchar ();
Putchar (c );
Return 0;
}

The getchar () function obtains a character from the standard input device. c = getchar () indicates that the character obtained by the getchar () function is assigned to c.

 

 

# Include <stdio. h>
Int main ()
{
Char c;
C = getchar ();
Putchar (c );
C = getchar ();
Putchar (c );
C = getchar ();
Putchar (c );
Return 0;
}

The above program allows three characters to be input at a time. For example, if abc is input, a is processed by the first c = getchar () and putchar (c), followed by B by the second, c is the third

 

 

    

 

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.