0% found this document useful (0 votes)
10 views63 pages

Initiation C

C is a general-purpose programming language created in 1972, known for its speed and versatility, particularly in UNIX systems. Learning C provides a strong foundation for understanding other programming languages due to its similar syntax. The document covers the basics of C programming, including its structure, data types, control statements, and examples of coding practices.

Uploaded by

Yacine Yacine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views63 pages

Initiation C

C is a general-purpose programming language created in 1972, known for its speed and versatility, particularly in UNIX systems. Learning C provides a strong foundation for understanding other programming languages due to its similar syntax. The document covers the basics of C programming, including its structure, data types, control statements, and examples of coding practices.

Uploaded by

Yacine Yacine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Basics of ‘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

Difference between C and C++


•C++ was developed as an extension of C, and both
languages have almost the same syntax
•The main difference between C and C++ is that C++
support classes and objects, while C does not
Background

C is a structured programming language. It


is considered a high-level language because
it allows the programmer to concentrate on
the problem at hand and not worry about
the machine that the program will be using.
That is another reason why it is used by
software developers whose applications
have to run on many different hardware
platforms.
4
C Install IDE
An IDE (Integrated Development Environment) is used to edit AND
compile the code.

Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These


are all free, and they can be used to both edit and debug C code.

Note: Web-based IDE's can work as well, but functionality is limited.

We will use Code::Blocks in our tutorial, which we believe is a good


place to start.

You can find the latest version of Codeblocks at


http://www.codeblocks.org/. Download the mingw-setup.exe file, which
will install the text editor with a compiler.
C Quickstart
Let's create our first C file.

Open Codeblocks and go to File > New > Empty File.

Write the following C code and save the file as myfirstprogram.c (File > Save File as):
C Programs

It's time to write your first C program.

Topics discussed in this section:


Structure of a C Program
Your First C Program
Comments
The Greeting Program
9
Structure of a C Program
10
The Greeting Program
11
#include <stdio.h>

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.

Line 3: Another thing that always appear in a C program is main().


This is called a function. Any code inside its curly brackets {} will be executed.

Line 4: printf() is a function used to output/print text to the screen.


In our example, it will output "Hello World!".

Line 5: return 0 ends the main() function.


The Greeting Program

14
Examples of Block Comments
15
Examples of Line Comments
16
Identifiers

One feature present in all computer


languages is the identifier. Identifiers allow
us to name data and other objects in the
program. Each identified object in the
computer is stored at a unique address.

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.

Topics discussed in this section:


Variable Declaration
Variable Initialization
29
Variables
30
Examples of Variable Declarations and Definitions

31
‘B’

Variable Initialization
32
Constants

Constants are data values that cannot be


changed during the execution of a program.
Like variables, constants have a type. In this
section, we discuss Boolean, character,
integer, real, complex, and string constants.

Topics discussed in this section:


Constant Representation
Coding Constants
33
Symbolic Names for Control Characters 34
Examples of Integer Constants

35
Examples of Real Constants

36
C Assignment Operators

 An assignment operator is used for


assigning a value to a variable. The most
common assignment operator is =
 Operator Example Same as
= a=b a=b
 += a += b a = a+b
 -= a -= b a = a-b
 *= a *= b a = a*b
 /= a /= b a = a/b
 %= a %= b a = a%b
C Relational Operators

 A relational operator checks the relationship


between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value
0.
 Relational operators are used in decision making
and loops.
Operator Meaning of Operator Example
 == Equal to 5 == 3
returns 0
 > Greater than 5>3
returns 1
 < Less than 5 < 3 returns
0
 != Not equal to 5 != 3
returns 1
 >= Greater than or equal to 5 >= 3
Programming In C

Control Statements

Objectives of these slides:


to introduce the main kinds of C
control flow
Control Structures
 There may be situations where the
programmer requires to alter normal flow of
execution of program or to perform the
same operation a no. of times.
 Various control statements supported by c
are-
 Decision control statements
 Loop control statements
Decision Control Statements
 Decision control statements alter the
normal sequential execution of the
statements of the program depending
upon the test condition to be carried out
at a particular point in program.
 Decision control statements supported
by c are:-
 if statement
 if-else statement
 Else if Ladder
 Nested If
 switch statement
Decision Control Statements
 if statement
 Most simple and powerful decision control statement.
 It executes a statement or block of statements only if
the condition is true.
 Syntax: if (condition) if (condition)
{
statement 1;

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();

Block of printf(“enter the number”);


if scanf(“%d”,&num)
if(num%2==0)
Next
statement {
printf(“\n Number is even”);
STOP
}
printf(“ End of program”);
getch();
}
if – else statement
 In case of if statement, the block of statements is executed only
when the condition is true otherwise the control is transferred to
the next statement following if block.
 But if specific statements are to be executed in both cases
(either condition is true or false) then if – else statement is used.
 In if – else statement a block of statements are executed if the
condition is true but a different block of statements is executed
when the condition is false.
 Syntax: if (condition)
False
{ Test
Condition
statement 1;
statement 2; True
} Block of if Block of else
else
{ Next statement
statement 3;
} STOP
Exercise: WAP to check whether a given no. is even or odd?

Nested if – else statement


 When an entire if-else is enclosed within the
body of if statement or/and in the body of
else statement, it is known as nested if-else
statement.
 The ways of representing nested if –else are-
if if else if (condition1)
(condition1) (condition1) {
statement 1;
if (condition 3)
{ {
statement 3; else
if if else {
(condition2) (condition2) statement 4;
} if (condition2)
statement
statement 1; 1; statement 2;

else else else


statement statement 3;
statement 2; 2; }
}
If- else- if ladder
 In a program involving multiple conditions, the
nested if else statements makes the program
very difficult to write and understand if nested
more deeply.
 For this ,we use if-else-if ladder.
false
 condition 1
Syntax: if (condition1)
false
statement1; condition 2
true
else if(condition2) false
Statement 1 true condition 3
statement2;
else if(condition3) Statement 2 true

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

statement; Next statement


for loop
 Most versatile and popular of three loop
structures.
 Is used in those situations when a programmer
knows in advance the number of times a
statement or block will be executed.
 It contains loop control elements all at one place
while in other loops they are scattered over the
program and are difficult to understand.
 Syntax:-
for (initialization; condition; increment/decrement)
{
Statement( s);
}
The for is a sort of while

for (expr1; expr2; expr3)


statement;

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;

for(j = 10; j > 20; j++)


statement;
for(j = 10; j > 0; j--)
statement;
Incrementing and Decrementing
Incrementing
 Add 1 to c by writing:
c = c + 1;

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

 Take 1 from c by writing:


c = c - 1;

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

C provides a run time library function exit()


which when encountered in a program causes
the program to terminating without executing
any statement following it.
 Syntax: exit(status);
Status is an integer variable or constant.
 If the status is 0,then program normally
terminates without any errors.
A non-zero status indicates abnormal
termination of the program.
 The exit() function is defined in the process.h
header file.
Difference b/w exit() & break

Exit() is used to transfer the


control completely out of the
program whereas break is used
to transfer the control out of the
loop or switch statement.

You might also like