These are some notes for the Programming in C that will help you to know about the fundamentals of the C language.
Here is the simple guide to understand C language -
- What is C ?
Yes, C is a programming language. It is a high level, compiled and well structure language. You can say that it is the father f all the mordern programming language as it is the basic of all the language.\
- 🕰️ History
A long time ago a man named Dennis Ritchie has got an idea to develop a language in early 1970s at the bell laboratories. so, he created it and named C.
In professional way we can say
The C programming language was developed by Dennis Ritchie in the early 1970s at Bell Labs. It was initially created to develop the Unix operating system, which was originally written in assembly language. C was designed to provide a balance between high-level language features and low-level programming capabilities, making it ideal for system-level programming.
now let's take a look to its structure:
#include <stdio.h>
int main() {
// Prints "Hello, World!" to the console
printf("Hello, World!\n");
return 0;
}
In above the first line shows the import- this line tells that we have just rendered a header file into our file.
Note: header file is a file that contains some predefined functions, macros, pointers etc..
then the main function comes in which all the code is return. In C all the file is one function.
In Main function there are some more code that we learn later.\
Now you know some points of C language. So let's dive deeper into It.
topics....
✏️ Phases of program developement.
When we develop a program we have to break it into various parts to make work simple so anyone can easily understand and work on the project. Program developement contains various phases depending upon the program. The basic phases are as follow:
-
Problem Definition: Clearly define the problem you're trying to solve. Understand the requirements and expected output.
-
Algorithm Design: Develop a step-by-step procedure (algorithm) to solve the problem.
-
Flowchart Creation: Represent the algorithm using a flowchart to visualize the flow of execution.
-
Writing the Program (Coding): Implement the algorithm in a programming language like C.
-
Compilation: Convert the C program into machine code using a compiler.
-
Testing and Debugging: Run the program to check for errors (bugs) and ensure it meets the problem's requirements.
-
Delivery: Deliver the final result and fulfil the requirements of the customer.
-
Documentation and Maintenance: Document the program for future reference and update it as necessary.
Algorithm development -
Algorithm, blueprint of a program. An Alogrithm is a set of instruction used to solve a specific problem and can be used as an blueprint{plan} of an program.For ex..
Algorithm 1: Add two numbers entered by the user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
this is a basic example of a alogorithm.
Flowchart
This awesome thing in the programming. This is same do the same work as the algorithm do but it is different because it is the visual representation of an algorithm. For ex.. there are some rules of boxes that everyone have to follow.Some basic of them are as follow:
Programme Debugging
Program debugging is the process of identifying and fixing errors or bugs in a computer program.
now what is bug, a insect ?
Obviously No, A bug refers to the errors that occurs in the program and the errors canbe of three types\
- Syntax Errors
Mistakes in the code's grammar or structure that prevent it from being understood by the compiler or interpreter.
- Runtime Errors
The error that is not visible at the compiling stage but occured in the running process are called runtime error.
- Logical Errors
Errors where the code runs without crashing but produces incorrect results due to flawed logic. Here’s a simplified explanation of input and output statements in C programming, following the style you've used in your notes:
Input and Output Statements in C
Input and output (I/O) statements are crucial for interacting with users in a C program. These are used to receive data from the user and display results back to them.
1. Output Statement
The output statement is used to display information to the user. In C, the most common output function is printf()
which is stored in stdio.h
file.
-
Syntax:
printf("format_string", variables);
-
Example:
printf("Hello, World!\n");
In this example,
printf
prints "Hello, World!" to the console. The\n
adds a new line after the message.
2. Input Statement
The input statement allows the program to accept data from the user. The most common function for input is scanf()
.
-
Syntax:
scanf("format_string", &variable);
-
Example:
int num; printf("Enter a number: "); scanf("%d", &num);
In this example,
printf
asks the user to enter a number, andscanf
reads that number and stores it in the variablenum
. The&
symbol is used to pass the address of the variable toscanf
.
3. Complete Example
Here’s a simple C program that combines input and output statements:
#include <stdio.h>
int main() {
int num;
// Output statement
printf("Enter a number: ");
// Input statement
scanf("%d", &num);
// Output the result
printf("You entered: %d\n", num);
return 0;
}
uuhhhh... lot of code, let's go through it for simple understanding.
- The program starts by including the standard input/output library with
#include <stdio.h>
. - Then
main
function comes, Inmain
function it declares an integer variablenum
. - We used
printf
to ask the user to enter a number. - Then
scanf
to read the user input and store it innum
. - Finally outputs the value entered by the user using another
printf
.
Note:
Input: Use `scanf()` to get data from users.
Output: Use `printf()` to display messages or results.
Constant
A constant refers to a value that can't be changed and remains same all the time. If we try to change the value it will through an error. For constant definition we use two keywords-
#define
.const
.
For better understanding let's chack an example-
#include <stdio.h>
#define PI 3.14
int main() {
const int radius = 5;
float area;
area = PI * radius * radius;
printf("Area of the circle: %.2f\n", area);
return 0;
}
What's the difference?
#define
is a preprocessor directive and the constant declared by this has a global scope. But the const
is a reserved keyword and the constant declared by this has only local scope.
for key differences watch this 👉
Variables and Data types
Let’s learn about variables and data types in C Programming. We will first look at Variables in C, Variables are used to store the value during the execution of a program. The name itself means, the value of variable can be changed hence the name “Variable“. The variables are stored in Main Memory.
There are set of rules to be followed while declaring variables and data types in C Programming:
- The 1st letter should be alphabet.
- Variables can be combination of alphabets and digits.
- Underscore (_) is the only special character allowed.
- Variables can be written in both Uppercase and Lowercase or combination of both.
- Variables are Case Sensitive. i.e,
Name
andname
are considered different - No Spaces allowed between Characters.
- Variable name should not make use to the C Reserved Keywords.
- Variable name should not start with a number.
Syntax
#include <stdio.h>
void main()
{
float f = 3.1415;
printf("This is my float: %f \n", f);
}
Before defining the variable we have to define it's data types. Data types are- For better understanding I prefer to read above notes.
Operators & Expressions
these are also precisely explained in the pdf, so check them and go ahead.
❔ Conditionals
conditionals allow you to execute code based on whether a condition is true or false. The most common conditional statements in C are:
- if Statement
- if-else Statement
- else-if Ladder
- switch Statement
- Ternary Operator (?:)
Let's go through each -
1. if Statement
The if statement is used to execute a block of code only if a specified condition is true.
Syntax:
if (condition) {
// Code to execute if the condition is true
}
Example:
#include <stdio.h>
int main() {
int number = 10;
if (number > 5) {
printf("Number is greater than 5\n");
}
return 0;
}
Explanation: If the value of number is greater than 5, the code inside the if block is executed. In this case, "Number is greater than 5" will be printed.
2. if-else Statement
The if-else statement is used when you want to execute one block of code if the condition is true and another block if it is false.
Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example:
#include <stdio.h>
int main() {
int number = 3;
if (number > 5) {
printf("Number is greater than 5\n");
} else {
printf("Number is not greater than 5\n");
}
return 0;
}
Explanation: If number is greater than 5, the first block will run, otherwise the else block is executed.
- else-if Ladder
The else-if ladder allows you to check multiple conditions. If the first condition is false, it checks the next one, and so on.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the above conditions are true
}
Example:
#include <stdio.h>
int main() {
int number = 0;
if (number > 0) {
printf("Number is positive\n");
} else if (number < 0) {
printf("Number is negative\n");
} else {
printf("Number is zero\n");
}
return 0;
}
Explanation: If number is greater than 0, the first block will execute. If number is less than 0, the second block will execute. If neither condition is true, the else block will print "Number is zero".
4. switch Statement
The switch statement is used when you have multiple cases to choose from based on the value of an expression. It's often used as an alternative to long else-if ladders.
Syntax:
switch (expression) {
case constant1:
// Code to execute if expression == constant1
break;
case constant2:
// Code to execute if expression == constant2
break;
// Add more cases as needed
default:
// Code to execute if none of the above cases match
}
Example:
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Explanation: The value of day is compared with each case. If it matches case 3, "Wednesday" is printed. If no case matches, the default block is executed.
5. Ternary Operator (?:)
The ternary operator is a shorthand for if-else statements. It takes three operands and is often used for simple conditional expressions.
Syntax:
condition ? expression_if_true : expression_if_false;
Example:
#include <stdio.h>
int main() {
int number = 4;
const char *result;
result = (number % 2 == 0) ? "Even" : "Odd";
printf("Number is %s\n", result);
return 0;
}
Explanation: If the number is divisible by 2, result is set to "Even". Otherwise, it’s set to "Odd".
6. switch Statement
The switch statement allows you to test the value of a variable against multiple cases. If a match is found, the corresponding block is executed. It's often used in place of long else-if ladders.
Syntax:
switch (expression) {
case constant1:
// Code to execute if expression == constant1
break;
case constant2:
// Code to execute if expression == constant2
break;
// Additional cases
default:
// Code to execute if no cases match
}
Example:
#include <stdio.h>
int main() {
int day = 2;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Explanation: The value of day is checked against each case. In this case, it matches 2, so "Tuesday" is printed. If no cases match, the default case will run, but it's optional.
♾️ Loops
A loop is a statement in which a block of code run until the specified condition is true, If the condition is false then the code will not run. ther are many types of loops, some of the important are:
- while Loop
The while loop is used to repeatedly execute a block of code as long as a specified condition is true. The condition is checked before the loop body executes.
Syntax:
while (condition) {
// Code to execute as long as the condition is true
}
Example:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("i = %d\n", i);
i++;
}
return 0;
}
Explanation: The loop runs while i <= 5. For each iteration, it prints the value of i and then increments it. The loop stops when i becomes 6 (condition is false).
- do-while Loop
The do-while loop is similar to the while loop, but the condition is checked after the loop body executes. This ensures that the loop runs at least once, even if the condition is false initially.
Syntax:
do {
// Code to execute
} while (condition);
Example:
#include <stdio.h>
int main() {
int i = 6;
do {
printf("i = %d\n", i);
i++;
} while (i <= 5);
return 0;
}
Explanation: The loop runs once even though the condition i <= 5 is false. This is because the condition is checked after the loop body.
- for Loop
The for loop is used when you know in advance how many times you want to repeat a block of code. It consists of three parts: initialization, condition, and increment.
Syntax:
for (initialization; condition; increment) {
// Code to execute
}
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("i = %d\n", i);
}
return 0;
}
Explanation: int i = 1: Initialization step, where i is set to 1. i <= 5: Condition that checks if the loop should continue. i++: Increment step that increases i after each iteration. The loop will run 5 times, printing the value of i each time.
there are some statements used in loops are:
- break Statement
The break statement is used to exit a loop or a switch statement immediately, even if the condition hasn't been satisfied.
Example in a loop:
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i equals 5
}
printf("i = %d\n", i);
}
return 0;
}
Explanation: The loop will terminate when i equals 5, because break forces the loop to exit.
- continue Statement
The continue statement is used to skip the current iteration and continue with the next iteration of the loop.
Example:
Copy code
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip printing when i equals 3
}
printf("i = %d\n", i);
}
return 0;
}
Explanation: When i is 3, the continue statement skips the rest of the loop body and jumps to the next iteration, skipping the print statement for i = 3.
- goto Statement
The goto statement allows you to jump to a labeled section of code. It's rarely used because it can make code harder to understand, but it’s useful in certain cases like breaking out of nested loops.
Syntax:
goto label_name;
label_name:
// Code to jump to
Example:
#include <stdio.h>
int main() {
int i = 1;
loop_start:
printf("i = %d\n", i);
i++;
if (i <= 5) {
goto loop_start; // Jump back to loop_start label
}
return 0;
}
Explanation: The goto statement forces the program to jump back to the label loop_start, creating a manual loop.