0% found this document useful (0 votes)
13 views

Unit-2-Part-A

The document outlines the curriculum for the Bachelor of Technology (B.Tech-CSE) course, specifically focusing on the subject 'Introduction to Programming with C'. It covers fundamental topics such as data types, variables, constants, operators, and the structure of C programs, including the main function and header files. Additionally, it explains concepts like typecasting, expressions, and comments in C programming.

Uploaded by

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

Unit-2-Part-A

The document outlines the curriculum for the Bachelor of Technology (B.Tech-CSE) course, specifically focusing on the subject 'Introduction to Programming with C'. It covers fundamental topics such as data types, variables, constants, operators, and the structure of C programs, including the main function and header files. Additionally, it explains concepts like typecasting, expressions, and comments in C programming.

Uploaded by

pranav verma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Course Name: Bachelor of Technology (B.

Tech-CSE)
Course Code: 19002200
Subject: Introduction to Programming with C (Unit-2)

Faculty Name:
Dr. Saba Hilal

Professor (CSE)
School of
Engineering &
Technology

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Topics
• Datatypes in C
• Variables
• Constants
• Operators
• Variable Declaration, Initialization
• Keywords
• Typecasting in C
• Expressions
• Comments
• Header Files
• Function main()
• Operators and Operations

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


What are Data Types in C Programming?
In C programming, data types are exactly what their name suggests.
They represent the kind of data that can be stored. They are used to
declare functions and variables in a program.

There are three main categories of data types:


• Basic/Primitive
• Derived
• User-Defined.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


What are Data Types in C Programming?

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


What are Data Types in C Programming?

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


What are Variables?
In programming, a variable is a container (storage area) to hold
data.
To indicate the storage area, each variable should be given a unique
name (identifier).
Variable names are just the symbolic representation of a memory
location.
For example: int playerScore = 95;
Here, playerScore is a variable of int type. Here, the variable is
assigned an integer value 95.
The value of a variable can be changed, hence the name variable.
Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C
Rules for naming a variable
• A variable name can only have letters (both uppercase and
lowercase letters), digits and underscore.
• The first letter of a variable should be either a letter or an
underscore.
• There is no rule on how long a variable name (identifier) can be.
However, you may run into problems in some compilers if the
variable name is longer than 31 characters.
Note: You should always try to give meaningful names to variables.
For example: firstName is a better variable name than fn.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Rules for naming a variable
C is a strongly typed language. This means that the variable type
cannot be changed once it is declared. For example:

int number = 5; // integer variable


number = 5.5; // error
double number; // error

Here, the type of number variable is int. You cannot assign a floating-point (decimal) value 5.5 to this
variable. Also, you cannot redefine the data type of the variable to double. By the way, to store the decimal
values in C, you need to declare its type to either double or float.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Constants
If you want to define a variable whose value cannot be changed, you
can use the const keyword. This will create a constant. For example,

const double PI = 3.14;


Here, PI is a symbolic constant; its value cannot be changed.

const double PI = 3.14;


PI = 2.9; //Error

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Named Constants

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Operators
An operator is a symbol that instructs the compiler to carry out
particular mathematical, logical, or conditional operations. It
works on a number or a variable as a symbol.
For instance, the addition and subtraction operators in any C
application are + and -.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Operators

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Variable Declaration
The main purpose of variables is to
store data in memory. Its value may
be changed during execution.

The variable declaration indicates


that the operating system is going
to reserve a piece of memory with
that variable name.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Variable Initialization
In c programming language,
variable can be initialized in the
declaration statement of any block
(either it may main’s block or any
other function’s block).

While declaring a variable you can


provide a value to the variable with
assignment operator.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Keywords in C
• Keywords are predefined,
reserved words used in
programming that have special
meanings to the compiler.
• Keywords are part of the syntax
and they cannot be used as an
identifier.
• As C is a case sensitive language,
all keywords must be written in
lowercase. Here is a list of all
keywords allowed in ANSI C.
Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C
Type Casting in C
Type casting is the process in which the compiler automatically converts one data type in a program to
another one. Type conversion is another name for type casting. For instance, if a programmer wants to store
a long variable value into some simple integer in a program, then they can type cast this long into the int.
Thus, the method of type casting lets users convert the values present in any data type into another data
type with the help of the cast operator, like:

(type_name) expression

Types of Type Casting in C


The process of type casting can be performed in two major types in a C program. These are:
•Implicit
•Explicit

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Type Casting in C
C Language Implicit Type Casting
Implementing the implicit type casting is very easy in a program. We use it to convert the data type of any
variable without losing the actual meaning that it holds. The implicit type casting occurs automatically.

In simpler words, the implicit type casting performs the conversion without altering any of the values
stored in the program’s variables.

Follow these points to understand what rules the implicit type casting follows in a C program:
•If we are performing a conversion on two of the different data types in a program, then the conversion of
the lower data type to the higher data type will occur automatically.
•If we suppose that we are performing the type casting operation among two different data types like float
and int, then the resultant value would be the floating data type (float).

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Type Casting in C
C Language Implicit Type Casting

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Type Casting in C
C Language Explicit Type Casting
This process is not at all like the implicit type casting in C, where the conversion of data type occurs
automatically. Conversely, in the case of explicit type casting, the programmer needs to force the conversion.
In simpler words, one has to perform type casting on the data types on their own.

Syntax:
(name_of_data_type) expression

Here, name_of_data_type refers to the name of that data type to which we want to convert the available data
type in the code. This expression can be a constant, a variable, or even an actual expression in a program.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Type Casting in C
C Language In-built Data Type Casting Functions
There are basically 5 different in-built type casting functions available in the C language. These are:

atof(): We use it to convert the data type string into the data type float.
atoi(): We use it to convert the data type string into the data type int.
atbol(): We use it to convert the data type string into the data type long.
itoba(): We use it to convert the data type int into the data type string.
ltoa(): We use it to convert the data type long into the data type string.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Type Casting in C
C Language Explicit Type Casting

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Expressions in C
An expression is a union of operators and
operands.
The expression is used to calculate a single
value that is saved in a variable.
The action or operation to be carried out is
indicated by the operator.
The objects to which we apply the operation
are known as operands.
The given diagram is an example of how a C
expression looks like using the variable,
operator, and operands:

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Comments in C
Comments can be used to explain code, and to
make it more readable. It can also be used to
prevent execution when testing alternative
code. Comments can be singled-lined or multi-
lined.

Single-line Comments
Single-line comments start with two forward
slashes (//).
Any text between // and the end of the line is
ignored by the compiler (will not be executed).

Multi-line comments
Multi-line comments start with /* and ends
with */.
Any text between /* and */ will be ignored by
the compiler.
Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C
Header Files in C
In C language, header files contain a set of
predefined standard library functions.

The .h is the extension of the header files in C


and we request to use a header file in our
program by including it with the C
preprocessing directive “#include”.

C language has numerous libraries that include


predefined functions to make programming
easier.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Header Files in C

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Header Files in C
stdio.h
The stdio.h is one of the most commonly used header file in C.
This file enables us to use the input and output functionality in C.
As stdio.h contains more than 40 functions that are used to perform input and
output operations, we get a lot of different ways to get and display data in C.

iostream.h
iostream provides basic input and output services for C++ programs. iostream uses
the objects cin , cout , cerr , and clog for sending data to and from the standard
streams input, output, error (unbuffered), and log (buffered) respectively.

conio.h
This header file declares several useful library functions for performing "console
input and output" from a program. If you are using functions like clrscr(), getch(),
putch, cputs, etc we need to include conio.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


main() function in C
The main function is an integral part of the programming
languages such as C.
The main function in C is the entry point of a program where
the execution of a program starts.
It is a user-defined function that is mandatory for the
execution of a program because when a C program is executed,
the operating system starts executing the statements in the
main() function.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


main() function in C
Important Points about C main Function
• It is the function where the program’s execution starts.
• Every program has exactly one main function.
• The name of this function should be “main” not anything
else.
• The main function always returns an integer value or void.
• The main function is called by OS, not the user.
Types of C main Functions
• Main function with no arguments and void return type
• Main function with no arguments and int return type
• Main function with the Command Line Arguments

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


main() function in C
• Main function with the Command Line Arguments

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Arithmetic operators and operations in C
Arithmetic Operators are the type of operators in C that are
used to perform mathematical operations in a C program.
They can be used in programs to define expressions and
mathematical formulas.
The C arithmetic operators are the symbols that are used to
perform mathematical operations on operands. There are a
total of 9 arithmetic operators in C to provide the basic
arithmetic operations such as addition, subtraction,
multiplication, etc.

Types of Arithmetic Operators in C


The C Arithmetic Operators are of two types based on the
number of operands they work. These are as follows:
Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C
Arithmetic operators and operations in C
Types of Arithmetic Operators in C
The C Arithmetic Operators are of two types based on the
number of operands they work. These are as follows:

Binary Arithmetic Operators


Unary Arithmetic Operators

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Arithmetic operators and operations in C
1. Binary Arithmetic Operators in C
The C binary arithmetic operators operate or work on two operands. C
provides 5 Binary Arithmetic Operators for performing arithmetic functions
which are as follows:

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Arithmetic operators and operations in C
2. Unary Arithmetic Operators in C
The unary arithmetic operators operate or work with a single operand. In C,
we have two unary arithmetic operators which are as follows:

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Relational Operators in C
Relational operators are used for the comparison of two values
to understand the type of relationship a pair of number shares.
For example, less than, greater than, equal to, etc. Let’s see
them one by one

Equal to operator: Represented as ‘==’, the equal to operator checks whether the
two given operands are equal or not. If so, it returns true. Otherwise, it returns
false. For example, 5==5 will return true.
Not equal to operator: Represented as ‘!=’, the not equal to operator checks
whether the two given operands are equal or not. If not, it returns true. Otherwise,
it returns false. It is the exact boolean complement of the ‘==’ operator. For example,
5!=5 will return false.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Relational Operators in C
Greater than operator: Represented as ‘>’, the greater than operator
checks whether the first operand is greater than the second operand or
not. If so, it returns true. Otherwise, it returns false. For example, 6>5 will
return true.
Less than operator: Represented as ‘<‘, the less than operator checks
whether the first operand is lesser than the second operand. If so, it returns
true. Otherwise, it returns false. For example, 6<5 will return false.
Greater than or equal to operator: Represented as ‘>=’, the greater than
or equal to operator checks whether the first operand is greater than or
equal to the second operand. If so, it returns true else it returns false. For
example, 5>=5 will return true.
Less than or equal to operator: Represented as ‘<=’, the less than or equal
to operator checks whether the first operand is less than or equal to the
second operand. If so, it returns true else false. For example, 5<=5 will also
return true.
Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C
Logical Operators in C
They are used to combine two or more conditions/constraints or to complement
the evaluation of the original condition under consideration. They are described
below:
Logical AND operator: The ‘&&’ operator returns true when both the conditions
under consideration are satisfied. Otherwise, it returns false. For example, a && b
returns true when both a and b are true (i.e. non-zero).

Logical OR operator: The ‘||’ operator returns true even if one (or both) of the
conditions under consideration is satisfied. Otherwise, it returns false. For
example, a || b returns true if one of a or b, or both are true (i.e. non-zero). Of
course, it returns true when both a and b are true.

Logical NOT operator: The ‘!’ operator returns true the condition in consideration
is not satisfied. Otherwise, it returns false. For example, !a returns true if a is false,
i.e. when a=0.
Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C
Bitwise Operators in C
• The & (bitwise AND) in C or C++ takes two numbers as operands and does AND
on every bit of two numbers. The result of AND is 1 only if both bits are 1.
• The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on
every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
• The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR
on every bit of two numbers. The result of XOR is 1 if the two bits are different.
• The << (left shift) in C or C++ takes two numbers, the left shifts the bits of the
first operand, and the second operand decides the number of places to shift.
• The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the
first operand, and the second operand decides the number of places to shift.
• The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Bitwise Operators in C

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Assignment Operators in C
Assignment
operators
are used to
assign
values to
variables.

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C


Reference Books and Links
1. Ashok N. Kamthane, “Computer Basics and C Programming”, Pearson Education.
2. E. BalaGuruswamy, “Programming in ANSI C”, 2008.
3. V Rajaraman, “Computer Basics and C Programming”, PHI.
4. Herbert Schildt, “C The Complete Reference” Fourth Edition, 2000.
5. YashwantKanetkar, “Let us C” eighth edition, 2002.
6. Kernighan and d. Ritchie, “The ANSI C Programming Language”, 2000.
7. StephennPrata, “C Primer Plus” Fourth Edition, 2001.
8. Schaum’s Outline Series, “Programming with C”, 2nd Edition, 1996.
9. https://byjus.com/gate/character-set-in-c/#:~:text=All%20the%20character%20sets%20used,8%20bits%20or%20even%20less
10. https://logicmojo.com/operators-in-c

Programme Name Semester- B.Tech-I-Sem Subject- Introduction To Programming with C

You might also like