What is C Language?
C is a general-purpose programming language created by Dennis Ritchie in the early 1970s at Bell Labs. It is one of the most widely used and influential programming languages, known for its efficiency, portability, and flexibility. C is the foundation for many other languages such as C++, Java, and Python.
C is a procedural programming language, which means the program follows a step-by-step approach with instructions that define the flow of control, making it easier to understand and implement.
Common Keywords in C:
int
: Used to declare integer variables.float
: Used to declare floating-point variables.char
: Used to declare character variables.return
: Used to return a value from a function.if
,else
: Conditional statements to make decisions.while
,for
,do
: Looping statements for repeated execution.break
: Used to exit from a loop or switch.continue
: Skips the current iteration of a loop and proceeds to the next one.switch
,case
: Used for multi-way decision making.struct
: Defines a structure to group different data types.typedef
: Used to define new names for existing data types.void
: Specifies that a function does not return a value.static
: Used for static variables (retains value between function calls) or static functions.extern
: Used to declare variables or functions that are defined in another file.const
: Defines constant values that cannot be modified after initialization.sizeof
: Used to get the size (in bytes) of a variable or data type.
Operators in C
C uses various operators to perform operations on variables and values. These can be classified into different types:
Arithmetic Operators: For mathematical operations.
+
, -
, *
, /
, %
(Addition, Subtraction, Multiplication, Division, Modulus)==
, !=
, >
, <
, >=
, <=
(Equality, Not equal, Greater than, Less than, Greater than or equal, Less than or equal)Logical Operators: For logical operations.
&&
, ||
, !
(AND, OR, NOT)Bitwise Operators: For bit-level operations.
&
, |
, ^
, ~
, <<
, >>
(AND, OR, XOR, NOT, Left shift, Right shift)Assignment Operators: For assigning values to variables.
=
, +=
, -=
, *=
, /=
, etc.Increment/Decrement Operators: Used to increase or decrease a variable's value by 1.
++
, --
Ternary Operator: A shorthand for
if-else
statements.condition ? expr1 : expr2
Key Features of C:
- Low-level operations: C provides direct access to hardware and memory, making it ideal for systems programming.
- Portable: C programs can run on different machines with little or no modification.
- Efficient: It allows fine control over system resources, making it very fast and resource-efficient.
- Structured programming: C supports modular programming, where programs are divided into functions to perform specific tasks.
What Are Variables in C?
In C programming, a variable is a storage location with a name and a data type that holds a value. Variables are used to store data that can be used or modified during the execution of a program.
C provides several built-in data types to handle different types of data.
1. Primitive Data Types (also known as Basic Data Types)
These are the most fundamental data types in C. They are used to store single values, such as integers, floating-point numbers, and characters.
a) int (Integer)
- The
int
data type is used to store integer values (whole numbers) without decimal points. - Size: Typically 4 bytes (but this can vary depending on the system and compiler).
- Range: Varies depending on the system (commonly -32,768 to 32,767 for a 16-bit system, and -2,147,483,648 to 2,147,483,647 for a 32-bit system).
Example:
#include <stdio.h>
int main() {
int age = 25;
printf("Age: %d\n", age); // %d is used to print integers
return 0;
}
Output:
Age: 25
b) float (Floating-point Number)
- The
float
data type is used to store single-precision floating-point numbers (numbers with decimal points). - Size: Typically 4 bytes.
- Range: Approx. ±3.4E−38 to ±3.4E+38 (depending on the system and compiler).
Example:
#include <stdio.h>
int main() {
float height = 5.9;
printf("Height: %.1f\n", height); // %.1f is used to print a float with 1 decimal place
return 0;
}
Output:
Height: 5.9
c) double (Double-Precision Floating-Point Number)
- The
double
data type is used to store double-precision floating-point numbers, which offer more precision than thefloat
type. - Size: Typically 8 bytes.
- Range: Approx. ±1.7E−308 to ±1.7E+308 (depending on the system and compiler).
Example:
#include <stdio.h>
int main() {
double pi = 3.14159265359;
printf("Pi: %.5f\n", pi); // %.5f is used to print a double with 5 decimal places
return 0;
}
Output:
Pi: 3.14159
d) char (Character)
- The
char
data type is used to store single characters. - Size: Typically 1 byte.
- Range: The
char
data type can store 256 different values (usually from -128 to 127 in a signed char, or 0 to 255 in an unsigned char).
Example:
#include <stdio.h>
int main() {
char grade = 'A';
printf("Grade: %c\n", grade); // %c is used to print a character
return 0;
}
Output:
Grade: A
2. Derived Data Types
These data types are derived from the primitive data types. They allow you to store and organize more complex data.
a) Arrays
- An array is a collection of variables of the same type that are stored in contiguous memory locations.
- Arrays are indexed starting from 0.
Example:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // An array of integers
printf("First number: %d\n", numbers[0]); // Accessing the first element of the array
return 0;
}
Output:
First number: 10
b) Pointers
- A pointer is a variable that stores the memory address of another variable.
- Pointers allow direct manipulation of memory.
Example:
#include <stdio.h>
int main() {
int num = 100;
int *ptr = # // Pointer 'ptr' stores the memory address of 'num'
printf("Value of num: %d\n", *ptr); // Dereferencing the pointer to get the value of 'num'
return 0;
}
Output:
Value of num: 100
c) Structures
- A structure is a user-defined data type that allows you to group variables of different types together under a single name.
- Structures are useful for organizing related data.
Example:
#include <stdio.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person p1 = {"Alice", 30}; // Declare and initialize a structure variable
printf("Name: %s\n", p1.name);
printf("Age: %d\n", p1.age);
return 0;
}
Output:
Name: Alice
Age: 30
d) Unions
- A union is similar to a structure, but unlike a structure, a union allows storing different types of data in the same memory location. Only one member of a union can hold a value at a time.
Example:
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
data.i = 10; // Assign value to integer member
printf("Data as integer: %d\n", data.i);
data.f = 220.5; // Assign value to float member
printf("Data as float: %.2f\n", data.f);
data.str[0] = 'H'; // Assign value to string member
data.str[1] = 'i';
data.str[2] = '\0';
printf("Data as string: %s\n", data.str);
return 0;
}
Output:
Data as integer: 10
Data as float: 220.50
Data as string: Hi
Note: In a union, changing the value of one member affects the others because they all share the same memory location.
3. Enumerated Data Type (enum)
- An enum is a user-defined data type that consists of a set of named integer constants. It is used to assign meaningful names to integer values, making the code more readable.
Example:
#include <stdio.h>
enum Day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
int main() {
enum Day today;
today = Wednesday;
printf("Today is day number: %d\n", today); // 3 (since Wednesday is the 3rd day in the enum)
return 0;
}
Output:
Today is day number: 3