Unit - 2 Operators Notes BCA Semester 1
In C programming, expressions and operators play a crucial role in performing various tasks. The table below provides an overview of different operators and their associativity:
| Description | Operators | Associativity |
|----------------------|--------------|-----------------|
| Function Expression | () | Left to Right |
| Array Expression | [] | Left to Right |
| Structure Operator | -> | Left to Right |
| Structure Operator | . | Left to Right |
| Unary minus | - | Right to Left |
| Increment/Decrement | ++, -- | Right to Left |
| One’s compliment | ~ | Right to Left |
| Negation | ! | Right to Left |
| Address of | & | Right to Left |
| Value of address | * | Right to Left |
| Type cast | (type) | Right to Left |
| Size in bytes | sizeof | Right to Left |
| Multiplication | * | Left to Right |
| Division | / | Left to Right |
| Modulus | % | Left to Right |
| Addition | + | Left to Right |
| Subtraction | - | Left to Right |
| Left shift | << | Left to Right |
| Right shift | >> | Left to Right |
| Less than | < | Left to Right |
| Less than or equal to | <= | Left to Right |
| Greater than | > | Left to Right |
| Greater than or equal to | >= | Left to Right |
| Equal to | == | Left to Right |
| Not equal to | != | Left to Right |
| Bitwise AND | & | Left to Right |
| Bitwise exclusive OR | ^ | Left to Right |
| Bitwise inclusive OR | \| | Left to Right |
| Logical AND | && | Left to Right |
| Logical OR | \|\| | Left to Right |
| Conditional | ?: | Right to Left |
| Assignment | =, *=, /=, %=, +=, -=, &=, ^=, \|=, <<=, >>= | Right to Left |
| Comma | , | Right to Left |
Console-Based Input/Output and Related Functions
Input in C refers to providing the program with data, while output is the act of displaying data on the screen, writing it to a printer, or storing it in a file. C programming offers built-in functions for reading input and displaying output.
The standard input-output header file, stdio.h, includes definitions for two crucial functions: printf() and scanf(). The printf() function is used to display output, while scanf() is employed for taking input from the user.
```c
#include <stdio.h>
void main() {
int i;
// Displaying a message and prompting the user for input
printf("Please enter a value...");
// Reading the value entered by the user
scanf("%d", &i);
// Displaying the entered number as output
printf("\nYou entered: %d", i);
}
```
In the example above, %d within scanf() and printf() serves as a format specifier, indicating the expected input/output type.
Other important functions for input/output include getchar(), putchar(), gets(), and puts():
- `getchar()` reads a character from the terminal and returns it as an integer.
- `putchar()` displays the character passed to it on the screen and returns the same character.
- `gets()` reads a line from standard input into the specified buffer until a newline or EOF is encountered.
- `puts()` writes the string and a trailing newline to standard output.
```c
#include <stdio.h>
void main() {
int c;
// Taking a character as input
printf("Enter a character");
c = getchar();
// Displaying the entered character
putchar(c);
}
```
Additionally, there are differences between `scanf()` and `gets()` in terms of how they handle input, particularly spaces. `scanf()` stops reading at spaces, while `gets()` considers spaces as characters.
Header Files in C
Header files in C contain function and variable definitions, which are imported into a program using the preprocessor `#include` statement. They have a ".h" extension and contain declarations for a specific group of functions. There are two types of header files in C:
1. System Header Files: These come with the compiler.
2. User Header Files: These are written by programmers.
Header files are essential when using functions from C libraries, allowing the compiler to recognize and include the necessary declarations. For example, `#include <stdio.h>` includes the standard input-output functions.
```c
#include <stdio.h>
void main() {
// Code using functions from the stdio.h header file
}
```
Preprocessor Directives
The C preprocessor is responsible for transforming the program before actual compilation. It processes directives starting with `#`. Common preprocessor directives include:
1. `#include`: Used to include files.
2. `#define`: Defines macros.
3. `#undef`: Undefines macros.
4. `#ifdef`: Checks if a macro is defined.
5. `#ifndef`: Checks if a macro is not defined.
6. `#if`: Conditional compilation.
7. `#else`, `#elif`, `#endif`: Conditional compilation alternatives.
8. `#error`: Generates an error during preprocessing.
9. `#pragma`: Provides instructions to the compiler.
```c
#include <stdio.h>
#define PI 3.1415
void main() {
// Code using #include and #define
printf("%f", PI);
}
```
No comments:
Post a Comment