C Programming

Input and Output (scanf/printf); Operators in C

C-CAT

Input and Output (scanf/printf)

printf — Formatted Output

#include <stdio.h>

int main() {
    int num = 42;
    float pi = 3.14159;
    char name[] = "Alice";

    // Basic output
    printf("Number: %d\n", num);
    printf("Float: %.2f\n", pi);      // 2 decimal places
    printf("String: %s\n", name);

    // Width and alignment
    printf("%10d\n", num);        //        42 (right-aligned, width 10)
    printf("%-10d|\n", num);      // 42        | (left-aligned)
    printf("%010d\n", num);       // 0000000042 (zero-padded)

    // Multiple values
    printf("%s is %d years old and earns %.2f\n", "Bob", 30, 55000.75);

    // Special characters
    printf("Tab:\there\n");         // Tab character
    printf("Newline:\n");           // New line
    printf("Backslash: \\\n");      // Literal backslash
    printf("Quote: \"\n");          // Literal double quote

    return 0;
}

Format Specifiers Reference

SpecifierTypeDescription
%dintSigned decimal integer
%iintSigned integer (like %d)
%uunsigned intUnsigned decimal
%ffloat/doubleDecimal floating point
%efloat/doubleScientific notation (1.2e+03)
%gfloat/doubleShorter of %f or %e
%ccharSingle character
%schar*String
%pvoid*Pointer address
%xunsigned intHexadecimal (lowercase)
%Xunsigned intHexadecimal (UPPERCASE)
%ounsigned intOctal
%ldlongLong integer
%lldlong longLong long integer
%%—Literal percent sign

scanf — Formatted Input

#include <stdio.h>

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

    // Reading integers
    printf("Enter age: ");
    scanf("%d", &age);           // & = address of operator (required!)

    // Reading floats
    printf("Enter salary: ");
    scanf("%f", &salary);        // use %f for float, %lf for double

    // Reading strings (no spaces)
    printf("Enter name: ");
    scanf("%49s", name);         // name is already a pointer; no & needed; limit to 49 chars

    printf("Name: %s, Age: %d, Salary: %.2f\n", name, age, salary);

    // Reading multiple values at once
    int x, y;
    scanf("%d %d", &x, &y);     // Enter: 3 5

    return 0;
}

Reading Strings with Spaces

// scanf stops at space — can't read full name with spaces
// Use fgets() instead:
char fullname[100];
printf("Enter full name: ");
fgets(fullname, sizeof(fullname), stdin);
// Note: fgets includes the '\n' at end; remove it:
fullname[strcspn(fullname, "\n")] = '\0';
printf("Full name: %s\n", fullname);

Operators in C

Arithmetic Operators

int a = 17, b = 5;

printf("a + b = %d\n", a + b);  // 22
printf("a - b = %d\n", a - b);  // 12
printf("a * b =
%d\n", a * b);  // 85
printf("a / b = %d\n", a / b);  // 3  (integer division; truncates)
printf("a %% b = %d\n", a % b); // 2  (modulus/remainder)

// Float division
float fa = 17.0, fb = 5.0;
printf("fa / fb = %.2f\n", fa / fb);  // 3.40

Relational Operators

int x = 10, y = 20;

printf("%d\n", x == y);   // 0 (false)
printf("%d\n", x != y);   // 1 (true)
printf("%d\n", x < y);    // 1 (true)
printf("%d\n", x > y);    // 0 (false)
printf("%d\n", x <= 10);  // 1 (true)
printf("%d\n", y >= 20);  // 1 (true)

Logical Operators

int a = 1, b = 0;   // 1=true, 0=false

printf("a && b = %d\n", a && b);  // 0 (AND: both must be true)
printf("a || b = %d\n", a ||
b);  // 1 (OR: at least one true)
printf("!a = %d\n", !a);          // 0 (NOT: invert)
printf("!b = %d\n", !b);          // 1

// Short-circuit evaluation
int x = 5;
// if first is false in &&, second NOT evaluated
if (x > 10 && printf("This won't print!")) {}  // Short-circuit

Assignment Operators

int x = 10;

x += 5;   // x = x + 5 = 15
x -= 3;   // x = x - 3 = 12
x *= 2;   // x = x * 2 = 24
x /= 4;
// x = x / 4 = 6
x %= 4;   // x = x % 4 = 2

printf("x = %d\n", x);    // 2

Increment/Decrement Operators

int a = 5;

// Pre-increment: increment THEN use value
int b = ++a;   // a=6, b=6

// Post-increment: use value THEN increment
int c = a++;   // c=6, a now 7

printf("a=%d, b=%d, c=%d\n", a, b, c);  // 7, 6, 6

// In loops (both equivalent):
for (int i = 0; i < 5; i++) {}   // i++ or ++i has same effect here

Conditional (Ternary) Operator

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

int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("Max: %d\n", max);   // 20

Operator Precedence

LevelOperatorsAssociativity
1 (highest)() [] -> .Left-to-right
2! ~ ++ -- (unary) sizeof (type) * &Right-to-left
3* / %Left-to-right
4+ -Left-to-right
5<< >>Left-to-right
6< <= > >=Left-to-right
7== !=Left-to-right
8& (bitwise AND)Left-to-right
9^ (bitwise XOR)Left-to-right
10`` (bitwise OR)
11&&Left-to-right
12`
13?:Right-to-left
14 (lowest)= += -= etc.Right-to-left

Continue learning

Related notes

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.