首页 > 其他分享 >c | C语言

c | C语言

时间:2024-04-05 14:12:00浏览次数:13  
标签:name int C语言 char ++ printf pvowels

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

相关文章

  • C语言数据结构专题--顺序表(1基础)
    前言我们在对C语言有一定的了解之后,我们就可以开始数据结构的学习了,数据结构多用指针、结构体、动态内存开辟等知识,若对这些知识还不太了解的朋友,就需要加深其理解了,那么废话不多说,我们正式开始本节的学习什么是数据结构数据结构是由"数据"和"结构"两个词相组合得到的......
  • 【C语言】函数递归——高手都在用的小技巧
    文章目录1.什么是递归2.递归的主要思想3.递归举例说明3.1n的阶乘3.2顺序打印⼀个整数的每⼀位4.递归与迭代4.1求第n个斐波那契数1.什么是递归递归简单来说就是一个函数自己调用自己,是不是感觉很莫名其妙,我第一次学习的时候就觉得为什么函数会自己调用自己......
  • 杨氏矩阵(C语言)
    文章目录问题技术名词解释思路关键代码运行代码问题有一个数字矩阵,矩阵的每行从左到右是递增的,矩阵从上到下是递增的,请编写程序在这样的矩阵中查找某个数字是否存在。要求:时间复杂度小于O(N);技术名词解释杨氏矩阵: 矩阵的每行从左到右是递增的,每列从上到下是递增的......
  • C语言—用EaxyX绘制实时钟表
     代码效果如图#undefUNICODE#undef_UNICODE#include<graphics.h>#include<conio.h>#include<math.h>#definewidth640#definehigh480#definePI3.14159intmain(){ initgraph(width,high); intcenter_x,center_y; center_x=width/2;......
  • C语言经典例题(17) --- 最小公倍数、单词倒置、你是天才吗?、完美成绩、判断整数的奇偶
    1.最小公倍数正整数A和正整数B的最小公倍数是指能被A和B整除的最小的正整数,设计一个算法,求输入A和B的最小公倍数。输入描述:输入两个正整数A和B。输出描述:输出A和B的最小公倍数。输入:57输出:35#include<stdio.h>intmain(){inta=0;intb=0;int......
  • C语言经典例题(18) --- 判断字母、三角形判断、衡量人体胖瘦程度、翻转金字塔图案、平
    1.判断是不是字母题目描述:KK想判断输入的字符是不是字母,请帮他编程实现。输入描述:多组输入,每一行输入一个字符。输出描述:针对每组输入,输出单独占一行,判断输入字符是否为字母,输出内容详见输出样例。输入:A6输出:Aisanalphabet.6isnotanalphabet......
  • 16.C语言错题整理
    一些C语言错题//求n的阶乘intsum=1;intn;printf("请输入n的值:");scanf("%d",&n);for(intj=1;j<n+1;++j){sum*=j;}printf("%d\n",sum);inthee=0;intb=1;for(......
  • DFS 全排列问题 C语言代码
    深度优先搜索(DFS)是一种遍历算法,尽可能深地向子树中的结点搜索,直到达到一定的深度,再回溯到上层的结点,继续搜索未被访问的结点。全排列问题给定4个数1234,求他们所有可能的排列结果。代码#include<stdio.h>voiddfs(intx);inti;inta[4];intresult[4];/......
  • C语言 | Leetcode C语言题解之第8题字符串转换整数atoi
    题目:题解:intmyAtoi(char*s){inti=0;intout=0;intpol=1;intlen=strlen(s);if(len==0)return0;while(s[i]=='')i++;//删除空格if(s[i]=='-'){//判断正负pol=-1;i++;}else......
  • c语言中关于字符数组赋值问题
    一维数组代码#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;constintN=1010;charstr[N];charst[N];chars1[N];chars2[N];/*abcdeabcdeabcdeabcde*/intmain(){ scanf("%s",&str+1); ......