Beruflich Dokumente
Kultur Dokumente
C data types are defined as the data storage format that a variable can store
a data to perform a specific operation.
Data types are used to define a variable before to use in a program.
Size of variable, constant and array are determined by data types.
BASIC DATA TYPES IN C LANGUAGE:
1.1. INTEGER DATA TYPE:
Integer data type allows a variable to store numeric values.
“int” keyword is used to refer integer data type.
The storage size of int data type is 2 or 4 or 8 byte.
It varies depend upon the processor in the CPU that we use. If we are using 16
bit processor, 2 byte (16 bit) of memory will be allocated for int data type.
Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of
memory for 64 bit processor is allocated for int datatype.
int (2 byte) can store values from -32,768 to +32,767
int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
If you want to use the integer value that crosses the above limit, you can go for
“long int” and “long long int” for which the limits are very high.
1.2. CHARACTER DATA TYPE:
Character data type allows a variable to store only one character.
Storage size of character data type is 1. We can store only one character using
character data type.
“char” keyword is used to refer character data type.
For example, ‘A’ can be stored using char datatype. You can’t store more than
one character using char data type.
Please refer C – Strings topic to know how to store more than one characters in
a variable.
1.3. FLOATING POINT DATA TYPE:
Floating point data type consists of 2 types. They are
1. float
2. double
1. FLOAT:
Float data type allows a variable to store decimal values.
Storage size of float data type is 4. This also varies depend upon the processor
in the CPU as “int” data type.
We can use up-to 6 digits after decimal using float data type.
For example, 10.456789 can be stored in a variable using float data type.
2. DOUBLE:
Double data type is also same as float data type which allows up-to 10 digits
after decimal.
The range for double datatype is from 1E–37 to 1E+37.
Example:
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
return 0;
}
OUTPUT:
Arithmetic Operators
#include <stdio.h>
int main()
return 0;
Output:
Sum: 35
Difference: 15
Product: 250
Quotient: 2
Remainder: 5
Relational operator
Relational operators are binary operators(operates on two operands) and are used
to relate or compare two operands. There are four relational operators in C (i.e <,
<=, >, >=). If the relationship between the operands is correct, it will return 1 and
returns 0 otherwise.
Apart from four relational operators, C has two equality operator (== and !=) as
well for comparing operands. Now let's take a look at different relational and
equality operators and how they operate on the operands.
#include <stdio.h>
int main()
return 0;
OUTPUT:
25 < 10 : 0
25 <= 10: 0
25 > 10 : 1
25 >= 10: 1
25 == 10: 0
25 != 10: 1
A switch statement tests the value of a variable and compares it with multiple
cases. Once the case match is found, a block of statements associated with that
particular case is executed.
If a case match is found, then the default statement is executed, and the control
goes out of the switch block.
SYNTAX:
switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
Statement-x;
EXAMPLE:
include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}
RESULT:
Value is 8
CODE:
#include <stdio.h>
#include <math.h>
#define PI 3.142
void main()
{
float radius, area;
OUTPUT:
An array can be of any type, For example: int , float , char etc. If an array is of
type int then it's elements must be of type int only. ... An array of one
dimension is known as a one-dimensional array or 1-D array
SYNTAX:
datatype anArrayname[sizeofArray];
EXAMPLE:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], n;
printf("How many element you want to store in the array ? ");
scanf("%d",&n);
printf("Enter %d element to store in the array : ",n);
for(int i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("The Elements in the Array is : \n");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
getch();
}
#include <stdio.h>
int main(void)
printf("Main\n");
int view()
printf("View\n");
return 1;
}
printf("GEEKS");
return 0;
OUTPUT :
view
Main
GEEKS
SYNTAX:
EXAMPLE:
#include <stdio.h>
#include <string.h>
int main()
{
char string[40];
strcpy(str, "This is a test string");
puts(string);
return 0;
}
RESULT:
This is a test string
Putchar():
SYNTAX:
#include <stdio.h>
int main()
{
return (0);
}
RESULT:
SYNTAX:
if (condition 1)
{
// block 1
else if (codition 2)
// block 2
else if(codition 3)
// block 3
else
// default block
EXAMPLE:
#include<stdio.h>
#include<conio.h>
void main()
int x, y, z, ch;
clrscr();
printf("\n1.Addition\n2.Subtraction\n3.Multiplication\4.Division\n");
scanf("%d", &ch);
if (ch == 1)
z = x + y;
else if (ch == 2)
z = x - y;
}
else if(ch == 3)
z = x * y;
else if(c == 4)
z = x/y ;
else
getch();
Link Provides instruction to the compiler to link function from the library function
main( ) Every C program must have a main() function which is the starting point of
{ the program execution.
gets():
Reads characters from the standard input (stdin) and stores them as a C string into
str until a newline character or the end-of-file is reached.
SYNTAX:
char * gets ( char * str );
EXAMPLE:
#include <stdio.h>
#define MAX 15
int main()
{
char buf[MAX];
return 0;
}
RESULT:
INPUT: HELLO
OUTPUT: HELLO
getchar() :
SYNTAX:
int getchar(void);
EXAMPLES:
include <stdio.h>
int main()
{
printf("%c", getchar());
return 0;
}
RESULT:
INPUT : HELLO
OUTPUT:HELLO
SECTION C
A while loop is the most straightforward looping structure. The basic format of
while loop is as follows:
while (condition) {
statements;
}
It is an entry-controlled loop. In while loop, a condition is evaluated before
processing a body of the loop. If a condition is true then and only then the body of
a loop is executed. After the body of a loop is executed then control again goes
back at the beginning, and the condition is checked if it is true, the same process is
executed until the condition becomes false. Once the condition becomes false, the
control goes out of the loop.
After exiting the loop, the control goes to the statements which are immediately
after the loop. The body of a loop can contain more than one statement. If it
contains only one statement, then the curly braces are not compulsory. It is a good
practice though to use the curly braces even we have a single statement in the
body.
In while loop, if the condition is not true, then the body of a loop will not be
executed, not even once. It is different in do while loop which we will see shortly.
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Do-While loop
A do-while loop is similar to the while loop except that the condition is always
executed after the body of a loop. It is also called an exit-controlled loop.
do {
statements
} while (expression);
As we saw in a while loop, the body is executed if and only if the condition is true.
In some cases, we have to execute a body of the loop at least once even if the
condition is false. This type of operation can be achieved by using a do-while loop.
In the do-while loop, the body of a loop is always executed at least once. After the
body is executed, then it checks the condition. If the condition is true, then it will
again execute the body of a loop otherwise control is transferred out of the loop.
Similar to the while loop, once the control goes out of the loop the statements
which are immediately after the loop is executed.
The critical difference between the while and do-while loop is that in while loop
the while is written at the beginning. In do-while loop, the while condition is
written at the end and terminates with a semi-colon (;)
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
Output:
2
4
6
8
10
12
14
16
18
20
A for loop is a more efficient loop structure in 'C' programming. The general
structure of for loop is as follows:
CODE:
#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
{
printf("%d\n",number); //to print the number
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
SYNTAX:
Data_Type Array_Name[Row_Size][Column_Size]
Data_type: This will decide the type of elements will accept by two
dimensional array in C. For example, If we want to store integer values then
we declare the Data Type as int, If we want to store Float values then we
declare the Data Type as float etc
Array_Name: This is the name you want to give it to this C two dimensional
array. For example students, age, marks, employees etc
Row_Size: Number of Row elements an array can store. For example,
Row_Size =10 then array will have 10 rows.
Column_Size: Number of Column elements an array can store. For example,
Column_Size = 8 then array will have 8 Columns.
EXAMPLE:
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
RESULT:
Each and every small unit in c program is known as tokens. We can also say that
tokens are the basic buildings blocks in C language which are constructed together
to write a C program. Tokens are of 6 types.
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. Operators
Keywords are pre-defined words in a C compiler.Each keyword is meant to
perform a specific function in a C program.Since keywords are referred names for
compiler, they can’t be used as variable name. Eg: int,void,long.
Identifiers are the name give to each program element in program.Names given to
identify Variables, functions and arrays are examples for identifiers.For example
int x; here int is the keyword and x is the identifier.
Constants are variables whose values can not be modified by the program once
they are defined. Constants have a fixed value and they are also known as
literals.Keyword const is used before datatype .
Strings are array of characters terminated by a null character. For example a string
‘toy’ has got 4 characters in it ‘t’ , ‘o’ , ‘y’ & the null character.
Symbols other than the Alphabets and Digits and white-spaces are called Special
symbols. Eg: * , & ,^ ,@, etc.
CODE:
#include <stdio.h>
void main()
{ int num1, num2, num3;
OUTPUT:
Formal Parameter : A variable and its type as they appear in the prototype
of the function or method.
Actual Parameter : The variable or expression corresponding to a formal
parameter that appears in the function or method call in the calling
environment.
Modes:
IN: Passes info from caller to calle.
OUT: Callee writes values in caller.
IN/OUT: Caller tells callee value of variable, which may be updated by
callee.
Pass By Value : This method uses in-mode semantics. Changes made to formal
parameter do not get transmitted back to the caller. Any modifications to the
formal parameter variable inside the called function or method affect only the
separate storage location and will not be reflected in the actual parameter in the
calling environment. This method is also called as call by value.
CODE:
#include <stdio.h>
a += b;
int main(void)
int x = 5, y = 7;
// Passing parameters
func(x, y);
return 0;
OUTPUT:
In func, a = 12 b = 7
In main, x = 5 y = 7
CODE:
#include <stdio.h>
*i = *j;
*j = temp;
int main(void)
swapnum(&a, &b);
return 0;
EXAMPLE:
a is 20 and b is 10