linux下的C语言快速学习—从1+1开始。

最近刚进一个公司去实习,进入这个公司以后刚开始有两周的实训,一般是给一个自己从未接触的领域来学习,我的题目是linux下使用Shell和C实现一个RSS全文转化的功能,类似与http://feedex.net/ 这个网站功能,我对这些特别不熟,我还不知道整个的解决方案是什么样的,由于时间紧迫只能边学边想了。如果各位有什么解决方案期待您给我留言。今天开始学习linux下的c语言编程。

1、从1+1开始

建立一个文件main.c使用gedit打开写下如下代码:

#include"stdio.h"
void main()
{
int a,b;
printf("please Enter Two Numbers:");
scanf("%d%d",&a,&b);
printf("%d",(a+b));
}

然后打开终端

jerry@ubuntu:~$ gcc main.c

jerry@ubuntu:~$ ./a.out

please Enter Two Numbers:1 1

2

对于以上几句命令的简单解释:

gcc是linux平台的c编译器,编译后在当前生成可执行文件a.out,直接输入可执行文件的路径就可以执行它了。如果不想让执行文件的名字为a.out,可以用gcc的-o参数来指定文件名:

jerry@ubuntu:~$ gcc main.c -o main

jerry@ubuntu:~$ ./main

please Enter Two Numbers:1 1

2、简单的函数调用

建立一个hanshu.c文件

#include"stdio.h"

void Clock(int hour,int minute)
{
  if(hour<24&&hour>-1&&minute>-1&&minute<60)
  {
    printf("%d:%d\n",hour,minute);
  }
  else
  {
      printf("wrong Time!\n");
  } 
}

void main()
{
    Clock(12,14);
    Clock(100,11);
}

  然后打开终端:

jerry@ubuntu:~$ gcc hanshu.c

jerry@ubuntu:~$ ./a.out

12:14

wrong Time!

注:在这里我犯了经常使用面向对象编程语言的人的错误,我刚开始把Click函数写在来main后面,然后报了
hanshu.c:8: warning: conflicting types for ‘Clock’
hanshu.c:4: note: previous implicit declaration of ‘Clock’ was here
的错误。
2、小综合实例—随机数产生与打印
#include "stdio.h"
#include "stdlib.h"
#define N 20

int a[N];

void gen_random(int upper)
{
   int i;
  for( i=0;i<N;i++)
  {
     a[i]=rand()%upper;
  }
}

void printf_random()
{
    int i=0;
    for(i=0;i<N;i++)
    {
      printf("%d  ",a[i]);
    }
    printf("\n");
}

void main()
{
   gen_random(5);
   printf_random();
}

 然后打开终端:

jerry@ubuntu:~$ gcc digui.c -o random

jerry@ubuntu:~$ ./random

3  1  2  0  3  0  1  2  4  1  2  2  0  4  3  1  0  1  2  1  

对于已经已经熟悉C语言基础的来说,只是使用一些实例来熟悉一下编程环境,没有什么新的东西可言。只是练练手罢了。

原文地址:https://www.cnblogs.com/JerryWang1991/p/2186679.html