C Programming Basics: Data Types, Operators, printf/scanf and Output Questions · সি প্রোগ্রামিংয়ের মৌলিক বিষয়: ডেটা টাইপ, অপারেটর, printf/scanf ও আউটপুট প্রশ্ন
A C program is built from tokens (keywords, identifiers, constants, strings, operators and punctuators) and stores data in typed variables: char is 1 byte, int is usually 4 bytes, float 4 and double 8, though only char's size is fixed by the C standard. Expressions are evaluated by operator precedence and associativity, integer division truncates toward zero (7 / 2 = 3, −7 / 2 = −3), and mixed types are converted to the wider type. printf and scanf use format specifiers such as %d (int), %c (char), %f (float/double in printf), %lf (double in scanf) and %s (string), and scanf needs the address of each variable (&x).
সি প্রোগ্রাম টোকেন (কিওয়ার্ড, আইডেন্টিফায়ার, কনস্ট্যান্ট, স্ট্রিং, অপারেটর ও পাংচুয়েটর) দিয়ে গঠিত এবং ডেটা রাখে নির্দিষ্ট টাইপের ভেরিয়েবলে: char ১ বাইট, int সাধারণত ৪ বাইট, float ৪ ও double ৮ বাইট, যদিও সি স্ট্যান্ডার্ড কেবল char-এর আকারই নির্দিষ্ট করে দেয়। এক্সপ্রেশনের মান বের হয় অপারেটরের প্রিসিডেন্স ও অ্যাসোসিয়েটিভিটি অনুযায়ী; পূর্ণসংখ্যার ভাগে ভগ্নাংশ বাদ দিয়ে ফল শূন্যের দিকে ছাঁটা হয় (৭ / ২ = ৩, −৭ / ২ = −৩), আর ভিন্ন টাইপ মিশলে ছোট টাইপটি বড় টাইপে রূপান্তরিত হয়। printf ও scanf ফরম্যাট স্পেসিফায়ার ব্যবহার করে, যেমন %d (int), %c (char), printf-এ %f (float/double), scanf-এ %lf (double) ও %s (স্ট্রিং); আর scanf-কে প্রতিটি ভেরিয়েবলের অ্যাড্রেস (&x) দিতে হয়।
- Asked in past exams
- 170
- Papers
- 92
1. The building blocks: tokens, keywords and identifiers
Where C comes from. Dennis Ritchie developed C at Bell Labs in 1972, from the earlier language B. It is often called a middle-level language: it has high-level structures but can also work close to the hardware (pointers, bit operations). The smallest complete program:
#include <stdio.h> /* header file: declares printf */
int main(void) { /* execution always starts in main */
printf("Hello\n"); // a one-line comment (C99 and later)
return 0;
}
Before it runs, a program passes through four stages: the preprocessor handles lines starting with # (#include, #define), the compiler turns C into assembly, the assembler makes an object file (.o or .obj), and the linker joins it with library code into an executable (.exe on Windows). Comments (/* … */, or // to the end of the line) are removed and never reach the compiled program.
The compiler first breaks a C program into tokens, the smallest units that mean something. There are six kinds:
| Token kind | Examples |
|---|---|
| Keywords | int, float, if, for, return, static |
| Identifiers (names you choose) | total, main, _count, var_9 |
| Constants (literals) | 42, 3.14, 'A', 0x1A |
| String literals | "Hello" |
| Operators | +, =, ==, &&, ++, & |
| Punctuators (special symbols) | ;, ,, (, ), {, }, [, ] |
Keywords are reserved words that cannot be used as names. C89/C90 has 32 keywords (auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while). C99 added 5 (37 in all) and C11 added 7 more (44). Exams that say "C has 32 keywords" mean C89. Java has 50 keywords (including the unused goto and const), and Python 3 has 35 (True, False and None among them). sizeof is a keyword and an operator in C, but Java has no sizeof; in Java, objects are created with the new operator.
Identifier rules (C and C++; Java differs only where noted):
- Use only letters, digits and the underscore
_(Java also allows$). - The first character must be a letter or an underscore, never a digit.
- No spaces or other symbols (
#,-,@), and not a keyword. - Names are case-sensitive:
Total,totalandTOTALare three different names.
| Name | Valid? | Why |
|---|---|---|
var_9 |
Yes | Letters, digit and underscore |
_ |
Yes in C/C++ | A single underscore is a legal C name (in Java it is a keyword from Java 9) |
9_var |
No | Starts with a digit |
No#of-students |
No | # and - are not allowed |
float |
No | Keyword |
star |
Yes | Not a keyword (only struct, static etc. are) |
Worked example 1: count the tokens. How many tokens are in printf("i = %d, &i = %x", i, &i);?
printf(identifier) → 1(→ 2"i = %d, &i = %x": a whole string literal is one token, whatever is inside it → 3,i,→ 4, 5, 6&i→ 7, 8);→ 9, 10
Answer: 10 tokens. Spaces are not tokens, and nothing inside quotes is counted separately.
2. Data types and their sizes
A data type fixes how many bytes a variable uses, how its bits are read and which operations are allowed. C's types fall into four groups:
Classifications differ: many textbooks put void among the primary types (the usual keyed answer to "void is which type?"), and the C standard itself counts arrays, structures, unions, functions and pointers as derived types.
The C standard fixes only minimum ranges; sizeof(char) is always 1, and the rest depends on the compiler and machine:
| Type | Standard minimum | Typical 32-bit (x86) | 64-bit Windows | Typical 64-bit Linux | printf specifier |
|---|---|---|---|---|---|
char |
8 bits | 1 byte | 1 byte | 1 byte | %c |
short |
16 bits | 2 | 2 | 2 | %hd |
int |
16 bits | 4 | 4 | 4 | %d or %i |
long |
32 bits | 4 | 4 | 8 | %ld |
long long (C99) |
64 bits | 8 | 8 | 8 | %lld |
float |
about 6 digits | 4 | 4 | 4 | %f |
double |
about 10 digits (15 in practice) | 8 | 8 | 8 | %f (printf), %lf (scanf) |
long double |
at least double |
12 (gcc) | 8 (MSVC), 16 (MinGW-w64) | 16 | %Lf |
| any pointer | – | 4 | 8 | 8 | %p |
64-bit Windows keeps long at 4 bytes (the LLP64 model); 64-bit Linux makes it 8 (LP64).
So printf("%zu", sizeof(int)); prints 4 on today's compilers (exam papers often write %d, which works on most 32-bit systems but strictly needs %zu or a cast to int). Old 16-bit compilers such as Turbo C used a 2-byte int, which is why some older keys say 2; when the question gives no system, answer 4.
Ranges follow from the number of bits n: an unsigned type holds 0 to 2ⁿ − 1, and a signed type (two's complement) holds −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.
| Bits | Signed range | Unsigned range |
|---|---|---|
8 (char) |
−128 to 127 | 0 to 255 |
16 (short) |
−32,768 to 32,767 | 0 to 65,535 |
32 (int) |
−2,147,483,648 to 2,147,483,647 | 0 to 4,294,967,295 |
Java fixes every size on every machine: byte 1 byte, short 2, int 4, long 8, float 4, double 8 and char 2 (a UTF-16 code unit); the size of boolean is not specified. Python has no fixed-size integers: an int grows as needed (2**100 prints 1267650600228229401496703205376), and a Python float is a C double.
3. Variables, constants and literals
A variable is a named memory location. int age; declares it, int age = 30; declares and initializes it, and age = 31; assigns a new value.
- A local variable (inside a function) that is not initialized holds an indeterminate ("garbage") value in C. Global and
staticvariables start at 0. - In Java, a local variable must be assigned before use, or the program does not compile; fields of an object get default values (0,
false,null). SoTest t; System.out.println(t.i);is a compile error ("variable t might not have been initialized"), whileTest t = new Test();prints 0 for anintfield.
| Point | Local variable | Global variable |
|---|---|---|
| Declared | Inside a function or block | Outside all functions |
| Visible | Only in that function or block | In every function after its declaration |
| Lifetime | While the function runs | The whole program run |
| Default value | Garbage (indeterminate) | 0 |
Constants are values that cannot change:
const int MAX = 100;: a typed, read-only variable.#define PI 3.14159: a preprocessor macro, replaced as text before compiling (no type, no=, no semicolon).enum color { RED, GREEN, BLUE };gives named integer constants 0, 1, 2.- Java uses
final(final int MAX = 100;); Python has no true constants, only the naming customMAX = 100.
Literals and their types:
| Literal | Meaning | Type |
|---|---|---|
26 |
Decimal | int |
032 |
Octal (leading 0) = 26 | int |
0x1A |
Hexadecimal = 26 | int |
26L, 26U |
Long, unsigned | long, unsigned int |
1.414 |
Floating constant | double (not float!) |
1.414f or 1.414F |
Floating constant | float |
'A' |
Character constant = 65 | int in C (char in C++) |
"A" |
String: 'A' plus '\0' |
array of 2 char |
Because a character is stored as its code, 'A' is 65, 'a' is 97 ('a' - 'A' = 32) and '0' is 48. In C, sizeof('A') is sizeof(int) (4), but in C++ it is 1. Escape sequences: \n new line, \t tab, \0 null character (ends every string), \\ backslash, \' and \" quotes.
4. Operators, precedence and associativity
| Group | Operators |
|---|---|
| Arithmetic | + - * / % |
| Relational | < <= > >= == != (result 1 or 0 in C) |
| Logical | && || ! (short-circuit) |
| Bitwise | & | ^ ~ << >> |
| Assignment | = += -= *= /= %= and so on |
| Increment / decrement | ++ -- |
| Conditional | ?: |
| Special | sizeof, , (comma), & (address of), * (indirection), . and -> (member access) |
Precedence says which operator binds first; associativity settles ties between operators of the same level:
| Level (high → low) | Operators | Associativity |
|---|---|---|
| 1 | () [] -> . postfix ++ -- |
Left to right |
| 2 | prefix ++ --, unary + -, ! ~, * (indirection), & (address), sizeof, casts |
Right to left |
| 3 | * / % |
Left to right |
| 4 | + - |
Left to right |
| 5 | << >> |
Left to right |
| 6 | < <= > >= |
Left to right |
| 7 | == != |
Left to right |
| 8 – 10 | &, then ^, then | |
Left to right |
| 11 – 12 | &&, then || |
Left to right |
| 13 | ?: |
Right to left |
| 14 | = += -= … |
Right to left |
| 15 | , |
Left to right |
The rules exams test most:
*,/and%share one level, above+and-. So2 + 3 * 4 - 10 / 2 % 3= 2 + 12 − (5 % 3) = 2 + 12 − 2 = 12, and7 / 2 * 2= 3 × 2 = 6 (left to right).- Integer division truncates toward zero (fixed by C99; C89 left negatives to the compiler):
7 / 2= 3,-7 / 2= −3. The remainder takes the sign of the dividend:-7 % 2= −1,7 % -2= 1, and5 % 1= 0. In C,%works only on integers (usefmodfor floating values). Java behaves like C here. Python's//floors instead:-7 // 2is −4 and-7 % 2is 1. - Assignment is right to left:
d = c = 5sets c, then d, to 5. - The comma operator evaluates its operands left to right and gives the value of the right one:
int a = (1, 2, 3);sets a to 3. (Without the parentheses the commas would separate declarations instead.) - A relational operator gives 1 (true) or 0 (false) in C. So with x = 20, y = 10, z = 5,
x > y > zis(20 > 10) > 5=1 > 5= 0. (Python chains comparisons, so20 > 10 > 5isTruethere.) &&and||short-circuit: with x = 0 and y = 0, afterif (x++ && y++)x is 1 but y is still 0, becausex++gave 0 (false) andy++never ran.- Bitwise with 11 (1011) and 3 (0011):
11 | 3= 11,11 & 3= 3,11 ^ 3= 8,~11= −12,11 << 2= 44,11 >> 1= 5. Use|to set a bit and&(with an inverted mask) to reset it. For a non-negative x,x & 7equalsx % 8. - XOR swap without a third variable:
a ^= b; b ^= a; a ^= b;turns a = 10, b = 20 into 20 10. - In Java,
charvalues add as numbers:System.out.print('D' + 'E' + 'F');prints 207 (68 + 69 + 70). But once a String appears,+joins text:2 + 3 + "x" + 2 + 3is 5x23.
A neighbouring C++ fact often asked here: the operators ::, ., .*, ?: and sizeof cannot be overloaded; ++, <<, () and ~ can. Java has no user-defined operator overloading.
5. Type conversion
Implicit conversion happens automatically. Small integer types (char, short) are first promoted to int; then, when two operands differ, the "lower" one is converted to the "higher" one:
Explicit conversion (a cast) is written (type) value. Converting a floating value to an integer truncates toward zero: (int)3.99 is 3 and (int)-3.99 is −3 (it never rounds).
Worked example 2: mixed types.
float p = 10.5;
int a = 5 * p + 5.0;
printf("%d\n", a);
5 * p: theint5 becomesfloat, giving 52.5.52.5 + 5.0:5.0is adouble, so the sum is thedouble57.5.- Storing 57.5 in an
intdrops the fraction.
Output: 57.
More conversions that cost marks:
- With
int a = 7, b = 2;:a / b= 3,(double)a / b= 3.5, but(double)(a / b)= 3.0, because the division already happened in integers. - Signed meets unsigned:
-1 < 1uis 0 (false). The −1 is converted tounsigned int, where it becomes 4,294,967,295. - Overflow: unsigned arithmetic wraps around: storing 255 + 1 back in an
unsigned char(uc++oruc = uc + 1) gives 0, although the expressionuc + 1itself is theint256, becauseucis promoted tointfirst. Overflow of a signed integer (INT_MAX + 1) is undefined behaviour in C. Storing an out-of-range value in a signed type is implementation-defined (gcc turnschar c = 127; c++;into −128). - Java widens automatically but needs a cast to narrow:
int x = 3.5;does not compile. Java defines overflow:byte b = 127; b++;gives −128 andInteger.MAX_VALUE + 1gives −2147483648. - Python:
int(3.99)is 3,int(-3.99)is −3,7 / 2is always 3.5 (true division) andround(2.5)is 2 (ties round to even).
6. Input and output: printf and scanf
| Specifier | Used for |
|---|---|
%d, %i |
int (signed decimal) |
%u |
unsigned int |
%ld, %lld, %hd |
long, long long, short |
%c |
One character |
%s |
A string (char array ending in '\0') |
%f |
float or double in printf; float in scanf |
%lf |
double in scanf (also accepted by printf since C99) |
%Lf |
long double |
%e, %g |
%e scientific notation; %g uses %e or %f depending on the exponent and drops trailing zeros |
%x/%X, %o |
Hexadecimal, octal |
%p |
A pointer (address) |
%zu |
size_t, the type of sizeof (C99) |
%% |
A literal % sign |
Facts that exams ask:
%dand%iare the same in printf. In scanf they differ:%ialso reads octal and hex prefixes, so typing012gives 10 with%ibut 12 with%d, and0x1Agives 26 with%i.- scanf needs addresses:
scanf("%d", &x);. An array name is already an address, sochar name[10]; scanf("%s", name);takes no&. - printf returns the number of characters it printed, and scanf returns the number of items it read successfully (or
EOF). Soif (printf("0")) i = 5; else i = 3; printf("%d", i);prints 05: printf printed one character and returned 1 (true). - A number printed with
%cshows the character with that code:printf("%c", 100);prints d, and withint x = 107; char y = 'Q';,printf("%c%d", x, y);prints k81. - Width and precision:
%5d→42(right-aligned in 5 places),%-5d→42,%05d→00042,%.2fof 3.14159 →3.14,%8.3fof 3.14159 →3.142(rounded). getchar()/putchar()read and write one character.gets()was removed in C11 because it cannot limit input; usefgets().- Java prints with
System.out.println(...)orSystem.out.printf("%d%n", x)and reads withScanner(nextInt()); Python usesprint(...)andinput(), which always returns a string (int(input())for a number).
7. Increment and decrement
| Form | Effect | Value of the expression |
|---|---|---|
++x (prefix) |
x increases by 1 first | The new value |
x++ (postfix) |
x increases by 1 afterwards | The old value |
--x, x-- |
The same, decreasing | New / old value |
Trace int a = 5, b = a++ * 2, c = ++a * 2; (each initializer is finished before the next starts):
b = a++ * 2uses the old a (5), so b = 10; then a = 6.c = ++a * 2makes a = 7 first, so c = 14.
Result: a = 7, b = 10, c = 14. Other rules:
sizeofdoes not evaluate its operand.int i = 12; int j = sizeof(i++);leaves i at 12; printingi, jgives 12 4.++and--need a variable (an lvalue):5++and++(a + b)do not compile.c + 1 >= cdoes not change c;c++,c = c + 1andc += 1all do.- Java:
int C = 10; System.out.println(C--);prints 10 (the old value), then C is 9. Python has no++operator; writex += 1.
8. Find-the-output questions, and when there is no answer
Most questions in this chapter show a short program and ask for its output. Work in this order:
- Write down every variable with its starting value, and its type (watch for
char,unsignedandfloat). - Evaluate each expression by precedence, then associativity; do integer division and
%exactly. - Apply side effects (
++,--, assignments) at the right moment, and remember short-circuiting. - Copy the format string exactly: a space, a comma or a
\nin it appears in the output; a missing space joins numbers.
Worked example 3: exact spacing.
int i = 4;
printf("%d %d", +1, i++);
printf("%d", i++);
+1is just the number 1 (unary plus), andi++gives 4, then i becomes 5.- The first printf prints
1 4. - The second prints the old i, 5 (then i becomes 6). Its format has no leading space.
The exact output is 1 45; the keyed option "1 4 5" is that answer written with spaces.
When the C standard gives no answer. If a program modifies a variable twice, or modifies it and also reads it for any purpose other than computing the new value, and the two are unsequenced (no sequence point between them), its behaviour is undefined (C11 6.5p2): the standard allows any result, and different compilers (or optimisation levels) really do differ. i = i + 1 is fine; i = i++ and a[i] = i++ are not. gcc with -Wall warns: "operation on 'i' may be undefined".
| Code | Status in C | What to answer |
|---|---|---|
i++ + ++i |
Undefined behaviour | "Undefined". (In Java it is well defined: left to right, so with i = 5, i = i++ + ++i gives 12.) |
printf("%d%d", ++i, i++) |
Undefined (i changed twice among the arguments) | "Undefined". Keys that say 64… for i = 4 assumed the arguments were evaluated right to left. |
++z + y - 1 - y + z + x++ |
Undefined (z is changed and read separately) | With x = y = z = 5 (as in the paper), a keyed 16 assumes ++z happens before the later z is read (6 + 5 − 1 − 5 + 6 + 5); the standard does not promise this, and the other order gives 15. |
int i = i++; |
Undefined (reads an uninitialized i and modifies it) | "Garbage value / undefined" |
x = x++; and a[i] = i++; |
Undefined | (In Java, x = x++ leaves x unchanged.) |
f() + g() |
Unspecified, not undefined: either function may run first | "Cannot be predicted" (for example, which of func3(6) - func2(4,5) / func1(1,2,3) is called first) |
INT_MAX + 1 |
Undefined (signed overflow) | Unsigned arithmetic wraps; signed does not have to. |
In an MCQ, choose "undefined behaviour" or "compiler dependent" whenever it is an option. If it is not, the key almost always assumes one particular compiler's order, so pick the option that order produces and expect the question may be contested.
Two more traps from real papers:
print 9//2is Python 2 syntax. In Python 3 it is a SyntaxError (print is a function:print(9//2)prints 4). Keys usually give 4, reading the question as "what is 9//2".register static int i = 10;does not compile: a declaration may have only one storage-class specifier. And you cannot take the address (&) of aregistervariable.
Practice · অনুশীলন
- What is printed by: int x = 20, y = 10, z = 5; printf("%d", x > y > z);
Answer
- It is evaluated as (x > y) > z = 1 > 5, which is false (0) in C.
- Which of these are valid C identifiers: var_9, 9_var, _, No#of-students?
Answer
var_9 and _ are valid. 9_var starts with a digit, and No#of-students contains # and -, which are not allowed.
- In C and in Python, what are −7 / 2 (Python: −7 // 2) and −7 % 2?
Answer
C truncates toward zero: −7 / 2 = −3 and −7 % 2 = −1. Python floors: −7 // 2 = −4 and −7 % 2 = 1.
- What does printf("%d %i", 012, 0x1A); print?
Answer
10 26. 012 is an octal constant (8 + 2 = 10) and 0x1A is hexadecimal (16 + 10 = 26); %d and %i both print an int in decimal.
- int i = 12; int j = sizeof(i++); printf("%d %d", i, j); — what is printed on a system with 4-byte int?
Answer
12 4. sizeof only looks at the type of its operand and does not evaluate it, so i++ never runs.
Asked in past exams · আগের পরীক্ষায় যেভাবে এসেছে
Our past papers have 170 questions on this topic (90 MCQ, 80 written) from 92 exams. Here are 12 of them; each links to its full paper.
Which of the following operators cannot be overloaded in C/C++?
- a)Bitwise right shift assignment
- b)Address of
- c)Indirection
- d)Structure reference (correct answer)
Explanation · ব্যাখ্যা
Correct answer: (d) Structure reference
"Structure reference" is the name of the member-access (dot) operator
a.b, which selects memberbof objecta. C++ does not allow.to be overloaded; the other operators that cannot be overloaded are::(scope resolution),.*(member access through a pointer to member) and?:(conditional). Note that C has no operator overloading at all, so the question is really about C++.Do not confuse it with "structure dereference"
a->b, which can be overloaded (smart pointers do it).For example, a smart-pointer class can define
Account* operator->()so thatacct->balanceworks, but no class can change whatacct.balancemeans.Why the other options are wrong
- (a) Bitwise right shift assignment:
>>=, like every compound-assignment operator, can be overloaded, e.g.T& operator>>=(int n);. - (b) Address of: Unary
&can be overloaded, which is whystd::addressofexists to get an object's real address. - (c) Indirection: Unary
*is routinely overloaded by iterators and smart pointers, e.g.T& operator*();.
Tip: The ones you can never overload:
::..*?:.Sources: Operator overloading - cppreference.com; Operators in C and C++ - Wikipedia
The term '%i' is used for _____ data type too.
- a)char
- b)int (correct answer)
- c)none of the above
- d)float
Explanation · ব্যাখ্যা
Correct answer: (b) int
%iis the second conversion specifier for a signedint, alongside%d(hence "too" in the stem).- In
printf,%dand%iare identical: both print anintin decimal.printf("%i %d", 42, 42);prints42 42. - In
scanf, they differ.%dreads only decimal, while%idetects the base from the prefix: a leading0means octal and0xmeans hexadecimal. Run on the input010,%istores 8 but%dstores 10; on0x1A,%istores 26.
Why the other options are wrong
- (a) char: A single character uses
%c. - (c) none of the above: Wrong, because (b) int is correct.
- (d) float: A float uses
%f(or%e/%g).
Tip:
%d=%ifor printing; for reading,%ialso accepts octal (0…) and hex (0x…).Source: printf, fprintf, sprintf, snprintf - cppreference.com
Source: scanf, fscanf, sscanf - cppreference.comWhich is the correct output?
int i = 4; printf("%d %d", +1,i++); printf("%d", i++);- a)4 5 6
- b)5 7 8
- c)6 4 6
- d)1 4 5 (correct answer)
Explanation · ব্যাখ্যা
Correct answer: (d) 1 4 5
Start with i = 4.
printf("%d %d", +1, i++);+1is unary plus applied to 1, so it is just the constant 1.i++passes the old value 4, then i becomes 5.- This prints
1 4.
printf("%d", i++);passes 5, then i becomes 6. It prints5.
The full output is
1 45. The second printf adds no space before its number, and the option lists the three numbers as 1 4 5. A test run printed1 45.Each call modifies
ionly once, and a function call is a sequence point, so this code is well defined. It is not an undefined-behaviour puzzle.Why the other options are wrong
- (a) 4 5 6: This ignores the
+1argument and treats the values as already incremented. - (b) 5 7 8: i only ever takes the values 4, 5 and 6, so 7 and 8 cannot appear.
- (c) 6 4 6: This is the output of a different code,
printf("%d%d", ++i, i++). Here there is no++i.
Tip:
i++gives the old value, and+1is simply 1.In Java, which operator is used to create an object?
- a)class
- b)scanf
- c)print
- d)None (correct answer)
Explanation · ব্যাখ্যা
Correct answer: (d) None
In Java an object is created with the
newoperator, for exampleBox b = new Box();.newallocates memory for the object, runs the constructor named after it, and returns a reference to the new object. Sincenewis not among options (a) to (c), the answer is (d).Why the other options are wrong
- (a) class: A keyword used to declare (define) a class, not to create an instance of it.
- (b) scanf: A C library input function; it does not exist in Java (Java reads input with classes such as
Scanner). - (c) print: A method of
System.outthat writes text to the console; it creates nothing.
Tip: Object creation in Java =
new+ a constructor call, e.g.new ClassName().Which operator can be overloaded?
- a)++ (correct answer)
- b)::
- c). (dot)
- d)0
Explanation · ব্যাখ্যা
Correct answer: (a) ++
The increment operator can be overloaded for a class in both forms: prefix as
T& operator++()and postfix asT operator++(int)(the dummyintparameter marks the postfix form). In a test, a struct holdingv = 3definedN& operator++() { ++v; return *this; }; after++n,n.vwas 4.Why the other options are wrong
- (b)
::: The scope resolution operator is one of the operators C++ never allows to be overloaded. - (c)
.(dot): Member access is also on the cannot-be-overloaded list (with.*and?:). - (d) 0: This is an integer literal, not an operator, so it cannot be overloaded. (If this option was meant to be the function-call operator
(), note that()can be overloaded too, asoperator().)
Tip: Cannot overload:
::..*?:sizeof; almost everything else, including++,--,[],()and<<, can be.In Java, which operator is used to create an object?
- a)class
- b)scanf
- c)print
- d)new (correct answer)
Explanation · ব্যাখ্যা
Correct answer: (d) new
In Java an object is created with the
newoperator, for exampleBox b = new Box();.newallocates memory for the object, runs the constructor named after it, and returns a reference to the new object. Sincenewis not among options (a) to (c), the answer is (d).Why the other options are wrong
- (a) class: A keyword used to declare (define) a class, not to create an instance of it.
- (b) scanf: A C library input function; it does not exist in Java (Java reads input with classes such as
Scanner). - (c) print: A method of
System.outthat writes text to the console; it creates nothing.
Tip: Object creation in Java =
new+ a constructor call, e.g.new ClassName().In C, to retain the values of variables between function calls, we should use:
What is the output of the following C code?
#include<stdio.h> int main(){ int arr[5] = {1,2,3,4,5}; int *p = (arr+2); printf("%d", p[-1]); return 0; }In Java, which keyword prevents a method from being overridden?
Which C operator below has right-to-left associativity?
Find Output:
int fun(int *p) { *p = *p + 10; return *p; } int main() { int x = 5; cout << fun(&x); return 0; }Find Output:
int main() { int x=3, y=2; if (x==3) y=2; else y=3; cout<<x<<" "<<y<<endl; return 0; }
Common mistakes · সাধারণ ভুল
- Giving a number for code like i++ + ++i in C: it is undefined behaviour, so choose "undefined" when the options allow it.
- Treating 1.414 as a float: an unsuffixed floating constant is a double; write 1.414f for a float.
- Using %f to read a double with scanf: scanf needs %lf for double (printf accepts %f for both).
- Putting & before an array name in scanf("%s", name): the array name is already an address.
- Forgetting that integer division truncates: 7 / 2 is 3 and (double)(7 / 2) is 3.0, not 3.5.
- Assuming int is exactly 4 bytes by rule: the standard guarantees only 16 bits; 4 bytes is the usual answer for modern compilers.
বাংলায় সংক্ষেপে
- সি প্রোগ্রাম ছয় ধরনের টোকেনে গঠিত: কিওয়ার্ড, আইডেন্টিফায়ার, কনস্ট্যান্ট, স্ট্রিং লিটারেল, অপারেটর ও পাংচুয়েটর; C89-এ কিওয়ার্ড ৩২টি।
- আইডেন্টিফায়ার অক্ষর বা আন্ডারস্কোর দিয়ে শুরু হয়, এতে কেবল অক্ষর, অঙ্ক ও _ থাকে এবং বড়-ছোট হাতের অক্ষর আলাদা ধরা হয়।
- সাধারণ আকার: char ১, short ২, int ৪, float ৪, double ৮ বাইট; কেবল char-এর ১ বাইট স্ট্যান্ডার্ডে নির্দিষ্ট। n বিটের সাইনড রেঞ্জ −২ⁿ⁻¹ থেকে ২ⁿ⁻¹ − ১।
- পূর্ণসংখ্যার ভাগে ভগ্নাংশ বাদ দিয়ে ফল শূন্যের দিকে ছাঁটা হয় (৭ / ২ = ৩), % কেবল পূর্ণসংখ্যায় চলে, আর float থেকে int-এ রূপান্তরে ভগ্নাংশ বাদ যায়।
- *, /, % একই স্তরের এবং +, −-এর চেয়ে আগে; ইউনারি, ?: ও অ্যাসাইনমেন্ট অপারেটরের অ্যাসোসিয়েটিভিটি ডান থেকে বামে।
- printf-এ %d/%i int, %c char, %s স্ট্রিং, %f float/double; scanf-এ double-এর জন্য %lf এবং ভেরিয়েবলের আগে & লাগে।
- একই ভেরিয়েবল এক এক্সপ্রেশনে দুবার বদলালে (যেমন i++ + ++i) আচরণ আনডিফাইন্ড; এর নির্দিষ্ট কোনো আউটপুট নেই।
Sources
- ISO/IEC 9899:2011 (C11), committee draft N1570, 2011
- cppreference.com: C Operator Precedence
- cppreference.com: C arithmetic types
- cppreference.com: printf, fprintf, sprintf (C format specifiers)
- cppreference.com: Order of evaluation (C)
- The Java Language Specification, Java SE 8: Chapter 4, Types, Values and Variables, 2015
- The Java Language Specification, Java SE 8: §3.9 Keywords, 2015
- Python 3 documentation: Lexical analysis, Keywords
- Dennis M. Ritchie, The Development of the C Language, Bell Labs, 1993
Updated 2026-10-18. Written by ARBS Academy; the past exam questions are from public recruitment exams.