Basics of C Programming
Basics of C Programming
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
Features of C Language
5. Special Symbols
Address of
the memory
location.
Usually it’s a
hexadecimal
number
Variable Initialization
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 the 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.
Write a C program to print square and cube of a given number n.
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
Logical NOT ! !1 0
Bitwise Operators
• Bitwise Operators : are those operators that perform
operations at bit level.
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
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) ;
Comma Operator
C
H
A
R
T
Evaluate the given expressions
Expressions on Relational and Logical Operators
Expressions on Increment and Decrement Operators
Example 1: Example 2:
Consider, a=10 and b=20 Consider, a=5 and b=2
res = a++ + b++ res = a-- + b--
Example 3: Example 4:
Consider, a=20 and b=5 Consider, a=5 and b=2
res = ++a + ++b What is res?
res = --a + --b
Example 5:
Consider, a=40 ,b=30,c=20,d=10
res = ++a + b++ - --c * d++
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:
Down casting
integer to char
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.000000
d = a / (float)b; c=1.500000