थोडक्यात सारांश
C is a general-purpose, structured programming language developed by Dennis Ritchie at Bell Laboratories in 1972. It is fast, portable and close to the hardware, so it is used for operating systems and embedded systems. This topic covers the structure of a C program, character set, tokens, data types, variables, constants, operators, and input/output functions.
महत्त्वाचे मुद्दे
- C was developed to write the UNIX operating system. It is called a middle-level language (has high-level features and low-level access).
- Structure of a C program: documentation (comments) → preprocessor directives (#include) → global declarations → main() function → user-defined functions.
- Every C program starts execution from main(). Statements end with a semicolon (;).
- Tokens are the smallest units: keywords, identifiers, constants, strings, operators and special symbols.
- C has 32 keywords in C89 (int, float, if, else, while, return, etc.). Keywords cannot be used as variable names.
- Identifier rules: letters, digits and underscore only; must not start with a digit; case-sensitive; no keywords.
- Basic data types: int, float, double, char, void. Sizes depend on the compiler; on common 32/64-bit systems int is 4 bytes, char 1 byte, float 4 bytes, double 8 bytes.
- Format specifiers: %d (int), %f (float), %lf (double), %c (char), %s (string).
- Operators: arithmetic (+ − * / %), relational (< > <= >= == !=), logical (&& || !), assignment (= += −=), increment/decrement (++ −−), conditional (?:), bitwise (& | ^ ~ << >>), sizeof.
- Integer division drops the fraction: 7/2 gives 3, and 7 % 2 gives 1.
- Type conversion: implicit (automatic, smaller to larger type) and explicit (type casting), e.g. (float)7/2 gives 3.5.
- Input/output: printf() and scanf() (in stdio.h); getchar() and putchar() for single characters. scanf needs & before ordinary variables.
महत्त्वाच्या संज्ञा
- Preprocessor directive – line starting with # processed before compilation, e.g. #include, #define.
- Variable (चल) – named memory location whose value can change.
- Constant – value that does not change; e.g. #define PI 3.14 or const int N = 10;
- Header file – file with declarations of library functions, e.g. stdio.h, math.h.
- Operator precedence – order in which operators are evaluated (*, /, % before +, −).
सूत्रे
A simple C program:
- Precedence (high to low, main ones): () → ++ −− ! (unary) → * / % → + − → < <= > >= → == != → && → || → ?: → = += −=
- Pre-increment ++a increases first, then uses; post-increment a++ uses first, then increases.
५ महत्त्वाचे प्रश्न
Q1. What are tokens in C? Name the types.
Tokens are the smallest individual units of a C program. Types: keywords, identifiers, constants, strings, operators and special symbols (like { } ; ,).
Q2. Is the statement b = a++ + ++a; safe to use in C? Explain.
This expression modifies a twice without a sequence point, so the C standard calls it undefined behaviour; different compilers can give different answers. Good programs never write such expressions. Use separate statements instead.
Q3. Evaluate: int x = 10, y = 3; find x / y, x % y, (float)x / y and x > y && y > 5.
- x / y = 3 (integer division)
- x % y = 1 (remainder)
- (float)x / y = 3.333333 (x converted to float first)
- x > y is 1 (true), y > 5 is 0 (false); 1 && 0 = 0
- Result of logical expression = 0
Q4. Evaluate step by step: int r = 2 + 3 * 4 - 8 / 2 % 3;
- * , / and % have the same precedence, evaluated left to right: 3 * 4 = 12
- 8 / 2 = 4, then 4 % 3 = 1
- Expression becomes 2 + 12 − 1
- r = 13
Q5. Explain the structure of a C program with an example.
- Documentation section: comments /* … */ about the program.
- Link section: #include <stdio.h> (header file in angle brackets).
- Definition section: #define PI 3.14.
- Global declaration section.
- main() function: declarations and executable statements inside { }.
- Subprogram section: user-defined functions.
- Give the sum-of-two-numbers program as an example.
परीक्षेसाठी टीप
Practise operator precedence questions and write output carefully, including spaces and newlines (\n). Remember scanf needs & for int, float and char variables.