Step into the first day of C programming

Source: Internet
Author: User

I want to develop a website after Ruby, but Ruby is a high-level language that hides a lot of the underlying stuff, so familiarize yourself with the C language first.

First, the file name of the C program ends in. C.

C Format of the program:

First line #include<stdio.h>

#是一个预处理标准, which is used to preprocess text to indicate that the line code is to be processed first and run before the code is compiled

Include is a directive that reads files from <> into

Stdio.h represents the standard input and output header file (the header file is like a book directory, the reader through the directory can easily check

Read to the desired content, Stdio.h declares the standard input and output function, and adds this header if a standard input/output function is to be used

File

    

The first C language program:  

#include <stdio.h>
int main ()
{
printf ("I like C language");
return 0;
}

The function of the first line is that the compiler compiles all the contents of the header file stdio.h to the first line of the current program, and since Stdio.h declares all the input and output functions, the following can call the printf function directly.

Main () represents a function, translated into Chinese is the main function, in each C program will be compiled first is the main function, () is a fixed format, {} is also a fixed format, which contains parts called function blocks or function body

Each program needs to indicate the type of return, here is int, that is to return an integer to the operating system after execution of the program is completed

The Mian function is fundamentally different from other functions: general functions are called or activated by other functions, such as the printf function

is activated by the Mian function, but the main function is automatically executed at the beginning of the program

printf is the abbreviated form of print format, and I like C language is the information that the main function passes to the printf function, and the printf function receives this information and prints this information to the screen

Return 0 means that the Mian function returns a 0 value to the operating system, which returns an execution status result after the execution of the general function, 0 for normal, not 0 for the exception

Comments in the C language:

Single-line Comment

/*....*/Multi-line comments

The statement terminator, as long as the line contains; The statement ends with the words, for example; spaces in a statement are generally negligible

For example: x=a+b; or x = a + b; Are the same, it is recommended to use the first one, meaning to assign the value of A+b to X

#include <stdio.h>
int main ()
{
{
printf ("I like C language");
printf ("I like C language");
return 0;
}
}

{} contains part of a block, the outer {} contains a function block, inside {} block contains multiple statements (two printf statements), exactly a compound statement

Expression: x=c=a+b means assigning the value of A+b to C and assigning it to X

#include <stdio.h>
int main ()
{
int a=1,b=2,c=3,x=0;
X=c=a+b;

printf ("%d", x); 
return 0;
}

%d for the output is an integer, corresponding to the following x, execute this program to get 3 results

Operator:  

#include <stdio.h>
int main ()
{
printf ("%d", 5%2);
return 0;
}

The 5%2 represents the modulo, which is 5 divided by 2 to take its remainder, put this value in the preceding "" in the output

Operation Priority:

printf ("%d", 1+1*2+3);

The above sequence of operations: 1*2+1+3,

printf ("%d", (+) * (2+3));

The above sequence of operations: the value of (+) multiplied by (2+3) the same as the primary operation

= is an assignment operator in the C language

#include <stdio.h>
int main ()
{
int x=0;
X+=1;
printf ("%d", x);
return 0;
}

X+=1 indicates that the value of x is added to 1, and then the value is assigned to X, instead of x-=1

Similar to the following: X*=2, X/=2,x%=2,x%=1+2 (the first to ask for 1+2 value)

Self-add: X + + adds 1 to the value of X.

Decrement: x--The value of X by 1

#include <stdio.h>
int main ()
{
int x=2;
printf ("%d", x--);
return 0;
}

The above program will first output the value of X to the screen, and then minus 1, if you want to get the result minus 1, you can use the following notation

#include <stdio.h>
int main ()
{
int x=2;
printf ("%d\n", x--);
printf ("%d\n", X);

  return 0;

}

\ n means line break

The above program can also be written like this:

#include <stdio.h>
int main ()
{
int x=2;

x--;
printf ("%d", x);
return 0;
}

It's a bit wrong to say so much, how do I execute the above program files? That's what I was thinking.

I was in the Linux system, using vim to edit the file, for example: Vim 1.c gcc compiled file compilation command: GCC 1.c, a a.out file will appear to execute this file:./a.out

You can also use the GCC 1.c-o Hello custom file name

Step into the first day of C programming

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.