Core Concepts and Syntax in C Programming

The main Function and Basic Output

Every C program must contain a main function, which serves as the entry point. A minimal version looks like this:

int main(void) {
    return 0;
}

The void keyword explicitly indicates the function expects no arguments. To display text, use the printf library function from <stdio.h>:

#include <stdio.h>

int main(void) {
    printf("hello world\n");
    return 0;
}

Source editors often support toggling comments with Ctrl+K, C (comment) and Ctrl+K, U (uncomment).

Fundamental Data Types and Memory Size

C offfers several primitive types to represent different kinds of data:

char        // single character
short       // short integer
int         // integer
long        // long integer
long long   // extended long integer
float       // single-precision floating-point
double      // double-precision floating-point

The sizeof operator returns the amount of memory (in bytes) occupied by a type or variable. Use %zu as the format specifier for size_t values when printing:

#include <stdio.h>

int main(void) {
    printf("%zu\n", sizeof(char));       // 1
    printf("%zu\n", sizeof(short));      // 2
    printf("%zu\n", sizeof(int));        // 4
    printf("%zu\n", sizeof(long));       // 4 (platform-dependent)
    printf("%zu\n", sizeof(long long));  // 8
    printf("%zu\n", sizeof(float));      // 4
    printf("%zu\n", sizeof(double));     // 8
    return 0;
}

Memory units build from bits upward: 8 bits form 1 byte, followed by KB, MB, GB, TB, and PB. The standard mandates sizeof(long) >= sizeof(int).

A type's primary role is to request an appropriate block of memory when declaring a variable:

int main(void) {
    int age = 25;
    double temperature = 36.6;
    return 0;
}

Variables and Constants

Declaring Variables

Variable declaration follows the pattern type identifier = initializer;:

int main(void) {
    short distance = 120;
    int count = 300;
    float price = 19.99;
    return 0;
}

Variables are categorized by location: local (inside a block {}) and global (outside any function).

int total = 100;            // global scope

int main(void) {
    int total = 5;          // local scope shadows global
    printf("total = %d\n", total);   // prints 5
    return 0;
}

When a local and global variable share the same name, the local one takes precedence inside its block.

Reading and Printing Values

scanf reads formatted input, and printf writes formatted output. To avoid security warnings in some environments, define _CRT_SECURE_NO_WARNINGS before including headers, or use a project setting.

Example – summing two integers:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
    int a = 0, b = 0;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    int result = a + b;
    printf("Sum: %d\n", result);
    return 0;
}

Scope and Lifetime

  • Local variables: scope is limited to the enclosing block; lifetime begins upon block entry and ends upon block exit. Accessing them outside their block causes an error.
int main(void) {
    {
        int x = 42;
        printf("%d\n", x);   // valid
    }
    // printf("%d\n", x);    // invalid – x is out of scope
    return 0;
}
  • Global variables: scope spans the entire source file (and possibly other files if declared with extern). Their lifetime is the duration of the program execution.
int count = 0;

void increment(void) {
    count++;
}

int main(void) {
    increment();
    printf("count = %d\n", count);   // valid
    return 0;
}

Different Forms of Constants

  1. Literal constents – hard‑coded values:
42;
3.14;
'X';
"hello";
  1. const‑qualified variables – values that cannot be modified after initialization, yet they remain variables in C:
int main(void) {
    const int limit = 100;
    // limit = 200;   // error: assignment of read-only variable
    return 0;
}
  1. #define symbolic constants – textual substitution performed by the preprocessor:
#define THRESHOLD 100
#define GREETING "Hello, World!"

int main(void) {
    int val = THRESHOLD;
    printf("%d\n", val);
    printf("%s\n", GREETING);
    return 0;
}
  1. Enumeration constants – named integer values grouped together:
enum Color { RED, GREEN, BLUE };
enum Gender { MALE, FEMALE, NONE };

int main(void) {
    enum Color selected = RED;
    // RED = 10;   // error: RED is a constant
    return 0;
}

Strings and the Null Terminator

C has no built‑in string type. Strings are represented as arrays of char terminated by the null character '\0'. The strlen function from <string.h> returns the number of characters before the terminator.

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

int main(void) {
    char str1[] = "abc";                  // automatically includes '\0'
    char str2[] = {'a', 'b', 'c', '\0'};   // explicit terminator
    char str3[] = {'a', 'b', 'c'};          // missing '\0' — undefined behavior

    printf("%s\n", str1);          // abc
    printf("%zu\n", strlen(str1)); // 3

    printf("%zu\n", strlen(str3)); // unpredictable value
    return 0;
}

Escape Sequences

Escape sequences represent special characters within string literals. Common ones include \n (newline), \t (tab), \\ (backslash), \" (double quote), and \0 (null character). They allow embedding characters that would otherwise be impossible to write directly.

Control Structures: Selection and Iteration

Branching with if-else

#include <stdio.h>

int main(void) {
    int answer;
    printf("Study hard? (1/0): ");
    scanf("%d", &answer);

    if (answer == 1) {
        printf("Success awaits.\n");
    } else {
        printf("Consider another path.\n");
    }
    return 0;
}

Looping with while

#include <stdio.h>

int main(void) {
    int lines = 0;
    int threshold = 200;

    while (lines < threshold) {
        printf("Writing code line %d\n", lines + 1);
        lines++;
    }

    printf("Goal achieved!\n");
    return 0;
}

Writing and Using Functions

Functions encapsulate logic and promote reuse. The following example defines a function that adds two numbers:

#include <stdio.h>

int add(int x, int y) {
    return x + y;
}

int main(void) {
    int a = 0, b = 0;
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    int sum = add(a, b);
    printf("Result: %d\n", sum);
    return 0;
}

C follows a structured paradigm composed of sequence, selection, and iteration – building blocks for all programs.

Tags: c programming language fundamentals Data Types Variables functions

Posted on Sun, 23 Aug 2026 16:14:30 +0000 by andyg2