0% found this document useful (0 votes)
40 views27 pages

Microsoft Word - Chapter-1 Introduction & Datatypes

The document provides an introduction to C programming, including the structure of a C program, data types, and basic concepts like comments, tokens, constants, and strings. It discusses the main components of a C program like preprocessor directives, header files, functions, and standard input/output functions. Examples are given to demonstrate printing output, using escape sequences, and writing simple C programs. Key topics covered include data types, variables, functions, I/O functions, comments, and basic syntax rules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views27 pages

Microsoft Word - Chapter-1 Introduction & Datatypes

The document provides an introduction to C programming, including the structure of a C program, data types, and basic concepts like comments, tokens, constants, and strings. It discusses the main components of a C program like preprocessor directives, header files, functions, and standard input/output functions. Examples are given to demonstrate printing output, using escape sequences, and writing simple C programs. Key topics covered include data types, variables, functions, I/O functions, comments, and basic syntax rules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Chapter-1

Introduction and Data types


Structure of a C program: -
#include<stdio.h>
void main()
{
Body of main function
}

First Program of C Language


#include<stdio.h>
void main()
{
printf("Hello All");
}

#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.

C programming language --> High level language

include -> to insert/ add


<  left angular bracket
>  Right angular bracket

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 ……..");
}

Program-3 WAP to print the following output:


1
23
456

#include<stdio.h>
void main()
{
printf("1\n2 3\n4 5 6\n");
}

Q1. WAP to print the following pattern


1
23
456
1234
567
89
0

Q2. WAP to print


*
**
***
****

#include<stdio.h>
void main()
{
printf(" *\n");
printf(" * *\n");
printf(" * * *\n");
printf(" * * * *\n");
}

Q3. WAP to print


*
**
***
****
***
**
*
\t : -> Tabbed escape sequence. It is used to insert tabbed space in the statement.
Program-1
#include<stdio.h>
void main()
{
printf("Hello\tall");
}
Output:-
Hello all

Q. WAP to pint the following output: -


1
2
3
4
5
#include<stdio.h>
void main()
{
printf("1\n");
printf("\t2\n");
printf("\t\t3\n");
printf("\t\t\t4\n");
printf("\t\t\t\t5\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.

Shortcuts of Turbo C/C++


1. Ctrl+F9 -> To execute/run the program
2. Alt+F9 -> To compile the program
3. Alt+Enter -> Maximize and restore down the turbo c/c++ window
4. Alt+F5 -> To open the user screen (console window)

Note:- C language is a Case-sensitive language. That means uppercase (A-Z) and


lowercase (a-z) letters have different meaning and behavior in C language.
Example:- If we declare A and a as variables then both have different meaning in c
language.
int A;
int a;

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

1. Single Line Comment: - Single line comment is represented by //. If we want to


make a single line as comment then we will use // and will write that statement after //.
That statement is ignored by compiler.
Example: - int a; // Variable declaration
2. Multi Line Comment: - Multiline comment is represented by /*………*/. If we
want to make multiple line as comment then we will write those lines between /* and
*/. Those statements will be ignored by compiler.
Example: - printf(“Hello”); /* printf is an output function which is used to display
the message and value on the console screen */

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: - The smallest individual unit of program is known as token.

Tokens

Keywords Identifiers Constants Strings Operators Special


Symbols

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.

Rules to write Identifier (nomenclature rules): -


1. Name of variable, function or array consist of alphabets (A-Z or a-z), digits (0-9) or
underscore (_).
2. The name of variable, function or array can start with only any letter (A-Z or a-z) or
underscore (_).
3. The name cannot start with any digit (0-9).
4. White spaces are not allowed in the name.
5. Name cannot be any keyword or reserved word.
6. Cannot use any special symbol other than underscore (_).

Examples for valid names: -


1. Name
2. Roll_no
3. roll_no
4. _fee
5. Fee_12
6. A1
7. a1

Examples for invalid names: -


1. 1a
2. Roll number
3. @id
4. Name$
3. Constant: - Constants are also known as Literals. These refer to fixed values that
cannot change during the execution of a program.

Constant

Numeric Character
Constant Constant

Integer Floating Single String


Point character

4. String: - String is the sequence of characters enclosed inside the double quotation
marks.
Example: - “Hello All”

5. Operators: - An operator is a special symbol which tells the compiler as well as OS


to perform specific mathematical and logical operations on operands.
Example: - +, -, /, *, <, >, etc.
Result = a + b
Where: 1. =, + both are operators.
2. a, b, Result -> operands.
Categories of Operators
Categories of
Operators

Unary Binary Ternary


Operators Operators Operators

Different types of Operators in C Programming Language: -


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Conditional Operators
5. Assignment Operators
6. Bitwise Operators
7. Increment and Decrement Operators
8. Special Operators (Miscellaneous Operators)

6. Special Symbols: - Special symbols are some specific characters used in C


language, which have a specific meaning that cannot be changed.

Some Symbols used in C language:


o Square brackets [ ]: The opening and closing brackets represent the single
sing and
multidimensional subscripts.
o Simple brackets ( ): It is used in function declaration and function calling. For
example, printf() is a pre--defined function.
o Semicolon ; -> It is used to terminate the statement.
o Curly braces { }: It is used in the opening and closing of the code. It is used in the
opening and closing of the loops.
o Comma (,): It is used for separating for more than one statement and for example,
separating function parameters in a function call, separating the variable when printing
the value of more than one variable using a single printf statement.
o Hash/pre-processor
processor (#): It is used for pre-processor
processor directive. It basically
denotes that we are using the header file.
o Asterisk (*): This symbol is used to represent pointers and also used as an operator
for multiplication.
o Tilde (~): It is used as a destructor to free memory.
o Period (.): It is used to access a member of a structure or a union.
o Backslash \ : It is used with escape sequence characters.
o Percentage % : It is used with wild card characters. Etc.
Data-> Data is raw fact and figure. It is meaningless.
Information-> Information is the processed data means the result produced after processing is
known as information. It is meaningful.

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.

Categories of Data type

Basic Data Types Derived Data Types User Defined Data


Types

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

2. unsigned int: - It have only positive values (+ve).


Example: - 0, 25, 7895, 35
Range of int data type (16-bit system) memory occupied-2bytes
signed int:
-32768 to +32767
unsigned int:
0 to 65535

Range of int data type (32-bit system) memory occupied- 4bytes


signed int:
-2,147,483,648 to 2,147,483,647
unsigned int:
0 to 4,294,967,295

Formula for calculating range of data type:


1. For Signed data type
(n-1) (n-1)
-2 to +2 -1
Where n=number of bits

Example: - Find range of char data type?


Solution: - Size occupied by char data type = 1 byte
1 byte=8 bits
n=8
=> -2(n-1) to +2(n-1)-1
=> -2(8-1) to +2(8-1)-1
=> -2(7) to +2(7)-1
=> -128 to + (128-1)
=> -128 to +127
20 1
21 2
22 4
23 8
24 16
25 32
26 64
27 128
28 256
29 512
210 1024
211 2048
212 5096

1. For unsigned data type


0 to +2n-1
Where n=number of bits
Example: - Find range of char data type?
Solution: - Size occupied by char data type = 1 byte
1 byte=8 bits
n=8
0 to +2n-1
8
0 to 2 -1
0 to 256-1
0 to 255
2. char (Character data type): - This data type is used to store character value.
It occupies 1 byte of memory to store any single character.

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”;

Note: - 1. For single character char uses %c format specifier.


2. For string char uses %s format specifier.

3. float (Floating-point data type): - This data type is used to store


decimal means floating point values. It occupies 4 bytes of memory.
Example: - 2.5, 235.365
Note: - Format specifier used for float is %f.

4. double (Floating point): - It stores floating point means decimal values. It


occupies 8 bytes of memory.
Example: - 235.265, 26587.236
Note: - 1. Format specifier used for double is %lf.
2. Difference between float and double is,
a. float uses 4 bytes of memory, whereas double uses 8
bytes of memory
b. In float 6 precision digits (digits after point) will print,
whereas in double 15 precision digits will print.
5. long data type: - When we want increase the limit or range of any other data
type then we will use long with that data type.
1. long int
2. unsigned long int
3. long double

Data types, their size and range :-

Type Storage size Value range


char 1 byte -128 to 127
or
0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
-32,768 to 32,767
int
2 or 4 bytes Or
(signed int)
-2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535
or
0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 8 bytes or (4bytes for 32 bit OS) -9223372036854775808 to
9223372036854775807
unsigned long 8 bytes 0 to 18446744073709551615

Type Storage Value range Precision


size
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places

Variable: - Variable is name of reserved area allocated in memory. In


other words, Variable is the name of memory location, where we can be
store value. It is a combination of "vary + able" that means its value can
be changed.
Variable declaration syntax: -
datatype nameofvariable;
Example: - int rollnumber; //signed int
unsigned int salary; //unsigned int

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.

Format Specifier Type

%c Character

%d Signed integer

%e or %E Scientific notation of floats

%f Float values

%g or %G Similar as %e or %E (floating point value)

%hi Signed integer (short)

%hu Unsigned Integer (short)


Format Specifier Type

%i Unsigned integer

%l or %ld or %li Long

%lf Double

%Lf Long double

%lu Unsigned int or unsigned long

%lli or %lld Long long

%llu Unsigned long long

%o Octal representation

%p Pointer

%s String

%u Unsigned int

%x or %X Hexadecimal representation

%n Prints nothing

%% Prints % character

Sr. Number Base Range Smallest Largest Example


No. System 0 to (Base-1) Digit Digit
1. Decimal 10 0 to 9 0 9 985,
245, 846
2. Binary 2 0 to 1 0 1 101,
110011,
10110
3. Octal 8 0 to 7 0 7 754, 254
4. Hexadecimal 16 0 to f/F (15) 0 f/F 1AD,
Representation 256E,
of Digits 549F
0 to 9
10 -> a/A
11 -> b/B
12 -> c/C
13 -> d/D
14 -> e/E
15 -> f/F

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);
}

&- ampersand:- it is an address operator used to refer address of a variable.

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);
}

Input function:- scanf();


//Write a Program to take input from user
#include<stdio.h>
void main()
{
int roll;
printf("Enter a number:");
scanf("%d",&roll);
printf("%d",roll);
}

Printing fixed precision values after . in floating point numbers:-


If we want to print some fix digits after point(.) in float data type then we will use the following
syntax.
Syntax:- %0.numberofdigitsf
Example:- if we to print only 2 digits after point
%0.2f
Or
%.2f
It will give answer like this:- 5.26
If we to print 3 digits after point
%0.3f

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);
}

Write a program to calculate area of circle.


#include<stdio.h>
void main()
{
float r,area;
printf("Enter radius:");
scanf("%f",&r);
area=3.14*r*r;
printf("Area of Circle=%0.2f”,area);
}
Write a program to calculate area of triangle.
#include<stdio.h>
void main()
{
float b,h,area;
printf("Enter breath of triangle:");
scanf("%f",&b);
printf("Enter height of triangle:");
scanf("%f",&h);
area=0.5*b*h;
printf("Area of Triangle=%0.2f”,area);
}

/*WAP to display a single character*/


#include<stdio.h>
void main()
{
char a=’x’;
printf("Entered character is %c",a);
}
/*WAP to take a single character as input from user*/
#include<stdio.h>
void main()
{
char a;
printf("Enter a character:");
scanf("%c",&a);
printf("Entered character is %c",a);
}

/*WAP to display a string


(string:- string is a collection of characters. eg:- hello, world, hi)*/

#include<stdio.h>
void main()
{
char msg[10]="hello";
printf("%s",msg);
}

/* WAP to display the following message using string of characters.


Hello everyone,
My name is ....... . I am pursuing BCA from Aryabhatta College, Ajmer.*/

//WAP to take input from user as a string

#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 :

//WAP to diaplay a floating-point number


#include<stdio.h>
#include<conio.h>
void main()
{
float a=2.5;
clrscr();
printf("Value of a=%f",a);
getch();
}

//WAP to add two floating-point numbers


#include<stdio.h>
#include<conio.h>
void main()
{
float a=2.5,b=3.2,sum;
clrscr();
sum=a+b;
printf("%f+%f=%f",a,b,sum);
getch();
}
If we want to diaplay any particular number of digits (precision digits) after
.(point) then we need to use the following syntax:-

% 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

//WAP to enter a floating-point number.


#include<stdio.h>
void main()
{
float x;
printf("Enter a number");
scanf("%f",&x);
printf("Value of X=%0.2f",x);
}

1. WAP to perform division operation


2. WAP to display a floating point number up to 3 precision digits.

//WAP to convert a decimal number into octal number


#include<stdio.h>
void main()
{
int a;
printf("Enter a number");
scanf("%d",&a);
printf("Octal conversion of %d = %o",a,a);
}

/*WAP to convert a decimal number into Hexadecimal number*/


#include<stdio.h>
void main()
{
int a;
printf("Enter a number");
scanf("%d",&a);
printf("Hexadecimal conversion of %d = %x",a,a);
}

You might also like