C Asas

WBOY
Lepaskan: 2024-07-17 11:54:40
asal
1223 orang telah melayarinya

C Basics

C

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
Salin selepas log masuk

Mencipta Pembolehubah

Dalam C, pembolehubah digunakan untuk menyimpan data yang boleh dimanipulasi dalam atur cara. Berikut ialah panduan komprehensif untuk mencipta dan bekerja dengan pembolehubah dalam C:

Sintaks untuk Pengisytiharan Pembolehubah dan Permulaan

Pengisytiharan:

type variable_name;
Salin selepas log masuk

Contoh:

int number;
char letter;
float salary;
Salin selepas log masuk

Permulaan:

variable_name = value;
Salin selepas log masuk

Contoh:

number = 10;
letter = 'A';
salary = 50000.0;
Salin selepas log masuk

Pengisytiharan dan Permulaan Gabungan:

type variable_name = value;
Salin selepas log masuk

Contoh:

int age = 25;
double pi = 3.14159;
char grade = 'A';
Salin selepas log masuk

Peraturan untuk Nama Pembolehubah

  1. Nama pembolehubah mesti bermula dengan huruf (a-z, A-Z) atau garis bawah (_).
  2. Aksara seterusnya boleh berupa huruf, digit (0-9) atau garis bawah.
  3. Nama pembolehubah adalah sensitif huruf besar (myVar dan myvar berbeza).
  4. Nama pembolehubah tidak boleh menjadi kata kunci simpanan C (cth., int, return, void).

Contoh:

int age;
char _grade;
float salary_2024;
Salin selepas log masuk

Skop Pembolehubah

Skop pembolehubah ialah bahagian program di mana pembolehubah itu boleh diakses. Pembolehubah dalam C boleh menjadi:

Pembolehubah Setempat: Diisytiharkan di dalam fungsi atau blok dan boleh diakses hanya dalam fungsi atau blok itu.

void myFunction() {
    int localVar = 5;
    printf("%d", localVar);
}
Salin selepas log masuk

Pembolehubah Global: Diisytiharkan di luar semua fungsi dan boleh diakses daripada mana-mana fungsi dalam program.

int globalVar = 10;

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

int main() {
    myFunction(); // Outputs: 10
    return 0;
}
Salin selepas log masuk

Pemalar

Pemalar ialah pembolehubah yang nilainya tidak boleh diubah setelah ditetapkan. Mereka diisytiharkan menggunakan kata kunci const.

Contoh:

const int DAYS_IN_WEEK = 7;
const float PI = 3.14159;
Salin selepas log masuk

Nilai Lalai

Dalam C, pembolehubah tempatan yang tidak dimulakan mengandungi nilai sampah, manakala pembolehubah global dan statik dimulakan kepada sifar secara lalai.

Contoh:

#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;
}
Salin selepas log masuk

Isytiharkan Banyak Pembolehubah dengan atau tanpa Nilai

Anda boleh mengisytiharkan berbilang pembolehubah daripada jenis yang sama dalam satu baris, memisahkannya dengan koma. Anda juga boleh memulakannya sama ada pada masa pengisytiharan atau kemudian.

Contoh:

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

// Declaring and initializing some variables
int x = 10, y, z = 30;
Salin selepas log masuk

Satu Nilai kepada Berbilang Pembolehubah

Anda boleh menetapkan nilai yang sama kepada berbilang pembolehubah dengan merantai operator tugasan.

Contoh:

int m, n, o;
m = n = o = 50;
Salin selepas log masuk

Mencipta Komen

Komen dalam C ialah pernyataan tidak boleh laksana yang digunakan untuk menerangkan dan menerangkan kod. Ia penting untuk menjadikan kod lebih mudah dibaca dan diselenggara. C menyokong dua jenis ulasan:

1. Komen Satu Baris

Ulasan baris tunggal bermula dengan dua garis miring ke hadapan (//). Semua yang mengikuti // pada baris itu dianggap sebagai ulasan.

Sintaks:

// This is a single-line comment
int x = 10; // x is initialized to 10
Salin selepas log masuk

2. Komen Berbilang Baris

Komen berbilang baris bermula dengan /* dan berakhir dengan */. Segala-galanya di antara /* dan */ dianggap sebagai ulasan, tidak kira berapa banyak barisnya.

Sintaks:

/*
 This is a multi-line comment.
 It can span multiple lines.
*/
int y = 20; /* y is initialized to 20 */
Salin selepas log masuk

Contoh Penggunaan

Contoh Komen Sebaris:

#include <stdio.h>

int main() {
    // Print Hello, World!
    printf("Hello, World!\n"); // This prints the string to the console
    return 0;
}
Salin selepas log masuk

Contoh Ulasan Berbilang Baris:

#include <stdio.h>

int main() {
    /*
     This is a simple C program
     that prints Hello, World!
     to the console.
    */
    printf("Hello, World!\n");
    return 0;
}
Salin selepas log masuk

Contoh ini menggambarkan cara menggunakan komen satu baris dan berbilang baris dalam C untuk menjadikan kod lebih mudah dibaca dan diselenggara.

Input dan Output Asas dalam C

Dalam C, operasi input dan output dilakukan menggunakan fungsi perpustakaan standard. Fungsi yang paling biasa digunakan untuk input dan output asas ialah printf dan scanf.

1. Output menggunakan printf

Fungsi printf digunakan untuk mencetak teks dan pembolehubah ke konsol.

Sintaks:

printf("format string", variable1, variable2, ...);
Salin selepas log masuk

Penentukan Format:

  • %d atau %i - untuk integer
  • %f - untuk nombor titik terapung
  • %lf - untuk nombor titik terapung berketepatan dua
  • %c - untuk aksara
  • %s - untuk rentetan

Contoh:

#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;
}
Salin selepas log masuk

2. Input menggunakan scanf

Fungsi scanf digunakan untuk membaca input berformat daripada konsol.

Sintaks:

scanf("format string", &variable1, &variable2, ...);
Salin selepas log masuk

Contoh:

#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;
}
Salin selepas log masuk

Perkara Penting

  • Operator & digunakan dalam scanf untuk menghantar alamat pembolehubah tempat input akan disimpan.
  • Apabila membaca aksara dengan scanf, adalah penting untuk mengendalikan aksara baris baharu yang ditinggalkan dalam penimbal input.
  • scanf membaca rentetan sehingga aksara ruang putih pertama. Untuk membaca baris teks, fungsi seperti fgets boleh digunakan.

Membaca Baris Teks dengan fgets:

Sintaks:

fgets(buffer, size, stdin);
Salin selepas log masuk

Contoh:

#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;
}
Salin selepas log masuk

Menggunakan getchar dan putchar untuk Input dan Output Aksara

Contoh getchar:

#include <stdio.h>

int main() {
    char ch;

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

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

    return 0;
}
Salin selepas log masuk

putchar Example:

#include <stdio.h>

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

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

    return 0;
}
Salin selepas log masuk

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;
Salin selepas log masuk
  • short: Short integer type, typically 2 bytes.

Example:

  short numShort = 1000;
Salin selepas log masuk
  • long: Long integer type, varies by system (commonly 4 or 8 bytes).

Example:

  long numLong = 10000000000L;
Salin selepas log masuk
  • long long: Long long integer type, typically 8 bytes (C99 and later).

Example:

  long long bigNumber = 123456789012345LL;
Salin selepas log masuk
Floating-Point Types
  • float: Single-precision floating-point, typically 4 bytes.

Example:

  float numFloat = 3.14f;
Salin selepas log masuk
  • double: Double-precision floating-point, typically 8 bytes.

Example:

  double numDouble = 3.14159;
Salin selepas log masuk
  • long double: Extended precision floating-point, varies by system.

Example:

  long double extendedPi = 3.14159265358979323846L;
Salin selepas log masuk
Character Type
  • char: Character type, typically 1 byte. Stores ASCII values (0 to 127) or UTF-8 characters.

Example:

  char letter = 'A';
Salin selepas log masuk
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;
Salin selepas log masuk

2. Derived Data Types

Array
  • Syntax: type array_name[size];

Example:

  int numbers[5] = {1, 2, 3, 4, 5};
Salin selepas log masuk
Pointer
  • Syntax: type *pointer_name;

Example:

  int *ptr;
Salin selepas log masuk
Structure
  • Syntax:
  struct structure_name {
      type member1;
      type member2;
      // ...
  };
Salin selepas log masuk

Example:

  struct Person {
      char name[50];
      int age;
      float salary;
  };
Salin selepas log masuk

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;
}
Salin selepas log masuk
  • 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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

Output:

num is true
salary is false
Salin selepas log masuk

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;
}
Salin selepas log masuk

Output:

ptr is NULL
Salin selepas log masuk

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;
}
Salin selepas log masuk

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
Salin selepas log masuk

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;
}
Salin selepas log masuk

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];
Salin selepas log masuk
  • 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
Salin selepas log masuk

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;
Salin selepas log masuk

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
Salin selepas log masuk

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
Salin selepas log masuk

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
Salin selepas log masuk

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;
}
Salin selepas log masuk

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];
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
  }
Salin selepas log masuk
  • 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;
  }
Salin selepas log masuk
  • 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;
  }
Salin selepas log masuk
  • 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;
  }
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

& 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;
}
Salin selepas log masuk

* 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;
}
Salin selepas log masuk

-> 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;
}
Salin selepas log masuk

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
}
Salin selepas log masuk
  • 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;
}
Salin selepas log masuk

Output:

10 is positive.
Salin selepas log masuk

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;
}
Salin selepas log masuk

Output:

0 is zero.
Salin selepas log masuk

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;
}
Salin selepas log masuk

Output:

10 is positive and even.
Salin selepas log masuk

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;
}
Salin selepas log masuk

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
}
Salin selepas log masuk
Example:
#include <stdio.h>

int main() {
    int count = 1;

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

    return 0;
}
Salin selepas log masuk

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Salin selepas log masuk

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
}
Salin selepas log masuk
Example:
#include <stdio.h>

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

    return 0;
}
Salin selepas log masuk

Output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Salin selepas log masuk

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);
Salin selepas log masuk
Example:
#include <stdio.h>

int main() {
    int num = 1;

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

    return 0;
}
Salin selepas log masuk

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Salin selepas log masuk

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;
}
Salin selepas log masuk

Output:

Iteration: 1
Iteration: 2
Salin selepas log masuk

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;
}
Salin selepas log masuk

Output:

Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
Salin selepas log masuk

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;
Salin selepas log masuk
  • type: Data type of the variable the pointer points to.
  • *: Indicates that ptr is a pointer.
Example:
int *ptr;  // Pointer to an integer
Salin selepas log masuk

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
Salin selepas log masuk

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
Salin selepas log masuk

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
Salin selepas log masuk

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
Salin selepas log masuk

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;
}
Salin selepas log masuk

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
Salin selepas log masuk

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;
}
Salin selepas log masuk

Output:

Value at ptr pointing to int: 10
Value at ptr pointing to float: 3.14
Value at ptr pointing to char: A
Salin selepas log masuk

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
Salin selepas log masuk

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
Salin selepas log masuk

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));
}
Salin selepas log masuk

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
Salin selepas log masuk

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;
}
Salin selepas log masuk

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
Salin selepas log masuk

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
Salin selepas log masuk

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]);
}
Salin selepas log masuk

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
Salin selepas log masuk

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
}
Salin selepas log masuk
  • 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
}
Salin selepas log masuk

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
}
Salin selepas log masuk

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);
}
Salin selepas log masuk

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
}
Salin selepas log masuk

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");
}
Salin selepas log masuk

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
}
Salin selepas log masuk

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
}
Salin selepas log masuk

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
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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
};
Salin selepas log masuk
  • 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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

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;
}
Salin selepas log masuk

Atas ialah kandungan terperinci C Asas. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
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