Microsoft Word - Chapter-1 Introduction & Datatypes
Microsoft Word - Chapter-1 Introduction & Datatypes
#include<stdio.h>
# -> hash is known as preprocessor directive. It instructs the compiler, to do whatever is
written after it.
Compiler -> It is a translator, which translates instructions written in high level language
(source code) into low level language (object code). It can translate the whole code at
once.
stdio -> Full form is Standard Input Output. It is a header file (need to write the name
of this file at the top of the program). This header file contains many predefined functions. We
can use these predefined functions as per our need.
.h -> It is the file extension for header file. It indicates the type of file.
Predefined Function: - These functions were defined by the developer at the time of
creation of header files in C programming language. These functions can perform specific task.
If we want to use these functions then we simply need to call the function.
Example: getch();
#include<stdio.h>
# Preprocessor directive instructs the compiler to include the stdio.h header file
in the program before the actual compilation begins.
void main()
void -> It is a valueless return type, which means it do not return anything after the
complete execution of the function.
main() -> It is a user defined function. It is the starting point of the program, which
means the execution of the program begins with this point. This function is called by the
Operating system.
( -> left parenthesis (opening parenthesis)
) -> right parenthesis (closing parenthesis)
User-defined Function: - These functions are defined by programmers i.e. users at as per
his/her need.
{ -> Left Curly Braces/(Opening curly bracket/braces)
} -> Right Curly Braces/(Closing curly bracket/braces)
Example-1
#include<stdio.h>
void main()
{
printf("Hello World");
}
printf(); -> printf() is a pre-defined function. It is defined inside the stdio.h header file.
printf() is a output function. This function is basically used to print/display the message or the
value of any variable on the console (output) screen.
Note:-
1. If we want to display a message then we need to write the message between the
double quotations mark (“ “).
2. If we want to display the value of a variable then we need to write the name of the
variable outside the double quotations mark (“ “).
\n -> It is a new line escape sequence character. It is used to enter a new line, which means
it finishes the current line and moves the cursor at the beginning of the new line.
Program: -
Method-1
#include<stdio.h>
void main()
{
printf("Hello\n");
printf("All");
}
Method-2
#include<stdio.h>
void main()
{
printf("Hello\nAll");
}
Output:-
Hello
All
Program-2
#include<stdio.h>
void main()
{
printf("Hello all,\n");
printf("My name is ……..");
}
#include<stdio.h>
void main()
{
printf("1\n2 3\n4 5 6\n");
}
#include<stdio.h>
void main()
{
printf(" *\n");
printf(" * *\n");
printf(" * * *\n");
printf(" * * * *\n");
}
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hello”);
getch();
}
conio:- Full form of conio is Console Input Output. This header file has many console
related pre-defined Input/Output functions. Console means the output screen.
clrscr(); -> It is a predefined function, it’s full form is Clear Screen. This function is defined
inside the conio.h header file. This function is used to clear the console (output)
screen (means it can clear the old data that was displayed on the screen). This function will
place after the variable declaration in a C program.
getch(); -> It is a predefined Input function, It’s full form is get a character. This function is
defined inside the conio.h header file.
It is an input function and it takes a single character as an input. We can use
this function to hold the output screen, because until and unless we will not press any key
from the keyboard till then it can hold the screen, which means the next statement will not
execute until we will not press any key.
Comments: - Comments are those statements which are ignored by the compiler at the
time of compilation. We use comments for better and clear understanding of any statement.
Types of Comments: -
1. Single Line comment
2. Multi Line comment
Character Set: - Character set is the set of different characters available in C programming
language. Character set consist of the following: -
1. Uppercase letters (A-Z)
2. Lowercase letters (a-z)
3. Digits (0 to 9)
4. Special Symbols (, * $ % . @ # etc)
5. White Spaces (blank spaces)
White Space: - White space is the blank space which is ignored by the compiler unless it is
not the part of a string. White spaces are used to separate the words, or enter new line also. In
c language there are following types of white spaces available: -
1. Blank space
2. Horizontal tab ( \t )
3. New line ( \n )
4. Carriage Return ( \r )
5. Form feed ( \f )
Tokens
1. Keyword: - Keywords are the reserved words, that have a specific meaning and we
can’t change the meaning of any keyword.
Keywords are the basic building blocks for a program statement. In C language there are
32 keywords.
ANSI C Keywords: -
auto double int struct
break else long switch
case enum register typeof
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
2. Identifiers: - Identifiers refer to the name of variables, functions, arrays, etc. These
names are user defined and consist of sequence of underscore, letters and digits.
Constant
Numeric Character
Constant Constant
4. String: - String is the sequence of characters enclosed inside the double quotation
marks.
Example: - “Hello All”
Data Types: - A data type is used to declare the type of data that a variable can store and it
also indicates that how much memory is required to store the particular type of data.
In simple words we can say that data type tells the type of data and also tells how much
memory that data will use for storage.
1. Basic Data Types: - These are also known as Primary data types and
primitive data types or pre-defined data types. There some basic data types
available in C language that are as follow:
1. int
2. char
3. float
4. long
5. double
1. int (Integer Data Type): - Integer data type is used to store integer
numbers. This data type can occupy 2 bytes of memory in 16-bit System and
4 Bytes of memory in 32-bit or more bits System. It can store both negative
and positive numbers.
Example: - 5, 20, -25, 30, -358 etc.
Note:- Format specifier used for int data type is %d.
Types of int : -
1. signed int
2. unsigned int
1. signed int: - It have both positive (+ve) and negative (-ve) values.
Example: - 5, -32, 67, 6578, -765
Note: -
a. If we want to store any single character then we need to write that character
value inside the single quotation marks (‘ ‘).
Example: - char value=’a’;
b. If we want to store any string then we need to write that string value inside the
double quotation marks (“ “). For string we need to give the size of string so
that we can store more characters.
Example: - char name[10]=”Jhon”;
Format specifiers:-
The format specifiers are used in C for input and output purposes. Using this concept the compiler
can understand that what type of data is in a variable during taking input using the scanf() function
and printing using printf() function. Here is a list of format specifiers.
%c Character
%d Signed integer
%f Float values
%i Unsigned integer
%lf Double
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
Program-1
//WAP to print value of a number.
#include<stdio.h>
void main()
{
int n=10;
printf("Value of n=%d",n);
}
output:-
Value of n=10
Program-2
//WAP to add two numbers.
#include<stdio.h>
void main()
{
int a=10,b=20,sum;
sum=a+b;
printf("Sum=%d",sum);
}
Program-3
//WAP to multiply two numbers.
#include<stdio.h>
void main()
{
int a=10,b=20,multiply;
multiply=a*b;
printf("Multiplication=%d",multiply);
}
Program-4
//WAP to divide one number by another number.
#include<stdio.h>
void main()
{
int a=10,b=20,div;
div=a/b;
printf(“Division=%d",div);
}
Program-5
//WAP to subtract one number from another number.
#include<stdio.h>
void main()
{
int a=50,b=5,sub;
sub=a-b;
printf("Subtraction=%d",sub);
}
scanf():-> It is an input function which is defined inside the stdio.h header file we use this
function to take input from user. We used &(ampersand)sign with the name of variable to
refer it's address so that the value of variable can be stored at the location of the variable.
Syntax of scanf(): -
scanf(“format specifier”, &variablename);
Example: -
int a;
scanf(“%d”,&a);
Program:-6
//WAP to enter a number.
#include<stdio.h>
void main()
{
int n;
printf("Enter a number:");
scanf("%d",&a);
printf("Entered number is=%d",a);
}
Program:-7
//WAP to add two numbers.
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter 1st number:");
scanf("%d",&a);
printf("Enter 2nd number:");
scanf("%d",&b);
sum=a+b;
printf("Sum=%d",sum);
}
Program-8
//WAP to calculate the equation (a*a)+(2*a*b)+(b*b)
#include<stdio.h>
void main()
{
int a,b,result;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
result=(a*a)+(2*a*b)+(b*b);
printf("Result=%d",result);
}
Program-9
//WAP to enter a floating-point number.
#include<stdio.h>
void main()
{
float a;
printf("Enter a decimal number:");
scanf("%f",&a);
printf("Entered number is=%f",a);
}
Program-10
//WAP to add two floating-point numbers.
#include<stdio.h>
void main()
{
float a,b,sum;
printf("Enter 1st decimal number:");
scanf("%f",&a);
printf("Enter 2nd decimal number:");
scanf("%f",&b);
sum=a+b;
printf("Sum=%f",sum);
}
Program-11
//WAP to enter 5 numbers and calculate the average of those floating point
numbers.
#include<stdio.h>
void main()
{
float a,b,c,d,e,avg;
printf("Enter 5 numbers");
scnaf("%f %f %f %f %f",&a,&b,&c,&d,&e);
avg=(a+b+c+d+e)/5;
printf("Average=%f",avg);
}
Program-1
/*WAP to print a floating-point number with only 2 precision digits.*/
#include<stdio.h>
void main()
{
float f;
printf("Enter a number:");
scanf("%f",&f);
printf("Number= %0.2f",f);
}
Program-2
/*WAP to enter 2 numbers and perform division operation and print the result
with only 2 precision digits.*/
#include<stdio.h>
void main()
{
float a,b,div;
printf("Enter 1st number:");
scanf("%f",&a);
printf("Enter 2nd number:");
scanf("%f",&b);
div=a/b;
printf("Division= %0.2f",div);
}
#include<stdio.h>
void main()
{
char msg[10]="hello";
printf("%s",msg);
}
#include<stdio.h>
void main()
{
char name[10];
printf("Enter your name:");
scanf("%s",name);
printf("Hello %s, Welcome to the class",name);
}
WAP to enter name and salary of employee, and calculate annual salary of that
employee. (annual_salary=salary*12)
Output:-
Name of employee :
Salary :
Annual Salary :
% 0.numberofdigits f
or
% .numberofdigits f
eg:- 2.55
%0.2f -> 2.55
%0.7f -> 2.5500000
%.3f -> 2.550
Note:- This statement can be used with only printf() function, we cannot use it in
scanf() function.
/*WAP to print a floating point number that have 2 precision digits after .(point)*/
#include<stdio.h>
void main()
{
float x=2.6357;
printf("Value of X=%0.2f",x);
}
output:-
Value of X=2.64