0% found this document useful (0 votes)
27 views14 pages

Class 1 PDF

Uploaded by

sadman.sakib4400
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

Class 1 PDF

Uploaded by

sadman.sakib4400
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Introduction of C Programming

What is C?

C is a general-purpose, procedural computer programming language developed in 1972 by


Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. It is a
widely used language that serves as the foundation for many other modern programming
languages. Known for its efficiency and control over system resources, C is still popular in
systems programming, embedded systems, and high-performance computing.

Key Features of C:

1. Procedural Language: Emphasizes the sequence of computational steps to be carried


out.
2. Rich Library: Provides a variety of built-in functions.
3. Efficient and Fast: Low-level access to memory and minimal runtime overhead.
4. Portability: Programs written in C can be compiled and run on many different machines.
5. Modularity: Supports structured programming through functions.
6. Low-level Manipulation: Allows manipulation of bits, bytes, and addresses, making it
ideal for system programming.

Basic Structure of a C Program:

A simple C program includes the following elements:

 Preprocessor Directives: Instructions processed before compilation (e.g., #include).


 main() Function: Entry point of every C program.
 Variable Declarations: Specifies variables used in the program.
 Statements and Expressions: Perform operations and computations.
 Comments: Enhance readability and maintainability.

Here is an example of a basic C program:

c
Copy code
#include <stdio.h> // Preprocessor directive to include the standard input-
output header file

int main() { // Main function where execution begins


printf("Hello, World!\n"); // Print "Hello, World!" to the console
return 0; // Return 0 to the operating system indicating successful
execution
}
Explanation of the Example:

 #include <stdio.h>: Includes the standard input-output library needed for using
printf.
 int main(): Declares the main function, the starting point of the program.
 printf("Hello, World!\n");: Calls the printf function to print "Hello, World!"
followed by a new line.
 return 0;: Ends the main function, returning 0 to indicate successful execution.

Compiling and Running a C Program:

1. Write the Code: Save your program in a file with a .c extension (e.g., hello.c).
2. Compile the Code: Use a compiler like gcc (GNU Compiler Collection) to compile the
code.

sh
Copy code
gcc hello.c -o hello

3. Run the Program: Execute the compiled program.

sh
Copy code
./hello

Conclusion: C remains a powerful and versatile language, offering a mix of high-level and
low-level functionality. Its influence on many other languages and its continued use in system-
level programming make it an essential language to learn for aspiring programmers.
Understanding C provides a strong foundation in programming concepts and helps in learning
other languages more easily.

In C programming, data types are essential for defining the nature and size of the data that can be
stored in variables. They help the compiler allocate the appropriate amount of memory and
interpret the data correctly. Here’s an overview of the primary data types in C:

Basic Data Types

1. Integer Types:
o int: Represents integer numbers. The size is typically 4 bytes, but it can vary
based on the system.

c
Copy code
int a = 10;
o short int: A shorter version of int, usually 2 bytes.

c
Copy code
short int b = 5;

o long int: A longer version of int, usually 4 or 8 bytes.

c
Copy code
long int c = 100000L;

o long long int: For even larger integers, typically 8 bytes.

c
Copy code
long long int d = 10000000000LL;

o unsigned int: Represents non-negative integers, the size is similar to int.

c
Copy code
unsigned int e = 50;

2. Floating-Point Types:
o float: Represents single-precision floating-point numbers, typically 4 bytes.

c
Copy code
float f = 3.14f;

o double: Represents double-precision floating-point numbers, typically 8 bytes.

c
Copy code
double g = 3.14159265359;

o long double: Extended precision floating-point numbers, usually more than 8


bytes.

c
Copy code
long double h = 3.141592653589793238L;

3. Character Type:
o char: Represents a single character, typically 1 byte.

c
Copy code
char i = 'A';
o unsigned char: Unsigned version of char.

c
Copy code
unsigned char j = 'B';

o signed char: Signed version of char.

c
Copy code
signed char k = 'C';

Derived Data Types

1. Arrays: Collection of elements of the same type.

c
Copy code
int arr[10]; // Array of 10 integers

2. Pointers: Variables that store the address of another variable.

c
Copy code
int *ptr; // Pointer to an integer

3. Structures: User-defined data types that group different types together.

c
Copy code
struct Person {
char name[50];
int age;
float salary;
};

4. Unions: Similar to structures but share the same memory location for all members.

c
Copy code
union Data {
int intVal;
float floatVal;
char charVal;
};
5. Enumerations: User-defined data type consisting of integral constants.

c
Copy code
enum Weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday};

Void Type

 void: Used for functions that do not return a value or to indicate an empty parameter list.

c
Copy code
void functionName(void) {
// Function code
}

Type Modifiers

Type modifiers change the size or range of the basic data types:

 signed: Default for int and char, allows both positive and negative values.
 unsigned: Only positive values.
 short: Shortens the size.
 long: Increases the size.

Example Program

Here is an example program illustrating different data types:

c
Copy code
#include <stdio.h>

int main() {
int a = 10; // Integer
float b = 3.14f; // Floating-point
double c = 3.14159265359; // Double precision floating-point
char d = 'A'; // Character
unsigned int e = 50; // Unsigned integer

printf("int: %d\n", a);


printf("float: %.2f\n", b);
printf("double: %.11f\n", c);
printf("char: %c\n", d);
printf("unsigned int: %u\n", e);

return 0;
}
Operators:

In C programming, operators are special symbols used to perform operations on variables and
values. The different types of operators in C can be categorized as follows:

1. Arithmetic Operators:
o Addition (+): Adds two operands.

c
Copy code
int a = 5 + 3; // a = 8

o Subtraction (-): Subtracts the second operand from the first.

c
Copy code
int a = 5 - 3; // a = 2

o Multiplication (*): Multiplies two operands.

c
Copy code
int a = 5 * 3; // a = 15

o Division (/): Divides the numerator by the denominator.

c
Copy code
int a = 10 / 2; // a = 5

o Modulus (%): Returns the remainder of the division.

c
Copy code
int a = 10 % 3; // a = 1

2. Relational Operators:
o Equal to (==): Checks if two operands are equal.

c
Copy code
if (a == b) { /* code */ }

o Not equal to (!=): Checks if two operands are not equal.

c
Copy code
if (a != b) { /* code */ }
o Greater than (>): Checks if the first operand is greater than the second.

c
Copy code
if (a > b) { /* code */ }

o Less than (<): Checks if the first operand is less than the second.

c
Copy code
if (a < b) { /* code */ }

o Greater than or equal to (>=): Checks if the first operand is greater than or equal
to the second.

c
Copy code
if (a >= b) { /* code */ }

o Less than or equal to (<=): Checks if the first operand is less than or equal to the
second.

c
Copy code
if (a <= b) { /* code */ }

3. Logical Operators:
o Logical AND (&&): Returns true if both operands are true.

c
Copy code
if (a && b) { /* code */ }

o Logical OR (||): Returns true if at least one of the operands is true.

c
Copy code
if (a || b) { /* code */ }

o Logical NOT (!): Returns true if the operand is false.

c
Copy code
if (!a) { /* code */ }
4. Bitwise Operators:
o Bitwise AND (&): Performs a bitwise AND operation.

c
Copy code
int a = 5 & 3; // a = 1 (0101 & 0011 = 0001)

o Bitwise OR (|): Performs a bitwise OR operation.

c
Copy code
int a = 5 | 3; // a = 7 (0101 | 0011 = 0111)

o Bitwise XOR (^): Performs a bitwise XOR operation.

c
Copy code
int a = 5 ^ 3; // a = 6 (0101 ^ 0011 = 0110)

o Bitwise NOT (~): Performs a bitwise NOT operation.

c
Copy code
int a = ~5; // a = -6 (inverts all bits)

o Left shift (<<): Shifts bits to the left.

c
Copy code
int a = 5 << 1; // a = 10 (0101 << 1 = 1010)

o Right shift (>>): Shifts bits to the right.

c
Copy code
int a = 5 >> 1; // a = 2 (0101 >> 1 = 0010)

5. Assignment Operators:
o Assignment (=): Assigns a value to a variable.

c
Copy code
int a = 5;

o Add and assign (+=): Adds and assigns the result.

c
Copy code
a += 3; // a = a + 3

o Subtract and assign (-=): Subtracts and assigns the result.

c
Copy code
a -= 3; // a = a - 3

o Multiply and assign (*=): Multiplies and assigns the result.

c
Copy code
a *= 3; // a = a * 3

o Divide and assign (/=): Divides and assigns the result.

c
Copy code
a /= 3; // a = a / 3

o Modulus and assign (%=): Computes the modulus and assigns the result.

c
Copy code
a %= 3; // a = a % 3

6. Increment and Decrement Operators:


o Increment (++): Increases an integer's value by one.

c
Copy code
a++; // equivalent to a = a + 1

o Decrement (--): Decreases an integer's value by one.

c
Copy code
a--; // equivalent to a = a - 1

7. Conditional (Ternary) Operator:


o Conditional (? :): Returns value based on the condition.

c
Copy code
int a = (x > y) ? x : y;

8. Comma Operator:
o Comma (,): Used to separate expressions.

c
Copy code
int a = (b = 3, b + 2); // b is set to 3, then a is set to b + 2
(a = 5)

9. Sizeof Operator:
o Sizeof (sizeof): Returns the size of a variable or datatype.

c
Copy code
int a = sizeof(int); // a = 4 (size of int)

10. Pointer Operators:


o Address-of (&): Returns the address of a variable.

c
Copy code
int a = 5;
int *p = &a; // p holds the address of a

o Dereference (*): Accesses the value at the address of a pointer.

c
Copy code
int a = 5;
int *p = &a;
int b = *p; // b is set to the value of a (b = 5)

11. Type Cast Operator:


o Type cast ((type)): Converts a variable from one type to another.

c
Copy code
int a = (int)3.14; // a = 3

Understanding and using these operators effectively is crucial for programming in C, as they
form the basis of most operations you perform on data.

Diffirent types of header file for use in c programme :

In C programming, header files serve as a way to declare functions, macros, constants, and data
types to be shared between different source files. There are various types of header files that can
be used in a C program. These can be categorized into standard library headers and user-defined
headers:

1. Standard Library Header Files

These headers are provided by the C standard library and are included using angle brackets <>.
They provide a range of functionalities, such as input/output operations, string manipulation,
mathematical computations, and more.
Here are some common standard library headers:

 <stdio.h>: Standard Input and Output functions (e.g., printf, scanf).


 <stdlib.h>: General utility functions (e.g., memory allocation, random numbers).
 <string.h>: String handling functions (e.g., strlen, strcpy).
 <math.h>: Mathematical functions (e.g., sqrt, pow).
 <ctype.h>: Character type functions (e.g., isdigit, toupper).
 <time.h>: Date and time functions.
 <assert.h>: Diagnostics (e.g., assert macro).
 <errno.h>: Macros reporting error conditions.
 <float.h>: Limits of float types.
 <limits.h>: Sizes of basic types.
 <stdint.h>: Fixed-width integer types.
 <stddef.h>: Common macro definitions and types (e.g., NULL, size_t).
 <stdbool.h>: Using bool, true, and false makes code easier to read and understand
compared to using integers.
 <conio.h>: Helps to reduce the extra data or junk in the output. For this reason we are
use getch(); in last line of the programme in the proxy of return 0;

2. User-Defined Header Files

These headers are created by the user to organize code into manageable modules. They are
included using double quotes "". User-defined headers usually contain declarations of functions,
macros, and types specific to a particular project.

For example, consider a program that has a custom math library:

mathlib.h

c
Copy code
#ifndef MATHLIB_H
#define MATHLIB_H

int add(int a, int b);


int subtract(int a, int b);
double multiply(double a, double b);
double divide(double a, double b);

#endif // MATHLIB_H

mathlib.c

c
Copy code
#include "mathlib.h"

int add(int a, int b) {


return a + b;
}

int subtract(int a, int b) {


return a - b;
}

double multiply(double a, double b) {


return a * b;
}

double divide(double a, double b) {


if (b != 0) {
return a / b;
} else {
// Handle division by zero
return 0; // or some error code
}
}

main.c

c
Copy code
#include <stdio.h>
#include "mathlib.h"

int main() {
int x = 5, y = 3;
printf("Add: %d\n", add(x, y));
printf("Subtract: %d\n", subtract(x, y));
printf("Multiply: %lf\n", multiply(x, y));
printf("Divide: %lf\n", divide(x, y));
return 0;
}

3. System-Specific Header Files

These headers provide access to system-level functionalities and are specific to the operating
system. Examples include:

 <unistd.h>: Provides access to the POSIX operating system API (e.g., read, write,
fork).
 <fcntl.h>: File control options (e.g., open, fcntl).
 <sys/types.h>: Data types used in system calls.
 <sys/stat.h>: Data returned by the stat function.
Summary

 Standard Library Headers: Provided by the C standard library (e.g., <stdio.h>,


<stdlib.h>).
 User-Defined Headers: Created by the user for specific project needs (e.g.,
"mathlib.h").
 System-Specific Headers: Provide access to system-level functionalities (e.g.,
<unistd.h>, <fcntl.h>).

Including the appropriate header files helps in modularizing code, promoting code reuse, and
maintaining readability and organization in C programs.

What is the use of \n in C programming

In C programming, the \n character is used to insert a newline into a string. When printed, it
moves the cursor to the beginning of the next line. This is useful for formatting output to make it
more readable.

Here's an example:

c
Copy code
#include <stdio.h>

int main() {
printf("Hello, World!\n");
printf("This is a new line.\n");
return 0;
}

Output:

Copy code
Hello, World!
This is a new line.

In this example, \n is used to move to a new line after printing "Hello, World!" and after "This is
a new line."

Additionally, \n can be used in strings to handle multi-line text or to control the format of output
in more complex programs.

Conclusion: Understanding data types in C is fundamental to writing efficient and effective


programs. They define the nature of the data that variables can hold and allow the compiler to
allocate the necessary memory and interpret the data correctly. Using the appropriate data types
ensures that your program can handle data accurately and perform operations efficiently.

You might also like