DEV Community

Cover image for C Basics
Harsh Mishra
Harsh Mishra

Posted on

C Basics

C

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Creating Variables

In C, variables are used to store data that can be manipulated within a program. Here's a comprehensive guide on creating and working with variables in C:

Syntax for Variable Declaration and Initialization

Declaration:

type variable_name;
Enter fullscreen mode Exit fullscreen mode

Example:

int number;
char letter;
float salary;
Enter fullscreen mode Exit fullscreen mode

Initialization:

variable_name = value;
Enter fullscreen mode Exit fullscreen mode

Example:

number = 10;
letter = 'A';
salary = 50000.0;
Enter fullscreen mode Exit fullscreen mode

Declaration and Initialization Combined:

type variable_name = value;
Enter fullscreen mode Exit fullscreen mode

Example:

int age = 25;
double pi = 3.14159;
char grade = 'A';
Enter fullscreen mode Exit fullscreen mode

Rules for Variable Names

  1. Variable names must start with a letter (a-z, A-Z) or an underscore (_).
  2. Subsequent characters can be letters, digits (0-9), or underscores.
  3. Variable names are case-sensitive (myVar and myvar are different).
  4. Variable names cannot be C reserved keywords (e.g., int, return, void).

Examples:

int age;
char _grade;
float salary_2024;
Enter fullscreen mode Exit fullscreen mode

Variable Scope

The scope of a variable is the part of the program where the variable is accessible. Variables in C can be:

Local Variables: Declared inside a function or a block and accessible only within that function or block.

void myFunction() {
    int localVar = 5;
    printf("%d", localVar);
}
Enter fullscreen mode Exit fullscreen mode

Global Variables: Declared outside of all functions and accessible from any function within the program.

int globalVar = 10;

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

int main() {
    myFunction(); // Outputs: 10
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Constants

Constants are variables whose values cannot be changed once assigned. They are declared using the const keyword.

Example:

const int DAYS_IN_WEEK = 7;
const float PI = 3.14159;
Enter fullscreen mode Exit fullscreen mode

Default Values

In C, uninitialized local variables contain garbage values, while global and static variables are initialized to zero by default.

Example:

#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;
}
Enter fullscreen mode Exit fullscreen mode

Declare Many Variables with or without Values

You can declare multiple variables of the same type in a single line, separating them with commas. You can also initialize them either at the time of declaration or later.

Examples:

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

// Declaring and initializing some variables
int x = 10, y, z = 30;
Enter fullscreen mode Exit fullscreen mode

One Value to Multiple Variables

You can assign the same value to multiple variables by chaining the assignment operator.

Example:

int m, n, o;
m = n = o = 50;
Enter fullscreen mode Exit fullscreen mode

Creating Comments

Comments in C are non-executable statements that are used to describe and explain the code. They are essential for making the code more readable and maintainable. C supports two types of comments:

1. Single-Line Comments

Single-line comments start with two forward slashes (//). Everything following // on that line is considered a comment.

Syntax:

// This is a single-line comment
int x = 10; // x is initialized to 10
Enter fullscreen mode Exit fullscreen mode

2. Multi-Line Comments

Multi-line comments start with /* and end with */. Everything between /* and */ is considered a comment, regardless of how many lines it spans.

Syntax:

/*
 This is a multi-line comment.
 It can span multiple lines.
*/
int y = 20; /* y is initialized to 20 */
Enter fullscreen mode Exit fullscreen mode

Example Usage

Single-Line Comment Example:

#include <stdio.h>

int main() {
    // Print Hello, World!
    printf("Hello, World!\n"); // This prints the string to the console
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Multi-Line Comment Example:

#include <stdio.h>

int main() {
    /*
     This is a simple C program
     that prints Hello, World!
     to the console.
    */
    printf("Hello, World!\n");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

These examples illustrate how to use single-line and multi-line comments in C to make the code more readable and maintainable.

Basic Input and Output in C

In C, input and output operations are performed using standard library functions. The most commonly used functions for basic input and output are printf and scanf.

1. Output using printf

The printf function is used to print text and variables to the console.

Syntax:

printf("format string", variable1, variable2, ...);
Enter fullscreen mode Exit fullscreen mode

Format Specifiers:

  • %d or %i - for integers
  • %f - for floating-point numbers
  • %lf - for double-precision floating-point numbers
  • %c - for characters
  • %s - for strings

Example:

#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;
}
Enter fullscreen mode Exit fullscreen mode

2. Input using scanf

The scanf function is used to read formatted input from the console.

Syntax:

scanf("format string", &variable1, &variable2, ...);
Enter fullscreen mode Exit fullscreen mode

Example:

#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;
}
Enter fullscreen mode Exit fullscreen mode

Important Points

  • The & operator is used in scanf to pass the address of the variable where the input will be stored.
  • When reading characters with scanf, it's important to handle the newline character left in the input buffer.
  • scanf reads strings up to the first whitespace character. For reading a line of text, functions like fgets can be used.

Reading a Line of Text with fgets:

Syntax:

fgets(buffer, size, stdin);
Enter fullscreen mode Exit fullscreen mode

Example:

#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;
}
Enter fullscreen mode Exit fullscreen mode

Using getchar and putchar for Character Input and Output

getchar Example:

#include <stdio.h>

int main() {
    char ch;

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

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

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

putchar Example:

#include <stdio.h>

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

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

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode
  • short: Short integer type, typically 2 bytes.

Example:

  short numShort = 1000;
Enter fullscreen mode Exit fullscreen mode
  • long: Long integer type, varies by system (commonly 4 or 8 bytes).

Example:

  long numLong = 10000000000L;
Enter fullscreen mode Exit fullscreen mode
  • long long: Long long integer type, typically 8 bytes (C99 and later).

Example:

  long long bigNumber = 123456789012345LL;
Enter fullscreen mode Exit fullscreen mode
Floating-Point Types
  • float: Single-precision floating-point, typically 4 bytes.

Example:

  float numFloat = 3.14f;
Enter fullscreen mode Exit fullscreen mode
  • double: Double-precision floating-point, typically 8 bytes.

Example:

  double numDouble = 3.14159;
Enter fullscreen mode Exit fullscreen mode
  • long double: Extended precision floating-point, varies by system.

Example:

  long double extendedPi = 3.14159265358979323846L;
Enter fullscreen mode Exit fullscreen mode
Character Type
  • char: Character type, typically 1 byte. Stores ASCII values (0 to 127) or UTF-8 characters.

Example:

  char letter = 'A';
Enter fullscreen mode Exit fullscreen mode
Boolean Type
  • _Bool or bool: Represents true or false values (0 or 1).
    • To use boolean type, include <stdbool.h> header file.

Example:

  #include <stdbool.h>
  bool isValid = true;
Enter fullscreen mode Exit fullscreen mode

2. Derived Data Types

Array
  • Syntax: type array_name[size];

Example:

  int numbers[5] = {1, 2, 3, 4, 5};
Enter fullscreen mode Exit fullscreen mode
Pointer
  • Syntax: type *pointer_name;

Example:

  int *ptr;
Enter fullscreen mode Exit fullscreen mode
Structure
  • Syntax:
  struct structure_name {
      type member1;
      type member2;
      // ...
  };
Enter fullscreen mode Exit fullscreen mode

Example:

  struct Person {
      char name[50];
      int age;
      float salary;
  };
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode
  • 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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Output:

num is true
salary is false
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Output:

ptr is NULL
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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];
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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];
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

2. String Input with scanf()

To input a string with spaces in C, fgets() from <stdio.h> 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;
}
Enter fullscreen mode Exit fullscreen mode

3. String Functions

C provides several library functions for manipulating strings, declared in <string.h>.

  • 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;
  }
Enter fullscreen mode Exit fullscreen mode
  • 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;
  }
Enter fullscreen mode Exit fullscreen mode
  • 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;
  }
Enter fullscreen mode Exit fullscreen mode
  • 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;
  }
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Bitwise Operators

Bitwise operators perform operations on bits of integers.

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
#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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

& 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;
}
Enter fullscreen mode Exit fullscreen mode

* 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;
}
Enter fullscreen mode Exit fullscreen mode

-> 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;
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode
  • 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;
}
Enter fullscreen mode Exit fullscreen mode

Output:

10 is positive.
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Output:

0 is zero.
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Output:

10 is positive and even.
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode
Example:
#include <stdio.h>

int main() {
    int count = 1;

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

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode
Example:
#include <stdio.h>

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

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
Example:
#include <stdio.h>

int main() {
    int num = 1;

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

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Iteration: 1
Iteration: 2
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Value at ptr pointing to int: 10
Value at ptr pointing to float: 3.14
Value at ptr pointing to char: A
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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));
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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]);
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode
  • 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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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");
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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
};
Enter fullscreen mode Exit fullscreen mode
  • 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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)