You are on page 1of 16

Chapter II:

The C Language

In this comprehensive course, we will cover all the fundamental


concepts of C programming language, with meaningful examples.

NK by Dr. Nabil Kadache


A Brief Introduction to C Language

1 History and Importance

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.

2 Basic Syntax and Structure

C programming uses a combination of keywords, variables, data types, operators


and control statements to build reliable and efficient programs. We'll start with
syntax and basic code structure.

3 Key Features and Benefits

C is a powerful programming language, whose key features include low-level


memory access, efficient and fast execution, and a large standard library. It is used in
many domains, including system software, embedded systems, and game
development.
Variables and Data Types in C

Declaring and Initializing Variables


C supports several built-in data types including
integer, char, float, and double. The figure Different Data Types in C
shows how to declare variables and how to
initialize them with values.

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;

while (count <= 5) {


printf("%d\n", count);
count++;
}
return 0;
}
Syntax of Control Flow
Statements
3 for Loop
The while loop repeatedly executes a block of code as long as a condition is true.

Syntax:
for (initialization ; condition ; updation) {
// body of the loop, statements to be executed
}

Example:
#include <stdio.h>
int main() {
int count = 1;

while (count <= 5) {


printf("%d\n", count);
count++;
}
return 0;
}
Syntax of Control Flow
Statements
3 Do 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;

while (count <= 5) {


printf("%d\n", count);
count++;
}
return 0;
}
Examples of Control Flow in C

Nested If-Else Switch Statement

Illustrate complex if-else statements with Showcase the usage of the switch statement
nested conditions. with multiple cases.

While Loop For Loop

Provide examples of implementing a while Demonstrate the versatility of for loops in


loop for iterating over a collection. programming tasks.
Arrays and Strings in C

Declaring and Manipulating Arrays String Operations in C

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.

3 Flexibility in Data Manipulation:


C file handling offers operations to read, write, and seek within a file, providing adaptability to changing
requirements and improved memory management.
Different types of file handling operations
1 Opening and Closing a File:
File access throughout any program starts by opening it using fopen() function and ends by closing it using fclose()
function.

2 Reading from a File

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.

 Passing Arguments and Returning


Values :
functions receive arguments and return values to the calling code. Functions that return a value can be
used to generate results that can be passed to other parts of the program.

 Benefits of Modular Programming :


modular programming ensures that a program is simple, easy to debug, and easy to understand. It consists of
dividing program into a several files that may contain some functions. The C environment can link different
modules and generate a single program
Conclusion and Next Steps
Takeaways Next Steps
• A thorough understanding of key C 1. Practice coding in C language
programming concepts 2. Develop an analytical mindset for
• Expertise to write efficient and problem solving
optimized programs 3. Explore advanced C programming concepts
• The ability to solve complex problems
using a modular approach

You might also like