Professional Documents
Culture Documents
Basics of C Programming-1
Basics of C Programming-1
Topics
History of C Language
Advantages of C Language
Structure of C Program
Character Set
Tokens in C
Data Types
Variables
Input / Output Statements
Simple Programs
Operators and Expressions
Type Conversions
Software
A software is a set of programs, which is designed to
perform a well-defined task.
Program
A program is a set of instructions written to perform a
specific task.
Programmer
A Person who writes a program is known as programmer.
Types of Computer Programming Languages
• Machine Level Programming Language : The program instructions are
written in zeros and ones, instructions are in binary form. Execution is
faster as instructions are already in 0’s and 1’s.
• Symbolic Level or Assembly Level Programming Language : a
programmer writes instructions using symbolic instruction code instead of
binary codes. Symbolic codes are meaningful abbreviations such as SUB is
used for substation operation, MUL for multiplication etc. A translator
known as Assembler is used to convert instructions into 0’s and 1’s.
• High Level Programming Language : The programming languages that are
close to human languages (example like English languages) are called the
high-level languages. A translator known as Compiler is used to convert
instructions to 0’s and 1’s.
Examples : Fortran, COBOL, Basic, Pascal, C, C++, Java.
History of C Language
C programming language was developed in 1972 by Dennis
Ritchie at bell laboratories of AT&T (American Telephone &
Telegraph), located in the U.S.A.
Dennis Ritchie is known as the founder of the C Language.
It was developed to overcome the problems of previous languages
such as B, BCPL, etc.
Initially, C language was developed to be used in UNIX operating
system. It inherits many features of previous languages such as B and
BCPL.
History of C Language
Advantages of C Language
• It is Structured Programming Language
• Modularity : Ability to break down large modules into
smaller sub modules.
• Portability : Ability to run programs on different
machines.
• Extendibility : Ability to extend existing software by
adding new features.
• Speed : Execution of program is fast.
• Provides wide variety of operators and data types.
Structure of a C Program
Example
5. Special Symbols
• Some special characters are used in C, and they have a
special meaning which cannot be used for another purpose.
Examples: [] , {} # * $ ()
6. Operators
• An operator is symbols that represents the type of the
operation to be performed on operands
Data Types
• A data type tells the type of the value to be stored in the
memory locations.
Basic Data Types,
Data Types Size in Bytes Format Specifier
char 2 %c
int 4 %d
float 4 %f
void 0 NA
char
Arrays
int
Stack
float Queues
Linked List
Primitive Data types:
They are basic data types, which
help us to store single value. Non Primitive Data types:
Example int a; They are derived data types, which are
derived from basic data types. They help
us to store more than one value.
Example int a[10];
List of Data Types in C
int a;
float b;
char c;
double d;
Variable and Memory Location
Address of
the memory
location.
Usually it’s a
hexadecimal
number
Variable Initialization
• Assigning an initial value to a variable is variable
initialization.
Types of initializing a Variable:
1. Compile Time Initialization int a=20
2. Run Time Initialization
int a;
scanf(“%d”, &a);
Both methods are used to specify the values for the variables.
In Compile time the value of the variable is specified before
compiling the program whereas in run time the value for the
variable is specified/prompted when the program is running
with the help of any standard input function, such as scanf()
Input and Output Statements in C
• Any program takes some input from the user processes it and produces output
• Input means to provide the program with some data to be used/processed in
the program and Output means to display resultant data on screen or write
the data to a printer or a file.
• C programming language has standard libraries that allows us to give input and
display output in a program. The stdio.h or standard input output library in C
has functions for input and output.
Program
Output
Input
Types of I/O functions in C
Input/output
functions
Formatted Unformatted
I/O functions I/O functions
scanf() getchar()
printf() putchar()
puts()
gets()
Formatted Output Function: printf()
• printf() is a standard formatted output function used to
display output of a program on the output device.
• It is defined in the header file #include<stdio.h>
Syntax of using a printf() statement
Expected Output:
Name : Raghavendra Patil
Place : Belagavi
Write a C program to Display Your USN, Name, Branch and Semester
Expected Output:
USN: 2KL20CS001
NAME: AMIT
BRANCH: CSE
SEM : 2
Write a C program to find the sum of two numbers.
Program
Input Output
5
Sum=15
10
Write a C program to find the area of circle.
Program
Input Output
2 Area =12.568
Write a C program to find the area and perimeter of a rectangle.
Write a C program to read the base and height of triangle as input and
then compute area of triangle
Write a C program to compute simple_interest for a given p, t, r.
Program
Input Output
10000 Simple
1
Interest=1000
10
Write a C program to read two integer numbers and swap the numbers.
Operators in C
An Operator is a symbol that tells the type of the operation to be performed on the operands
Types of Operators
Unary Operators
• Unary Operators : A Unary Operator is an operator that
takes a single operand in an expression.
+ Addition 6+2 9
- Subtraction 6-2 5
* Multiplication 6*2 12
/ Division 6/2 3
% Modulus 6%2 0
Logical NOT ! !1 0
Bitwise Operators
• Bitwise Operators : are those operators that perform
operations at bit level.
| Bitwise OR 1|2 3
~ Bitwise NOT ~5 10
Note: The left shift and right shift operators should not be used for negative numbers.
Bitwise Operators Examples
In shift operations the first operand is the number to be shifted and the second
operand tells by how many bits first operand should be shifted. In right shift the
every bits are shifted towards right, and vice versa in left shift.
0001 0100
4>>2=1 1<<2=4
Discarded
Note: 4 bit examples
Assignment Operators
Example: a = 10
Short Hand Assignment Operators
Operator Type Example Result
+= a+=2 a=a+2
-= a-=2 a=a-2
*= a*=2 a=a*2
/= a/=2 a=a/2
Ternary Operator (?:)
Example:
Largest = a > b ? a : b;
Largest = a > b ? ( a > c ? a : c) : (b > c ? b : c) ;
Expressions
• Expression: Sequence of operators and operands is known
as expression
• Expressions evaluates to a single value.
Precedence
• Precedence : tells the order in which the operators in a complex
expression should evaluated.
Example: 2+5*2. Here 5*2 first evaluates
Associativity
• Associativity : Associativity is used when there are two or
more operators of same precedence in an complex
expression.
• It can LR or RL
Example: 2 *3 / 2
P
R
E
C
D
E
N
C
E
C
H
A
R
T
Evaluate the given expressions
Expressions on Relational and Logical Operators
Expressions on Increment and Decrement Operators
Type Conversion in C
• The type conversion process in C is basically converting
one type of data type to other to perform some
operation.
• The conversion is done only between those data types
wherein the conversion is possible, Example – char to int
and vice versa.
Examples:
Explicit Type Conversion
• Explicit Type Conversion: This process is also called type
casting and it is user defined. Here the user can type
cast the result to make it of a particular data type.
• Explicit type conversion is temporary.
int a = 3 , b = 2;
float c, d;
c = a / b; c=1
d = a / (float)b; c=1.5