Class 1 PDF
Class 1 PDF
What is C?
Key Features of C:
c
Copy code
#include <stdio.h> // Preprocessor directive to include the standard input-
output header file
#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.
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
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:
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;
c
Copy code
long int c = 100000L;
c
Copy code
long long int d = 10000000000LL;
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;
c
Copy code
double g = 3.14159265359;
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';
c
Copy code
signed char k = 'C';
c
Copy code
int arr[10]; // Array of 10 integers
c
Copy code
int *ptr; // Pointer to an integer
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
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
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
c
Copy code
int a = 5 - 3; // a = 2
c
Copy code
int a = 5 * 3; // a = 15
c
Copy code
int a = 10 / 2; // a = 5
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 */ }
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 */ }
c
Copy code
if (a || b) { /* code */ }
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)
c
Copy code
int a = 5 | 3; // a = 7 (0101 | 0011 = 0111)
c
Copy code
int a = 5 ^ 3; // a = 6 (0101 ^ 0011 = 0110)
c
Copy code
int a = ~5; // a = -6 (inverts all bits)
c
Copy code
int a = 5 << 1; // a = 10 (0101 << 1 = 1010)
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;
c
Copy code
a += 3; // a = a + 3
c
Copy code
a -= 3; // a = a - 3
c
Copy code
a *= 3; // a = a * 3
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
c
Copy code
a++; // equivalent to a = a + 1
c
Copy code
a--; // equivalent to a = a - 1
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)
c
Copy code
int a = 5;
int *p = &a; // p holds the address of a
c
Copy code
int a = 5;
int *p = &a;
int b = *p; // b is set to the value of a (b = 5)
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.
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:
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:
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.
mathlib.h
c
Copy code
#ifndef MATHLIB_H
#define MATHLIB_H
#endif // MATHLIB_H
mathlib.c
c
Copy code
#include "mathlib.h"
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;
}
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
Including the appropriate header files helps in modularizing code, promoting code reuse, and
maintaining readability and organization in C programs.
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.