C
1. HelloWorld
#include<stdio.h>
int main(){
printf("Hello World");
return 0;
}
2.DataType
Integer:char int short long / long long
Unsigned integers:use unsigned keyword before Integer
Float point numbers:float double
Structures
boolean
#define BOOL char
#define FALSE 0
#define TRUE 1
3.Array
int arr[10];
int arr[1][2];
arr[9]
4.If
if(condition)
{
dosomething();
}else if(...){
}else ...
5.String
//In c ,there is not a keyword of String like in java
//Instead,string is arraies of char
//Use pointer to define a simple string
char* name="Hello";//This is only be-read,and can not be changed;
//Use local array notation [] to manipulate a string
char name[]="Hello";
//Actual length is 6.The last is the string terminatior
char name[6]="Hello";
char* name="Hello";
char name2[]="Hello";
int strName=strlen(name);
int strName2=strlen(name2);
printf("strName: %d\n",strName);//5
printf("strName2: %d",strName2);//5
String Comparison
int strncmp(str1,str2,MaxComparisonLength);//0 for equal; Unsafe version -> strcmp;
//<0 -> str1 < str2
String Concatenation
char dest[20]="Hello";
char str[20]="World";
char* final=strncat(dest,str,1);
printf("|%s|",final);// |HelloW|
printf("\n%s",strncat(dest,str,20));//HelloWWorld
6.For
int arr[5]={1,2,3,4,5};
int mutiResult=1;
int i;
for(i=0;i<5;i++){
mutiResult=mutiResult*arr[i];
}
printf("%d",mutiResult);//120
7.While
int n=0;
while(1){
n++;
if(n>10){
break;
}
}
while(n>10){
n++;
if(n%2==1)
{
continue
}
printf("%n",n);
}
8.Function
//Function either return one value nor not
void hello()
{
}
9.Static
'static' is a keyword in the C programming language. It can be used with variables and functions.
int runner()
{
static int count = 0;
count++;
return count;
}
10.Pointer
A pointer is essentially a simple integer variable which holds a memory address that points to a value, instead of holding the actual value itself.
//Dereferencing a pointer use *
int a=1;
//By pointing at vars use &
int* p_to_a=&a;
a+=1;
*p_to_a+=1;
printf("%d",a)//3
char* name="Hello";
printf("%c\n",name[1]);//e
int i=0;
for(i=0;i<strlen(name);i++){
printf("%c",name[i]);//Hello
}
11.Structure
C structures are special, large variables which contain several named variables inside.
struct point {
int x;
int y;
}
struct point p;
p.x=10;
p.y=20;
Typedef
typedef struct{
int age;
char* name;
} person;
person John;
John.age=13;
Structures can also hold pointers - which allows them to hold strings, or pointers to other structures as well - which is their real power.
12.Function arguments by reference
void addOne(int* n){
(*n)++;
}
int n;
addOne(&n);
Pointers to structures
void move(point* p){
p->x++;//(*p).x++
p->y++;
}
13.Dynamic Allocation
typedef struct {
char *name;
int age;
} person;
person* person1=(person*)malloc(sizeof(person));
person1->name="John";//(*person1).name="John"
person1->age=22;
free(myperson);
better way:
person* person1=NULL;
person* person1=(person*)malloc(sizeof(person));
person1->name="John";//(*person1).name="John"
free(myperson);
14.Array And Pointers
char vowels[] = {'A', 'E', 'I', 'O', 'U'};
char *pvowels = vowels;
int i;
// Print the addresses
for (i = 0; i < 5; i++) {
printf("&vowels[%d]: %p, pvowels + %d: %p, vowels + %d: %p\n", i, &vowels[i], i, pvowels + i, i, vowels + i);
}
// Print the values
for (i = 0; i < 5; i++) {
printf("vowels[%d]: %c, *(pvowels + %d): %c, *(vowels + %d): %c\n", i, vowels[i], i, *(pvowels + i), i, *(vowels + i));
}
Dynamic Memory Allocation for Arrays
int nrows = 2;
int ncols = 5;
int i, j;
// Allocate memory for nrows pointers
char **pvowels = (char **) malloc(nrows * sizeof(char *));
// For each row, allocate memory for ncols elements
pvowels[0] = (char *) malloc(ncols * sizeof(char));
pvowels[1] = (char *) malloc(ncols * sizeof(char));
pvowels[0][0] = 'A';
pvowels[0][1] = 'E';
pvowels[0][2] = 'I';
pvowels[0][3] = 'O';
pvowels[0][4] = 'U';
pvowels[1][0] = 'a';
pvowels[1][1] = 'e';
pvowels[1][2] = 'i';
pvowels[1][3] = 'o';
pvowels[1][4] = 'u';
for (i = 0; i < nrows; i++) {
for(j = 0; j < ncols; j++) {
printf("%c ", pvowels[i][j]);
}
printf("\n");
}
// Free individual rows
free(pvowels[0]);
free(pvowels[1]);
// Free the top-level pointer
free(pvowels);
Exercise
The first seven rows of Pascal's triangle are shown below. Note that row i contains i elements. Therefore, to store the numbers from the first three rows, one would require 1 + 2 + 3 = 6 memory slots.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Complete the skeleton code given below to store the numbers from the first three rows of Pascal's triangle in a two-dimensional "array" using dynamic memory allocation. Note that you must allocate exactly six memory slots to store those six numbers. No extra memory should be allocated. At the end of your program, free all the memory blocks used in this program.
15.Recursion
#include <stdio.h>
unsigned int multiply(unsigned int x, unsigned int y)
{
if (x == 1)
{
/* Terminating case */
return y;
}
else if (x > 1)
{
/* Recursive step */
return y + multiply(x-1, y);
}
/* Catch scenario when x is zero */
return 0;
}
int main() {
printf("3 times 5 is %d", multiply(3, 5));
return 0;
}
Exercise
Define a new function called factorial() that will compute the factorial by recursive multiplication (5! = 5 x 4 x 3 x 2 x 1). Note that by convention, the factorial of 0 is equal to 1 (0! = 1).
16.Pointer Arithmetic
#include <stdio.h>
int main()
{
int intarray[5] = {10,20,30,40,50};
int i;
for(i = 0; i < 5; i++)
printf("intarray[%d] has value %d - and address @ %x\n", i, intarray[i], &intarray[i]);
int *intpointer = &intarray[3]; //point to the 4th element in the array
printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 4th element
intpointer++; //now increase the pointer's address so it points to the 5th elemnt in the array
printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 5th element
return 0;
}
17.Function Pointer
void (*pf)(int);
(pf)(5)
标签:name,int,C语言,char,++,printf,pvowels
From: https://www.cnblogs.com/alexmaodali/p/18115703