IT Solution Menu

N O T I C E

Project on Computer topics, Programs for practicals for BCA Students, and Mobile Android app requirements. Please pay less and get more immediate delivery for school and higher level. Thank You. Contact me - at +91-8800304018(w)

Saturday, July 13, 2024

Operators in C by Md. Farrukh Asif

 


Operators in C:

o   Arithmetic Operators:

o   Relational Operators:

o   Logical Operators:

o   Assignment Operators:

o   Increment and Decrement Operators:

o   Bitwise Operators:

o   Conditional (Ternary) Operator:

o   Comma Operator:

o   ‘C’  Programming Examples:

ü C Input/Output

C Language:

C for the beginner 's by Md Farrukh Asif

Operators in C by Md. Farrukh Asif

C's Control Flow by Md. Farrukh Asif

String handling in C by Md Farrukh Asif

The Function of C Language by Md. Farrukh Asif

C for beginner's programming with output. Course Code – 106 [‘C’]) TMBU,BGP

BCA SOLVED EXAMINATION QUESTIONS WITH ANSWERS Course Code – 106 [‘C’]) TMBU,BGP

C input/output (I/O) is an essential part of programming that allows you to read data from and write data to various sources, such as the console, files, and other devices. In C, I/O operations are typically performed using functions from the standard I/O library, which includes functions like printf, scanf, fopen, fclose, and more.

Here's an overview of some common input/output operations in C:

1. Standard Input and Output (stdin and stdout):

·         printf: Used to print formatted output to the standard output (usually the console).

·         scanf: Used to read formatted input from the standard input (usually the keyboard).

·         putchar and putc: Used to write a character to the standard output.

·         getchar and getc: Used to read a character from the standard input.

2. File I/O:

·         fopen: Used to open a file for reading or writing.

·         fclose: Used to close a file.

·         fscanf and fprintf: Used for reading and writing formatted data to/from a file.

·         fgets and fputs: Used for reading and writing strings to/from a file.

3. Error Handling:

·         perror: Used to print a system error message based on the errno variable.

4. Formatted Output:

·         printf and fprintf allow you to format the output using format specifiers like %d, %f, %s, etc.

·         sprintf is used to format a string and store it in a character array.

C  I/O functions provide a wide range of options for reading and writing data. When using file I/O, make sure to handle errors properly, check for successful file openings, and close files when you're done to prevent data loss or file corruption.

Remember to include the necessary header file

#include <stdio.h>  to use these I/O functions in your C programs.

ü C Operators

In C, operators are symbols or special keywords that are used to perform various operations on data. C operators can be broadly categorized into the following groups:

Arithmetic Operators:

·         + (Addition)

·         - (Subtraction)

·         x (Multiplication)

·         / (Division)

·         % (Modulo, or remainder)

Relational Operators:

·         == (Equal to)

·         != (Not equal to)

·         < (Less than)

·         > (Greater than)

·         <= (Less than or equal to)

·         >= (Greater than or equal to)

Logical Operators:

·         && (Logical AND)

·         || (Logical OR)

·         ! (Logical NOT)

Assignment Operators:

·         = (Assignment)

·         += (Add and assign)      a=a+10;      a+=10;

·         -= (Subtract and assign)

·         *= (Multiply and assign)

·         /= (Divide and assign)

·         %= (Modulo and assign)

Increment and Decrement Operators:

·         ‘++’ (Increment by 1)      a=a+1; a++;

·         ‘- -' (Decrement by 1)

Bitwise Operators:

·         & (Bitwise AND)

·         | (Bitwise OR)

·         ^ (Bitwise XOR)

·         ~ (Bitwise NOT)

·         << (Left shift)

·         >> (Right shift)

Conditional (Ternary) Operator:

·         ? : (Conditional operator)

Example:

int a = 10, b = 5;

int max = (a > b) ? a : b;                         // max will be 10

Comma Operator:

·         , (Comma operator)

These are the most commonly used operators in C. Understanding how to use these operators is fundamental to writing C programs, as they allow you to manipulate data and control program flow.

Top of Form

ü  ‘C’  Programming Examples

Certainly! Here are some simple C programming examples to illustrate various concepts and common tasks:

1. Hello World:

The classic "Hello, World!" program.

 #include <stdio.h>

int main()

{ printf("Hello, World!\n");

return 0;

}

2. Input  and Output:

·         Read and print an integer.

 #include <stdio.h>

int main()

{ int num;

printf("Enter an integer: ");

scanf("%d", &num);

printf("You entered: %d\n", num);

 return 0;

}

3. Conditionals (If-Else):

·         Check if a number is even or odd.

 #include <stdio.h>

int main()

{ int num;

printf("Enter an integer: ");

scanf("%d", &num);

if (num % 2 == 0)

{ printf("%d is even.\n", num); }

else

{ printf("%d is odd.\n", num); }

return 0; }

 4. Loops (For Loop):

·         Print numbers from 1 to 10.

 #include <stdio.h>

 int main() {

for (int i = 1; i <= 10; i++)

{ printf("%d ", i); }

printf("\n");

return 0;

}

5. Arrays:

·         Find the sum of elements in an array.

 #include <stdio.h>

 int main()

{

int numbers[] = {1, 2, 3, 4, 5};

int sum = 0;

for (int i = 0; i < 5; i++)

{ sum += numbers[i]; }

printf("Sum of elements: %d\n", sum);

return 0; }

6. Functions:

·         Create a function to calculate the factorial of a number.

#include <stdio.h>

 int factorial(int n)

{ if (n == 0)

{ return 1; }

return n * factorial(n - 1); }

int main()

{ int num;

printf("Enter a number: ");

scanf("%d", &num);

 int result = factorial(num);

printf("Factorial of %d is %d\n", num, result);

return 0;

}

 7. Structures:

·         Define a structure to represent a point in 2D space.

 #include <stdio.h>

struct Point { int x; int y; };

int main()

{ struct Point p;

p.x = 3; p.y = 5;

printf("Coordinates: (%d, %d)\n", p.x, p.y);

return 0;

 }

These examples cover some fundamental aspects of C programming. You will learn later how to create a new program and complex program .

*** See You Again ***

************************

Share and Subscribe with Like

************************





No comments:

Post a Comment

Top Popular Post