Linux c编程实例_例子

例一:字符与整型变量的实现

#include <stdio.h>

int main()
{
    int c1,c2;
    char c3;
    c1=‘a‘-‘A‘;
    c2=‘b‘-‘B‘;
    c3=‘c‘-32;
    printf("c1 is %d and c2 is %d\n",c1,c2);
    printf("c3 is %d and %c\n",c3,c3);
                  //字符在内存中是以ASCII码存在的, a就是65等等
                 //字符型变量可以与整型变量进行运算     

}

结果是:

32 32

67 C

字符串常量是 “”里面的

字符常量是 ‘’里面的

例二:

#include "stdio.h"

int main()
{
 char a,b;
 a=97;
 b=98;
 printf("%c%c\n",a,b);
 printf("%d%d",a,b);
 } 

ab

9798

-------------------------------

为什么字符串常量末尾要加一个‘\o’,因为字符串是以ASCII存储的,要有一个结束的标志位。不让很难判断字符串在内存中占据多少空间。

例三:指针数组与二级指针【linux c 编程第84页】

#include "stdio.h"
int main()
{
  int a[5]={1,3,5,7,9 };
  int *p[5],i;
  int **pp=p;// 相当于 int a=12;int *b=&a;int **c=&b; 最后一个的这里 

  for(i=0;i<5;i++)
  p[i]=&a[i];

  for(i=0;i<5;i++)
  printf("%d\n",*p[i]);

  for(i=0;i<5;i++,pp++)
  printf("%d",**pp);
}

例四:指针和数组的关系。经典例子

#include "stdio.h"
int main()
{
//经典例子 linux c程序 第85页
  int a[2][5]={1,3,5,7,9,2,4,6,8,10};
  int (*p)[5],i; // int (*p)[5] 表示p是一个指针,指向含有5个元素的一维指针,并且p是一维数组 的首地址
  p=a;
  for(i=0;i<5;i++)
   printf("%d ",(*p)[i]);
   printf("\n");

   p++;  //p加1,指向二维数组a的第二行
   for(i=0;i<5;i++)
    printf("%d ",(*p)[i]);
       printf("\n");
   return 0;

}
时间: 2024-08-28 14:13:08

Linux c编程实例_例子的相关文章

Linux文件编程实例

//捕获fopen调用中的错误 #include <stdio.h> #include <errno.h> #include <string.h> #define MYFILE "missing.txt" int main(  ) { FILE* fin; fin=fopen( MYFILE,"r" ); if( fin==(FILE*)NULL ) { printf( "%s: %s\n",MYFILE,st

Linux多进程编程实例

前言:编写多进程程序时,我们应该了解一下,创建一个子进程时,操作系统内核是怎样做的.当通过fork函数创建新的子进程时,内核将父进程的用户地址空间的内容复制给子进程,这样父子进程拥有各自独立的用户空间,当父进程修该变量的值时不会影响子进程中的相应变量.但为了提高效率,Linux采用了COW(copy on write)算法,子进程创建时,父子进程享有相同的地址空间,只是在页表中设置cow标识,只有在父进程或子进程执行写数据操作时,才为子进程申请一个物理页,将父进程空间中相应数据所在页的内容复制到

Linux 多线程编程实例

一.多线程 VS 多进程 和进程相比,线程有很多优势.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护代码段和数据.而运行于一个进程中的多个线程,他们之间使用相同的地址空间.正是这样,同一进程下的线程之间共享数据空间,数据可以相互使用,并且线程间切换也要更快些,可以更有效的利用CPU. 二.程序设计 [注] 头文件<pthread.h> 编译时要加载动态库 libpthread.a,使用 -lpthread 1.创建线程2.等待线程3.关闭线程4.退出清除

Linux管道编程实例

#include <unistd.h> #include <signal.h> #include <stdio.h> char parent[] = "a message from parrent"; char child[] = "a message from child"; main () { int chan1[2], chan2[2]; int pid; char buf[100]; pipe(chan1); pipe(c

linux网络编程实例

获取服务器时间 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #

Linux 多线程编程 实例 2

编写一个程序,开启3个线程,这3个线程的ID分别为A.B.C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示:如:ABCABC….依次递推. 使用条件变量来实现: #include <pthread.h>#include <stdio.h>#include <unistd.h>static pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t condA ;s

【转】Linux共享内存编程实例

原文地址:http://blog.csdn.net/pcliuguangtao/article/details/6526119 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 6

基于树莓派的Linux串口编程_实现自发自收

串口是计算机上一种非常通用设备通信的协议,常用PC机上包含的是RS232规格的串口,具有连接线少,通讯简单,得到广泛的使用. Linux对所有设备的访问是通过设备文件来进行的,串口也是这样,为了访问串口,只需打开其设备文件即可操作串口设备.在linux系统下面,每一个串口设备都有设备文件与其关联,设备文件位于系统的/dev目录下面.如linux下的/ttyS0,/ttyS1分别表示的是串口1和串口2. 树莓派UART端口的位置:见下图的GPIO14(TXD).GPIO 15(RXD) 本文是基于

Linux内核编程:Linux2.6内核源码解析_进程遍历 &nbsp; &nbsp; &nbsp; &nbsp;

/*     *File    : test.c   *Author  : DavidLin        *Date    : 2014-12-07pm        *Email   : [email protected] or [email protected]        *world   : the city of SZ, in China        *Ver     : 000.000.001        *history :     editor      time