C For Beginners
C For Beginners
com
ZWM4E6SAYB C for Beginners
• Introduction to C
• Variables in C
• Datatypes in C
• Input / Output in C
•
ZWM4E6SAYB Operators in C
[email protected]
• C Control Statements
• Arrays in C
• Functions in C
• Strings in C
• Structures and Union
• Pointers in C DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
This file is meant for personal use by [email protected] only.
Proprietary
Sharingcontent. ©Great Learning.
or publishing All RightsinReserved.
the contents Unauthorized
part or full uselegal
is liable for or distribution
action. prohibited
[email protected]
ZWM4E6SAYB Introduction to C
Features:
• Powerful
• Used in low- level as well as high level applications
Preproccesor directives
Global declarations;
void main()
[email protected]
{
ZWM4E6SAYB
local declarations;
statement 1;
statement 2;
:
statement n;
}
DO NOT WRITE ANYTHING
User defined functions HERE. LEAVE THIS SPACE FOR
WEBCAM
This file is meant for personal use by [email protected] only.
Proprietary
Sharingcontent. ©Great Learning.
or publishing All RightsinReserved.
the contents Unauthorized
part or full uselegal
is liable for or distribution
action. prohibited
Comments
# include<stdio.h>
#include<conio.h>
[email protected]
ZWM4E6SAYB void main()
{
getch();
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;
[email protected]
ZWM4E6SAYB
Syntax:
datatype variable_name;
Assignment:
variable_name = value
Scope of a Variable
Local Global
Data Types
[email protected]
ZWM4E6SAYB
Typecasting
• C allows for conversions between the basic data types
- Implicit and Explicit
[email protected]
ZWM4E6SAYB
float a = 5.25;
int num = 5 + 13.75; // num = 18
int b = (int) a; // b = 5
•
ZWM4E6SAYB
getchar() and putchar()
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
[email protected]
ZWM4E6SAYB
• %c – Character
• %s – String
• %f - Float
Logical Assignment
Arithmetic Operators:-
Relational Operators:-
Logical Operators:
Bitwise Operators:-
Assignment Operator:-
Conditional Statements:-
•
ZWM4E6SAYB
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.
The if statement:-
• Selection and execution of statements based on a given condition is done by the if statement.
Syntax:
[email protected]
ZWM4E6SAYB
if (condition)
{
statement 1;
statement 2;
}
• Depending on the result of the condition, the if - else statement executes one of the two potential
statements.
Syntax:
[email protected]
ZWM4E6SAYB
if (condition)
{
statement 1; // if block
statement 2;
}
else
{ DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
statement 3: // else block WEBCAM
} This file is meant for personal use by [email protected] only.
Proprietary
Sharingcontent. ©Great Learning.
or publishing All RightsinReserved.
the contents Unauthorized
part or full uselegal
is liable for or distribution
action. prohibited
C Control Statements
if (condition1)
[email protected]{
ZWM4E6SAYB
statement 1;
if(condition2)
statement 2;
else
statement 3;
}
else
{ DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
statement 3: WEBCAM
} This file is meant for personal use by [email protected] only.
Proprietary
Sharingcontent. ©Great Learning.
or publishing All RightsinReserved.
the contents Unauthorized
part or full uselegal
is liable for or distribution
action. prohibited
C Control Statements
Loops:-
• Loops are used to re-execute a part of a code a given number of times, depending upon the
condition specified.
Entry controlled:-
[email protected]
ZWM4E6SAYB
• 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.
1) For loop
Syntax:
for(i=0 ;i<n; i++){
[email protected]
ZWM4E6SAYB
//do something
}
2) While Loop
Syntax:
while(condition is True){
//do something
}
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
This file is meant for personal use by [email protected] only.
Proprietary
Sharingcontent. ©Great Learning.
or publishing All RightsinReserved.
the contents Unauthorized
part or full uselegal
is liable for or distribution
action. prohibited
C Control Statements
Switch case:-
Switch case:-
#include<stdio.h>
#include<conio.h>
[email protected]
ZWM4E6SAYB
void main() {
int n= 1;
switch (n)
{
case 1: Statement 1;
break;
case 2: Statement 2;
break;
default: Statement; DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
} WEBCAM
This file is meant for personal use by [email protected] only.
Proprietary
Sharingcontent. ©Great Learning.
or publishing All RightsinReserved.
the contents Unauthorized
part or full uselegal
is liable for or distribution
action. prohibited
[email protected]
ZWM4E6SAYB Arrays in C
● An array is a collection of elements where the data types of all the elements are same.
Definition:
Data_type name_of_array>[<size_of_array>]
[email protected]
ZWM4E6SAYB
Note: Size of an array should always be an integer it cannot be a real value.
Example:
Array initialization:
1) Static Initialization
int a[10]={0,1,2,3,4,5,6}
[email protected]
ZWM4E6SAYB
char c[5]={‘h’,’e’,’l’,’l’,’o’}
2) Dynamic Initialization
● Functions are blocks of code which are used to perform specific tasks .
● A recursive function can call itself during the course of execution of a program.
A Function example:
void main()
{
printf(“ %d”, sum(5,10));
}
[email protected]
Strings can be initialized in following ways:-
ZWM4E6SAYB
char a [ ] = “Hola”;
● 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:
[email protected]
struct student student
ZWM4E6SAYB
{ name Structure
char name[10]; rollno allocates space
int rollno; for each variable
cgpa
float cgpa; separately
};
● Union also provides a way for us to create our own data type called “ user-defined data type”.
[email protected]
union student student
ZWM4E6SAYB
{ name Union shares the
char name[10]; rollno Memory memory
int rollno; allocated for
cgpa
float cgpa; variables
};
● 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:
[email protected]
ZWM4E6SAYB
int a =20;
int *p=&a; 1001 2063
20 1001
a *p
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
This file is meant for personal use by [email protected] only.
Proprietary
Sharingcontent. ©Great Learning.
or publishing All RightsinReserved.
the contents Unauthorized
part or full uselegal
is liable for or distribution
action. prohibited
Pointers in C
Example of Pointer:
#include <stdio.h>
void main ()
{
[email protected]
int y = 5;
ZWM4E6SAYB
int *p;
p = &y;
printf("Address of y : %x\n", &y );
printf("Content of p: %x\n", p );
printf("Content of *p: %d\n", *p );
}
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
This file is meant for personal use by [email protected] only.
Proprietary
Sharingcontent. ©Great Learning.
or publishing All RightsinReserved.
the contents Unauthorized
part or full uselegal
is liable for or distribution
action. prohibited
Summary
•
ZWM4E6SAYB
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. DO NOT WRITE ANYTHING
• A brief introduction to pointers, how to declare and use them with example. HERE. LEAVE THIS SPACE FOR
WEBCAM
This file is meant for personal use by [email protected] only.
Proprietary
Sharingcontent. ©Great Learning.
or publishing All RightsinReserved.
the contents Unauthorized
part or full uselegal
is liable for or distribution
action. prohibited