Introduction
C is a high-level programming language that was developed in the early 1970s by Dennis
Ritchie. It is widely used for system and application software. The key features of C include
simplicity, portability, and efficiency.
Example:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
Questions:
1. Who developed the C programming language?
2. Write a simple C program to print "Hello, World!".
Structure of C Language
A C program consists of functions and declarations. Every C program must have a `main`
function.
Example:
```c
#include <stdio.h>
int main() {
// Your code here
return 0;
```
Questions:
1. What is the purpose of the `main` function in a C program?
2. Explain the structure of a basic C program.
Identifiers
Identifiers are names given to various program elements such as variables, functions, and
arrays.
Example:
```c
int age;
float salary;
```
Questions:
1. What are identifiers in C?
2. Name two elements in a C program that can have identifiers.
Data Types & Variables
Data types specify the type of data that a variable can hold. C supports several data types
like `int`, `float`, `char`, and `double`.
int
- Definition: The `int` data type is used to store integers, which are whole numbers without
any fractional part.
- Range: Typically, the range of `int` is from -2,147,483,648 to 2,147,483,647 (32-bit
system).
- Example:
```c
int age = 25;
int year = 2025;
```
- Questions:
1. What kind of numbers can be stored in the `int` data type?
2. Declare an integer variable named `score` with a value of 100.
float
- Definition: The `float` data type is used to store floating-point numbers, which are
numbers with a fractional part.
- Range: Typically, the range of `float` is approximately 1.2E-38 to 3.4E+38.
- Example:
```c
float temperature = 36.6;
float pi = 3.14;
```
- Questions:
1. What kind of numbers can be stored in the `float` data type?
2. Declare a float variable named `height` with a value of 5.9.
char
- Definition: The `char` data type is used to store single characters.
- Range: The range of `char` is typically from -128 to 127 or 0 to 255 (8-bit system).
- Example:
```c
char grade = 'A';
char initial = 'J';
```
- Questions:
1. What does the `char` data type store?
2. Declare a character variable named `letter` with a value of 'B'.
double
- Definition: The `double` data type is used to store double-precision floating-point
numbers. It provides more precision than the `float` data type.
- Range: Typically, the range of `double` is approximately 2.3E-308 to 1.7E+308.
- Example:
```c
double largeNumber = 1.23456789012345;
double precisePi = 3.141592653589793;
```
- Questions:
1. How is the `double` data type different from the `float` data type?
2. Declare a double variable named `distance` with a value of 12345.6789.
void
- Definition: The `void` data type represents the absence of any data type. It is used
primarily for functions that do not return a value.
- Example:
```c
void displayMessage() {
printf("Hello, world!");
```
- Questions:
1. What is the use of the `void` data type in C?
2. Write a function using `void` that prints "Goodbye, world!".
Constants & Operators
Constants are fixed values that do not change during the execution of a program. Operators
are symbols that perform operations on variables and values.
Example:
```c
const int DAYSINWEEK = 7;
int result = 5 + 3;
```
Questions:
1. What are constants in C? Give an example.
2. What is an operator? Provide an example of an arithmetic operator.
### Working with Conditional & Looping Statements
Conditional statements like `if`, `else` control the flow of execution based on conditions.
Looping statements like `for`, `while` repeat a block of code.
Example:
```c
// Conditional statement
if (age > 18) {
printf("Adult\n");
// Looping statement
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
```
Questions:
1. Explain the use of `if` statements in C.
2. Write a `for` loop to print numbers from 1 to 5.
Switch Statement
The `switch` statement allows a variable to be tested for equality against a list of values.
Example:
```c
int day = 3;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid day");
```
Questions:
1. How does a `switch` statement work in C?
2. Write a `switch` statement to print the name of the days of the week.
Strings
- Definition: A string is a sequence of characters terminated by a null character `'\0'`. In C,
strings are actually arrays of characters.
- Example:
```c
char name[] = "Alice";
char greeting[20] = "Hello, World!";
```
- Questions:
1. What is a string in C?
2. How is a string different from a character array?
Arrays
- Definition: An array is a collection of elements of the same type placed in contiguous
memory locations. It can store a fixed-size sequential collection of elements.
- Example:
```c
int numbers[5] = {1, 2, 3, 4, 5};
char vowels[5] = {'a', 'e', 'i', 'o', 'u'};
```
- Questions:
1. What is an array in C?
2. Write an array to store the first 5 prime numbers.