![]() |
VOOZH | about |
C constructs usually mean the basic building blocks of the C language things like variables, data types, operators, loops, conditionals, functions, arrays, pointers, etc.
Declaration
Definition
// This is only declaration. y is not allocated memory by this statement
extern int y;// This is both declaration and definition, memory to x is allocated by this statement.
int x;
Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically (or statically) scoped. See this for more details.
See print “Geeks for Geeks” without using a semicolon for answer.
main is necessary for executing the code. Code is
C support only 2 loops:
Semi-colon is needed by the compiler and as the name suggests Preprocessors are programs that process our source code before compilation. Therefore the semi-colon is not required.
If a header file is included within < > then the compiler searches for the particular header file only within the built-in include path. If a header file is included within " ", then the compiler searches for the particular header file first in the current working directory, if not found then in the built-in include path.
Let’s take an example to understand it.
Suppose : double a=1.5; int b=10 and we want to calculate a+b
By type demotion, float type a will convert to int. Therefore a=1 and a+b=1+10=11 but we know that correct answer is 11.5 which will only get by type promotion. So the conclusion is that by type demotion we will not get the correct answer.
| #include | import |
|---|---|
| #include is a statement not a keyword. | While import is a keyword. |
| It is processed by pre-processor software. | It is processed by compiler. |
| It increases the size of the code. | It doesn't increases the size of the code. Here, even if we write import java.lang.*; it will not attach all the class. Rather it will give permission to access the class of java.lang |
A local static variable is a variable whose lifetime doesn't end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables. Static variables can be used to count the number of times a function is called. Also, static variables get the default value as 0. For example, the following program prints "0 1"
C is a procedural language. The main features of C language include low-level access to memory, simple set of keywords, and clean style. These features make it suitable for system programming like operating system or compiler development.
1) The expression 'i++' returns the old value and then increments i. The expression ++i increments the value and returns new value.
2) Precedence of postfix ++ is higher than that of prefix ++.
3) Associativity of postfix ++ is left to right and associativity of prefix ++ is right to left.
4) In C++, ++i can be used as l-value, but i++ cannot be. In C, they both cannot be used as l-value.
See Difference between ++*p, *p++ and *++p for more details.
l-value or location value refers to an expression that can be used on left side of assignment operator. For example in expression "a = 3", a is l-value and 3 is r-value.
l-values are of two types:
Refer lvalue and rvalue in C language for details.
We can use recursion for this purpose.
The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler.
Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time. See Understanding “volatile” qualifier in C for more details.
Yes, the const means that the variable cannot be assigned a new value. The value can be changed by other code or pointer. For example the following program works fine.
Local variables are declared inside a block or function but global variables are declared outside the block or function to be accessed globally.
Local Variables | Global Variables |
|---|---|
| Declared inside a block or a function. | Variables that are declared outside the block or a function. |
| By default, variables store a garbage value. | By default value of the global value is zero. |
| The life of the local variables is destroyed after the block or a function. | The life of the global variable exists until the program is executed. |
| Variables are stored inside the stack unless they are specified by the programmer. | The storage location of the global variable is decided by the compiler. |
| To access the local variables in other functions parameter passing is required. | No parameter passing is required. They are globally visible throughout the program. |
when a local variable has the same name as a global variable, then:
There can be only two things when there is an infinite loop in the program.
Below is the program for infinite loop in C:
Here, In for loop there is no break statement & In while and do-while loop there is no statement that is making the condition false.
Such loops will keep running forever unless externally stopped (e.g., program termination). In competitive programming environments, this often results in a “Time Limit Exceeded (TLE)” error, but in normal C execution, the program just hangs.
For more information, refer to the article - Loops in C
In C programming, type conversion refers to converting a value from one data type to another. This can be done either implicitly by the compiler or explicitly by the programmer.
Implicit Type Conversion occurs automatically when a value of a smaller data type is assigned to a larger data type (e.g., int to float).
Explicit Type Casting is done manually by the programmer to convert a value from one data type to another using the cast operator.
For more information, refer to the article - Type Casting and Type Conversion
Mixing them up can cause unexpected results and is a common beginner mistake.
For Example: Let's look at a code that uses "goto" (it's a keyword) as a variable
Output: This code will throw the following error:
ERROR!
Traceback (most recent call last):
File "<main.py>", line 3
int main() {
^^^^
SyntaxError: invalid syntax
The code is throwing "SyntaxError" because a keyword is used as variable.
Switch statement compares the value inside switch() to each case value. Execution starts from the first matching case and continues until a break is found. Without break, fall-through occurs (following cases also run).
For example, the following program executes all cases (including default) after case 2.
Size of the memory block (e.g., int = 4 bytes)
Type of value (integer, float, char, etc.)
Conditions involve ranges (if (x > 10 && x < 20))
Conditions involve logical operations
Switch-case is preferred when checking against multiple constant values (e.g., menu options).
C provides several categories of operators:
Example:
Differences from if-else:
Both break and continue are used to control the flow inside loops, but they do so differently:
Example with break:
Example with continue:
The size of a data type tells how much memory it occupies. It affects performance, memory usage, and data range.
Common sizes (may vary by system):
char: 1 byteint: 4 bytesfloat: 4 bytesdouble: 8 bytesizeof operator in C?The sizeof operator returns the size (in bytes) of a variable or data type at compile time.
Why it's important:
Notes:
sizeof is evaluated at compile time, not run time.
Can also be used with arrays to determine their total memory usage
char variable to store small numbers?char is essentially a 1-byte integer (8 bits).