0% found this document useful (0 votes)
2 views44 pages

Example

The document provides a comprehensive overview of programming concepts including ASCII vs Unicode, memory layout, operator precedence and associativity in C, and the Euclidean algorithm for finding the greatest common divisor (GCD). It also discusses various types of loops and their characteristics, as well as key differences in control structures. Additionally, it includes examples and questions related to C programming syntax and data types.

Uploaded by

backupwhatsapp9493
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views44 pages

Example

The document provides a comprehensive overview of programming concepts including ASCII vs Unicode, memory layout, operator precedence and associativity in C, and the Euclidean algorithm for finding the greatest common divisor (GCD). It also discusses various types of loops and their characteristics, as well as key differences in control structures. Additionally, it includes examples and questions related to C programming syntax and data types.

Uploaded by

backupwhatsapp9493
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASCII

Extended ASCII
Unicode
Unicode-emoji
ASCII Vs Unicode :Key Differences

Feature ASCII Unicode


7-bit (128 chars), extended to 21-bit space (over 1 million
Bits
8-bit (256) code points)

All human languages,


English letters, digits,
Coverage symbols, emojis, technical
punctuation, control codes
characters

Backward-compatible (ASCII
Compatibility Very limited (English only)
is included in Unicode)

UTF-8, UTF-16, UTF-32


Encodings Single byte per character
(variable length)

A = U+0041, 漢 = U+6F22, 😀
Example A = 65
= U+1F600
Layout of a Program in Memory
0x7FFFFFFF
Stack Segment Stack Grows
Downwards

Memory
Addresses
in Hex
Dynamic Area
Data Segment
Static Area
0x10000000

Text Segment
0x04000000
Reserved
0
int A;
Stack
int B=10;
main(){
Heap
int Alocal;
int *p; BSS

p=(int*)malloc(10); Data
}
Code
Floats are not Reals

Int’s:
eg. 40000 * 40000 --> 1600000000
600000* 600000 --> ?
Floats:
Eg 2 : Is (x + y) + z = x + (y + z)?

eg
(1e20 + -1e20) + 3.14 --> 3.14
1e20 + (-1e20 + 3.14) --> ??
Need to understand details of underlying implementations
Operator Associativity: Left-to-Right or Right-to-Left

Operator Type Associativity Example


Arithmetic (+ - * /) Left-to-right a-b+c
Assignment (= +=) Right-to-left a=b=c
Unary (++a, --a) Right-to-left x = ++*p;
Conditional (?:) Right-to-left a?b:c

condition ? expr_if_true : expr_if_false


int result = a ? b : c ? d : e;
int result = (a ? b : c) ? d : e;
Operator Precedence and Associativity in C

Precedence (High
Operator Class Operators Associativity
→ Low)
Highest Unary (postfix) expr++, expr-- Left to Right
++expr, --expr, -,
Unary (prefix) Right to Left
!, &
Multiplicative *, /, % Left to Right
Additive +, - Left to Right
Relational <, <=, >, >= Left to Right
Equality ==, != Left to Right
Logical AND && Left to Right
Logical OR `
=, +=, -=, *=, /=,
Lowest Assignment Right to Left
%=
Operator Associativity: Left-to-Right or Right-to-Left

condition ? expr_if_true : expr_if_false

int x = 5;
int y = (x > 0) ? 10 : (x < 0) ? 20 : 30;

int y = (x > 0) ? 10 : ((x < 0) ? 20 : 30);

int y = ((x > 0) ? 10 : (x < 0)) ? 20 : 30; // left to right incorrect


Which of the following is NOT a valid identifier in C?
A. abc
B. 10abc
C. simple_interest
D. Empl_1

What is the size of a double data type in C (usually)?


A. 2 bytes
B. 4 bytes
C. 8 bytes
D. 1 byte
Which keyword is used to define a read-only variable
in C?
A. static
B. constant
C. final
D. const
Which of the following is a valid character constant in C?
A. 'A'
B. "A"
C. A
D. A'
The output of printf("%d", 25+144); is:
A. 25144
B. 169
C. 25+144
D. Compilation Error

What does the following line do? char c; c = getchar();


A. Prints a character on the screen
B. Reads a character from the keyboard and stores it in c
C. Converts character to string
D. Terminates the program
Literals vs Variables:
(without suffix) is a double literal (8 bytes).
Type Casting
Arithmetic Operator Precedence
Arithmetic Operator Precedence
Prefix vs postfix operators
Dangling if
Logical and Bitwise
Shift Left-Right
Euclidean Algorithm

Greatest Common Divisor (GCD)


Examples
Example 1: GCD(48, 18)
• Step 1: 48 ÷ 18 = 2 remainder 12 → GCD(48,18) =
GCD(18,12)

• Step 2: 18 ÷ 12 = 1 remainder 6 → GCD(18,12) =


GCD(12,6)

• Step 3: 12 ÷ 6 = 2 remainder 0 → Stop

• Result: GCD(48,18) = 6
Example 2: GCD(23, 3)
• Step 1: 23 ÷ 3 = 7 remainder 2 → GCD(23,3) =
GCD(3,2)

• Step 2: 3 ÷ 2 = 1 remainder 1 → GCD(3,2) =


GCD(2,1)

• Step 3: 2 ÷ 1 = 2 remainder 0 → Stop

• Result: GCD(23,3) = 1
Pseudo Code
Euclid Algo.
Key Differences( for, while , do-while)
Feature for loop while loop do-while loop
Done inside loop Done separately Done separately
Initialization
header before loop before loop

At start (before 1st At start (before 1st At end (after 1st


Condition check
iteration) iteration) iteration)

Fixed number of Condition-based


At least one
Use case iterations (e.g., repetition, unknown
execution guaranteed
arrays, counters) iterations

May be zero times if May be zero times if


Execution count condition false condition false At least once always
initially initially

Compact (init,
More flexible but less Good when body
Readability condition, update in
compact must run at least once
one line)
Function
Function call
Function call

You might also like