Chapter 2 C Language
Chapter 2 C Language
The C Language
C is one of the oldest and most widely used programming languages today.
Developed at Bell Labs in the 1970s, C has greatly influenced many modern
programming languages and operating systems.
The reserved keywords used to declare primitive variables are : short, int, long (for integer type), float, double (for
real type) and char for character. We can use the unsigned prefix for numerical to declare variables that take only
positive values.
Syntax of Control Flow
Statements
1 If Statement
The if statement allows execution of a block of code based on a condition.
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;
// Check if the number is greater than 5
if (number > 5)
{ printf("The number is greater than 5.\n"); }
else
{ printf("The number is not greater than 5.\n"); }
return 0;
}
Syntax of Control Flow
Statements
1 Switch Statement
The switch statement provides a way to execute multiple branches of
code based on different cases. Example:
Syntax: #include <stdio.h>
int main() {
switch (expression) {
int choice;
case constant1:
printf("Choose an option (1-3):\n");
// Code to execute if expression equals constant1
scanf("%d", &choice);
break;
switch (choice) {
case constant2:
case 1:
// Code to execute if expression equals constant2
printf("You chose option 1.\n");
break;
break;
// Add more cases as needed
case 2:
default:
printf("You chose option 2.\n");
// Code to execute if expression doesn't match any case
break;
}
case 3:
printf("You chose option 3.\n");
break;
default:
printf("Invalid choice. Please choose between 1, 2, and 3.\n");
break;
}
return 0;}
Syntax of Control Flow
Statements
3 While Loop
The while loop repeatedly executes a block of code as long as a condition is true.
Syntax:
while (condition) {
// Code to be executed while the condition is true
}
Example:
#include <stdio.h>
int main() {
int count = 1;
Syntax:
for (initialization ; condition ; updation) {
// body of the loop, statements to be executed
}
Example:
#include <stdio.h>
int main() {
int count = 1;
Syntax:
while (condition) {
// Code to be executed while the condition is true
}
Example:
#include <stdio.h>
int main() {
int count = 1;
Illustrate complex if-else statements with Showcase the usage of the switch statement
nested conditions. with multiple cases.
C provides an array data structure that allows Strings are a sequence of characters that are
us to store a fixed number of items of the same terminated by a null character. We'll see how C
data type. We'll learn how to declare and represents strings as arrays of characters and
initialize arrays, and how to manipulate them how to perform common string operations
with loops such as for and while. such as concatenation, length, and
comparison.
File handling in C
Why File Handling is Important in C
Access to external Data:
1 Files can be a convenient, easy-to-read way to store data outside of the program's main function.
Persistence of Data :
2
File handling allows information to be stored permanently on disk, enabling retrieval and
processing at different times.
To read data from a file, use the fscanf() function which reads formatted input from a file.
3 Writing to a File
To write data to a file, use the fprintf() function which writes formatted output to a file.
Example illustrating file handle functions use:
#include <stdio.h>
#include <stdlib.h>
int main(){
int v=10;
char *s= " is an integer value";
char str1[10], str2[10], str3[10],str4[10];
FILE *f=fopen("MyFile.txt","w+");
if (f==NULL) {
printf("File Creation Error");
exit(0);
}
else printf("File create successfully\n");
fprintf(f,"%d %s",v,s);
fclose(f);
f=fopen("MyFile.txt","r");
v=0;
fscanf(f,"%d %s %s %s %s",&v,str1,str2,str3,str4);
printf("File content:%d %s %s %s %s \n",v,str1,str2,str3,str4);
fclose(f);
}
Remarks and best Practices for File Handling in C
There are several other functions to manage file read/write operations, some manipulate data in binary
format, bytes and character strings.
Properly closing files : Close all files that have been opened and released all system resources that were
reserved for the file.
Checking for errors: It is best to add a confirmation message after each file read or write operation is
completed. File handling functions are unsafe I/O operations.
Functions and Modular Programming in C
Defining and Calling Functions :
Functions are the building blocks of any C program, as they help us organize code into logical units. We'll
discuss how to define and call functions, and examine the different types of functions such as void, return
value, and parameter value.