Chapter II overview of C language

Keywords: C

Chapter II overview of C language

1. Simple C program example

cat first.c
#include <stdio.h>
int main(void)
{
    
    int num;
    num = 1;

    printf("I am a simple ");
    printf("Computer. \n");
    printf("My favorite number is %d besause it is first.\n",num);

    return 0;
}

Compilation execution:

[root@hans ~]# gcc -o first first.c 
[root@hans ~]# ls -rlt first
-rwxr-xr-x 1 root root 8536 Aug  5 08:30 first
[root@hans ~]# ./first 
I am a simple Computer. 
My favorite number is 1 besause it is first.

C program analysis

2. Program analysis

​ C program contains one or more functions, which are the basic modules of C program.

​ /* */ Notes in C language, / * and * / are the contents of notes. Comments are only intended to help readers understand the program, and the compiler ignores them.

#include <stdio.h>   /*This is the first line of the program. Input all the contents of stdio.h file into the position of this line, #include this line of code is a C preprocessor directive. All C compilers provide stdio.h file, which contains input and output functions for the compiler. The file name means standard input and output header file.*/
int main(void)  /*The main function and C program must be executed from the main() function. In addition to the main function, other functions can be named at will, but the main() function must be the start function
int Is the return type of the main() function, indicating that the value returned by the main() function is an integer. The parentheses after the function name contain some information about the incoming function. In this case, no information is passed. Therefore, it is void.*/
{   /*Inside the curly braces is the function body. All C functions use curly braces to mark the beginning and end of the function as a whole. They cannot be omitted, and they must appear in pairs. This is the beginning*/
    
    int num; /*Variable declaration means to set a variable num, which is of type int.
    int Is a keyword in C language, which represents the data type in C language. In C language, all variables must be declared before they can be used. When naming variables, meaningful variable names or identifiers should be used.
    C99 And C11 allow longer identifier names, but the compiler recognizes only the first 63 characters. Only 31 characters are allowed for external identifiers.
    It can be named with lowercase letters, uppercase letters, numbers and underscores (), and the first character must be a character or an underscore, not a number.*/
    num = 1;/*Assignment, you can assign different values to num, but the assigned values must be of type int. Assignment expression statements assign values from the right to the left*/

    printf("I am a simple ");/*printf Function, using the printf function to output the information passed to the printf function. The contents in () are parameters, which have specific values are arguments, and formal parameters are variables used to save values in functions*/
    printf("Computer. \n");
    printf("My favorite number is %d besause it is first.\n",num);/*%d Is a placeholder, indicating that a variable is to be printed here, and d indicates that the variable is a decimal integer*/

    return 0; /*int main(void) It indicates that the main() function returns an integer after execution. The C function with return value should have a return statement, which starts with return as the keyword, followed by the value to be returned. And ends with a semicolon*/
} /*End of function*/

4 reasons for declaring variables:

  • Put all variables in one place to facilitate readers to find and understand the purpose of the program
  • Declaring variables will prompt you to make some plans before writing your program
  • Declaring variables can help you find small errors hidden in your program, such as misspelling variable names.
  • If the variable is not declared in advance, the C program will not compile.

The standard before C99 requires that declarations be placed at the top of the block. The advantage is that putting declarations together makes it easier to understand the purpose of the program. C99 allows variables to be declared when necessary. The advantage is that if you declare a variable before assigning a value to the variable, you will not forget to assign a value to the variable.

2.1 skills to improve program readability

  1. Select a meaningful function name
  2. The program should write comments
  3. Separate conceptual parts with empty lines in a function.
  4. Each statement occupies one line.

2.3 printing multiple values

#include <stdio.h>
int main(void){
	printf("There are %d feet in %d fathoms\n", 12, 2);
	return 0;
}

2.4 multiple functions

#two_func.c
#include <stdio.h>
void butler(void); /*Function prototype, newly added in C90 standard.*/
int main(void){
	printf("I will summon the butler function.\n");
    butler();
    printf("Yes. Bring me some tea and writeable DVDs.\n");
    
    return 0;
}
void butler(void){  /*No return value and no parameters*/
    printf("You rang, sir?\n")
}
/* 
butler()The execution of the function mainly depends on where the function is called in main(), rather than where the function butler() is
 If you write the function prototype: void butler(void), you can put the butler() function anywhere. If it is not written, the definition of the butler() function must be in front of the function calling it.
If the program is written like this, an error will be reported during compilation:
#include <stdio.h>
/*void butler(void); Comment out the function prototype*/
int main(void){
	printf("I will summon the butler function.\n");
    butler();
    printf("Yes. Bring me some tea and writeable DVDs.\n");
    
    return 0;
}
void butler(void){  /*No return value and no parameters*/
    printf("You rang, sir?\n")
}
*/

2.5 keywords and reserved identifiers

ISO C keyword

auto extern short while
break float signed _Alignas
case for sizeof _Alignof
char goto static _Atomic
const if struct _Bool
continue inline switch _Complex
default int typedef _Generic
do long unio _Imaginary
double register unsigned _Noreturn
else restrict void _Static_assert
enum return volatile _Thread_local

If you use keywords improperly, the compiler treats them as syntax errors. These are not used as variable names.

3 programming practice

  1. Print name in format

    #include <stdio.h>
    
    int main(void){
        printf("Hans Wang\n");
        printf("Hans\nWang\n");
        printf("Hans");
        printf(" Wang\n");
    }
    
    #gcc -o name name.c
    #./name
    Hans Wang
    Hans
    Wang
    Hans Wang
    
  2. Print name and address

    #include <stdio.h>
    int main(void){
        printf("name: Hans Wang\n");
        printf("address: ShanDong HeZe\n");
        
        return 0;
    }
    
  3. Convert the age into days and display these two values, regardless of leap years

    #include <stdio.h>
    
    int main(void){
        int age;
        int day = 365;
        int days;
    
        printf("Please input your age:");
        scanf("%d", &age);
    
        days = age * day;
    
        printf("Your age is %d,equel %d days.\n",age,days);
    
        return 0;
    }
    
  4. To generate the specified output, two custom functions are called in addition to the main() function

    #include <stdio.h>
    void jolly(void);
    void deny(void);
    
    int main(void){
       jolly(); 
       jolly(); 
       jolly(); 
       deny();
        return 0;
    }
    
    void jolly(void){
        printf("For he's a jolly good fellow!\n");
    }
    void deny(void){
        printf("Which nobody can deny!\n");
    }
    
  5. Generate specified output

    #include <stdio.h>
    void br(void);
    void ic(void);
    
    int main(void){
        br();
        ic();
        printf("India,China,\n");
        printf("Brazil,Russia\n");
    }
    
    
    void br(void){
        printf("Brazil,Russia,");
    }
    void ic(void){
        printf("India,China\n");
    }
    
  6. Write a program to create an integer variable and calculate the square of the two digits of the variable.

    #include <stdio.h>
    
    int main(void){
    
        int toes;
        toes = 10;
        
        int toes2;
        int toes_double;
        toes2 = 2 * toes;
        toes_double = toes * toes;
        
        printf("toes = %d\ntoes2 = %d\ntoes_double = %d\n", toes, toes2, toes_double);
    
        return 0;
    }
    
  7. Generates the specified output simple.

    #include <stdio.h>
    
    void smile(void){
        printf("Smile!");
    }
    
    int main(){
        smile();
        smile();
        smile();
        printf("\n");
        smile();
        smile();
        printf("\n");
        smile();
        printf("\n");
    
        return 0;
    }
    
  8. Write a program to call another function in the function.

    #include <stdio.h>
    
    void one_three(void);
    void two(void);
    
    int main(){
        printf("starting now:\n");
        one_three();
        printf("done!\n");
        return 0;
    }
    
    void one_three(void){
        printf("one\n");
        two();
        printf("three\n");
    }
    
    void two(void){
        printf("two\n");
    
    }
    

Posted by zingbats on Sat, 27 Nov 2021 19:10:10 -0800