第一章的程序

创建一个程序

              #include  

               int   main (void)
               {
                   printf(" Hello  World ! " ); 
                   return  0;
               }

C语言的源代码的扩展名是 .c
C++的扩展名是 .cpp

编辑一个程序

        #include 

        int main(void)
        {
           printf("\"If at first you don't  succeed,   try, try,  try );
           return 0;
        }

注释

位于/* 和 /之间的任意文本都是注释
//后面的内容也是注释,这是两种记号,两种方式
/

  • Written by Ivor Horton
  • Copyright 2012
    */

也可以修饰注释,使它们比较突出
/********************************************* *

  • This is a vary important comment *
  • so please read this. *
    ********************************************** */

// 是行注释
/* / 是块注释
行注释 说明 本行 // 之后的所有内容都是注释
/
是一个整体
*/ 也是一个整体
就像是一对括号
括号里面的都是注释

  • 抄课本跟自己理解之后区别

预处理指令(preprocessing directive)

常见的标准库头文件
math.h ctype.h stdbool.h stdio.h string.h

定义main()函数

是两个括号之间执行某组操作的一段代码
每个 c 程序都必须有一个main()函数
关键字int表示main()函数的返回值的类型

关键字

int char double long short signed unsigned sizeof const auto for else default do while if goto switch case break return (常用到的)

函数体

在函数名称后面位于起始及结束的两个大括号之间的代码块

输出信息

printf()是一个标准的库函数

参数

包含在函数名后的圆括号内的项称为参数,它指定要传送给函数的数据。

三字母序列

控制符

1.4

         //program 1.4 Another Simple C program - Displaying a Quotation
         #include 

         int  main (void)
         {
            printf("My formula for success?\nRise  early, work late, strike oil.\n");
            return 0;
         }      准则  早起 工作晚

1.5

          #include 

          int main(void)
          {
             printf("\"It is a wise father that knows his own child.\"\nShakespears\n");
             return 0;
          }

1.6

          #include

          int main(void)
          {
             printf("\"It is a wise father that knows his  own child.\"\nShakespears\n");
             return 0;
          } 

1.7

          #include 

         int main (void)
         {
            printf("Be  careful!!\n\a");
            return 0;
         }

试试看:将所学的知识用于实践

下面的例子将前面学到的知识用于实践。首先,看看下面的代码,检查自己是否理解它的作用。然后输入这些代码,编译、链接并执行,看看会发生什么。
#include

                            int main (void)
                            {
                                printf(" Hi there !\n\n\nThis program is a bit");
                                printf(" longer than the others.");
                                printf(" \nBut really it's only more text.\n\n\n\a\a");
                                printf("Hey,wait a minute!! What was that???\n\n");
                                printf("\t1.\tA bird ?\n");
                                printf("\t2.\tA plane?\n");
                                printf("\t3.\tA control character?\n");
                                printf("\n\t\t\b\bAnd how will this look when it prints out?\n\n");
                                return 0;
                            }

标准库函数printf()
空两行是由3个转义序列\n生成的

习题1. 1 编写一个程序,用两个printf()语句分别输出自己的自己的名字及地址。
习题1. 2 将上一个练习改成所有的输出只用一个printf()语句。
习题1. 3 编写一个程序,输出下列文本,格式如下所示:
"It's freezing in here," he said coldly.

你可能感兴趣的:(第一章的程序)