Understanding Programming Languages
Understanding Programming Languages
1. Low-Level Languages
These are closer to the machine's native language (binary), and they are
interpreted directly by the hardware.
1. Machine Language:
All programs need to be translated into machine code before a computer can
understand and execute them.
Ex:- computer can understand only o ,1 language we convert into 0,1 form
through high level language.
24 —–> 11000
14 ——> 01110
Advantages:
2. No translator is required.
Disadvantages:
2. Assembly Language
NOTE:
Advantages:
Why Learn C?
C is one of the most popular and foundational programming languages in the world.
Learning C makes it easier to learn languages like Java, Python, C++, and C#, as many
share similar syntax.
C helps you understand how computer memory and low-level operations
work(Controlling how data is stored in RAM).
C is faster than many other high-level languages like Java or Python.
It can be used in both applications and technologies(like games, text editors, and
databases.)
Features of C Language
1. Simplicity
o C has a simple syntax with a limited number of keywords and data types, making it easy to
learn and use.
2. Mid-level Language
o C combines the features of both high-level and low-level languages.
3. Portability
o C programs are portable, meaning code written on one system can be run on another with
minimal changes.
4. Modularity
o Code in C can be broken into functions and reused in different parts of the program or other
programs.
5. Structured Language
o C allows the use of functions, control structures (like if-else, loops), and decision-making
statements to create well-organized programs.
6. Extensibility
o New features or functionalities can be easily added to a C program without affecting the
existing code.
Applications of C programming
1. Operating Systems – Used to develop system software like Windows and UNIX.
2. Compilers – Helps build compilers for other programming languages.
3. Device Drivers – Used to write drivers for hardware devices.
4. Game Development – Provides high-performance code for game engines.
5. Database Systems – Used in creating database software like MySQL, Oracle, SQL server.
6. System Utilities – Creates utility programs like file managers and editors.
7. Scientific Computing – Used in simulations, calculations, and research tools.
Steps to Solve Problems (in Programming):
What is Pseudocode?
Example of Pseudocode
Problem:
Find the largest of two numbers.
Pseudocode:
START
Input number A
Input number B
IF A > B THEN
Print "A is larger"
ELSE
Print "B is larger"
END IF
STOP
What is a Flowchart?
A flowchart is a visual diagram that shows the steps of a process or algorithm using
different shapes and arrows. It helps us understand the flow of the logic clearly.
Flowchart Symbols
Various symbols represent different operations in a C Language Flowchart. Each symbol
plays a specific role:
1. Oval: Represents start and end points.
Programming Environments:
A programming environment is the set of tools and software that
programmers use to write, test, and debug their code. Common
programming environments include:
Text Editors: Simple tools for writing code (e.g., Notepad++, Sublime
Text).
Integrated Development Environments (IDEs): Comprehensive
software that includes a code editor, compiler, debugger, and other
tools (e.g., Visual Studio, Eclipse, PyCharm).
Compilers and Interpreters: Programs that translate source code
into machine code or directly execute the code.
Example: Cling (Interpreter), gcc (Compilers)
Basic Structure OF C Language:
Each C Program is consists of 6 main sections, these sections are named
as Documentation Section, Link Section, Definition Section, Global
Declaration Section, Main Function Section, Subprogram Section. You will
find a brief explanation about each section below.
1. Documentation Section:
The Documentation Section consists of a set of comment lines giving the
name of the Programmer, date, and other details about the program. The
documentation section helps anyone to get an overview of the program.
Comments may appear anywhere within a program. The text between
/* */ appears as a comment in C.
Example: /* This is a comment */
2. Link section or Preprocessor Directives or Header Files :
The Link Section refers to the part of a C program where header files are
included using the #include directive. These header files contain
declarations for functions, macros, constants, and data types that are part
of the C Standard Library or user-defined libraries.
Header files like <stdio.h>, <stdlib.h>, and <string.h> provide prototypes
for standard functions.
Example:
<stdio.h> : printf(), scanf()
<stdlib.h> : malloc() Note: For memory allocation.
<string.h> : strlen ().
<math.h> : The math.h header file contains declarations for mathematical
functions.
3. Definition Section
The Definition Section is the part of a C program where constants are
defined using the #define directive. These are typically written at the
beginning of the program, before the main() function.
4. Global Declaration:
The Global Declaration Section is the part of a C program where variables,
functions or structures are declared outside of all functions, typically above
the main() function. They are accessible to all functions and variables in the
program.
5. Main() Function
main() function is mandatory for any program and it includes two parts,
1. declaration part
2. executable part.
6. Subprograms:
In C programming, subprograms refer to functions or procedures that
perform a specific task.
Example:
/* Documentation Section
File : age.c
Author : You
Date: [Link]
Description : A program to calculate age based on the year of birth.
*/
// Link Section
#include <stdio.h>
// Definition Section
#define BORN 2000
// Main() Function
int main(void)
{
// Declaration Part
int current = 2021;
// Executable Part
printf("Age: %d\n", age(current));
return 0;
}
// Subprograms Section (User-defined Function)
int age(int current) {
return current - BORN;
}
C Comments:
The comments in C are human-readable notes in the source code of a C
program used to make the program easier to read and understand. They are not
a part of the executable program by the compiler or an interpreter.
Two Types of Comments in C
1. Single-line Comments :
Single-line comments are used to comment out a single line of code or a part
of it. The single line comments in C start with two forward slashes (//), and
everything after the slashes on that line is considered a comment.
Syntax: // your comment here
Example:
#include <stdio.h>
int main()
{
// create integer variable
int age = 25;
// print the age variable
printf("Age: %d", age);
return 0;
}
2. Multi-line Comments:
Multi-line comments in C are used write comments that span more than one
line. They are generally used for longer descriptions or for commenting out
multiple lines of code. In C language, these comments begin with /* and end
with */. Everything between these markers is treated as a comment.
Syntax: /* your comment here*/
Example:
/*
Name: Anu
Date: 2-08-2025
Program name: Display Text
*/
#include <stdio.h>
int main()
{
printf("Welcome to GeeksforGeeks");
return 0;
}
C Hello World Program
The “Hello World” program is the first step towards learning any
programming language. It is also one of the simplest programs that is used to
introduce aspiring programmers to the programming language. It typically
outputs the text "Hello, World!" to the console screen.
Example:
// Header file for input output functions
#include <stdio.h>
// Main function: entry point for execution
int main()
{
// Writing print statement to print hello world
printf("Hello World");
return 0;
}
Keywords in C?
Keywords in C are reserved words that have a special meaning to the [Link]
cannot use them as names for variables, functions, or identifiers. All these words
are the 32 reserved keywords of c language. Keywords are predefined. These
reserved words have special meanings to the compiler.
● After the first letter identifier can have character, digit, underscore.
● Identifiers are case sensitive, uppercase and lowercase letters are distinct.
Keywords Identifiers
Keywords are predefined word that Identifiers are the values used to define
gets reserved for working program different programming items such as
that have special meaning and variables, integers, structures, unions and
cannot get used anywhere else. others and mostly have an alphabetic
character.
Specify the type/kind of entity. Identify the name of a particular entity.
It always starts with a lowercase First character can be a uppercase,
letter. lowercase letter or underscore.
A keyword should be in lower case An identifier can be in upper case or lower
and can only contains alphabetical case and can consist of alphabetical
characters. characters, digits and underscores.
They help to identify a specific They help to locate the name of the entity
property that exists within a that gets defined along with a keyword.
computer language.
int, char, if, while, do, class etc. Test, count1, high_speed, etc.
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the
variable can store like integer, character, floating, double, etc.
Example:
int number;
void
1. Primary (Basic) Data Types in C:
int main()
{
int age = 25;
char grade = 'A';
float pi = 3.14f;
double gravity = 9.80665;
unsigned int items = 100;
short int temp = -300;
unsigned short int speed = 60000;
long int population = 1500000000L;
printf("%d", *ptr);
Function A block of code that performs a specific #include <stdio.h>
task and can return a value.
// Function definition
int add(int a, int b)
{
return a + b;
}
int main()
{
// Function call
int result = add(5, 3);
printf("The sum is: %d", result);
return 0;
}
3. User-Defined Data Types :
Data Type Description Example
struct A structure groups different struct Person
data types into a single unit. {
Each member has its own int age;
memory. char name[20];
char address[100];
};
Example 1:
// variable declaration
int marks;
// variable definition
marks = 10;
Example 2:
NOTE: We can do declaration and definition in a single step too, like this
(recommended).
int num = 3;
Accessing Variables
The data stored inside a C variable can be easily accessed by using the variable's
name.
Example:
#include <stdio.h>
int main()
{
// create integer variable declared
int num = 3;
// Access the value stored in memory
printf("%d", num);
return 0;
}
Changing Stored Values
We can also update the value of a variable with a new value whenever needed by
using the assignment operator =.
Example:
#include <stdio.h>
int main()
{
// Create integer variable
int n = 3;
// Change the stored data
n = 22;
// Access the value stored in memory
printf("%d", n);
return 0;
}
Scope of Variables in C
We have told that a variable can be accessed anywhere once it is declared, but it
is partially true. A variable can be accessed using its name anywhere in a specific
region of the program called its scope.
Example:
// num cannot be accessed here
int main()
{
// num cannot be accessed here
{
// Variable declaration
int num;
}
// Cannot be accessed here either
return 0;
}
Constant variables:
In C programming, const is a keyword used to declare a variable as constant, meaning
its value cannot be changed after it is initialized.
Syntax:
Name of the
variable
Example:
Types of Constants:
1. Integer Constants
An integer constant in C is a fixed whole number value without a decimal point that
cannot be changed during program execution.
Example:
const int age = 25; // age is an integer constant with value 25
const int year = 2025; // year is an integer constant with value 2025
2. Character Constants:
Character constants are single characters enclosed in single quotes. They represent
individual characters like letters, digits, or symbols.
Example:
const char char_const = 'A';
3. Floating Constants
Floating-point constants represent real numbers and always include a decimal point.
Example:
const float PI = 3.14;
4. String Constants
String constants are sequences of characters enclosed in double quotes.
Example:
const char* MESSAGE = "Hello, World!";
Variables
There are two ways to classify variables
[Link] Variables
Example:
#include <stdio.h>
void display()
{
int x = 10; // Local variable
printf("x = %d\n", x);
}
int main()
{
display();
// printf("%d", x); // ERROR: x is not accessible here
return 0;
}
2. Global Variables
• Declared outside of all functions (usually at the top of the file).
• Accessible by all functions in the same file (and even in other files
using extern).
Example:
#include <stdio.h>
int g = 5; // Global variable
void func()
{
printf("g = %d\n", g); // Accessible here
}
int main()
{
printf("g = %d\n", g); // Accessible here
func();
return 0;
}
Constant variables:
In C programming, const is a keyword used to declare a variable as constant,
meaning its value cannot be changed after it is initialized.
Syntax:
Name of the
variable
Example:
1. Integer Constants
An integer constant in C is a fixed whole number value without a decimal point
that cannot be changed during program execution.
Example:
const int age = 25; // age is an integer constant with value 25
const int year = 2025; // year is an integer constant with value 2025
2. Character Constants:
Character constants are single characters enclosed in single quotes. They represent
individual characters like letters, digits, or symbols.
Example:
const char char_const = 'A';
3. Floating Constants
Floating-point constants represent real numbers and always include a decimal
point.
Example:
const float PI = 3.14;
4. String Constants
String constants are sequences of characters enclosed in double quotes.
Example:
const char* MESSAGE = "Hello, World!";
Operators
Example:
Int a = 10, b = 5;
Int sum = a + b; // here '+' is an operator
Types of operators:
1. Arithmetic Operators:
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc…
Operator Description Example
+ Addition 5 + 3 = 8
- Subtraction 5 - 3 = 2
* Multiplication 5 * 3 = 15
/ Division 6 / 3 = 2
% Modulus (remainder) 5 % 3 = 2
Example:
#include <stdio.h>
int main()
{
int a = 10, b = 3;
printf("sum = %d\n", a + b);
printf("sub = %d\n", a - b);
printf("mul = %d\n", a * b);
printf("divi = %d\n", a / b);
printf("mod = %d\n", a % b);
return 0;
}
Output:
sum = 13
sub = 7
mul = 30
divi = 3
mod = 1
Example:
#include <stdio.h>
int main()
{
int x = 5, y = 10;
printf("x == y: %d\n", x == y);
printf("x != y: %d\n", x != y);
printf("x > y: %d\n", x > y);
printf("x < y: %d\n", x < y);
printf("x < y: %d\n", x <= y);
printf("x < y: %d\n", x >=y);
return 0;
}
Output:
x == y :0
x != y :1
x>y :0
x<y :1
x<y :1
x<y :0
3. Logical Operators
4. Assignment Operators:
An assignment operator is used for assigning a value to a variable. The most
common assignment operator is =
Syntax:
variable = value (or) variable (or) expression
1. variable = value;
example:
int a;
a = 10; // Assigns the value 10 to variable 'a'
2. variable = variable;
example:
int a = 5;
int b;
b = a; // Assigns the value of 'a' (which is 5) to 'b'
3. variable = expression;
example:
int a = 4, b = 6;
int result;
result = a + b; // result = 4 + 6 → 10
Operator Description Example
= Assign a = 10
+= Add and assign a += 5 → a = a + 5
-= Subtract and assign a -= 5 → a=a-5
*= Multiply and assign a *= 5 → a=a*5
/= Divide and assign a /= 5 → a=a/5
%= Modulus and assign a %= 5 → a=a%5
Example:
#include <stdio.h>
int main()
{
int a = 10;
printf("Initial value of a: %d\n", a);
a += 5;
printf("After a += 5: %d\n", a); // a = a + 5 → 15
a -= 5;
printf("After a -= 5: %d\n", a); // a = a - 5 → 10
a *= 5;
printf("After a *= 5: %d\n", a); // a = a * 5 → 50
a /= 5;
printf("After a /= 5: %d\n", a); // a = a / 5 → 10
a %= 5;
printf("After a %= 5: %d\n", a); // a = a % 5 → 0
return 0;
}
4. Bitwise Operators in C
Bitwise operators perform manipulations of data at the bit level.
These operators also perform the shifting of bits from right to left.
Bitwise operators are not applied to float or double, long double, void,
etc.
Operator Description Ex: Explanation
& Bitwise AND 5&9 0101 & 1001 = 0001 → 1
| | Bitwise 13
OR
^ Bitwise Exclusive OR 5 ^ 9 0101 ^ 1001 = 1100 → 12
(XOR)
~ One's complement ~5 ~0101 = 0110 (in 4 bits) → -6
(NOT)
>> Shift right 9 >> 1 1001 >> 1 = 0100 → 4
<< Shift left 5 << 1 0101 << 1 = 1010 → 10
1. & Bitwise AND:
Compares each bit of two numbers and returns 1 only if both bits are 1.
Example:
5&9
= 0101 & 1001
= 0001 → 1
2. Bitwise OR: 5 | 9
Binary:
0101 (5)
| 1001 (9)
= 1101 → 13
3. Bitwise XOR: 5 ^ 9
Binary:
0101 (5)
^ 1001 (9)
= 1100 → 12
1. One's Complement (~)
1 becomes 0,
0 becomes 1.
2. 2’s Complement
Example: 16 - 00010000
int a = 16;
Example: 16 - 00010000
int a = 16;
The ternary operator, also known as the conditional operator in the C language
can be used for statements of the form if-then-else.
Syntax :
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b; // if a>b then max=a else max=b
printf("Maximum = %d\n", max);
6. Special Operator
Example 1:
int x = 5, y = 5;
printf("Post-Increment = %d\n", x++); // Post-Increment : 6
Example 2:
int a = 5, b = 5;
printf("Prefix Increment: ++a = %d\n", ++a); // Post-Increment : 6
Precedence
When an expression has multiple operators, precedence determines the
order in which operators are evaluated.
Example:
int x = 10 + 5 * 2;
• Here * has higher precedence than +.
• So expression is evaluated as 10 + (5 * 2) = 10 + 10 = 20.
Associativity
When operators have the same precedence, associativity decides the
direction of evaluation (left-to-right or right-to-left).
Example – Left to Right Associativity
int x = (20 / 5) * 2;