Unit-1- Programming in C
Unit-1- Programming in C
1. C Character Set
2. Identifiers
Identifiers are names used to identify variables, functions, arrays, etc. They must
follow these rules:
Examples:
3. Keywords
5. Operators
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, <, >, <=, >=
Logical Operators: &&, ||, !
Bitwise Operators: &, |, ^, ~, <<, >>
Assignment Operators: =, +=, -=, *=, /=, %=
Increment/Decrement Operators: ++, --
Conditional (Ternary) Operator: ? :
Special Operators: sizeof, &, * (pointer), ->, .
1. Arithmetic Operators
Example:
int a = 10, b = 3;
printf("Addition: %d\n", a + b); // Output: 13
printf("Modulus: %d\n", a % b); // Output: 1
2. Relational Operators
Example:
int x = 5, y = 10;
printf("%d\n", x < y); // Output: 1 (True)
printf("%d\n", x == y); // Output: 0 (False)
3. Logical Operators
Example:
int a = 1, b = 0;
printf("%d\n", a && b); // Output: 0 (False)
printf("%d\n", !a); // Output: 0 (False)
4. Bitwise Operators
Operate on bits:
& (AND), | (OR), ^ (XOR), ~ (NOT), << (Left Shift), >> (Right Shift).
Example:
5. Assignment Operators
Assign values:
int a = 10;
a += 5; // Equivalent to a = a + 5
printf("%d\n", a); // Output: 15
6. Increment/Decrement Operators
Modify a variable by 1:
++ (Increment), -- (Decrement).
Example:
int x = 5;
printf("%d\n", ++x); // Output: 6 (Pre-increment)
printf("%d\n", x--); // Output: 6 (Post-decrement)
Example:
8. Special Operators
Example:
int x = 5;
printf("Size of x: %lu\n", sizeof(x)); // Output: 4 (size in bytes on most systems)
Input Functions:
The scanf function is used to read formatted input from the standard input
(typically the keyboard). It allows the user to input values into variables during
program execution.
int num;
scanf("%d", &num);
&num ensures that the value entered is stored in the variable num.
When executed, the program will wait for the user to type an integer, which will
then be assigned to num.
Syntax:
scanf("format_specifier", &variable);
o scanf: Reads formatted input.
int x;
scanf("%d", &x);
int num = 5;
In this case, %d is a placeholder for an integer value, and the output will be:
The value is 5
Other common format specifiers include %f for floats, %c for characters, and %s
for strings. The printf function is highly versatile, enabling detailed control over
output formatting.
%d for integers
%f for floats
%c for characters
%s for strings
. Preprocessor Directives
3. Conditional Compilation:
o Enables or disables certain parts of the code during compilation based
on conditions.
o Example:
#ifdef DEBUG
printf("Debugging Mode\n"); // Only included if DEBUG is defined
#endif
Preprocessor directives do not produce any executable code; they are instructions
to the compiler to prepare the source code before actual compilation. This makes
them a powerful tool for modular, maintainable, and conditional programming.
Preprocessor directives are commands that are processed before the compilation of
the program. They begin with #.
Define Macros:
#define PI 3.14
Conditional Compilation:
#ifdef DEBUG
printf("Debugging Mode\n");
#endif
8. Storage Classes
Storage in C programming refers to the way variables are allocated, managed, and
maintained in memory. The storage class of a variable defines several key
properties:
Each storage class serves different purposes, allowing efficient memory and
performance management in programs.
count++;
int main() {
increment();
increment();
increment();
return 0;
Summary
Definition: Character sets in C programming refer to the set of characters that can be
used to write the programs. This includes letters, digits, symbols, and control characters.
Examples: A-Z, a-z, 0-9, special characters like @, #, $, etc.
2. Identifiers
Definition: Identifiers are names given to various program elements such as variables,
functions, arrays, etc.
Rules: Must begin with a letter or an underscore (_) followed by letters, digits, or
underscores. They are case-sensitive.
Examples: main, total_sum, _index
3. Keywords
Definition: Keywords are reserved words in C that have special meaning and cannot be
used as identifiers.
Examples: int, return, if, else, while, for, break, continue
4. Data Types
Definition: Data types specify the type of data that a variable can hold.
Categories:
o Basic Data Types: int, char, float, double
o Derived Data Types: Arrays, Pointers, Structures, Unions
o Enumeration Data Type: enum
o Void Data Type: void
5. Operators
Definition: Operators are symbols that perform operations on variables and values.
Types:
o Arithmetic Operators: +, -, *, /, %
o Relational Operators: ==, !=, >, <, >=, <=
o Logical Operators: &&, ||, !
o Bitwise Operators: &, |, ^, ~, <<, >>
o Assignment Operators: =, +=, -=, *=, /=, %=
o Increment/Decrement Operators: ++, --
o Conditional (Ternary) Operator: ?:
6. Input/Output Functions
Definition: Input/Output functions are used to get input from the user and display output
to the user.
Functions:
o Input Functions: scanf(), getchar(), gets()
o Output Functions: printf(), putchar(), puts()
7. Preprocessor Directives
Definition: Preprocessor directives are commands that are processed before the actual
compilation of code begins.
Examples: #include, #define, #if, #else, #endif, #ifdef, #ifndef
8. Storage Classes
Definition: Storage classes define the scope, visibility, and lifetime of variables/functions
within a C program.
Types:
o Automatic (auto): Default storage class for local variables.
o Register (register): Suggests that the variable be stored in a CPU register.
o Static (static): Preserves the value of a variable across function calls.
o External (extern): Allows variables/functions to be accessed across multiple
files.