Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C for Beginners
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Agenda
• Introduction to C
• Variables in C
• Datatypes in C
• Input / Output in C
• Operators in C
• C Control Statements
• Arrays in C
• Functions in C
• Strings in C
• Structures and Union
• Pointers in C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
• C was first introduced by Dennis Ritchie.
• C is a procedure-oriented language.
• C is a widely used and popular programming language for developing system application software.
• It can be called as a mid-level programming language.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Features:
Some of the features of C Programming are:-
• Easy to learn
• Structured Programming language
• Machine Independent or Portable
• Powerful
• Used in low- level as well as high level applications
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Basic block of a C Program
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Preproccesor directives
Global declarations;
void main()
{
local declarations;
statement 1;
statement 2;
:
statement n;
}
User defined functions
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Comments
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
/* This is my First C Program
Author name: */
# include<stdio.h>
#include<conio.h>
void main()
{
printf(“C Programming”); // Single line Comment
getch();
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Tokens
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
C tokens are smallest individual units in a program.
● Identifiers- user defined names/abbreviations
● Keywords - words which are specific to C language
● Constants - values which don't change
● Strings - sequence of characters
● Operators - act on operands and generates output
● Special Symbols - used for preprocessor directives (#)
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variables in C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variables in C
What is a Variable?
• It’s a name given to a memory location where the value is not fixed, it can change with the course of
time. Ex : int num;
Syntax:
datatype variable_name;
Assignment:
variable_name = value
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Scope of a Variable
• Scope generally speaks about the visibility of variables.
- local variable and global variable
Local Global
Variables in C
void main ()
{
int rollno = 10;
}
int y = 50;
void main ()
{
int x = 100;
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Data Types in C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Data Types in C
Data Types
Primitive Data type Derived Data type User Defined type
Boolean
Character
Integer
Float
Double
Void
Array
Pointer
References
Structure
Union
Enum
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Typecasting
• C allows for conversions between the basic data types
- Implicit and Explicit
Data Types in C
int num = 5 + 13.75; // num = 18
float a = 5.25;
int b = (int) a; // b = 5
Implicit conversion Explicit conversion
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Input / Output in C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Input / Output in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Input and Output functions in C are:-
• printf() and scanf()
• gets() and puts()
• getchar() and putchar()
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Input / Output in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Format Specifiers:-
The format specifiers will inform the scanf() function, what kind of input is being fed through input
device. It also tells printf() function what type of data has to be printed on the output device.
• %d – Integer
• %c – Character
• %s – String
• %f - Float
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
• C primarily supports 5 types of operators
Arithmetic Relational Bitwise
Logical Assignment
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Arithmetic Operators:-
• Used for mathematical operations
• We have unary and binary operators
• Unary – ( ++, --)
• Binary – (+ , - , *, /, %)
Relational Operators:-
• Used to compare the values of two operands
• Some of them are ( < , <= , >=, ==)
• We can also check for equality of operands, whether an operand is greater or smaller than the
other.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Logical Operators:
• To combine one or multiple conditions we can use Logical operators.
• Logical operators always give us a Boolean value, i.e. true or false
• Some of them are ( && - AND, || - OR, ! – NOT)
Bitwise Operators:-
• We can perform bit-level operations on the operands.
• Some of them are ( & - bitwise AND, |- bitwise OR)
• It converts them to bit-level first and then performs the operations.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Assignment Operator:-
• It is used to assign a value to a given variable.
• The value on the right side of the assignment operator is assigned to operand at left side.
• We also have some short hand assignment operators ( +=, -=, /=).
.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Conditional Statements:-
• Also known as selection statements.
• Based on the conditions specified we can make some decisions.
• Anytime we execute conditional statements we get a Boolean value in return.
• If the condition executed returns a true value, a set of statements are executed else another set of
statements are executed.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
The if statement:-
• Selection and execution of statements based on a given condition is done by the if statement.
Syntax:
if (condition)
{
statement 1;
statement 2;
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
The if-else statement:-
• Depending on the result of the condition, the if - else statement executes one of the two potential
statements.
Syntax:
if (condition)
{
statement 1; // if block
statement 2;
}
else
{
statement 3: // else block
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
The Nested if-else statement:-
• A nested if-else contains one or more if-else statements.
if (condition1)
{
statement 1;
if(condition2)
statement 2;
else
statement 3;
}
else
{
statement 3:
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Loops:-
• Loops are used to re-execute a part of a code a given number of times, depending upon the
condition specified.
Entry controlled:-
• The condition is checked each-time before entering the loop. If the condition is satisfied then only
the loop body gets executed. The loop body does not get executed if the condition is false in the
first iteration.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Entry controlled loops are:-
1) For loop
Syntax:
for(i=0 ;i<n; i++){
//do something
}
2) While Loop
Syntax:
while(condition is True){
//do something
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Switch case:-
● The switch statement works as a multiway branch statement.
● It’s a modified form of if-else.
● It’s a common alternative to the if statement when you want to get multiple results.
● Default condition gets executed when no conditions are met.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
C Control Statements
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Switch case:-
#include<stdio.h>
#include<conio.h>
void main() {
int n= 1;
switch (n)
{
case 1: Statement 1;
break;
case 2: Statement 2;
break;
default: Statement;
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arrays in C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arrays in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
● An array is a collection of elements where the data types of all the elements are same.
● An array stores all the elements in contiguous memory locations.
● Every element in an array has a specified index.
● Size of an array in C is fixed at the time of definition
We have two types in Arrays:
Single dimensional
Multi-dimensional
1 x 3 Array
2 x 3 Array
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arrays in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Definition:
An array can be defined in the following manner in C:
Data_type name_of_array>[<size_of_array>]
Note: Size of an array should always be an integer it cannot be a real value.
Example:
int a[10]; //single dimensional array
float b[3][3]; //float - dimensional array
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arrays in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Array initialization:
1) Static Initialization
int a[10]={0,1,2,3,4,5,6}
char c[5]={‘h’,’e’,’l’,’l’,’o’}
2) Dynamic Initialization
int a[10]; //array is created with garbage value
for (int i=0 ;i<10; i++)
scanf(“%d”,&a[i]);
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functions in C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functions in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
● Functions are blocks of code which are used to perform specific tasks .
● In C a function needs to be declared before it’s used.
● Functions have a function definition , function body and a return type.
● Functions with return type except void needs to return a value at the end.
● Functions with return type void do not return any value.
● A recursive function can call itself during the course of execution of a program.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functions in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
A Function example:
int sum (int a, int b)
{
return a+b;
}
void main()
{
printf(“ %d”, sum(5,10));
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Strings in C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Strings in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
● Strings are used to store sequence of characters as information.
● string.h header file provides some built-in string manipulation functions.
Strings can be initialized in following ways:-
char a[5] = {‘H’,’o’,’l’,’a’};
char a [ ] = “Hola”;
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Strings in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
String functions are accessible by importing <string.h>.
● strlen() Find length of string
● strcpy() Copy one string to other
● strcat() Join two strings
● strcmp() Compare two strings
● strlwr() Converts string to lowercase
● strupr() Converts string to uppercase
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Structures and Union
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Structures and Union
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
● Structures provides a way for us to create our own data type called “ user-defined data type”
● Assume that we have to store the details of a student, so we can use structure to store it as below:
struct student
{
char name[10];
int rollno;
float cgpa;
} ;
name
rollno
cgpa
student
Structure
allocates space
for each variable
separately
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Structures and Union
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
● Union also provides a way for us to create our own data type called “ user-defined data type”.
● Here each variable shares the memory collectively.
union student
{
char name[10];
int rollno;
float cgpa;
} ;
name
rollno
cgpa
student
Union shares the
memory
allocated for
variables
Memory
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pointers in C
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pointers in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
● Pointers are variables which store the address of a variable.
● They have data type just like variables, for example an integer type pointer can hold the address of
an integer variable and an character type pointer can hold the address of char variable.
Example:
int a =20;
int *p=&a; 1001 2063
20 1001
a *p
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pointers in C
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Example of Pointer:
#include <stdio.h>
void main ()
{
int y = 5;
int *p;
p = &y;
printf("Address of y : %xn", &y );
printf("Content of p: %xn", p );
printf("Content of *p: %dn", *p );
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Summary
• We saw an introduction to C, features available in C and tokens available in C.
• Then, we started with variables, how to declare and use them.
• Next, we came across, what are the data types which we can use here in C language.
• Then we went through the input and output functions, how can we process the input and display
results.
• Operators available in C through which we can perform various operations on our data.
• Various controls statements such as if, if..else, loops and switch case.
• Next, we introduced arrays in C, how to declare and use them with examples.
• Next up, we saw what functions are, how to use them in the program and recursion.
• Later we came across Strings, how to declare and built-in functions available in string.h.
• We introduced Structures and Union with an example for each.
• A brief introduction to pointers, how to declare and use them with example.

C for beginners.pdf

  • 1.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C for Beginners
  • 2.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Agenda • Introduction to C • Variables in C • Datatypes in C • Input / Output in C • Operators in C • C Control Statements • Arrays in C • Functions in C • Strings in C • Structures and Union • Pointers in C
  • 3.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Introduction to C
  • 4.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Introduction to C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM • C was first introduced by Dennis Ritchie. • C is a procedure-oriented language. • C is a widely used and popular programming language for developing system application software. • It can be called as a mid-level programming language.
  • 5.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Introduction to C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Features: Some of the features of C Programming are:- • Easy to learn • Structured Programming language • Machine Independent or Portable • Powerful • Used in low- level as well as high level applications
  • 6.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Basic block of a C Program DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Preproccesor directives Global declarations; void main() { local declarations; statement 1; statement 2; : statement n; } User defined functions
  • 7.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Comments DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM /* This is my First C Program Author name: */ # include<stdio.h> #include<conio.h> void main() { printf(“C Programming”); // Single line Comment getch(); }
  • 8.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Tokens DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM C tokens are smallest individual units in a program. ● Identifiers- user defined names/abbreviations ● Keywords - words which are specific to C language ● Constants - values which don't change ● Strings - sequence of characters ● Operators - act on operands and generates output ● Special Symbols - used for preprocessor directives (#)
  • 9.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Variables in C
  • 10.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Variables in C What is a Variable? • It’s a name given to a memory location where the value is not fixed, it can change with the course of time. Ex : int num; Syntax: datatype variable_name; Assignment: variable_name = value
  • 11.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Scope of a Variable • Scope generally speaks about the visibility of variables. - local variable and global variable Local Global Variables in C void main () { int rollno = 10; } int y = 50; void main () { int x = 100; }
  • 12.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Data Types in C
  • 13.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Data Types in C Data Types Primitive Data type Derived Data type User Defined type Boolean Character Integer Float Double Void Array Pointer References Structure Union Enum
  • 14.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Typecasting • C allows for conversions between the basic data types - Implicit and Explicit Data Types in C int num = 5 + 13.75; // num = 18 float a = 5.25; int b = (int) a; // b = 5 Implicit conversion Explicit conversion
  • 15.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Input / Output in C
  • 16.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Input / Output in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Input and Output functions in C are:- • printf() and scanf() • gets() and puts() • getchar() and putchar()
  • 17.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Input / Output in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Format Specifiers:- The format specifiers will inform the scanf() function, what kind of input is being fed through input device. It also tells printf() function what type of data has to be printed on the output device. • %d – Integer • %c – Character • %s – String • %f - Float
  • 18.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Operators in C
  • 19.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Operators in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM • C primarily supports 5 types of operators Arithmetic Relational Bitwise Logical Assignment
  • 20.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Operators in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Arithmetic Operators:- • Used for mathematical operations • We have unary and binary operators • Unary – ( ++, --) • Binary – (+ , - , *, /, %) Relational Operators:- • Used to compare the values of two operands • Some of them are ( < , <= , >=, ==) • We can also check for equality of operands, whether an operand is greater or smaller than the other.
  • 21.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Operators in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Logical Operators: • To combine one or multiple conditions we can use Logical operators. • Logical operators always give us a Boolean value, i.e. true or false • Some of them are ( && - AND, || - OR, ! – NOT) Bitwise Operators:- • We can perform bit-level operations on the operands. • Some of them are ( & - bitwise AND, |- bitwise OR) • It converts them to bit-level first and then performs the operations.
  • 22.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Operators in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Assignment Operator:- • It is used to assign a value to a given variable. • The value on the right side of the assignment operator is assigned to operand at left side. • We also have some short hand assignment operators ( +=, -=, /=). .
  • 23.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Control Statements
  • 24.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Control Statements DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Conditional Statements:- • Also known as selection statements. • Based on the conditions specified we can make some decisions. • Anytime we execute conditional statements we get a Boolean value in return. • If the condition executed returns a true value, a set of statements are executed else another set of statements are executed.
  • 25.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Control Statements DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM The if statement:- • Selection and execution of statements based on a given condition is done by the if statement. Syntax: if (condition) { statement 1; statement 2; }
  • 26.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Control Statements DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM The if-else statement:- • Depending on the result of the condition, the if - else statement executes one of the two potential statements. Syntax: if (condition) { statement 1; // if block statement 2; } else { statement 3: // else block }
  • 27.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Control Statements DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM The Nested if-else statement:- • A nested if-else contains one or more if-else statements. if (condition1) { statement 1; if(condition2) statement 2; else statement 3; } else { statement 3: }
  • 28.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Control Statements DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Loops:- • Loops are used to re-execute a part of a code a given number of times, depending upon the condition specified. Entry controlled:- • The condition is checked each-time before entering the loop. If the condition is satisfied then only the loop body gets executed. The loop body does not get executed if the condition is false in the first iteration.
  • 29.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Control Statements DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Entry controlled loops are:- 1) For loop Syntax: for(i=0 ;i<n; i++){ //do something } 2) While Loop Syntax: while(condition is True){ //do something }
  • 30.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Control Statements DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Switch case:- ● The switch statement works as a multiway branch statement. ● It’s a modified form of if-else. ● It’s a common alternative to the if statement when you want to get multiple results. ● Default condition gets executed when no conditions are met.
  • 31.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited C Control Statements DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Switch case:- #include<stdio.h> #include<conio.h> void main() { int n= 1; switch (n) { case 1: Statement 1; break; case 2: Statement 2; break; default: Statement; }
  • 32.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Arrays in C
  • 33.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Arrays in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM ● An array is a collection of elements where the data types of all the elements are same. ● An array stores all the elements in contiguous memory locations. ● Every element in an array has a specified index. ● Size of an array in C is fixed at the time of definition We have two types in Arrays: Single dimensional Multi-dimensional 1 x 3 Array 2 x 3 Array
  • 34.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Arrays in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Definition: An array can be defined in the following manner in C: Data_type name_of_array>[<size_of_array>] Note: Size of an array should always be an integer it cannot be a real value. Example: int a[10]; //single dimensional array float b[3][3]; //float - dimensional array
  • 35.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Arrays in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Array initialization: 1) Static Initialization int a[10]={0,1,2,3,4,5,6} char c[5]={‘h’,’e’,’l’,’l’,’o’} 2) Dynamic Initialization int a[10]; //array is created with garbage value for (int i=0 ;i<10; i++) scanf(“%d”,&a[i]);
  • 36.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Functions in C
  • 37.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Functions in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM ● Functions are blocks of code which are used to perform specific tasks . ● In C a function needs to be declared before it’s used. ● Functions have a function definition , function body and a return type. ● Functions with return type except void needs to return a value at the end. ● Functions with return type void do not return any value. ● A recursive function can call itself during the course of execution of a program.
  • 38.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Functions in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM A Function example: int sum (int a, int b) { return a+b; } void main() { printf(“ %d”, sum(5,10)); }
  • 39.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Strings in C
  • 40.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Strings in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM ● Strings are used to store sequence of characters as information. ● string.h header file provides some built-in string manipulation functions. Strings can be initialized in following ways:- char a[5] = {‘H’,’o’,’l’,’a’}; char a [ ] = “Hola”;
  • 41.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Strings in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM String functions are accessible by importing <string.h>. ● strlen() Find length of string ● strcpy() Copy one string to other ● strcat() Join two strings ● strcmp() Compare two strings ● strlwr() Converts string to lowercase ● strupr() Converts string to uppercase
  • 42.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Structures and Union
  • 43.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Structures and Union DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM ● Structures provides a way for us to create our own data type called “ user-defined data type” ● Assume that we have to store the details of a student, so we can use structure to store it as below: struct student { char name[10]; int rollno; float cgpa; } ; name rollno cgpa student Structure allocates space for each variable separately
  • 44.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Structures and Union DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM ● Union also provides a way for us to create our own data type called “ user-defined data type”. ● Here each variable shares the memory collectively. union student { char name[10]; int rollno; float cgpa; } ; name rollno cgpa student Union shares the memory allocated for variables Memory
  • 45.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Pointers in C
  • 46.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Pointers in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM ● Pointers are variables which store the address of a variable. ● They have data type just like variables, for example an integer type pointer can hold the address of an integer variable and an character type pointer can hold the address of char variable. Example: int a =20; int *p=&a; 1001 2063 20 1001 a *p
  • 47.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited Pointers in C DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Example of Pointer: #include <stdio.h> void main () { int y = 5; int *p; p = &y; printf("Address of y : %xn", &y ); printf("Content of p: %xn", p ); printf("Content of *p: %dn", *p ); }
  • 48.
    Proprietary content. ©GreatLearning. All Rights Reserved. Unauthorized use or distribution prohibited DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Summary • We saw an introduction to C, features available in C and tokens available in C. • Then, we started with variables, how to declare and use them. • Next, we came across, what are the data types which we can use here in C language. • Then we went through the input and output functions, how can we process the input and display results. • Operators available in C through which we can perform various operations on our data. • Various controls statements such as if, if..else, loops and switch case. • Next, we introduced arrays in C, how to declare and use them with examples. • Next up, we saw what functions are, how to use them in the program and recursion. • Later we came across Strings, how to declare and built-in functions available in string.h. • We introduced Structures and Union with an example for each. • A brief introduction to pointers, how to declare and use them with example.