0% found this document useful (0 votes)
2 views7 pages

The C

The document provides an overview of the C programming language, highlighting its history, features, and importance in software development. It explains key concepts such as loops (for, while, do-while), control statements (break, continue, goto), and the structure of a basic C program, including preprocessor directives and header files. Additionally, it includes examples of C code to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

The C

The document provides an overview of the C programming language, highlighting its history, features, and importance in software development. It explains key concepts such as loops (for, while, do-while), control statements (break, continue, goto), and the structure of a basic C program, including preprocessor directives and header files. Additionally, it includes examples of C code to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

The C programming language is a general-purpose, operating system-agnostic,


and procedural language that supports structured programming and provides
low-level access to the system memory. Dennis Ritchie invented C language in
1972 at AT&T (then called Bell Laboratory), where it was implemented in the
UNIX system on DEC PDP II. It was also the successor of the B programming
language invented by Ken Thompson. C was designed to overcome the
problems encountered by BASIC, B, and BPCL programming languages. By
1980, C became the most popular language for mainframes, microcomputers,
and minicomputers.
Features of C Programming
Loved by programmers for doing low-level coding and embedded programming, C has
found its way gradually into the semiconductor, hardware, and storage industries. The
most important features provided by the C programming languages include:
• It has inbuilt functions and operators that can solve virtually any complex
problem
• C is the combination of both low level (assembly) and high-level programming
languages; also, it can be used to write an application and interact with low-
level system memory and hardware
• It can be written on practically any operating system and even works in most
handheld devices
• Programs written in C are speedy due to the support provided by its datatypes
and operators
• It is easily extendable, as C++ was derived from C with additions like OOPS
and other features
• The functions and operators are supported by the libraries provided by the
programming language itself

Get a firm foundation in C, the most commonly used programming language in software
development with the C Programming Course.
C Program (“Hello World”)
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 1, b=2, c=0;
int c = a + b;
printf(“Hello World”);
printf(“C = “ %d, c);
return 0;
}
Preprocessor Directives
The #include in the first line of the C program is referred to as preprocessor directives.
All of the preprocessors will start with the # symbol. It is the first line that is executed,
having the library file ending with .h. The stdio.h in the above program will have libraries
associated with print to the console output. This will also associate the program with the
library. The compiler to transform the program before compilation will use the
preprocessor. Macros are also similar to preprocessors, but they are all user-defined
and help in expanding the values in all places in the program.

For example:
#define AREA=354;
This will substitute the variable AREA with 354 everywhere in the program and requires
less effort by the programmers in the event a change is needed in the future.

Header Files
There are some standard header files provided by the language, which can be used in
the program to do mathematical or logical calculations or even print to the console or
files. In the above example, you have used printf function, which prints the output to the
console. The stdio.h header file will have relevant or associated code for printing the
output to the console, so it has to be included upfront in the program for execution.

main() function
This is an important function in the C program within which the content of the program
or logic or calculation will be written. The main can have return types, and in the above
example, it has an integer as the return types.

2. LOOPS IN C

Loops in C Language
Introduction
In programming, loops are used to execute a block of code repeatedly until a specified
condition is met. C language provides three types of loops: for, while, and do-while
loops. Each loop has its unique use case and syntax, which we will explore in detail.

For Loop
The for loop is commonly used when the number of iterations is known before entering
the loop. It consists of three parts: initialization, condition, and increment/decrement.

Syntax
c
Copy code
for (initialization; condition; increment/decrement) {
// Statements to be executed
}
Example
c
Copy code
#include <stdio.h>

int main() {
int i;
for (i = 0; i < 10; i++) {
printf("Number: %d\n", i);
}
return 0;
}
In this example, the loop starts with i initialized to 0. It continues to run as long as i is
less than 10, and i is incremented by 1 in each iteration. The output will be the numbers
from 0 to 9.

While Loop
The while loop is used when the number of iterations is not known beforehand and
depends on a condition evaluated before each iteration.

Syntax
c
Copy code
while (condition) {
// Statements to be executed
}
Example
c
Copy code
#include <stdio.h>

int main() {
int i = 0;
while (i < 10) {
printf("Number: %d\n", i);
i++;
}
return 0;
}
In this example, the loop starts with i initialized to 0 and continues to run as long as i is
less than 10. The condition is evaluated before each iteration, and i is incremented
inside the loop. The output will be the numbers from 0 to 9.
Do-While Loop
The do-while loop is similar to the while loop, but the condition is evaluated after
executing the block of code. This guarantees that the code inside the loop is executed
at least once.

Syntax
c
Copy code
do {
// Statements to be executed
} while (condition);
Example
c
Copy code
#include <stdio.h>

int main() {
int i = 0;
do {
printf("Number: %d\n", i);
i++;
} while (i < 10);
return 0;
}
In this example, the loop starts with i initialized to 0. The code block is executed, and
then the condition i < 10 is evaluated. Since the condition is checked after the code
execution, the output will be the numbers from 0 to 9.

Nested Loops
Loops can be nested within other loops. This is useful for multi-dimensional data
structures like matrices.

Example
c
Copy code
#include <stdio.h>

int main() {
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("i = %d, j = %d\n", i, j);
}
}
return 0;
}
In this example, the outer loop runs three times, and for each iteration of the outer loop,
the inner loop runs three times. The output will be a combination of i and j values,
demonstrating the nested loop structure.

Loop Control Statements


C provides several control statements to manage the flow of loops, including break,
continue, and goto.

Break Statement
The break statement terminates the loop immediately, and the program control moves
to the statement following the loop.

Example
c
Copy code
#include <stdio.h>

int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("Number: %d\n", i);
}
return 0;
}
In this example, the loop terminates when i equals 5. The output will be the numbers
from 0 to 4.

Continue Statement
The continue statement skips the current iteration and proceeds to the next iteration of
the loop.

Example

Copy code
#include <stdio.h>

int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
printf("Number: %d\n", i);
}
return 0;
}
In this example, when i equals 5, the continue statement skips the rest of the loop body
for that iteration. The output will be the numbers from 0 to 4 and 6 to 9.

Goto Statement
The goto statement transfers control to the labeled statement within the same function.
It's generally advised to avoid using goto for managing loops due to readability and
maintainability concerns.

Example
c
Copy code
#include <stdio.h>

int main() {
int i = 0;
start:
printf("Number: %d\n", i);
i++;
if (i < 10) {
goto start;
}
return 0;
}
In this example, the goto statement transfers control back to the start label, effectively
creating a loop. The output will be the numbers from 0 to 9.

Infinite Loops
An infinite loop runs indefinitely because the condition never becomes false. These are
often used in systems that require continuous operation until an external intervention
occurs.

Example
c
Copy code
#include <stdio.h>

int main() {
while (1) {
printf("This will run forever\n");
}
return 0;
}
In this example, the condition 1 is always true, so the loop runs forever, printing the
message repeatedly.

You might also like