C Programming Syntax Examples

C++ Programming Syntax Examples

This document demonstrates basic C programming syntax with examples.

This document demonstrates basic C++ programming syntax with examples.

#include <stdio.h>

int main()
{
    // Your code goes here
    return 0;
}
#include <iostream>
using namespace std;

int main()
{
    // Your code goes here
    return 0;
}

Output Statements

Using cout

// Print "Hello, World!" and append a newline character at the end of the output
cout << "Hello, World!" << endl;

Using printf

// Print "Hello, World!" and append a newline character at the end of the output
printf("Hello, World!\n");

This prints “Hello, World!” followed by a newline.

Using puts

// Print "Hello, World!" and append a newline character at the end of the output
puts("Hello, World!");

puts automatically appends a newline after printing the string.

Formatted Output

// formatted output using printf
printf("Hello, %s! You have %.2f new messages.\n", "Alice", 5.6789);

Uses format specifiers:

  • %s for strings
  • %.2f for floats with 2 decimal places
// formatted output using cout and iomanip
#include <iomanip> // required for manipulators like setprecision
// ...
cout << "Hello, " << "Alice" << "! You have " << fixed << setprecision(2) << 5.6789 << " new messages.\n";

Uses I/O manipulators:

  • fixed to ensure decimal format
  • setprecision(2) for floats with 2 decimal places

Variables

Declaring and Initializing Variables

// declaring and initializing variables
int age = 25;
float height = 5.9;
char initial = 'A';
char initial2 = 66; // ASCII value for 'B'
printf("Age: %d, Height: %.1f, Initials: %c %c\n", age, height, initial, initial2);
// declaring and initializing variables
int age = 25;
float height = 5.9;
char initial = 'A';
char initial2 = 66; // ASCII value for 'B'
cout << "Age: " << age << ", Height: " << height << ", Initials: " << initial << " " << initial2 << endl;

Declares and initializes variables of different types: int, float, char.

Constants

// declaring a constant variable
const float PI = 3.14;
float radius = 2.0;
float area = PI * radius * radius;
printf("Area of circle with radius %.2f is %.2f\n", radius, area);
// declaring a constant variable
const float PI = 3.14;
float radius = 2.0;
float area = PI * radius * radius;
cout << "Area of circle with radius " << radius << " is " << area << endl;

Uses const to declare a constant value for PI and calculates the area of a circle.

Input Statements

Using cin

// input statement using cin
int userAge;
cout << "Enter your age: ";
cin >> userAge;
cout << "Your age is: " << userAge << endl;

Reads an integer input from the user using cin.

Using scanf

// input statement using scanf
int userAge;
printf("Enter your age: ");
scanf("%d", &userAge);
printf("Your age is: %d\n", userAge);

Reads an integer input from the user using scanf.

Arithmetic Operations

// arithmetic operations
int a = 10, b = 3;
printf("Addition: %d + %d = %d\n", a, b, a + b);
printf("Subtraction: %d - %d = %d\n", a, b, a - b);
printf("Multiplication: %d * %d = %d\n", a, b, a * b);
printf("Division: %d / %d = %d\n", a, b, a / b);
printf("Modulus: %d %% %d = %d\n", a, b, a % b);
a += 5; // equivalent to a = a + 5
printf("After a += 5, a = %d\n", a);
b *= 2; // equivalent to b = b * 2
printf("After b *= 2, b = %d\n", b);
// arithmetic operations
int a = 10, b = 3;
cout << "Addition: " << a << " + " << b << " = " << a + b << endl;
cout << "Subtraction: " << a << " - " << b << " = " << a - b << endl;
cout << "Multiplication: " << a << " * " << b << " = " << a * b << endl;
cout << "Division: " << a << " / " << b << " = " << a / b << endl;
cout << "Modulus: " << a << " % " << b << " = " << a % b << endl;
a += 5; // equivalent to a = a + 5
cout << "After a += 5, a = " << a << endl;
b *= 2; // equivalent to b = b * 2
cout << "After b *= 2, b = " << b << endl;

Demonstrates basic arithmetic operators and compound assignment operators.

Increment and Decrement Operators

// prefix and postfix increment/decrement
int x = 5;
printf("Initial x: %d\n", x);
printf("Prefix increment: %d\n", ++x);                 // x becomes 6, then prints 6
printf("Postfix increment: %d\n", x++);                // prints 6, then x becomes 7
printf("Value of x after postfix increment: %d\n", x); // prints 7
printf("Prefix decrement: %d\n", --x);                 //   x becomes 6, then prints 6
printf("Postfix decrement: %d\n", x--);                // prints 6, then x becomes 5
printf("Value of x after postfix decrement: %d\n", x); // prints 5
// prefix and postfix increment/decrement
int x = 5;
cout << "Initial x: " << x << endl;
cout << "Prefix increment: " << ++x << endl;                 // x becomes 6, then prints 6
cout << "Postfix increment: " << x++ << endl;                // prints 6, then x becomes 7
cout << "Value of x after postfix increment: " << x << endl; // prints 7
cout << "Prefix decrement: " << --x << endl;                 //   x becomes 6, then prints 6
cout << "Postfix decrement: " << x-- << endl;                // prints 6, then x becomes 5
cout << "Value of x after postfix decrement: " << x << endl; // prints 5

Shows the difference between prefix (++x, --x) and postfix (x++, x--) operators.