C Language | Issue 20230225

C Language | Issue 20230225

1. Question 1

Which slice of the following code is NOT endless loop?

Which part of the following code is not an infinite loop?

A for (;(c=getchar())!='\n'; ) printf("*c",c);

B for(;;x+=1 );

C while(x=1){ x++;}

D for(i=10;;i--) sum += i;

Option A, when running, if you don’t enter a newline, it will keep looping;

B is an infinite loop;

C is an infinite loop;

D is an infinite loop;

getchar() is used to read a character from the standard input device. Then convert the character from unsigned char to int and return.

getchar() will return the character read, and if it returns EOF, it means that there is an error.

C for loop: https://www.runoob.com/cprogramming/c-for-loop.html

Syntax of for loop in C language:

for ( init; condition; increment )
{
statement(s);
}

Here is the control flow of the for loop:

  1. init will be executed first, and only once. This step allows you to declare and initialize any loop control variables. You can also not write any statement here, as long as a semicolon appears.
  2. Next, the condition will be judged. If true, the loop body is executed. If false, the loop body is not executed and control flow jumps to the next statement immediately following the for loop.
  3. After executing the body of the for loop, the flow of control jumps back to the increment statement above. This statement allows you to update loop control variables. This statement can be left blank as long as a semicolon appears after the condition.
  4. The condition is evaluated again. If it is true, the loop is executed, and the process is repeated (the loop body, then increment the step value, and then re-evaluate the condition). The for loop terminates when the condition becomes false.

Infinite loop

If the condition never becomes false, the loop becomes an infinite loop. A for loop can be used in the traditional sense to implement an infinite loop. Since none of the three expressions are required to form a loop, you can leave some conditional expressions blank to form an infinite loop.

#include <stdio.h>

int main ()
{
   for( ; ; )
   {
      printf("The loop will execute forever!\n");
   }
   return 0;
}

When the conditional expression is absent, it is assumed to be true. You can also set an initial value and increment expressions, but in general, C programmers prefer to use the for(;;) construct to represent an infinite loop.

**Note:** You can terminate an infinite loop by pressing the Ctrl + C keys.

2. Question 2

#include <stdio.h>

int main(void)
{
    int C;
    int a,b;
    a =5;
    b =7;
    C = a +++ b;
    printf("a: %d, b: %d, C: %d\n", a,b,C);
}

operation result:

PS D:\05_study\mark-down-doc\02-cStudy\source> make 01-test 
gcc -o 01-test 01-test.c -g -Wall
PS D:\05_study\mark-down-doc\02-cStudy\source> .\01-test.exe
a: 6, b: 7, C: 12

C Operators: https://www.runoob.com/cprogramming/c-operators.html

Operator precedence in C

The precedence of operators determines the combination of terms in an expression. This affects how an expression is evaluated. Some operators have higher precedence than others, for example, the multiplication and division operators have higher precedence than the addition and subtraction operators.

For example x = 7 + 3 * 2, here, x is assigned the value 13, not 20, because the operator * has higher precedence than +, so the multiplication 3*2 is calculated first, and then 7 is added.

The following table lists the operators in descending order of operator precedence, with operators with higher precedence appearing above the table and operators with lower precedence appearing below the table. In expressions, operators with higher precedence are evaluated first.

categoryoperatorCombination
suffix() [] -> . ++ - -from left to right
One yuan+ - ! ~ ++ - - (type)* & sizeoffrom right to left
multiply and divide* / %from left to right
addition and subtraction+ -from left to right
shift<< >>from left to right
relation< <= > >=from left to right
equal== !=from left to right
Bit AND AND&from left to right
Bitwise exclusive or XOR^from left to right
bit OR|from left to right
Logical AND AND&&from left to right
logical or OR||from left to right
condition?:from right to left
assignment= += -= *= /= %=>>= <<= &= ^= |=from right to left
comma,from left to right

Example 1:

#include <stdio.h>
 
int main()
{
   int a = 21;
   int b = 10;
   int c ;
 
   c = a + b;
   printf("Line 1 - c The value is %d\n", c );
   c = a - b;
   printf("Line 2 - c The value is %d\n", c );
   c = a * b;
   printf("Line 3 - c The value is %d\n", c );
   c = a / b;
   printf("Line 4 - c The value is %d\n", c );
   c = a % b;
   printf("Line 5 - c The value is %d\n", c );
   c = a++;  // Add 1 after assignment, c is 21, a is 22
   printf("Line 6 - c The value is %d\n", c );
   c = a--;  // Subtract 1 after assignment, c is 22, a is 21
   printf("Line 7 - c The value is %d\n", c );
}

run:

PS D:\05_study\mark-down-doc\02-cStudy\source> make 01-test 
gcc -o 01-test 01-test.c -g -Wall
PS D:\05_study\mark-down-doc\02-cStudy\source> .\01-test.exe
Line 1 - c The value is 31
Line 2 - c The value is 11
Line 3 - c The value is 210
Line 4 - c The value is 2
Line 5 - c value is 1
Line 6 - c The value is 21
Line 7 - c The value is 22

Tags: C programming language

Posted by coder_ on Sat, 04 Mar 2023 20:27:36 +1030