C Language Basic Grammar

Source: Internet
Author: User
Tags case statement function definition switch case ticket

#include <stdio.h>intMain () {intAge ; printf ("Input your age"); scanf ("%d",&Age ); if(age> A) {printf ("You are become old"); }    Else if(Age < -) {printf ("You is too young"); }    Else{printf ("You are strong"); }        }
View Code

Judgment statement:

if ()

{

}

else if ()

{

}

Else

{

}

Switch Case statement

#include <stdio.h>intMain () {intgrade; scanf ("%d",&grade); Grade/=Ten; Switch(grade) { Case Ten:         Case 9: printf ("A");  Break;  Case 8: printf ("B");  Break;  Case 7: printf ("C");  Break;  Case 6: printf ("D");  Break; }}
View Code

Loop while

#include <stdio.h>int  main () {    int n=0;     int sum=0;      while (n<=)    {        sum+ =N;        N+ +;            }    printf ("%d", sum);     return 0 ;}

For loop

#include <stdio.h>int  main () {    int  n;     int 0 ;      for (n=1; n<=; n++)    {        sum+ =n;    }    printf ("%d", sum);     return 0 ;}
View Code

Do and loop

intMain () {inti =0, sum =0; intN; scanf ("%d",&N);  Do{sum+=i; I++; } while(i<=N); printf ("%d", sum); return 0;}
View Code

The while statement is the first ticket after the car, do while the statement is to get on the bus ticket, Dowhile statement will be executed at least once.

function definition Call

#include <stdio.h>intGetmax (intXinty) {    intresult; returnX>y?x:y; }intMain () {intx,y,s; scanf ("%d%d",&x,&y); S=Getmax (x, y); printf ("%d", s); return 0;}

C-language functions are performed from the main function.

The parameters of the function definition are called formal parameters, (parameter)

The parameters of the function call are called actual parameters, (argument)

Pointer

#include <stdio.h>voidSwapint*x,int*y);intMain () {intA=4; intb=1; Swap (&a,&b); printf ("a=%d,b=%d", A, b); return 0;}voidSwapint*x,int*y) {    inttemp; Temp=*x; *x=*y; *y=temp;}
View Code

The concept of pointers

A pointer is a data type

A variable of pointer type is called a pointer variable

The pointer is not an address, and the value of the pointer variable is an address.

You want the pointer variable to point to which storage unit, let it save the address of which cell.

    • Save the address of a variable
    • Save the first address of an array
    • Save the first address of a string
    • Save the first address of a function

Basic principles for using pointer variables

Clear where the pointer is pointing--pointer initialization

What is the content of the explicit pointer pointing to the cell? ---base type

Can only point to data of the same base type.

Important applications of pointers

As a function parameter, passing a variable or function address to a function

Dynamically allocating memory to implement dynamic arrays and dynamic Data structures

A pointer to a function makes a function argument.

The called function calls different functions according to the different addresses that are passed in.

C language Basic syntax

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.