Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

1 Learning in C Program (1)

Uploaded by

likithkalyan2138
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

1 Learning in C Program (1)

Uploaded by

likithkalyan2138
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 170

Learning In C-

Program
Data Types in C Programming

What are Data Types in C?


Data types in C refer to the various types of data, such as
integer and float, that a C program can process. Each type
of data is represented by a specific keyword and is used
within a program to perform calculations, represent objects,
or store strings.

• A data type defines how a certain operation will be


performed on the data item specified as its arguments
such as an arithmetic operation or a comparison test.
• Data types also determine the amount of memory
allocated for storing a particular type of value.
• Data types are essential to C programming since they are
necessary to define the items used within any program.
Data Types in C Programming

Types Data Types


Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
Data Types in C Programming

1. Basic Data Types in C


• The fundamental data types in C, such as integers, float, characters, etc., are
used to represent simple values and are known as the "basic data types."
1. int
• The int data type is used to store integers (whole numbers) like -1, 0, 42, etc.
Syntax
• int variable_name;

Data Type Format Specifier Range Memory Size (in


bytes)
Int %d -2,147,483,648 to 4
2,147,483,647
short int %hd -32,768 to 32,767 2
unsigned int %u 0 to 4,294,967,295 4
unsigned short int %hu 0 to 65,535 2
long int %ld -2,147,483,648 to 4
2,147,483,647
unsigned long int %lu 0 to 4,294,967,295 4
long long int %lld -(2^63) to (2^63)-1 8
unsigned long long %llu 0 to 8
int 18,446,744,073,709
,551,615
Data Types in C Programming
Example of int Data Type in C - Compiler
2. char
The char data type is used to store single characters like 'A', '1',
or '$'. The character must be surrounded by single quotes and we
must use the %c format specifier to print it.
Syntax
char variable_name;

Example of char Data Type in C


#include
<stdio.h>
int main()
{
char myLetter =
'S'; printf("%c",
myLetter); return
0;
Representing single characters using ASCII
}
values
#include <stdio.h>
int main()
{
char a = 87, b = 66, c
= 75; printf("%c\n", a);
printf("%c\n", b);
printf("%c", c); return
3. float
The float data type is used to store single-precision floating-point numbers,
which can represent decimal values with limited precision. This data type size
is 4 bytes, i.e., it occupies 4 bytes of memory. It ranges between +/- 3.4E-38F
up until +/- 1 .7E+308F depending on the system’s architecture.

Syntax
float variable_name;
Example of float Data Type in C
#include <stdio.h>
int main() {
float myNum =
6.85;
printf("%f",
myNum);
4. double return 0;
}
The double data type is used to store double-precision floating-point numbers
i.e. 64 bits of decimal numbers or floating points, which offer higher precision
than float for decimal values. This data type size is 8 bytes, i.e., it occupies 8
bytes of memory. It ranges between 1.7E-308 to 1.7E+308.
Syntax #include <stdio.h>
double variable_name; int main()
{
double num = 20.63;
printf("%lf", num);
return 0;
}
Tricks To Remember Operators
Precedence Rule In C Programming
Language.
Examples showing use of Arithmetic
Operators
Examples showing use of Relational
Operators
Examples showing use of Logical
Operators
Examples showing use of Bitwise
Operators
Examples showing use of Increment and
Decrement Operators
Examples showing Examples showing
use of Increment use of Decrement
Operators Operators
Examples showing use of Assignment
Operators
#include <stdio.h>

int main() {
int x = 5;
x += 3;
printf("The value of x is %d\n",
x);
return 0;
}
#include <stdio.h>
#include <stdio.h>
int main() {
int main() { int x = 5;
int x = 10; x *= 4;
x -= 3; printf("The value of x is %d\n",
printf("The value of x is %d\n", x);
x);
return 0; return 0;
} }
Examples showing use of Conditional
Operators
Practice Problems on Operator Precedence and
Associativity in C
#include <stdio.h> #include <stdio.h> int main()
int main() int main() {
{ { int x = 50, y = 5;
int a = 11, b = 5, c = 2, int x = 5, y = 10; int z;
d=4, e=8; int z; z = x / 2 + y;
// int result = a / b * c; z = x * 2 + y; printf(“\n output
int result = a / (b * (c+ printf(“\n output = = %d”, z);
(d-e))); %d”, z); return 0;
printf("%d\n", result); return 0; } Output:30
return 0; } Output:20
int int main()
} main()
{ {
int x = 30, y = 12; int x = 80, y = 3;
int z; int z;
z = (x * 2 )/ y; z = x / 2 * y;
printf(“\n output = printf(“\n output =
%d”, z); %d”, z);
return 0; return 0;
} Output:5 } Output:120
Flowchart of if statement in C #include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers:");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
#include<stdio.h> {
int main() printf("%d is largest",b);
{ }
int number=0; if(c>a && c>b)
printf("Enter a number:"); {
scanf("%d",&number); printf("%d is largest",c);
if(number%2==0) }
{ if(a == b && a == c)
printf("%d is even number",num {
ber); printf("All are equal");
} }
return 0; return 0;
} }
Flowchart of the if-else statement in C
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to
#include<stdio.h>
int main()
vote...");
{ }
int number=0; else
printf("enter a number:"); {
scanf("%d",&number); printf("Sorry ... you can't
if(number%2==0) vote");
{ }
printf("%d is even
}
number",number);
}
Else
{
printf("%d is odd
number",number);
}
Nested if else statement in C
Syntax:
A nested if-else statement is an if statement inside another if
statement. The general syntax of nested if-else statement in C is
as follows:
#include <stdio.h>
int main()
{
int i = 10;
if (i == 10)
{
if (i < 15) // First if statement.
printf("i is smaller than 15\n"); // Nested -
if statement.
// Will only be executed if
statement above is true.
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
Nested if else statement in C Examples:
#include <stdio.h> Example 1: Grading System
Problem Statement: Determine the grade of a student based on their score. If
int main() the score is above 90, it's an 'A'. If the score is between 80 and 90, check if it's
{ above 85 for an 'A-' or else 'B+'. Below 80, if it's above 70, it's a 'C', otherwise,
it's a 'D'.
int score = 88;
// Check if score is greater than 90
if (score > 90)
{
printf("Grade: A");
} else
{
// Score is 90 or below, check if score is greater than 80
if (score > 80) {
// Score is between 81 and 90, check if score is
above 85
if (score > 85) {
printf("Grade: A-");
} else {
printf("Grade: B+");
}
} else {
// Score is 80 or below, check if score is above 70
if (score > 70) {
printf("Grade: C");
} else {
printf("Grade: D");
}
}
}
return 0;
}
If else-if ladder Statement
The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where
there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a
condition is true then the statements defined in the if block will be executed, otherwise if some other
condition is true then the statements defined in the else-if block will be executed, at the last if none of
the condition is true then the statements defined in the else block will be executed. There are multiple
else-if blocks possible. It is similar to the switch case statement where the default is executed instead
of else block if none of the cases is matched.
if(condition1){
//code to be executed if
condition1 is true
}else if(condition2){
//code to be executed if
condition2 is true
}
else if(condition3){
//code to be executed if
condition3 is true
}
...
else{
//code to be executed if all the
conditions are false
}
The example of an if-else-if ladder statement in C
language is given below.
#include <stdio.h>
#include<stdio.h> int main()
int main(){ {
int number=0; int marks;
printf("enter a number:"); printf("Enter your marks?");
scanf("%d",&number); scanf("%d",&marks);
if(number==10){ if(marks > 85 && marks <= 100)
printf("number is equals to 10"); {
} printf("Congrats ! you scored
else if(number==50){ grade A ...");
printf("number is equal to 50"); }
} else if (marks > 60 && marks <=
else if(number==100){ 85)
printf("number is equal to 100"); {
} printf("You scored grade B
else{ + ...");
printf("number is not equal to 10, 50 }
or 100"); else if (marks > 40 && marks <=
} 60)
return 0; {
} printf("You scored grade B ...");
}
else if (marks > 30 && marks <=
40)
{
printf("You scored grade C ...");
}
else
{
C Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute
multiple operations for the different possible values of a single variable called switch variable. Here, We
can define various statements in the multiple cases for the different values of a single variable.
Rules for switch statement in C language
1.The switch expression must be of an integer or character type.
2.The case value must be an integer or character constant.
3.The case value can be used only inside the switch statement.
4.The break statement in switch case is not must. It is optional. If there is no break statement
found in the case, all the cases will be executed present after the matched case. It is known
as fall through the state of C switch statement.

Valid Switch Invalid Valid Case Invalid Case


Switch
switch(x) switch(f) case 3; case 2.5;
switch(x+2.
switch(x>y) case 'a'; case x;
5)
switch(a+b-
case 1+2; case x+2;
2)
switch(func(x
case 'x'>'y'; case 1,2,3;
,y))
#include <stdio.h>
#include<stdio.h>
int main(){
int main() {
int number=0;
int num = 2;
printf("enter a number:");
switch (num) {
scanf("%d",&number);
case 1:
switch(number){
printf("Value is 1\n");
case 10:
break;
printf("number is equals to 10");
case 2:
break;
printf("Value is 2\n");
case 50:
break;
printf("number is equal to 50");
case 3:
break;
printf("Value is 3\n");
case 100:
break;
printf("number is equal to 100");
default:
break;
printf("Value is not 1, 2, or
default:
3\n");
printf("number is not equal to 10, 50
break;
or 100");
}
}
return 0;
return 0;
}
}
#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x>y && x+y>0)
{
case 1:
printf("hi");
break;
case 0:
printf("bye");
break;
default:
printf(" Hello bye ");
}

}
Thanking you

You might also like