Initiation C
Initiation C
By Pr. AMROUNE M.
What is C?
C is a general-purpose programming language
created by Dennis Ritchie at the Bell Laboratories
in 1972.
It is a very popular language, despite being old.
The main reason for its popularity is because it is a
fundamental language in the field of computer
science.
C is strongly associated with UNIX, as it was
developed to write the UNIX operating system.
Why Learn C?
•It is one of the most popular programming language
in the world
•If you know C, you will have no problem learning
other popular programming languages such as Java,
Python, C++, C#, etc, as the syntax is similar
•C is very fast, compared to other programming
languages, like Java and Python
•C is very versatile; it can be used in both
applications and technologies
Write the following C code and save the file as myfirstprogram.c (File > Save File as):
C Programs
int main() {
printf("Hello World!");
return 0;
}
Line 1: #include <stdio.h> is a header file library that lets us work with input and output functions
such as printf() (used in line 4). Header files add functionality to C programs.
Line 2: A blank line. C ignores white space. But we use it to make the code more readable.
14
Examples of Block Comments
15
Examples of Line Comments
16
Identifiers
17
Rules for Identifiers
18
Note
C is a case-sensitive language.
19
Examples of Valid and Invalid Names
20
Data Types
21
Character Types
22
Integer Types
23
Note
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)
24
Typical Integer Sizes and Values for Signed Integers
25
Floating-point Types
26
Note
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)
27
Type Summary
28
Variables
Variables are named memory locations that
have a type, such as integer or character,
which is inherited from their type. The type
determines the values that a variable may
contain and the operations that may be used
with its values.
31
‘B’
Variable Initialization
32
Constants
35
Examples of Real Constants
36
C Assignment Operators
Control Statements
statement 2;}
Next statement;
In above syntax : if
condition is true only then the statements within the
block are executed otherwise next statement in
sequence is executed.
/* Program to check whether a no. is even */
Flowchart # include<stdio.h>
# include<conio.h>
void main()
False {
Condition
int num;
True clrscr();
statement 3; Statement 3
else
Default statement
default statement;
Next statement
Switch statement
Switch is a multi-way decision making statement
which selects one of the several alternatives based
on the value of single variable or expression.
It is mainly used to replace multiple if-else-if
statement.
The if-else-if statement causes performance
degradation as several conditions need to be
evaluated before a particular condition is satisfied.
Syntax: switch (expression)
{
case constant1 : statement (s); [break;]
case constant2 : statement (s); [break;]
…………………………………….
default: statement (s)
}
Break statement
Break statement terminates the execution of the loop in
which it is defined.
The control is transferred immediately to the next
executable statement after the loop.
It is mostly used to exit early from the loop by skipping the
remaining statements of loop or switch control structures.
Syntax: break;
Looping Structures
When we want to repeat a group of
statements a no. of times, loops are used.
These loops are executed until the
condition is true.
When condition becomes false, control
terminates the loop and moves on to next
instruction immediately after the loop.
Various looping structures are-
while
do – while
for
LOOPING STATEMENTS
Loop is divided into two parts:
Body of the loop
Control of loop
Mainly control of loop is divided into two parts:
Entry Control loop (while, for)
Exit Control loop (do-while)
while statement
While loop is used to execute set of statements as
long as condition evaluates to true.
It is mostly used in those cases where the
programmer doesn’t know in advance how many
times the loop will be executed.
Syntax: while (condition)
{ true
condition statement
Statement 1 ;
Statement 2 ;
}
Statement after while loop
do- while
do-whileis similar to while except that its test
condition is evaluated at the end of the loop
instead at the beginning as in case of while loop.
So,in do-while the body of the loop always
executes at least once even if the test condition
evaluates to false during the first iteration.
Syntax: do Body of loop
{
statement 1; true
Test condition
statement 2;
}while (condition); false
is equivalent to:
expr1;
while (expr2) {
statement;
expr3;
}
Various other ways of writing same for loops
for for
i=1 (i=1; ;i++) (i=1;i<=15;
{ )
for (; i<=15;i ++)
……… {
{
…….. if (i>15) ………….
} break; i++;
…… }
}
Some Examples
for(i = 7; i <=77; i += 7)
statement;
for(i = 20; i >= 2; i -= 2)
statement;
Also: c += 1;
Also: c++;
Also: ++c;
/* Preincrementing and postincrementing */
#include <stdio.h>
int main()
{
int c;
c = 5;
printf("%d\n", c);
printf("%d\n",c++); /*post increment*/
printf("%d\n\n", c);
:
continued
Decrementing
Also: c -= 1;
Also: c--;
Also: --c;
Continue statement
Like break ,continue statement also skips the
remaining statements of the body of the loop
where it is defined but instead of terminating the
loop, the control is transferred to the beginning of
the loop for next iteration.
The loop continues until the test condition of the
loop become false.
Syntax: continue;
E.g. for (m=1;m<=3;m++)
{ Output:
for (n=1;n<=2;n++) 1 2
{ 2 1
if (m==n) 3 1
continue; 3 2
printf(“ m=%d n=%d”);
}
}
goto Statement
An unconditional control statement that causes the
control to jump to a different location in the program
without checking any condition.
It is normally used to alter the normal sequence of
program execution by transferring control to some
other part of the program.
So it is also called jump statement.
Syntax: goto label;
Label represents an identifier which is used to label
the destination statement to which the control should
be transferred.
label : statement;
The goto statement causes the control to be shifted
either in forward direction or in a backward direction .
exit() function