C Programming Notes - Module1
C Programming Notes - Module1
Module 1
1. Computer Languages
Categories of languages:
3. System Development
Overview of C
1. A Brief History of C
1. C was developed by Dennis Ritchie in the early 1970s at Bell Labs while working on
the UNIX operating system.
2. It evolved from earlier languages: BCPL (Martin Richards) → influenced B (Ken
Thompson) → led to C (Dennis Ritchie).
3. Originally used to write the UNIX OS, making UNIX the first OS written in a high-
level language.
4. The first C description appeared in The C Programming Language (1978) by Brian
Kernighan and Dennis Ritchie.
5. In 1983, ANSI formed a committee to standardize C → resulting in ANSI C (C89),
adopted in 1989, later by ISO.
6. Amendment 1 (1995) added new library functions.
7. C99 Standard (1999) introduced new features such as:
1. Variable-length arrays
2. inline keyword
3. restrict pointer qualifier
4. New data types: _Bool, _Complex, _Imaginary.
8. Despite C++ and Java, C remains vital for system-level programming, embedded
systems, and portability.
NCET, Bengaluru
25PSC151 Module 1
2. C Is a Middle-Level Language
Languages
* In machine level language computer only understand digital numbers i.e.in the
form of 0 and 1. So, instruction given to the computer is in the form binary digit,
which is difficult to implement instruction in binary code
* High level languages are machine independent, means it is portable. The language
in this category is Pascal, Cobol, Fortran etc. High level languages are understood by
the machine. So it need to translate by the translator into machine level. A translator is
software which is used to translate high level language as well as low level language
in to machine level language
3. C Is a Structured Language
NCET, Bengaluru
25PSC151 Module 1
4. C Is a Programmer’s Language
* C programs are fast and efficient. This is because C uses a powerful set of data types and
operators.
* C combines the power and capability of assembly language with the user friendly features
of a high-level language.
* C is the most widely used older programming language. It continues to go strong while
older programming languages such as BASIC and COBOL have been virtually forgotten.
* C is very much portable, which means programs written on a machine using C can be used
on other machines as well without any modification.
* A C program consists of a number of functions that are supported by C library. In fact, you
can create your own function, which can then be added to the C library.
Characteristics of C Language:
• C is a middle level language, which means it combines the features of high level language
with the functionality of an assembly language.
NCET, Bengaluru
25PSC151 Module 1
• C is renowned for its simplicity and is easy to use because of its structured approach. It has
a vast collection of keywords, operators, built-in functions and data types which make it
efficient and powerful.
• C is popular not just because it can be used as a standalone programming language, but also
as it can be used as an interface to other more visual languages.
a) The documentation section is used for displaying any information about the
program like the purpose of the program, name of the author, date and time
written etc, and this section should be enclosed within comment lines. The
statements in the documentation section are ignored by the compiler.
b) The link section consists of the inclusion of header files.
c) The definition section consists of macro definitions, defining constants etc,.
d) Anything declared in the global declaration section is accessible throughout
the program, i.e. accessible to all the functions in the program.
e) main() function is mandatory for any program and it includes two parts, the
declaration part and the executable part.
f) The last section, i.e. sub-program section is optional and used when we require
including user defined functions in the program.
9. Separate Compilation
file1.c → file1.obj
file2.c → file2.obj
Link all → program.exe
Shortcut keys to compile and run in TC++ environment
**Compile** Alt + F9` | Compiles the program and checks for syntax errors.
**Run / Execute** Ctrl + F9` | Runs the last compiled program.
**View Output Screen* Alt + F5` Displays the output (DOS window) after running the
program. |
A compiled C program creates and uses four logically distinct regions of memory. The first
region is the memory that actually holds the program's executable code. The next region is
memory where global variables are stored. The remaining two regions are the stack and the
heap. The stack is used for a great many things while your program executes. It holds the
return addresses of function calls, arguments to functions, and local variables. It will also
save the current state of the CPU. The heap is a region of free memory that your program can
use via C's dynamic memory allocation functions. Although the exact physical layout of each
of the four regions of memory differs among CPU types and C implementations, the diagram
in Figure 1-2 shows conceptually how your C programs appear in memory
Expressions in C
In C programming, data types classify the type of data a variable can hold, determining the
memory allocated and the operations that can be performed on it.
NCET, Bengaluru
25PSC151 Module 1
1. Type modifiers alter the size or sign of the base data type.
2. Available modifiers:
i. signed, unsigned, long, short
3. Examples:
i. short int, long int, unsigned int, long double
4. long was added in C99 for 64-bit integers.
5. Signed vs Unsigned:
i. Signed integers can store both positive and negative values.
NCET, Bengaluru
25PSC151 Module 1
Unsigned integers can store only non-negative values but have a higher
ii.
positive range.
6. When a modifier is used alone, int is assumed.
i. Example: unsigned → means unsigned int.
3. Identifier Names
Identifiers refer to the names of variables, constants, functions and arrays. These are user-
defined names is called Identifiers. These identifier are defined against a set of rules.
Rules for an Identifier
1. An Identifier can only have alphanumeric characters ( a-z , A-Z , 0-9 ) and underscore( _ ).
2. The first character of an identifier can only contain alphabet ( a-z , A-Z ) or underscore (
_).
3. Identifiers are also case sensitive in C. For example name and Name are two different
identifier in C.
4. Keywords are not allowed to be used as Identifiers.
5. No special characters, such as semicolon, period, whitespaces, slash or comma are
permitted to be used in or as Identifier.
6. C‟ compiler recognizes only the first 31 characters of an identifiers.
4. Variables
2. A variable name includes alphabets and numbers, but it must start with an alphabet.
3. It cannot accept any special characters, blank spaces except under score( _ ).
Keywords are predefined or reserved words that have special meanings to the
compiler. These are part of the syntax and cannot be used as identifiers in the
program. Keywords are not allowed to be used as identifiers. No special character, such as
semicolon, period, white spaces, slash or comma are permitted to be used in or as identifiers.
Keyword: Keywords are the reserved words of a language. Identifier: Identifiers are the user-
defined names of variables, functions, and labels.
Scope Description
NCET, Bengaluru
25PSC151 Module 1
Scope Description
Begins with { and ends with }. Includes local variables and function
Block scope
parameters.
Function prototype
For identifiers used in function prototypes (temporary).
scope
Function scope For labels used with goto; valid only inside the same function.
6. Type Qualifiers
1) Common qualifiers:
1. const – value cannot be changed once assigned.
2. volatile – tells the compiler that a variable’s value may change unexpectedly
(e.g., hardware register).
2) Example:
1) Specifiers:
1. auto – default for local variables; stored on the stack.
2. register – stored in CPU register (faster access).
3. static – retains value between function calls.
4. extern – refers to a global variable defined elsewhere (in another file).
2) Example:
3) static int count = 0;
4) extern int total;
8. Variable Initializations
Syntax:
int a = 10;
float rate = 2.5;
/*2. Write a program to demonstrate the use of printf statement to print values of
variables of different data types.*/
#include <stdio.h>
main()
{
// Declare and initialize variables
int num = 7;
float amt = 123.45;
char code = 'A';
double pi = 3.1415926536;
long int population_of_india = 10000000000;
char msg[] = "Hi";
// Print the values of variables
printf("\n NUM = %d \t AMT = %f \t CODE = %c \n PI = %e \t POPULATION OF INDIA
= %ld \n MESSAGE = %s", num, amt, code, pi, population_of_india, msg);
return 0;
}
/*3. Write a program to demonstrate the use of printf and scanf statements to read and
print values of variables of different data types.*/
#include <stdio.h>
main()
{
int num;
float amt;
char code;
double pi;
long int population_of_india;
char msg[10];
printf("\n Enter the value of num : ");
scanf("%d", &num);
printf("\n Enter the value of amt : ");
scanf("%f", &amt);
printf("\n Enter the value of pi : ");
scanf("%e", &pi);
printf("\n Enter the population of india : ");
scanf("%ld", &population_of_india);
printf("\n Enter the value of code : ");
scanf("%c", &code);
printf("\n Enter the message : ");
scanf("%s", msg);
printf("\n NUM = %d \n AMT = %f \n PI = %e \n POPULATION OF INDIA = %ld \n
CODE = %c \n MESSAGE = %s", num, amt, code, pi, population_of_india, msg);
return 0;
NCET, Bengaluru
25PSC151 Module 1
9. Constants
10. Operators
1) Arithmetic operators: +, -, *, /, %
2) Increment/Decrement: ++, --
3) Relational: <, >, <=, >=, ==, !=
4) Logical: &&, ||, !
5) Bitwise: &, |, ^, ~, <<, >>
6) Assignment: =, +=, -=, etc.
7) Pointer operators: & (address), * (value at address)
8) Conditional operator: ?: (ternary operator)
9) Sizeof: returns memory size of data type/variable.
10) Comma operator (,): used to separate expressions.
11) Dot (.) and Arrow (->): used to access structure members.
NCET, Bengaluru
25PSC151 Module 1
11. Expressions
Type conversion:
NCET, Bengaluru
25PSC151 Module 1
NCET, Bengaluru
25PSC151 Module 1
=> (15) * 2
=> Result = 30
Example 3: Combination of Relational and Arithmetic Operators
Expression: 10 + 5 > 12
Step 1: '+' has higher precedence than '>'
=> (10 + 5) > 12
=> 15 > 12
=> Result = 1 (True)
Example 4: Logical Operators
Expression: (5 > 3) && (8 < 10)
Step 1: Relational operators are evaluated first
=> (1) && (1)
=> 1 && 1 => Result = 1 (True)
Example 5: Assignment and Arithmetic
Expression: int a = 10; int b = 5; int c = a + b * 2;
Step 1: Multiplication (*) first => b * 2 = 10
Step 2: Addition => a + 10 = 20
=> c = 20
Example 6: Mixed Operators
Expression: int x = 10; int y = 5; int z = x++ + --y * 2;
Step 1: Pre-decrement (--y) => y = 4
Step 2: Post-increment (x++) uses current value => 10
Step 3: Multiplication (*) => 4 * 2 = 8
Step 4: Addition => 10 + 8 = 18
After evaluation: x = 11, y = 4, z = 18
Example 7: Conditional Operator
Expression: int a = 10, b = 20; int max = (a > b) ? a : b;
Step 1: (a > b) is false
=> max = b => 20
Example 8: Comma Operator
NCET, Bengaluru
25PSC151 Module 1
NCET, Bengaluru
25PSC151 Module 1
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2;
int add_res=0, sub_res=0, mul_res=0, idiv_res=0, modiv_res=0; float fdiv_res=0.0;
clrscr();
printf("\n Enter the first number : ");
scanf("%d", &num1);
printf("\n Enter the second number : ");
scanf("%d", &num2);
add_res= num1 + num2;
sub_res= num1 - num2;
mul_res = num1 * num2;
idiv_res = num1/num2;
modiv_res = num1%num2;
fdiv_res = (float)num1/num2;
printf("\n %d + %d = %d", num1, num2, add_res);
printf("\n %d - %d = %d", num1, num2, sub_res);
printf("\n %d ¥ %d = %d", num1, num2, mul_res);
printf("\n %d / %d = %d (Integer Division)", num1, num2, idiv_res);
printf("\n %d %% %d = %d (Moduluo Division)", num1, num2, modiv_res);
printf("\n %d / %d = %.2f (Normal Division)", num1, num2, fdiv_res);
return 0;
}
/*12. Write a program two find the largest of two numbers using ternary operator.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, large;
NCET, Bengaluru
25PSC151 Module 1
clrscr();
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
large = num1>num2?num1:num2;
printf("\n The largest number is: %d", large);
return 0;
}
/*13. Write a program two find the largest of three numbers using ternary operator.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, num3, large;
clrscr();
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
printf("\n Enter the third number: ");
scanf("%d", &num3);
large = num1>num2?(num1>num3?num1:num3):(num2>num3?num2:num3);
printf("\n The largest number is: %d", large);
return 0;
}
#include <stdio.h>
Int main()
{
int num1 = 3, num2 = 5;
printf("\n Initial value of num1 = %d and num2 = %d", num1, num2);
num1 += num2 * 4 - 7;
printf("\n After the evaluation of the expression num1 = %d and num2 = %d", num1, num2);
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
{
float radius;
double area, circumference;
NCET, Bengaluru
25PSC151 Module 1
clrscr();
printf("\n Enter the radius of the circle: ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
circumference = 2 * 3.14 * radius;
printf(" Area = %.2le", area);
printf("\n CIRCUMFERENCE = %.2e", circumference);
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
clrscr();
printf("\n Enter any character: ");
scanf("%c", &ch);
printf("\n The ascii value of %c is: %d",ch,ch);
return 0;
}
/*17. Write a program to read a character in upper case and then print it in lower
case.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
clrscr();
printf("\n Enter any character in uppercase: ");
scanf("%c", &ch);
printf("\n The character in lower case is: %c", ch+32);
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, temp;
clrscr();
printf("\n Enter the first number: ");
scanf("%d",&num1);
NCET, Bengaluru
25PSC151 Module 1
/*20. Write a program to swap two numbers without using a temporary variable.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2;
clrscr();
printf("\n Enter the first number: ");
scanf("%d",&num1);
printf("\n Enter the second number: ");
scanf("%d",&num2);
num1 = num1 + num2;
num2= num1 - num2;
num1 = num1 - num2;
printf("\n The first number is %d", num1);
printf("\n The second number is %d", num2);
return 0;
}
/*21. Write a program to calculate average of two numbers. Also print their deviation.*/
#include <stdio.h>
#include <conio.h>
main()
{
int num1, num2;
float avg, dev1, dev2;
printf("\n Enter the two numbers: ");
scanf("%d %d", &num1, &num2);
avg = (num1 + num2) / 2;
dev1 = num1 - avg;
dev2 = num2 - avg;
printf("\n AVERAGE = %.2f", avg);
printf("\n Deviation of first number = %.2f", dev1);
printf("\n Deviation of second number = %.2f", dev2);
return 0;
}
NCET, Bengaluru
25PSC151 Module 1
#include <stdio.h>
#include <conio.h>
main()
{
float fahrenheit;
float celsius;
printf("\n Enter the temperature in fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (0.56) * (fahrenheit - 32);
printf("\n Temperature in degrees celsius = %f", celsius);
return 0;
}
/*23. Write a program that displays the size of every data type.*/
#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
printf("\n The size of short integer is: %d", sizeof(short int));
printf("\n The size of unsigned integer is: %d", sizeof(unsigned int));
printf("\n The size of signed integer is: %d", sizeof(signed int));
printf("\n The size of integer is: %d",
sizeof(int));
printf("\n The size of long integer is: %d", sizeof(long int));
printf("\n The size of character is: %d", sizeof(char));
printf("\n The size of unsigned character is: %d", sizeof(unsigned char));
printf("\n The size of signed character is: %d", sizeof(signed char));
printf("\n The size of floating point number is: %d", sizeof(float));
printf("\n The size of double number is: %d", sizeof(double));
return 0;
}
/*24. Write a program to calculate the total amount of money in the piggybank, given
the coins of Rs 10, Rs 5, Rs 2, and Re 1.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num_of_10_coins, num_of_5_coins, num_of_2_coins, num_of_1_coins;
float total_amt = 0.0;
clrscr();
printf("\n Enter the number of Rs10 coins in the piggybank: ");
scanf("%d", &num_of_10_coins);
printf("\n Enter the number of Rs5 coins in the piggybank: ");
NCET, Bengaluru
25PSC151 Module 1
scanf("%d", &num_of_5_coins);
printf("\n Enter the number of Rs2 coins in the piggybank: ");
scanf("%d", &num_of_2_coins);
printf("\n Enter the number of Re1 coins in the piggybank: ");
scanf("%d", &num_of_1_coins);
total_amt = num_of_10_coins * 10 + num_of_5_coins * 5 + num_of_2_coins * 2 +
num_of_1_coins;
printf("\n Total amount in the piggybank = %f", total_amt);
getch();
return 0;
}
/*25. Write a program to calculate the bill amount for an item given its quantity sold,
value, discount, and tax.*/
#include <stdio.h>
#include <conio.h>
main()
{
float total_amt, amt, sub_total,
discount_amt, tax_amt, qty, val,
discount, tax;
printf("\n Enter the quantity of item sold: ");
scanf("%f", &qty);
printf("\n Enter the value of item: ");
scanf("%f", &val);
printf("\n Enter the discount percentage: ");
scanf("%f", &discount);
printf("\n Enter the tax: ");
scanf("%f", &tax);
amt = qty * val;
discount_amt = (amt * discount)/100.0;
sub_total = amt - discount_amt;
tax_amt = (sub_total * tax) /100.0;
total_amt = sub_total + tax_amt;
printf("\n\n\n ****** BILL ******");
printf("\n Quantity Sold: %f", qty);
printf("\n Price per item: %f", val);
printf("\n -------------");
printf("\n Amount: %f", amt);
printf("\n Discount: - %f", discount_amt);
printf("\n Discounted Total: %f", sub_total);
printf("\n Tax: + %f", tax_amt);
printf("\n -------------");
printf("\n Total Amount %f", total_amt);
return 0;
}
NCET, Bengaluru
25PSC151 Module 1
NCET, Bengaluru
25PSC151 Module 1
3)
NCET, Bengaluru
25PSC151 Module 1
4)
#include <stdio.h>
main()
{
NCET, Bengaluru
25PSC151 Module 1
int a, b;
printf("\n Enter two four digit numbers : ");
scanf("%2d %4d", &a, &b);
printf("\n The two numbers are : %d and %d", a, b);
return 0;
}
NCET, Bengaluru