Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views31 pages

Basic C Programming

The document provides an overview of C programming, detailing its evolution, structure, and key components such as variables, data types, operators, and control statements. It explains the significance of functions, arrays, pointers, structures, and unions in C, highlighting their roles in code organization and memory management. Additionally, it covers programming concepts like loops and decision-making statements, emphasizing best practices for variable declaration and initialization.

Uploaded by

Ruchi Banarjee
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)
7 views31 pages

Basic C Programming

The document provides an overview of C programming, detailing its evolution, structure, and key components such as variables, data types, operators, and control statements. It explains the significance of functions, arrays, pointers, structures, and unions in C, highlighting their roles in code organization and memory management. Additionally, it covers programming concepts like loops and decision-making statements, emphasizing best practices for variable declaration and initialization.

Uploaded by

Ruchi Banarjee
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/ 31

PROGRAMMING

BY: RUCHI BANARJEE


INTRODUCTION:

• Evolved from B which evolved from BCPL.


• C programming dialect was developed by Dennis Ritchie at the Bell Laboratories in 1972.
• C is a structured programming & High level language .
• It was imagined for executing UNIX working framework.
• Acting as bridge between machine language and the high level languages.
• C supports functions that enables easy maintainability of code, by breaking large file into
smaller modules
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
C Program Structure
Preprocessor Directives • Program defined by:
Global Declarations • global declarations
Function Definitions
• function definitions
int main () { • May contain preprocessor
Local Declarations directives
Statements • Always has one function
named main, may contain
}
others
THE GREETING PROGRAM
Examples of Line Comments
#include <stdio.h> Preprocessor Directive

int x; Global Declaration

int main () {
int y; Local Declaration
Function printf("Enter x and y: ");
scanf(&x,&y); Statements
printf("Sum is %d\n",x+y);
}
PREPROCESSOR
DIRECTIVES
Begin with #
Instruct compiler to perform some transformation to file
before compiling
Example: #include <stdio.h>
add the header file stdio.h to this file
.h for header file
stdio.h defines useful input/output functions
DECLARATIONS

• Global
• visible throughout program
• describes data used throughout program
• Local
• visible within function
• describes data used only in function
FUNCTIONS
• Consists of header and body
• header: int main ()
• body: contained between { and }
• starts with location declarations
• followed by series of statements
• More than one function may be defined
• Functions are called (invoked) - more later
Variables
• Named memory location
• Variables declared in global or local
declaration sections
• Syntax: Type Name;
• Examples:
int sum;
float avg;
char dummy;
VARIABLES – DATA TYPES:
There are four data types in C language. They are,
Multiple Variable
Declarations
• Can create multiple variables of the same type
in one statement:
int x, y, z;
is a shorthand for
int x;
int y;
int z;
- stylistically, the latter is often preferable
Variable Initialization
• Giving a variable an initial value
• Variables not necessarily initialized when
declared (value is unpredictable - garbage)
• Can initialize in declaration:
• Syntax: Type Name = Value;
• Example:
int x = 0;
Multiple Declaration
Initialization
• Can provide one value for variables initialized
in one statement:
int x, y, z = 0;
• Each variable declared and then initialized
with the value
DECLARING & INITIALIZING C VARIABLE:

•Variables should be declared in the C program before to use.


•Memory space is not allocated for a variable while declaration. It happens only on variable
definition.
•Variable initialization means assigning a value to the variable.

Type Syntax
data_type variable_name;
Example: int x, y, z; char
Variable declaration flat, ch;

data_type variable_name
= value;
Example: int x = 50, y =
Variable initialization 30; char flag = ‘x’, ch=’l’;
TYPES OF C OPERATORS:-

TYPES OF OPERATORS DESCRIPTION

These are used to perform mathematical


calculations like addition, subtraction,
Arithmetic_operators multiplication, division and modulus

These are used to assign the values for the


Assignment_operators variables in C programs.

These operators are used to compare the


Relational operators value of two variables.

These operators are used to perform


logical operations on the given two
Logical operators variables.

These operators are used to perform bit


Bit wise operators operations on given two variables.

Conditional operators return one value if


condition is true and returns another value
Conditional (ternary) operators is condition is false.

These operators are used to either


increase or decrease the value of the
DIFFERENCE BETWEEN PRE/POST INCREMENT & DECREMENT OPERATORS
IN C:
Below table will explain the difference between pre/post increment and decrement operators in C
programming language.

Operator Operator/Description
value of i is incremented before assigning it
Pre increment to the variable i
operator (++i)
value of i is incremented after assigning it to
Post increment opera the variable i
tor (i++)
Pre value of i is decremented before assigning it
decrement operator to the variable i
(–i)
Post decrement opera
tor (i–) value of i is decremented after assigning
IF”, “ELSE” AND “NESTED IF” DECISION CONTROL STATEMENTS IN C:-

if:-
Syntax:
if (condition)
{ Statements; }
Description:
In these type of statements, if condition is true, then respective block of code is executed.

if…else:-
Syntax:
if (condition)
{ Statement1; Statement2; }
else
{ Statement3; Statement4; }
Description:
In these type of statements, group of statements are executed when condition is true. If
condition is false, then else part statements are executed.
nested if:-
Syntax:
if (condition1){ Statement1; }
else_if(condition2)
{ Statement2; }
else Statement 3;
Description:
If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If
condition 2 also gets failure, then else part is executed.

•Loop control statements in C are used to perform looping operations until the given condition is
true. Control comes out of the loop statements once condition becomes false.
TYPES OF LOOP CONTROL STATEMENTS IN C:
There are 3 types of loop control statements in C language. They are,
•for
•while
•do-while

For:-
for (exp1; exp2; expr3)
{ statements; }
Where,
exp1 – variable initialization
( Example: i=0, j=2, k=3 )
exp2 – condition checking
( Example: i>5, j<3, k=3 )
exp3 – increment/decrement
( Example: ++i, j–, ++k )
While:-
while (condition)
{ statements; }
where,
condition might be a>5, i<10

do while:-

do { statements; }
while (condition);

where,
condition might be a>5, i<10
DIFFERENCE BETWEEN WHILE & DO WHILE LOOPS IN C LANGUAGE:

While do while

Loop is executed for


first time irrespective
of the condition. After
executing while loop
Loop is executed only for first time, then
when condition is true. condition is checked.
Array:-
C Array is a collection of variables belongings to the same data type. You can store
group of data of same data type in an array.
•Array might be belonging to any of the data types
•Array size must be a constant value.
•Always, Contiguous (adjacent) memory locations are used to store array elements in
memory.
•It is a best practice to initialize an array to zero or null while declaring, if we don’t
assign any values to array

TYPES OF C ARRAYS:
There are 2 types of C arrays. They are,
One dimensional array
Multi dimensional array
• Two dimensional array
• Three dimensional array
• four dimensional array etc…
Pointers:-
•Pointers in C language is a variable that stores/points the address of another
variable.
•A Pointer in C is used to allocate memory dynamically i.e. at run time.
•The pointer variable might be belonging to any of the data type such as int,
float, char, double, short etc.
Pointer Syntax : data_type *var_name; Example : int *p; char *p;
Where, * is used to denote that “p” is pointer variable and not a normal variable.
USES OF C FUNCTIONS:

• C functions are used to avoid rewriting same logic/code again and again in a program.
• There is no limit in calling C functions to make use of same functionality wherever
required.
• We can call functions any number of times in a program and from any place in a program.
• A large C program can easily be tracked when it is divided into functions.
• The core concept of C functions are, re-usability, dividing a big task into small pieces to
achieve the functionality and to improve understandability of very large C programs.
Structure:-

•Structure is a collection of different data types which are grouped


together and each element in a C structure is called member.

•If you want to access structure members in C, structure variable should


be declared.

•Many structure variables can be declared for same structure and


memory will be allocated for each separately.

•It is a best practice to initialize a structure to null while declaring, if we


don’t assign any values to structure
Union:-
•C Union is also like structure, i.e. collection of different data types which are grouped together.
Each element in a union is called member.

•Union and structure in C are same in concepts, except allocating memory for their members.
•Structure allocates storage space for all its members separately.

•Whereas, Union allocates one common storage space for all its members
•We can access only one member of union at a time.
•We can’t access all member values at the same time in union. But, structure can access all
member values at the same time.

• This is because, Union allocates one common storage space for all its members.
Where as Structure allocates storage space for all its members separately.

•Many union variables can be created in a program and memory will be allocated for each union
variable separately.
THANK YOU

You might also like