Bases du C

WBOY
Libérer: 2024-07-17 11:54:40
original
1229 Les gens l'ont consulté

C Basics

C

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
Copier après la connexion

Création de variables

En C, les variables sont utilisées pour stocker des données qui peuvent être manipulées au sein d'un programme. Voici un guide complet sur la création et l'utilisation de variables en C :

Syntaxe pour la déclaration et l'initialisation de variables

Déclaration :

type variable_name;
Copier après la connexion

Exemple :

int number;
char letter;
float salary;
Copier après la connexion

Initialisation :

variable_name = value;
Copier après la connexion

Exemple :

number = 10;
letter = 'A';
salary = 50000.0;
Copier après la connexion

Déclaration et initialisation combinées :

type variable_name = value;
Copier après la connexion

Exemple :

int age = 25;
double pi = 3.14159;
char grade = 'A';
Copier après la connexion

Règles pour les noms de variables

  1. Les noms de variables doivent commencer par une lettre (a-z, A-Z) ou un trait de soulignement (_).
  2. Les caractères suivants peuvent être des lettres, des chiffres (0-9) ou des traits de soulignement.
  3. Les noms de variables sont sensibles à la casse (myVar et myvar sont différents).
  4. Les noms de variables ne peuvent pas être des mots-clés réservés en C (par exemple, int, return, void).

Exemples :

int age;
char _grade;
float salary_2024;
Copier après la connexion

Portée variable

La portée d'une variable est la partie du programme où la variable est accessible. Les variables en C peuvent être :

Variables locales : Déclarées à l'intérieur d'une fonction ou d'un bloc et accessibles uniquement à l'intérieur de cette fonction ou de ce bloc.

void myFunction() {
    int localVar = 5;
    printf("%d", localVar);
}
Copier après la connexion

Variables globales : Déclarées en dehors de toutes les fonctions et accessibles depuis n'importe quelle fonction du programme.

int globalVar = 10;

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

int main() {
    myFunction(); // Outputs: 10
    return 0;
}
Copier après la connexion

Constantes

Les constantes sont des variables dont les valeurs ne peuvent pas être modifiées une fois attribuées. Ils sont déclarés à l'aide du mot clé const.

Exemple :

const int DAYS_IN_WEEK = 7;
const float PI = 3.14159;
Copier après la connexion

Valeurs par défaut

En C, les variables locales non initialisées contiennent des valeurs inutiles, tandis que les variables globales et statiques sont initialisées à zéro par défaut.

Exemple :

#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;
}
Copier après la connexion

Déclarez de nombreuses variables avec ou sans valeurs

Vous pouvez déclarer plusieurs variables du même type sur une seule ligne, en les séparant par des virgules. Vous pouvez également les initialiser soit au moment de la déclaration, soit ultérieurement.

Exemples :

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

// Declaring and initializing some variables
int x = 10, y, z = 30;
Copier après la connexion

Une valeur pour plusieurs variables

Vous pouvez attribuer la même valeur à plusieurs variables en chaînant l'opérateur d'affectation.

Exemple :

int m, n, o;
m = n = o = 50;
Copier après la connexion

Créer des commentaires

Les commentaires en C sont des instructions non exécutables qui sont utilisées pour décrire et expliquer le code. Ils sont essentiels pour rendre le code plus lisible et maintenable. C prend en charge deux types de commentaires :

1. Commentaires sur une seule ligne

Les commentaires sur une seule ligne commencent par deux barres obliques (//). Tout ce qui suit // sur cette ligne est considéré comme un commentaire.

Syntaxe :

// This is a single-line comment
int x = 10; // x is initialized to 10
Copier après la connexion

2. Commentaires sur plusieurs lignes

Les commentaires sur plusieurs lignes commencent par /* et se terminent par */. Tout ce qui se trouve entre /* et */ est considéré comme un commentaire, quel que soit le nombre de lignes qu'il s'étend.

Syntaxe :

/*
 This is a multi-line comment.
 It can span multiple lines.
*/
int y = 20; /* y is initialized to 20 */
Copier après la connexion

Exemple d'utilisation

Exemple de commentaire sur une seule ligne :

#include <stdio.h>

int main() {
    // Print Hello, World!
    printf("Hello, World!\n"); // This prints the string to the console
    return 0;
}
Copier après la connexion

Exemple de commentaire sur plusieurs lignes :

#include <stdio.h>

int main() {
    /*
     This is a simple C program
     that prints Hello, World!
     to the console.
    */
    printf("Hello, World!\n");
    return 0;
}
Copier après la connexion

Ces exemples illustrent comment utiliser les commentaires sur une seule ligne et sur plusieurs lignes en C pour rendre le code plus lisible et maintenable.

Entrée et sortie de base en C

En C, les opérations d'entrée et de sortie sont effectuées à l'aide des fonctions de bibliothèque standard. Les fonctions les plus couramment utilisées pour les entrées et sorties de base sont printf et scanf.

1. Sortie en utilisant printf

La fonction printf est utilisée pour imprimer du texte et des variables sur la console.

Syntaxe :

printf("format string", variable1, variable2, ...);
Copier après la connexion

Spécificateurs de format :

  • %d ou %i - pour les entiers
  • %f - pour les nombres à virgule flottante
  • %lf - pour les nombres à virgule flottante double précision
  • %c - pour les personnages
  • %s - pour les chaînes

Exemple :

#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;
}
Copier après la connexion

2. Saisie à l'aide de scanf

La fonction scanf est utilisée pour lire les entrées formatées depuis la console.

Syntaxe :

scanf("format string", &variable1, &variable2, ...);
Copier après la connexion

Exemple :

#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;
}
Copier après la connexion

Points importants

  • L'opérateur & est utilisé dans scanf pour transmettre l'adresse de la variable où l'entrée sera stockée.
  • Lors de la lecture de caractères avec scanf, il est important de gérer le caractère de nouvelle ligne laissé dans le tampon d'entrée.
  • scanf lit les chaînes jusqu'au premier caractère d'espacement. Pour lire une ligne de texte, des fonctions comme fgets peuvent être utilisées.

Lecture d'une ligne de texte avec fgets :

Syntaxe :

fgets(buffer, size, stdin);
Copier après la connexion

Exemple :

#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;
}
Copier après la connexion

Utilisation de getchar et putchar pour l'entrée et la sortie de caractères

getchar Exemple :

#include <stdio.h>

int main() {
    char ch;

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

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

    return 0;
}
Copier après la connexion

putchar Example:

#include <stdio.h>

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

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

    return 0;
}
Copier après la connexion

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;
Copier après la connexion
  • short: Short integer type, typically 2 bytes.

Example:

  short numShort = 1000;
Copier après la connexion
  • long: Long integer type, varies by system (commonly 4 or 8 bytes).

Example:

  long numLong = 10000000000L;
Copier après la connexion
  • long long: Long long integer type, typically 8 bytes (C99 and later).

Example:

  long long bigNumber = 123456789012345LL;
Copier après la connexion
Floating-Point Types
  • float: Single-precision floating-point, typically 4 bytes.

Example:

  float numFloat = 3.14f;
Copier après la connexion
  • double: Double-precision floating-point, typically 8 bytes.

Example:

  double numDouble = 3.14159;
Copier après la connexion
  • long double: Extended precision floating-point, varies by system.

Example:

  long double extendedPi = 3.14159265358979323846L;
Copier après la connexion
Character Type
  • char: Character type, typically 1 byte. Stores ASCII values (0 to 127) or UTF-8 characters.

Example:

  char letter = 'A';
Copier après la connexion
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;
Copier après la connexion

2. Derived Data Types

Array
  • Syntax: type array_name[size];

Example:

  int numbers[5] = {1, 2, 3, 4, 5};
Copier après la connexion
Pointer
  • Syntax: type *pointer_name;

Example:

  int *ptr;
Copier après la connexion
Structure
  • Syntax:
  struct structure_name {
      type member1;
      type member2;
      // ...
  };
Copier après la connexion

Example:

  struct Person {
      char name[50];
      int age;
      float salary;
  };
Copier après la connexion

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;
}
Copier après la connexion
  • 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;
}
Copier après la connexion

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;
}
Copier après la connexion

Output:

num is true
salary is false
Copier après la connexion

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;
}
Copier après la connexion

Output:

ptr is NULL
Copier après la connexion

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;
}
Copier après la connexion

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
Copier après la connexion

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;
}
Copier après la connexion

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];
Copier après la connexion
  • 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
Copier après la connexion

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;
Copier après la connexion

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
Copier après la connexion

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
Copier après la connexion

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
Copier après la connexion

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;
}
Copier après la connexion

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];
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
  }
Copier après la connexion
  • 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;
  }
Copier après la connexion
  • 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;
  }
Copier après la connexion
  • 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;
  }
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

& 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;
}
Copier après la connexion

* 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;
}
Copier après la connexion

-> 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;
}
Copier après la connexion

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
}
Copier après la connexion
  • 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;
}
Copier après la connexion

Output:

10 is positive.
Copier après la connexion

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;
}
Copier après la connexion

Output:

0 is zero.
Copier après la connexion

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;
}
Copier après la connexion

Output:

10 is positive and even.
Copier après la connexion

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;
}
Copier après la connexion

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
}
Copier après la connexion
Example:
#include <stdio.h>

int main() {
    int count = 1;

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

    return 0;
}
Copier après la connexion

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Copier après la connexion

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
}
Copier après la connexion
Example:
#include <stdio.h>

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

    return 0;
}
Copier après la connexion

Output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Copier après la connexion

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);
Copier après la connexion
Example:
#include <stdio.h>

int main() {
    int num = 1;

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

    return 0;
}
Copier après la connexion

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Copier après la connexion

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;
}
Copier après la connexion

Output:

Iteration: 1
Iteration: 2
Copier après la connexion

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;
}
Copier après la connexion

Output:

Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
Copier après la connexion

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;
Copier après la connexion
  • type: Data type of the variable the pointer points to.
  • *: Indicates that ptr is a pointer.
Example:
int *ptr;  // Pointer to an integer
Copier après la connexion

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
Copier après la connexion

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
Copier après la connexion

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
Copier après la connexion

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
Copier après la connexion

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;
}
Copier après la connexion

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
Copier après la connexion

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;
}
Copier après la connexion

Output:

Value at ptr pointing to int: 10
Value at ptr pointing to float: 3.14
Value at ptr pointing to char: A
Copier après la connexion

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
Copier après la connexion

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
Copier après la connexion

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));
}
Copier après la connexion

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
Copier après la connexion

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;
}
Copier après la connexion

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
Copier après la connexion

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
Copier après la connexion

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]);
}
Copier après la connexion

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
Copier après la connexion

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
}
Copier après la connexion
  • 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
}
Copier après la connexion

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
}
Copier après la connexion

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);
}
Copier après la connexion

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
}
Copier après la connexion

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");
}
Copier après la connexion

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
}
Copier après la connexion

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
}
Copier après la connexion

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
}
Copier après la connexion

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;
}
Copier après la connexion

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
};
Copier après la connexion
  • 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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

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;
}
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:dev.to
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
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