C-Programming Short Notes Removed
C-Programming Short Notes Removed
GATE फर्रे
Page No:- 01
C-PROGRAMMING
GATE फर्रे
5%3=2
-5 % 3 = -2
5 % -3 = 2
-5 % -3 = -2
Page No:- 02
C-PROGRAMMING
GATE फर्रे
= 0001
Page No:- 03
C-PROGRAMMING
GATE फर्रे
Syntax: x >> n = x / (2𝑛𝑛 ) it evaluates from left to right and discard, final
Example: 8 >> 3 Another value is right most
• 8 in binary: 0000 1000 method:
• 8 >> 3 → 0000 0001 = 1 8 / 23 = 8 / 8 ex. int a= 9,8,7;
=1
is interpreted as:
int a = 9;
8;
Bitwise NOT Operator (~) 7;
// so the final value of a=5;
~5
Operates bit-by-bit. • 5 = 0000 0101
Converts each 1 to 0 and
• ~5 = 1111 ex. int a= (9,8,7);
each 0 to 1.
1010 = -6 (in we apply the associativity of comma
~x = -(x+1)
two's operator.
complement)
// so, the final value of a=9;
!0 = true
! true = false
Syntax: x++, x--, First use the value and then perform
the increment or decrement.
int x = 5;
int y = x++; // y = 5, then x becomes 6
Page No:- 04
C-PROGRAMMING
GATE फर्रे
Ternary Operator
Only else block can’t exist. if block is necessary
Syntax: ++x, --x for else.
We do the increment or decrement first then You cannot write else without if.
will use the value in the expression. else {
Ex. int x = 5; // statement;
int y = ++x; // x becomes 6, then y = 6 } // invalid, compiler give the error of wrong
syntax
sizeof
If( )
is a compile-time operator used to determine Statement; // expression is mandatory in the if block
the memory size (in bytes) of a data type or If(1) –> this is perfect statements
Note: In C, if you do not use curly braces {} after an if
variable.
statement, only the very next single statement is
Returns size_t type (an unsigned integer). considered part of the if block.
Evaluated at compile time (except in Variable
Length Arrays).
No function call — it's an operator, not a There should be no statement between
the if and else block.
function.
You must not insert any statements
between if and else.
if (exp) {
If and else statements // statement1;
}
// statement2;
Syntax: if(expression) else {
{statements} // statement3;
}
else {statements}
Result: Compilation error — "else
without if"
There is no compulsion of the else block after if.
This means you can use if without using
else.
if (exp) {
statement;
} // valid syntax, there is no requirement of
the else block
Page No:- 05
C-PROGRAMMING
GATE फर्रे
Valid syntax:
There is all the blocks condition are true. But we while(expression) {
check first condition which is if(x > 10) and it comes Statements;
}
true then we will not execute to the next level, even
expression is not optional it is mandatory.
if the next blocks like else if(x>5), else(x>0) are true.
When to use
• When number of iterations is not known in
advance
Page No:- 06
C-PROGRAMMING
GATE फर्रे
Do-while
• break is not a mandatory statement.
• The expression must evaluate to an integer, char, or
Enum (not float, double, or string).
it executes the loop body first, then checks the
• Each case must use a constant value (no variables or
condition
ranges allowed).
How It Works
• default is optional and executes when no cases match.
• Always runs once, even if the condition is
• The position of the default case doesn’t matter.
initially false. • continue cannot be used inside a switch statement.
• If there is any statement between two case labels, it is
• After executing, it checks the condition.
ignored (not executed) unless one of the cases above
• Continues if condition is true. falls through.
• switch is generally faster than if-else for fixed constant
Expression is mandatory not optional.
comparisons.
• If break is not used and one case matches, execution
will continue through all subsequent cases, including
default.
Continue: To skip the current iteration of a loop and go to
the next iteration, used in loops, but in switch it will give
Switch Statement
switch (expression) {
case constant1:
// code
break;
…
default:
// code
}
Page No:- 07
C-PROGRAMMING
GATE फर्रे
= Assignment operators
Page No:- 08
C-PROGRAMMING
GATE फर्रे
Scanf in C
Syntax
• scanf () is defined in the <stdio.h> header file. return_type function_name(parameter_list) {
// body of function
• It is used to read input from the user.
}
• scanf () returns the number of input items
successfully assigned.
Prototype: A function prototype declares a
• If the input fails (like wrong format or EOF), function's name, return type, and parameters to
it returns 0 or EOF (-1). the compiler before its definition, enabling type
• Format specifiers like %d, %f, %c, %s is used checking and early function calls.
to specify the type of input expected. int add (int, int); // Function prototype
Note:
• Address-of operator & is used to pass the
• If you do not specify a return type, the
memory address of variables (except for default return type is considered as int.
strings). • If the definition or call mismatches the
• Do not use & with strings (character arrays), as prototype, the compiler throws an error.
the array name already represents the address.
Page No:- 09
C-PROGRAMMING
GATE फर्रे
auto
• Default for local variables.
Call by Value • Scope: Inside block/function.
• Lifetime: Till the block ends.
Formal arguments are the parameters listed in the
auto int a = 10; // usually just written as int a = 10;
function definition. When a function is called, copies
of actual values are passed to them.
register
Changes made to formal arguments do not affect
1. Stored in CPU registers (if available) for faster
actual arguments.
access.
This is the default behaviour in C.
2. Cannot get the address using & register int
Separate memory is allocated for the formal
counter;
parameters.
register int counter;
Page No:- 10
C-PROGRAMMING
GATE फर्रे
● Declares a variable defined in another file. auto Block Till Stack auto Garbage
(local) block (default) value
● Used for global sharing. ends (undefined)
extern int x; // Defined elsewhere
register Block Till CPU register Garbage
• If a variable is used before it is defined, (local) block Register value
ends (if (undefined)
you can declare it using extern. available)
• If a variable is used before it is defined, static Block / Entire Data static 0 (zero)
Page No:- 11
C-PROGRAMMING
GATE फर्रे
pt 100
30
Page No:- 12
C-PROGRAMMING
GATE फर्रे
We cannot perform arbitrary pointer arithmetic malloc(), calloc(), realloc(), and free() are used with
between unrelated pointers, but pointer - pointer is pointers for dynamic memory.
valid when both pointers point to elements of the int *p = (int *)malloc(sizeof(int) * 5);
same array. free(p);
In this case, the result is the difference in element
positions, not in bytes, because the compiler
automatically divides the address difference by the Dynamic memory allocation
size of the data type.
int a[10]; malloc () – Memory Allocation
int *p = &a[7]; Definition:
int *q = &a[2];
malloc (memory allocation) is used to dynamically
int diff = p - q;
allocate a single block of memory of a specified size (in
difference = (address at p - address at q) / sizeof(int)
bytes). It does not initialize the memory—it contains
garbage values.
Types of Pointers
Page No:- 13
C-PROGRAMMING
GATE फर्रे
char *a = "hello";
char *b = "hello";
Page No:- 14
C-PROGRAMMING
GATE फर्रे
Note: "hello" is a string literal, which means it • Positive or negative difference of ASCII
represents the address of the first character ('h') in values where mismatch occurs
memory.
printf ("%s", "hello" + 1); strcmp ("papu", "pake")
output: ello Compares: 'p' == 'p', 'a' == 'a', 'p' != 'k'
"hello" is a pointer to the first character ('h') 'p' - 'k' = 112 - 107 = 5
"hello" + 1 moves the pointer one character ahead
— to 'e'
Return value: 5
So printf starts printing from 'e'
Structure in C
strcat
Appends src string to the end of dest string, removing
'\0' of dest and adding one at the end.
strcmp
Compares two strings character by character.
Returns:
• 0 if both strings are equal
Page No:- 15
C-PROGRAMMING
GATE फर्रे
Feature Explanation struct {
int roll;
User-defined Unlike int, char, etc., created char name[20];
by the programmer } s1, s2;
This is anonymous and we can’t create object of
it but can use the variable s1 and s2 which we
declared.
Holds multiple e.g., int, float, char [], all
data types together
Page No:- 16
C-PROGRAMMING
GATE फर्रे
Dynamic Scoping
struct Student {
int roll_num = 9; // Invalid
Variable scope is decided at runtime.
char str [] = "Kunal"; // Invalid
The program searches the call stack to find the most
};
recent variable definition.
This is incorrect, because:
• A struct is only a blueprint, it does not
allocate memory until an object (variable) x=5
is created. foo() {
• In C, you cannot assign values to structure print(x);
members inside the definition. }
• Memory is only allocated when you bar() {
create an object like:
local x=10
struct Student s1;
foo # Output: 10 – because bar called foo, and
x=10 in bar
Structure definition is just a template. You can’t assign }
values inside it — you assign values only after creating
a variable.
we access structure members using either the dot (.)
operator or the arrow (->) operator
Union in C
A union is a special data type in C that allows storing
different types of data in the same memory location.
union Data {
int i;
float f;
char str[20];
};
All members share the same memory, and the size of
the union is equal to the size of its largest member.
Scoping
• Scoping defines where a variable can be
accessed in a program (its visibility or lifetime).
Static scoping
Variable scope is decided at compile time.
Local to the block will be preferred.
int x = 10;
void func () {
int x = 20;
printf ("%d", x); // Output: 20 (local x used)
}
Page No:- 17