首頁 > 後端開發 > C++ > 主體

C基礎知識

WBOY
發布: 2024-07-17 11:54:40
原創
1092 人瀏覽過

C Basics

C

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
登入後複製

創建變數

在 C 語言中,變數用於儲存可在程式中操作的資料。這是有關在 C:

中建立和使用變數的綜合指南

變數宣告和初始化的語法

聲明:

type variable_name;
登入後複製

範例:

int number;
char letter;
float salary;
登入後複製

初始化:

variable_name = value;
登入後複製

範例:

number = 10;
letter = 'A';
salary = 50000.0;
登入後複製

宣告與初始化組合:

type variable_name = value;
登入後複製

範例:

int age = 25;
double pi = 3.14159;
char grade = 'A';
登入後複製

變數名稱規則

  1. 變數名稱必須以字母(a-z、A-Z)或底線(_)開頭。
  2. 後續字元可以是字母、數字 (0-9) 或底線。
  3. 變數名稱區分大小寫(myVar 和 myvar 不同)。
  4. 變數名稱不能是 C 保留關鍵字(例如 int、return、void)。

範例:

int age;
char _grade;
float salary_2024;
登入後複製

變數範圍

變數的作用域是程式中可以存取該變數的部分。 C 中的變數可以是:

局部變數:在函數或區塊內聲明,並且只能在該函數或區塊內存取。

void myFunction() {
    int localVar = 5;
    printf("%d", localVar);
}
登入後複製

全域變數:在所有函數之外聲明,並可從程式內的任何函數存取。

int globalVar = 10;

void myFunction() {
    printf("%d", globalVar);
}

int main() {
    myFunction(); // Outputs: 10
    return 0;
}
登入後複製

常數

常數是指一旦賦值就無法改變其值的變數。它們是使用 const 關鍵字聲明的。

範例:

const int DAYS_IN_WEEK = 7;
const float PI = 3.14159;
登入後複製

預設值

在 C 中,未初始化的局部變數包含垃圾值,而全域變數和靜態變數預設為零。

範例:

#include <stdio.h>

int globalVar;  // default value 0

int main() {
    int localVar;  // contains garbage value
    printf("Global Variable: %d\n", globalVar);
    printf("Local Variable: %d\n", localVar);
    return 0;
}
登入後複製

聲明許多帶值或不帶值的變數

您可以在一行中聲明多個相同類型的變量,並用逗號分隔它們。您也可以在聲明時或稍後初始化它們。

範例:

// Declaring multiple variables without values
int a, b, c;

// Declaring and initializing some variables
int x = 10, y, z = 30;
登入後複製

一個值對應多個變數

您可以透過連結賦值運算子將相同的值指派給多個變數。

範例:

int m, n, o;
m = n = o = 50;
登入後複製

建立評論

C 中的註解是不可執行的語句,用來描述和解釋程式碼。它們對於使程式碼更具可讀性和可維護性至關重要。 C 支援兩種類型的註解:

1. 單行註釋

單行註解以兩個正斜線 (//) 開頭。該行 // 後面的所有內容都被視為註解。

文法:

// This is a single-line comment
int x = 10; // x is initialized to 10
登入後複製

2. 多行註釋

多行註解以/*開始,以*/結束。 /* 和 */ 之間的所有內容都被視為註釋,無論它跨越多少行。

文法:

/*
 This is a multi-line comment.
 It can span multiple lines.
*/
int y = 20; /* y is initialized to 20 */
登入後複製

用法範例

單行註解範例:

#include <stdio.h>

int main() {
    // Print Hello, World!
    printf("Hello, World!\n"); // This prints the string to the console
    return 0;
}
登入後複製

多行註解範例:

#include <stdio.h>

int main() {
    /*
     This is a simple C program
     that prints Hello, World!
     to the console.
    */
    printf("Hello, World!\n");
    return 0;
}
登入後複製

這些範例說明如何在 C 語言中使用單行和多行註解來使程式碼更具可讀性和可維護性。

C 語言的基本輸入與輸出

在C中,輸入和輸出運算是使用標準函式庫函數執行的。基本輸入和輸出最常用的函數是 printf 和 scanf。

1.使用printf輸出

printf 函數用於將文字和變數列印到控制台。

文法:

printf("format string", variable1, variable2, ...);
登入後複製

格式說明符:

  • %d 或 %i - 對於整數
  • %f - 用於浮點數
  • %lf - 用於雙精確度浮點數
  • %c - 字元
  • %s - 用於字串

範例:

#include <stdio.h>

int main() {
    int age = 25;
    float salary = 50000.0;
    double pi = 3.141592653589793;
    char grade = 'A';
    char name[] = "John";

    printf("Age: %d\n", age);
    printf("Salary: %.2f\n", salary);
    printf("Pi: %.15lf\n", pi);
    printf("Grade: %c\n", grade);
    printf("Name: %s\n", name);

    return 0;
}
登入後複製

2.使用scanf輸入

scanf 函數用於從控制台讀取格式化輸入。

文法:

scanf("format string", &variable1, &variable2, ...);
登入後複製

範例:

#include <stdio.h>

int main() {
    int age;
    float salary;
    double pi;
    char grade;
    char name[50];

    printf("Enter age: ");
    scanf("%d", &age);

    printf("Enter salary: ");
    scanf("%f", &salary);

    printf("Enter pi value: ");
    scanf("%lf", &pi);

    printf("Enter grade: ");
    scanf(" %c", &grade);  // Note the space before %c to consume any leftover newline character

    printf("Enter name: ");
    scanf("%s", name);  // Reads a single word, stops at whitespace

    printf("\nYou entered:\n");
    printf("Age: %d\n", age);
    printf("Salary: %.2f\n", salary);
    printf("Pi: %.15lf\n", pi);
    printf("Grade: %c\n", grade);
    printf("Name: %s\n", name);

    return 0;
}
登入後複製

重點

  • & 運算子在 scanf 中用來傳遞將儲存輸入的變數的位址。
  • 使用 scanf 讀取字元時,處理輸入緩衝區中留下的換行符非常重要。
  • scanf 讀取字串直到第一個空白字元。要讀取一行文本,可以使用 fgets 等函數。

使用 fgets 讀取一行文字:

文法:

fgets(buffer, size, stdin);
登入後複製

範例:

#include <stdio.h>

int main() {
    char name[50];

    printf("Enter your full name: ");
    fgets(name, sizeof(name), stdin);  // Reads a line of text including spaces

    printf("Your name is: %s", name);

    return 0;
}
登入後複製

使用 getchar 和 putchar 進行字元輸入和輸出

getchar 範例:

#include <stdio.h>

int main() {
    char ch;

    printf("Enter a character: ");
    ch = getchar();

    printf("You entered: ");
    putchar(ch);
    printf("\n");

    return 0;
}
登入後複製

putchar Example:

#include <stdio.h>

int main() {
    char ch = 'A';

    printf("The character is: ");
    putchar(ch);
    printf("\n");

    return 0;
}
登入後複製

Data Types in C

In C programming, data types specify the type of data that variables can store. C supports several basic and derived data types, each with specific properties. Here's a comprehensive guide to data types in C:

1. Primitive Data Types

Integer Types
  • int: Standard integer type, typically 4 bytes.

Example:

  int numInt = 100000;
登入後複製
  • short: Short integer type, typically 2 bytes.

Example:

  short numShort = 1000;
登入後複製
  • long: Long integer type, varies by system (commonly 4 or 8 bytes).

Example:

  long numLong = 10000000000L;
登入後複製
  • long long: Long long integer type, typically 8 bytes (C99 and later).

Example:

  long long bigNumber = 123456789012345LL;
登入後複製
Floating-Point Types
  • float: Single-precision floating-point, typically 4 bytes.

Example:

  float numFloat = 3.14f;
登入後複製
  • double: Double-precision floating-point, typically 8 bytes.

Example:

  double numDouble = 3.14159;
登入後複製
  • long double: Extended precision floating-point, varies by system.

Example:

  long double extendedPi = 3.14159265358979323846L;
登入後複製
Character Type
  • char: Character type, typically 1 byte. Stores ASCII values (0 to 127) or UTF-8 characters.

Example:

  char letter = 'A';
登入後複製
Boolean Type
  • _Bool or bool: Represents true or false values (0 or 1).
    • To use boolean type, include header file.

Example:

  #include <stdbool.h>
  bool isValid = true;
登入後複製

2. Derived Data Types

Array
  • Syntax: type array_name[size];

Example:

  int numbers[5] = {1, 2, 3, 4, 5};
登入後複製
Pointer
  • Syntax: type *pointer_name;

Example:

  int *ptr;
登入後複製
Structure
  • Syntax:
  struct structure_name {
      type member1;
      type member2;
      // ...
  };
登入後複製

Example:

  struct Person {
      char name[50];
      int age;
      float salary;
  };
登入後複製

Booleans in C

In C, booleans are typically represented using integer types, where 0 represents false and any non-zero value represents true. Let's explore how booleans are handled in C programming:

1. Boolean Representation

In C, there is no dedicated boolean type like in some other languages. Instead, integers (int) are commonly used to represent boolean values.

  • _Bool Type: Defined in C standard as a data type capable of holding only 0 (false) or 1 (true).

Example:

#include <stdio.h>

int main() {
    _Bool b1 = 1;  // true
    _Bool b2 = 0;  // false

    printf("b1: %d\n", b1);  // Output: 1 (true)
    printf("b2: %d\n", b2);  // Output: 0 (false)

    return 0;
}
登入後複製
  • Using stdbool.h: Introduced in C99, stdbool.h provides a clearer representation with bool, true, and false.

Example:

#include <stdio.h>
#include <stdbool.h>

int main() {
    bool isValid = true;
    bool isReady = false;

    printf("isValid: %d\n", isValid);  // Output: 1 (true)
    printf("isReady: %d\n", isReady);  // Output: 0 (false)

    return 0;
}
登入後複製

2. Boolean Evaluation

In C, expressions are evaluated to true (1) or false (0). Here are some examples of expressions and their boolean evaluations:

  • Integer and Float Values: Any non-zero integer or non-zero floating-point value is evaluated as true. Zero (0) and zero as float (0.0) are evaluated as false.

Example:

#include <stdio.h>

int main() {
    int num = 10;
    float salary = 0.0;

    if (num) {
        printf("num is true\n");
    } else {
        printf("num is false\n");
    }

    if (salary) {
        printf("salary is true\n");
    } else {
        printf("salary is false\n");
    }

    return 0;
}
登入後複製

Output:

num is true
salary is false
登入後複製

3. Null Pointer Evaluation

In C, a null pointer is evaluated as false in boolean context. A null pointer is typically represented as (type *)0.

Example:

#include <stdio.h>

int main() {
    int *ptr = NULL;

    if (ptr) {
        printf("ptr is not NULL\n");
    } else {
        printf("ptr is NULL\n");
    }

    return 0;
}
登入後複製

Output:

ptr is NULL
登入後複製

Type Casting: Implicit and Explicit

In C programming, type casting refers to converting a value from one data type to another. There are two types of type casting: implicit and explicit. Let's explore each in detail:

1. Implicit Type Casting (Automatic Type Conversion)

Implicit type casting occurs automatically by the compiler when compatible types are mixed in expressions. It promotes smaller data types to larger data types to avoid loss of data. It's also known as automatic type conversion.

Example:

#include <stdio.h>

int main() {
    int numInt = 10;
    double numDouble = 3.5;

    double result = numInt + numDouble;  // Implicitly converts numInt to double

    printf("Result: %.2lf\n", result);  // Output: 13.50

    return 0;
}
登入後複製

In this example, numInt (an integer) is implicitly converted to a double before performing the addition with numDouble.

2. Explicit Type Casting (Type Conversion)

Explicit type casting is performed by the programmer using casting operators to convert a value from one data type to another. It allows for precise control over the type conversion process but can lead to data loss if not used carefully.

Syntax:

(type) expression
登入後複製

Example:

#include <stdio.h>

int main() {
    double numDouble = 3.5;
    int numInt;

    numInt = (int)numDouble;  // Explicitly casts numDouble to int

    printf("numInt: %d\n", numInt);  // Output: 3

    return 0;
}
登入後複製

In this example, numDouble (a double) is explicitly cast to an int. The decimal part is truncated, resulting in numInt being 3.

Arrays in C

Arrays in C are collections of variables of the same type that are accessed by indexing. They provide a way to store multiple elements under a single name.

1. Declaring Arrays

To declare an array in C, specify the type of elements it will hold and the number of elements enclosed in square brackets [].

Syntax:
type arrayName[arraySize];
登入後複製
  • type: Data type of the array elements.
  • arrayName: Name of the array.
  • arraySize: Number of elements in the array.
Example:
int numbers[5];  // Array of 5 integers
登入後複製

2. Initializing Arrays

Arrays can be initialized either during declaration or after declaration using assignment statements.

Example:
int numbers[5] = {1, 2, 3, 4, 5};  // Initializing during declaration

// Initializing after declaration
int moreNumbers[3];
moreNumbers[0] = 10;
moreNumbers[1] = 20;
moreNumbers[2] = 30;
登入後複製

3. Accessing Array Elements

Array elements are accessed using zero-based indexing, where the first element is at index 0.

Example:
int numbers[5] = {1, 2, 3, 4, 5};
printf("First element: %d\n", numbers[0]);   // Output: 1
printf("Second element: %d\n", numbers[1]);  // Output: 2
登入後複製

4. Modifying Array Elements

Array elements can be modified by assigning new values to specific indices.

Example:
int numbers[5] = {1, 2, 3, 4, 5};
numbers[2] = 10;  // Modify the third element
printf("Modified third element: %d\n", numbers[2]);  // Output: 10
登入後複製

5. Multidimensional Arrays

C supports multidimensional arrays, which are arrays of arrays. They are useful for storing tabular data or matrices.

Example:
int matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

printf("Element at row 2, column 3: %d\n", matrix[1][2]);  // Output: 6
登入後複製

6. Passing Arrays to Functions

When passing arrays to functions, C passes them by reference. This means any modifications made to the array within the function affect the original array.

Example:
#include <stdio.h>

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    printArray(numbers, 5);  // Pass array and its size to function
    return 0;
}
登入後複製

Strings in C

In C programming, strings are arrays of characters terminated by a null ('\0') character. Let's explore how to work with strings, including input, output, and manipulation:

1. Declaring and Initializing Strings

Strings in C are arrays of characters. They can be declared and initialized in several ways:

Syntax:

char strName[size];
登入後複製

Example:

#include <stdio.h>

int main() {
    // Declaring and initializing a string
    char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

    // Alternatively, using string literal (implicitly adds '\0')
    char message[] = "Welcome";

    printf("Greeting: %s\n", greeting);  // Output: Hello
    printf("Message: %s\n", message);    // Output: Welcome

    return 0;
}
登入後複製

2. String Input with scanf()

To input a string with spaces in C, fgets() from is often used instead of scanf() because scanf() stops reading at the first space. Here’s how to use fgets():

Example:

#include <stdio.h>

int main() {
    char name[50];

    printf("Enter your name: ");
    fgets(name, sizeof(name), stdin);  // Read input including spaces

    printf("Hello, %s!\n", name);

    return 0;
}
登入後複製

3. String Functions

C provides several library functions for manipulating strings, declared in .

  • strlen(): Calculates the length of a string.

Example:

  #include <stdio.h>
  #include <string.h>

  int main() {
      char str[] = "Hello";
      int len = strlen(str);

      printf("Length of '%s' is %d\n", str, len);  // Output: Length of 'Hello' is 5

      return 0;
  }
登入後複製
  • strcpy(): Copies one string to another.

Example:

  #include <stdio.h>
  #include <string.h>

  int main() {
      char src[] = "Hello";
      char dest[20];

      strcpy(dest, src);

      printf("Copied string: %s\n", dest);  // Output: Copied string: Hello

      return 0;
  }
登入後複製
  • strcat(): Concatenates two strings.

Example:

  #include <stdio.h>
  #include <string.h>

  int main() {
      char str1[20] = "Hello";
      char str2[] = " World";

      strcat(str1, str2);

      printf("Concatenated string: %s\n", str1);  // Output: Concatenated string: Hello World

      return 0;
  }
登入後複製
  • strcmp(): Compares two strings.

Example:

  #include <stdio.h>
  #include <string.h>

  int main() {
      char str1[] = "Hello";
      char str2[] = "Hello";

      if (strcmp(str1, str2) == 0) {
          printf("Strings are equal\n");
      } else {
          printf("Strings are not equal\n");
      }

      return 0;
  }
登入後複製

4. Handling String Input Safely

When using fgets() for string input, ensure buffer overflow doesn't occur by specifying the maximum length of input to read.

Example:

#include <stdio.h>

int main() {
    char sentence[100];

    printf("Enter a sentence: ");
    fgets(sentence, sizeof(sentence), stdin);  // Read up to 99 characters plus '\0'

    printf("You entered: %s\n", sentence);

    return 0;
}
登入後複製

5. Null-Terminated Strings

C strings are null-terminated, meaning they end with a null character '\0'. This character indicates the end of the string and is automatically added when using string literals.

Example:

#include <stdio.h>

int main() {
    char message[] = "Hello";  // Automatically includes '\0'

    // Printing characters until '\0' is encountered
    for (int i = 0; message[i] != '\0'; ++i) {
        printf("%c ", message[i]);
    }
    printf("\n");

    return 0;
}
登入後複製

Operators in C

Operators in C are symbols used to perform operations on variables and values. They are categorized into several types based on their functionality.

Arithmetic Operators

Arithmetic operators are used for basic mathematical operations.

Operator Name Description Example
+ Addition Adds two operands x + y
- Subtraction Subtracts the right operand from the left x - y
* Multiplication Multiplies two operands x * y
/ Division Divides the left operand by the right operand x / y
% Modulus Returns the remainder of the division x % y
++ Increment Increases the value of operand by 1 x++ or ++x
-- Decrement Decreases the value of operand by 1 x-- or --x
#include <stdio.h>

int main() {
    int a = 10, b = 3;
    printf("a + b = %d\n", a + b);   // Output: 13
    printf("a / b = %d\n", a / b);   // Output: 3
    printf("a %% b = %d\n", a % b);  // Output: 1 (Modulus operation)

    int x = 5;
    x++;
    printf("x++ = %d\n", x);         // Output: 6

    int y = 8;
    y--;
    printf("y-- = %d\n", y);         // Output: 7

    return 0;
}
登入後複製

Assignment Operators

Assignment operators are used to assign values to variables and perform operations.

Operator Name Description Example
= Assignment Assigns the value on the right to the variable on the left x = 5
+= Addition Adds right operand to the left operand and assigns the result to the left x += 3
-= Subtraction Subtracts right operand from the left operand and assigns the result to the left x -= 3
*= Multiplication Multiplies right operand with the left operand and assigns the result to the left x *= 3
/= Division Divides left operand by right operand and assigns the result to the left x /= 3
%= Modulus Computes modulus of left operand with right operand and assigns the result to the left x %= 3
#include <stdio.h>

int main() {
    int x = 10;
    x += 5;
    printf("x += 5: %d\n", x);   // Output: 15

    return 0;
}
登入後複製

Comparison Operators

Comparison operators are used to compare values.

Operator Name Description Example
== Equal Checks if two operands are equal x == y
!= Not Equal Checks if two operands are not equal x != y
> Greater Than Checks if left operand is greater than right x > y
< Less Than Checks if left operand is less than right x < y
>= Greater Than or Equal Checks if left operand is greater than or equal to right x >= y
<= Less Than or Equal Checks if left operand is less than or equal to right x <= y
#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("a == b: %d\n", a == b);   // Output: 0 (false)
    printf("a < b: %d\n", a < b);     // Output: 1 (true)

    return 0;
}
登入後複製

Logical Operators

Logical operators combine Boolean expressions.

Operator Description Example
&& Logical AND x < 5 && x < 10
|| Logical OR x < 5 || x < 4
! Logical NOT !(x < 5 && x < 10)
#include <stdio.h>

int main() {
    int x = 3;
    printf("x < 5 && x < 10: %d\n", x < 5 && x < 10);   // Output: 1 (true)
    printf("x < 5 || x < 2: %d\n", x < 5 || x < 2);     // Output: 1 (true)

    return 0;
}
登入後複製

Bitwise Operators

Bitwise operators perform operations on bits of integers.

#include <stdio.h>

int main() {
    int x = 5, y = 3;
    printf("x & y: %d\n", x & y);   // Output: 1
    printf("x | y: %d\n", x | y);   // Output: 7

    return 0;
}
登入後複製

Ternary Operator

The ternary operator ? : provides a shorthand for conditional expressions.

#include <stdio.h>

int main() {
    int age = 20;
    char* status = (age >= 18) ? "Adult" : "Minor";
    printf("Status: %s\n", status);   // Output: Adult

    return 0;
}
登入後複製

sizeof Operator

The sizeof operator in C is used to determine the size of a variable or data type in bytes.

#include <stdio.h>

int main() {
    int size_of_int = sizeof(int);
    printf("Size of int: %zu bytes\n", size_of_int);  // Output: Size of int: 4 bytes

    return 0;
}
登入後複製

& Address-of Operator

The & operator in C returns the memory address of a variable.

#include <stdio.h>

int main() {
    int x = 10;
    int *ptr = &x;  // ptr now holds the address of x
    printf("Address of x: %p\n", (void *)ptr);  // Output: Address of x: 0x7ffee24a7a98

    return 0;
}
登入後複製

* Dereference Operator

The * operator in C is used to access the value stored at the address pointed to by a pointer.

#include <stdio.h>

int main() {
    int y = 10;
    int *ptr = &y;  // ptr now holds the address of y
    int value = *ptr;  // Dereferencing ptr to get the value stored at ptr (which is y)
    printf("Value of y: %d\n", value);  // Output: Value of y: 10

    return 0;
}
登入後複製

-> Arrow Operator

The -> operator in C is used to access members of a structure or union through a pointer.

#include <stdio.h>

struct Person {
    char name[20];
    int age;
};

int main() {
    struct Person person = {"John", 25};
    struct Person *ptrPerson = &person;  // ptrPerson now points to the person structure

    printf("Name: %s\n", ptrPerson->name);  // Accessing name using arrow operator
    printf("Age: %d\n", ptrPerson->age);    // Accessing age using arrow operator

    return 0;
}
登入後複製

If-Else Statements in C

The if-else statement in C is used for decision-making, allowing different blocks of code to be executed based on whether a specified condition evaluates to true or false.

Syntax

if (condition) {
    // Block of code to be executed if the condition is true
} else {
    // Block of code to be executed if the condition is false
}
登入後複製
  • if: The if keyword is followed by parentheses () containing the condition to be evaluated. If the condition is true, the code inside the curly braces {} following if is executed.

  • else: The else keyword is optional. If the if condition evaluates to false, the code inside the curly braces {} following else is executed.

Example

#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        printf("%d is positive.\n", num);
    } else {
        printf("%d is not positive.\n", num);
    }

    return 0;
}
登入後複製

Output:

10 is positive.
登入後複製

Multiple if-else Statements

You can use multiple if-else statements to check multiple conditions sequentially.

#include <stdio.h>

int main() {
    int num = 0;

    if (num > 0) {
        printf("%d is positive.\n", num);
    } else if (num < 0) {
        printf("%d is negative.\n", num);
    } else {
        printf("%d is zero.\n", num);
    }

    return 0;
}
登入後複製

Output:

0 is zero.
登入後複製

Nested if-else Statements

You can nest if-else statements within each other to create more complex decision structures.

#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        if (num % 2 == 0) {
            printf("%d is positive and even.\n", num);
        } else {
            printf("%d is positive and odd.\n", num);
        }
    } else if (num < 0) {
        printf("%d is negative.\n", num);
    } else {
        printf("%d is zero.\n", num);
    }

    return 0;
}
登入後複製

Output:

10 is positive and even.
登入後複製

if Statement Without else

The else part of the if-else statement is optional. If omitted, the code block associated with if is executed only if the condition is true.

#include <stdio.h>

int main() {
    int num = 0;

    if (num > 0) {
        printf("%d is positive.\n", num);
    }

    return 0;
}
登入後複製

Note:
In C, numbers (int and float types) are evaluated in a boolean context where any non-zero value is considered true, and 0 (zero) is considered false. For example, if (num) evaluates num as true if it's non-zero, and if (f) evaluates f as true if it's non-zero.

Loops in C

Loops in C are used to execute a block of code repeatedly based on a condition. C supports three types of loops:

  1. while Loop
  2. for Loop
  3. do-while Loop

1. while Loop

The while loop repeatedly executes a target statement as long as a given condition is true.

Syntax:
while (condition) {
    // statement(s) to be executed as long as the condition is true
}
登入後複製
Example:
#include <stdio.h>

int main() {
    int count = 1;

    while (count <= 5) {
        printf("Count: %d\n", count);
        count++;
    }

    return 0;
}
登入後複製

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
登入後複製

2. for Loop

The for loop is used when the number of iterations is known beforehand.

Syntax:
for (initialization; condition; increment/decrement) {
    // statement(s) to be executed repeatedly until the condition becomes false
}
登入後複製
Example:
#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        printf("Iteration: %d\n", i);
    }

    return 0;
}
登入後複製

Output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
登入後複製

3. do-while Loop

The do-while loop is similar to the while loop, except that it executes the block of code at least once, and then repeats the loop as long as a specified condition is true.

Syntax:
do {
    // statement(s) to be executed at least once
} while (condition);
登入後複製
Example:
#include <stdio.h>

int main() {
    int num = 1;

    do {
        printf("Number: %d\n", num);
        num++;
    } while (num <= 5);

    return 0;
}
登入後複製

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
登入後複製

Control Statements in Loops

  • break Statement: Terminates the loop immediately, and control passes to the next statement following the loop.

  • continue Statement: Skips the current iteration and proceeds to the next iteration of the loop.

Example of break:

#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            break;  // Exit the loop when i is 3
        }
        printf("Iteration: %d\n", i);
    }

    return 0;
}
登入後複製

Output:

Iteration: 1
Iteration: 2
登入後複製

Example of continue:

#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue;  // Skip iteration when i is 3
        }
        printf("Iteration: %d\n", i);
    }

    return 0;
}
登入後複製

Output:

Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
登入後複製

Pointers in C

Pointers are variables that store memory addresses of other variables. They allow efficient manipulation of data and dynamic memory allocation in C.

1. Declaring and Initializing Pointers

A pointer declaration follows this syntax:

type *ptr;
登入後複製
  • type: Data type of the variable the pointer points to.
  • *: Indicates that ptr is a pointer.
Example:
int *ptr;  // Pointer to an integer
登入後複製

2. Initializing Pointers

Pointers can be initialized to point to a specific variable or memory location using the address-of operator &.

Example:
int num = 10;
int *ptr = &num;  // ptr now holds the address of num
登入後複製

3. Accessing Value at a Pointer

To access the value stored at the memory address pointed to by a pointer, use the dereference operator *.

Example:
int num = 10;
int *ptr = &num;

printf("Value at ptr: %d\n", *ptr);  // Output: 10
登入後複製

4. Pointer Arithmetic

Pointers in C support arithmetic operations, which can be useful for navigating through arrays or dynamically allocated memory.

  • Increment/Decrement: Moves the pointer to the next or previous memory location of the same data type.
Example:
int arr[3] = {10, 20, 30};
int *ptr = arr;  // Points to the first element of arr

ptr++;  // Moves ptr to the next element
printf("Second element: %d\n", *ptr);  // Output: 20
登入後複製

5. Pointers and Arrays

In C, arrays are closely related to pointers. An array name can be used as a pointer to its first element.

Example:
int arr[3] = {10, 20, 30};
int *ptr = arr;  // Points to the first element of arr

printf("First element: %d\n", *ptr);  // Output: 10
ptr++;  // Move to the next element
printf("Second element: %d\n", *ptr);  // Output: 20
登入後複製

6. Pointers and Functions

Pointers are often used in function arguments to pass variables by reference, allowing functions to modify the original variables.

Example:
#include <stdio.h>

void square(int *ptr) {
    *ptr = (*ptr) * (*ptr);  // Squares the value pointed by ptr
}

int main() {
    int num = 5;
    square(&num);  // Pass num's address to square function
    printf("Squared value: %d\n", num);  // Output: 25

    return 0;
}
登入後複製

7. Null Pointers

A null pointer is a pointer that does not point to any memory location. It's commonly used to indicate that the pointer isn't currently pointing to valid data.

Example:
int *ptr = NULL;  // ptr is a null pointer
登入後複製

8. Void Pointers

Void pointers (void *) are pointers that can point to any data type, but they cannot be directly dereferenced because their type is unspecified.

Example:

#include <stdio.h>

int main() {
    int num = 10;
    float f = 3.14;
    char ch = 'A';

    void *ptr;

    ptr = &num;
    printf("Value at ptr pointing to int: %d\n", *(int *)ptr);

    ptr = &f;
    printf("Value at ptr pointing to float: %.2f\n", *(float *)ptr);

    ptr = &ch;
    printf("Value at ptr pointing to char: %c\n", *(char *)ptr);

    return 0;
}
登入後複製

Output:

Value at ptr pointing to int: 10
Value at ptr pointing to float: 3.14
Value at ptr pointing to char: A
登入後複製

Arrays as Pointers in C

In C, arrays and pointers are closely related concepts. Understanding how arrays decay into pointers and how pointers can be used to manipulate arrays is crucial for effective C programming.

1. Arrays and Pointers Relationship

Arrays in C can decay into pointers. When you use the array name in an expression, it often decays into a pointer to its first element.

Example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // arr decays into a pointer to its first element
登入後複製

Here, ptr points to the first element of the array arr.

2. Accessing Array Elements via Pointers

You can access array elements using pointer arithmetic. This is often more flexible than using array indexing because pointers can be incremented or decremented to traverse through the array.

Example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // Points to the first element of arr

printf("First element: %d\n", *ptr);    // Output: 1
printf("Second element: %d\n", *(ptr + 1));  // Output: 2
登入後複製

3. Pointer Arithmetic with Arrays

Pointer arithmetic allows you to navigate through an array using pointer operations like addition (+), subtraction (-), and dereferencing (*).

Example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // Points to the first element of arr

for (int i = 0; i < 5; i++) {
    printf("Element at index %d: %d\n", i, *(ptr + i));
}
登入後複製

4. Modifying Array Elements via Pointers

You can modify array elements using pointers just like with array indexing.

Example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // Points to the first element of arr

*(ptr + 2) = 10;  // Modify the third element of arr

printf("Modified third element: %d\n", arr[2]);  // Output: 10
登入後複製

5. Passing Arrays to Functions using Pointers

Arrays are commonly passed to functions in C using pointers. This allows functions to modify array elements directly.

Example:
#include <stdio.h>

void printArray(int *arr, int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    printArray(arr, 5);  // Pass array and its size to function using pointer
    return 0;
}
登入後複製

Pointers as Arrays in C

In C, pointers and arrays can often be used interchangeably due to their close relationship. Pointers can simulate array behavior, allowing direct access to memory locations.

1. Initializing Pointers to Arrays

Pointers can be initialized to point to the first element of an array. This allows for accessing array elements using pointer notation.

Example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // ptr points to the first element of arr
登入後複製

2. Accessing Array Elements using Pointers

Once initialized, pointers can be used to access array elements just like array indexing.

Example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // ptr points to the first element of arr

printf("First element: %d\n", ptr[0]);   // Output: 1
printf("Second element: %d\n", ptr[1]);  // Output: 2
登入後複製

3. Pointer Arithmetic with Arrays

Pointers support arithmetic operations that can simulate array indexing. This is useful for iterating through arrays or accessing elements.

Example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // ptr points to the first element of arr

for (int i = 0; i < 5; i++) {
    printf("Element at index %d: %d\n", i, ptr[i]);
}
登入後複製

4. Modifying Array Elements via Pointers

Arrays can be modified using pointers just like with array indexing.

Example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // ptr points to the first element of arr

ptr[2] = 10;  // Modify the third element of arr

printf("Modified third element: %d\n", arr[2]);  // Output: 10
登入後複製

Functions in C

Functions in C are blocks of code that perform a specific task. They provide modularity and reusability in programs by allowing code to be organized into manageable units.

1. Function Declaration and Definition

Syntax:
return_type function_name(parameter1, parameter2, parameter3, ...) {
    // Function body
    // Statements
    return expression;  // Optional return statement
}
登入後複製
  • return_type: Specifies the type of value returned by the function (void if no return value).
  • function_name: Name of the function.
  • parameter_list: List of parameters (inputs) the function accepts.
  • return: Optional statement to return a value to the calling code.
Example:
#include <stdio.h>

// Function declaration
int add(int a, int b);

int main() {
    int result;
    // Function call
    result = add(10, 20);
    printf("Sum: %d\n", result);
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;  // Return sum of a and b
}
登入後複製

2. Function Prototypes

Function prototypes declare the function signature (return type, name, and parameters) before its actual definition. This allows the compiler to verify function calls and enforce type checking.

Example:
#include <stdio.h>

// Function prototype
int add(int, int);

int main() {
    int result;
    result = add(10, 20);  // Function call
    printf("Sum: %d\n", result);
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;  // Return sum of a and b
}
登入後複製

3. Function Parameters

Functions can accept parameters (inputs) that are passed during function calls. Parameters allow functions to operate on different data each time they are called.

Example:
#include <stdio.h>

// Function declaration with parameters
void greet(char name[]);

int main() {
    char userName[] = "John";
    greet(userName);  // Pass userName to function
    return 0;
}

// Function definition
void greet(char name[]) {
    printf("Hello, %s!\n", name);
}
登入後複製

4. Return Statement

Functions can return a value using the return statement. The return type specifies the data type of the value returned by the function. If no return value is needed, the return type is void.

Example:
#include <stdio.h>

// Function declaration with return type int
int square(int num);

int main() {
    int result = square(5);  // Function call
    printf("Square: %d\n", result);
    return 0;
}

// Function definition
int square(int num) {
    return num * num;  // Return the square of num
}
登入後複製

5. Void Functions

Functions with void return type do not return any value. They are used for tasks that do not require a return value, such as printing output or performing operations without returning a result.

Example:
#include <stdio.h>

// Function declaration with void return type
void displayMessage();

int main() {
    displayMessage();  // Function call
    return 0;
}

// Function definition
void displayMessage() {
    printf("Welcome to C Programming!\n");
}
登入後複製

6. Recursive Functions

Recursive functions are functions that call themselves either directly or indirectly. They are useful for solving problems that can be broken down into smaller, similar sub-problems.

Example (Factorial):
#include <stdio.h>

// Function declaration for factorial calculation
int factorial(int n);

int main() {
    int num = 5;
    printf("Factorial of %d = %d\n", num, factorial(num));
    return 0;
}

// Function definition for factorial calculation
int factorial(int n) {
    if (n == 0 || n == 1)
        return 1;
    else
        return n * factorial(n - 1);  // Recursive call
}
登入後複製

7. Function Pointers

Function pointers hold the address of functions. They allow functions to be passed as arguments to other functions or stored in data structures, enabling dynamic function calls.

Example:
#include <stdio.h>

// Function declaration
int add(int a, int b);

int main() {
    int (*ptr)(int, int);  // Declare function pointer
    ptr = add;  // Assign address of add function to ptr

    int result = ptr(10, 20);  // Call add using function pointer
    printf("Sum: %d\n", result);
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;  // Return sum of a and b
}
登入後複製

8. Variable Number of Arguments

Functions in C can accept a variable number of arguments using ellipses (...). This feature is commonly used in functions like printf() and scanf().

Example:
#include <stdio.h>
#include <stdarg.h>

// Function declaration with variable arguments
double average(int num, ...);

int main() {
    double avg1 = average(3, 10, 20, 30);
    double avg2 = average(5, 1, 2, 3, 4, 5);

    printf("Average 1: %.2f\n", avg1);
    printf("Average 2: %.2f\n", avg2);

    return 0;
}

// Function definition with variable arguments
double average(int num, ...) {
    va_list args;
    double sum = 0.0;

    va_start(args, num);  // Initialize args to retrieve additional arguments
    for (int i = 0; i < num; i++) {
        sum += va_arg(args, int);  // Retrieve each argument
    }
    va_end(args);  // Clean up variable argument list

    return sum / num;  // Calculate average
}
登入後複製

9. Passing Function as Argument

In C, functions can be passed as arguments to other functions. This feature allows for flexibility and enables functions to operate on different behaviors based on the function passed.

Example:
#include <stdio.h>

// Function that takes another function as argument
void performOperation(int (*operation)(int, int), int a, int b) {
    int result = operation(a, b);
    printf("Result: %d\n", result);
}

// Functions to be used as arguments
int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int main() {
    // Pass function 'add' as argument
    performOperation(add, 10, 5);

    // Pass function 'subtract' as argument
    performOperation(subtract, 10, 5);

    return 0;
}
登入後複製

Structs in C

In C programming, a struct (structure) is a user-defined data type that allows you to group together data items of different types under a single name. It enables you to create complex data structures to organize and manipulate related pieces of data efficiently.

1. Declaring a Struct

Syntax:
struct struct_name {
    // Member variables (fields)
    data_type member1;
    data_type member2;
    // ... more members
};
登入後複製
  • struct_name: Name of the struct.
  • data_type: Data type of each member variable.
Example:
#include <stdio.h>

// Declare a struct
struct Person {
    char name[50];
    int age;
    float height;
};

int main() {
    // Declare struct variables
    struct Person person1;
    struct Person person2;

    // Accessing and modifying struct members
    strcpy(person1.name, "John");
    person1.age = 30;
    person1.height = 5.8;

    // Displaying struct members
    printf("Person 1: Name=%s, Age=%d, Height=%.2f\n", person1.name, person1.age, person1.height);

    return 0;
}
登入後複製

2. Accessing Struct Members

Struct members are accessed using the dot (.) operator.

Example:
#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p1 = {10, 20};

    // Accessing struct members
    printf("Coordinates: x=%d, y=%d\n", p1.x, p1.y);

    return 0;
}
登入後複製

3. Initializing Structs

Structs can be initialized at the time of declaration or later using assignment.

Example:
#include <stdio.h>

struct Rectangle {
    int length;
    int width;
};

int main() {
    // Initializing struct at declaration
    struct Rectangle rect1 = {10, 5};

    // Initializing struct later
    struct Rectangle rect2;
    rect2.length = 15;
    rect2.width = 8;

    // Accessing and displaying struct members
    printf("Rectangle 1: Length=%d, Width=%d\n", rect1.length, rect1.width);
    printf("Rectangle 2: Length=%d, Width=%d\n", rect2.length, rect2.width);

    return 0;
}
登入後複製

4. Nested Structs

Structs can contain other structs as members, enabling the creation of hierarchical data structures.

Example:
#include <stdio.h>

struct Date {
    int day;
    int month;
    int year;
};

struct Employee {
    char name[50];
    struct Date birthDate;
    float salary;
};

int main() {
    struct Employee emp1 = {"Alice", {15, 7, 1990}, 50000.0};

    // Accessing nested struct members
    printf("Employee: Name=%s, Birth Date=%d/%d/%d, Salary=%.2f\n",
           emp1.name, emp1.birthDate.day, emp1.birthDate.month, emp1.birthDate.year, emp1.salary);

    return 0;
}
登入後複製

5. Typedef for Structs

typedef can be used to create an alias (or shorthand) for structs.

Example:
#include <stdio.h>

typedef struct {
    int hours;
    int minutes;
    int seconds;
} Time;

int main() {
    Time t1 = {10, 30, 45};

    // Accessing typedef struct members
    printf("Time: %d:%d:%d\n", t1.hours, t1.minutes, t1.seconds);

    return 0;
}
登入後複製

6. Passing Structs to Functions

Structs can be passed to functions either by value or by reference (using pointers).

Example:
#include <stdio.h>

struct Student {
    char name[50];
    int rollNumber;
};

void displayStudent(struct Student s) {
    printf("Student: Name=%s, Roll Number=%d\n", s.name, s.rollNumber);
}

int main() {
    struct Student stu1 = {"Emma", 101};

    // Passing struct to function by value
    displayStudent(stu1);

    return 0;
}
登入後複製

7. Pointer to Struct

You can create pointers to structs and access struct members using arrow (->) operator.

Example:
#include <stdio.h>

struct Book {
    char title[100];
    char author[50];
    float price;
};

int main() {
    struct Book book1 = {"C Programming", "Dennis Ritchie", 39.95};
    struct Book *ptrBook;

    // Pointer to struct
    ptrBook = &book1;

    // Accessing struct members using pointer
    printf("Book: Title=%s, Author=%s, Price=%.2f\n",
           ptrBook->title, ptrBook->author, ptrBook->price);

    return 0;
}
登入後複製

8. Size of Struct

The sizeof operator can be used to determine the size of a struct in bytes.

Example:
#include <stdio.h>

struct Car {
    char model[50];
    int year;
    float price;
};

int main() {
    struct Car car1;

    printf("Size of struct Car: %lu bytes\n", sizeof(car1));

    return 0;
}
登入後複製

9. Dynamic Memory Allocation for Structs

You can allocate memory for structs dynamically using malloc, calloc, or realloc functions.

Example:
#include <stdio.h>
#include <stdlib.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point *ptrPoint;

    // Allocate memory dynamically for struct Point
    ptrPoint = (struct Point *)malloc(sizeof(struct Point));

    if (ptrPoint == NULL) {
        printf("Memory allocation failed.\n");
        return 1;
    }

    // Accessing and assigning values to struct members
    ptrPoint->x = 10;
    ptrPoint->y = 20;

    // Displaying values
    printf("Coordinates: x=%d, y=%d\n", ptrPoint->x, ptrPoint->y);

    // Free dynamically allocated memory
    free(ptrPoint);

    return 0;
}
登入後複製

以上是C基礎知識的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!
Operator Name Description Example
& AND Sets each bit to 1 if both bits are 1 x & y
| OR Sets each bit to 1 if one of two bits is 1 x | y
^ XOR Sets each bit to 1 if only one of two bits is 1 x ^ y
~ NOT Inverts all the bits ~x
<< Left Shift Shifts bits to the left x << 2
>> Right Shift Shifts bits to the right x >> 2