Skip to content

01 Intro

Basics

Digital data is in binary form.

  • 0/1 (bit)
  • 8 bits = 1 byte
  • 1024 bytes = 1KB

When write a C program to perform a task, it gets converted into binary instructions which a computer can understand

Variables

int x; // declaration
x=5; // initialization

int x = 5; // combined

Tokens

  • atomic unit of a code

  • Key words

    return
      void
    
  • Identifiers

    int x = 3;
    

Code Block

group of code within { ... }

Structure

print("hi")
print("hello")
#include <stdio.h>

void print_on_screen()
{
  printf("hi"); // ; = terminator
}

int main() // driver function of your program
{
  // code
  print_on_screen();

  int x = 4; // 

  return 0;
}

Data Types

Primitive

void (nothing) 0
boolean True/False
true/false
1
char ‘H’ 1 %c
int 34444 2 %d
float 334545.345534 4 %f
double 334545345534334545345534.334545345534334545345534 8 %

User-defined

(not in scope of current exam)

Math Operators

\[ + - * / \% \]
  • int/int = int

  • B

  • ^
  • Multiplication/Division (whichever comes first)
  • Addition/Subtraction (whichever comes first)

Type Casting

change in data type of a variable for a momentary purpose

implicit (automatic)

int/int \(\to\) int

  • 3/2 = 1.5
  • 3/2 = 1

int/float \(\to\) float

  • larger data type
int x=5;
float z = 5/(double) 5;

explicit(user-defined) type casting

int x = -5;
int y = 2;

-5/2; // -2.5 -2
5/-2; // -2.5 -2

5/-2.0f // -2.5

Relational Operators

\[ > , \ge , < , \le, ==, \ne, != \]
  • \(=\) and \(==\)
    • assignment
    • equality operator
const float pi = 3.14;

Logical Operators

\[ ! \\ \&\& \\ || \]
\[ [!0 \&\& 1 || 3\&\&!1] \]
int main()
{
  int x;

  // take input from user
  scanf("%d", &x); // storing the value in the address of x

  printf("%d", x);


  return 0;
}

// x = 5
int main()
{
  int x = 5;

  printf("%d", ++x); // 6
  printf("%d", x); // 6

  return 0;
}
Last Updated: 2023-01-25 ; Contributors: AhmedThahir

Comments