1 Unit
1 Unit
1 Unit
named as “C”
• The C programming language is named after the letter "C" because it
was developed as a successor to the B programming language. Dennis
Ritchie created C at Bell Labs between 1972 and 1973 to develop
utilities for Unix
C programming in VS code
Then c++ - GCc for windows or Mingw for windows
Run – next- finish
My computer- C program files- mingw64- open the folder- mingw32- bin- copy path- then
my computer properties- advance system setting- Environmental variables- path-edit-
new- paste the copied path- ok- ok- close
Open vs code IDE
Add new folder
Under that folder add new file main.c
Add extensions
C C++ extension for windows intellisense
Intellisense- used for suggestion code improvement
PROGRAM 1:
#include<stdio.h>
main()
{ Output: I see, I remember
/*printing begins….*/
Printf( “I see , I remember”);
/*printing ends……*/
}
Main() – It is a special function used by the C program to tell the computer where the program starts.
Every program must have exactly one main function. If we use more than one main() function , then the
compiler cannot understand which one marks the start of the program
( ) parentheses- The empty parentheses tells us that the main function has no arguments
{ , }- tells us the start and end of the function and also the end of the program. All the statements in
between the brackets forms the body of the function.
• Printf - Its is a predefined function .predefined means that it is the
function which is already defined and compiled and linked with our
program
• ; - Every statements in C should end with semicolon;
Program.c
Program.i
Compile
Program.obj
Program.exe
C library functions
• C Standard library functions or simply C Library functions are inbuilt functions in C
programming.
• The prototype and data definitions of these functions are present in their respective header
files. To use these functions we need to include the header file in our program
#define PI 3.14159265359
int main()
{
return 0;
}
Basic structure of C program
Documentation Section
Line 1- comments line
Link Section( #include) Line2- it provides instructions to
Definition Section the compiler to link functions from
Global Declaration the system library
main() Function Section Line3- defines all symbolic
{
constants
Declaration part;
Execution part;
Line4-some variables which are
} used in more than 1 function;
global variables are declared
outside the main function.
Data types
• It specifies the type of data that the variable can store like integer, character, floating, double, etc.
Each data type requires different amounts of memory and has some specific operations which can
be performed over it. The data type is a collection of data with values having fixed values, meaning
Types Description
Primitive data types are the most basic data types that are used for
Primitive Data Types
representing simple values such as integers, float, characters, etc.
User Defined Data Types The user-defined data types are defined by the user himself.
The data types that are derived from the primitive or built-in datatypes
Derived Types
are referred to as Derived Data Types.
1. Typedef
The typedef is a keyword that is used to provide existing data types with a new name. The C
typedef keyword is used to redefine the name of already existing data types.
C typedef Syntax:
typedef existing_name alias_name;
#include <stdio.h>
int main()
{
// using typedef name to declare variable
ll var = 20;
printf("%ld", var);
return 0;
}
1. Float and double
• Float and double are two primitive data types in C programming that are used to store decimal
values. They both store floating point numbers but they differ in the level of precision to which
they can store the values.
• float is used to store single-precision floating point numbers. It can store decimal values with
precision up to 6-7 decimal places.
• The size of the float is 4 bytes
• It can store values up to 7 decimal points without loss of precision.
• The format specifier for float is %f.
#include <stdio.h>
int main()
{
// Syntax of declaring and initializing the float variable
float myVariable = 789.123456f;
// printing floating point number
printf("Float value is %f", myVariable);
return 0;
• A variable name can contain alphabets, digits, and underscore (-) only.
• The starting letter can not be a digit.
• White spaces cannot be used.
• The name should not be reserved keyword or special character.
• We can declare and assign value to a variable in two ways.
• Valid variable- int harry ; float harry 123; float _harry123;
• Invalid variable- harry , int 77harry , $harry , char long; char 123
Declaration of variables
1) It tells the compiler what the variable name is
2) It tell the compiler what type of data the variable will hold
Declaration should be done at the beginning of statement block before they are used in the program
Declaration statement should always end with a semicolon ;
Int number;
Int amount;
Char abc, cdf;
Storage classes
Variable- characterstics
data type
Scope
Lifetime
Storage class
Memory
Main()
{
Int a;
{
Int b;
{
Int c;
}
}
}
• A storage class defines scope , default initial value and lifetime of a variable.
Auto storage class This is the default storage class for all the
#include<stdio.h>
variables declared inside a function or a block.
void fun()
Hence, the keyword auto is rarely used while
{
writing programs in C language.
auto int a=10;
++a;
printf("%d",a);
} Auto variables can be only accessed within the
void main() block/function they have been declared and not
{ outside them (which defines their scope). Of
fun(); course, these can be accessed within nested
fun(); blocks within the parent block/function in which
fun(); the auto variable was declared.
• This storage class is used to declare static variables which are popularly
used while writing programs in C language. Static variables have the
property of preserving their value even after they are out of their scope!
Hence, static variables preserve the value of their last use in their scope. So
we can say that they are initialized only once and exist till the termination
of the program. Thus, no new memory is allocated because they are not re-
declared.
• Their scope is local to the function to which they were defined. Global
static variables can be accessed anywhere in the program. By default, they
are assigned the value 0 by the compiler.
printf("%d\n",a);
}
void main()
{
fun();
fun();
fun();
}
External storage class
• They are same as global variable
• Scope- global to the program they are defined in
• Default initial value- 0
• Lifetime- Throughout the lifetime of the program
• A global variable can be changed by any function in the program
• Extern keyword- is used to inform our compiler that the given variable
is declared somewhere else
Register Storage
#include<stdio.h>
#include<stdlib.h>
int main(){
int x;
printf("The value of uninitialized variable is %d\n",x);
int *u;
printf("The value of uninitialized Pointer is %d\n",u);
return 0;
}
• In this example, the variable x is declared but not initialized. When it
is used in the printf() statement, the value of x is unknown and might
be any value that is currently held in that memory address. This
number could be very tiny, very big, or even negative.
• You must initialize variables before using them in C to prevent
garbage values. You can accomplish this by giving the variable a
specific value when declared or using an assignment statement later
in the program.
.
Local Variable
The variables declared inside a block are
automatic or local variables. The local variables
exist only inside the block in which it is declared.
#include <stdio.h>
int main(void)
{ for (int i = 0; i < 5; ++i)
{ printf("C programming"); }
// Error: i is not declared at this point
printf("%d", i);
return 0; }
Global variable
#include <stdio.h>
Assignment of a variable :
variable_name= constant/ value;
Initial value=9; year=year+1
Final value=3; the new value of year is equal to old
value of year +1
Bcf=d;
But c permits multiple assignments in one line
Initial value=9; final value=3; abc=6;
What is SCANF Function??
INVALID-
#define X=2.5
# define MAX 10
#define N 25;
#define N 5, m 10
#Define ARRAY 11
• #define PRICES& 100
Operator
An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform
addition.
C has a wide range of operators to perform various operations.
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on
numerical values (constants and variables).
Operator Meaning of Operator
* multiplication
/ division
remainder after division
%
(modulo division
C Increment and Decrement Operators
C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.
These two operators are unary operators, meaning they only operate on a single operand.
Example 2: Increment and Decrement Operators
Post-increment m++
Pre increment ++m
Post increment
#include<stdio.h>
int main()
{
int a=5;
printf("%d",a++);
return 0;
}
Pre increment
#include<stdio.h>
int main()
{
int a=5;
printf("%d",++a);
return 0;
}
#include<stdio.h>
Int main()
{
Int m=10;
Int n, n1;
n=++m;
n1=m++;
n--;
--n1;
n-=n1;
printf(“%d”, n);
Return 0;
}
What is the difference between the following 2
#include <stdio.h> codes?
int main() #include <stdio.h> //Program 1
{
int main()
int a = 1, b = 1, c;
c = a++ + b;
{
printf("%d, %d", a, b); int d, a = 1, b = 2;
} d = a++ + ++b;
printf("%d %d %d", d, a, b);
a) a = 1, b = 1 }
b) a = 2, b = 1 #include <stdio.h> //Program 2
c) a = 1, b = 2 int main()
d) a = 2, b = 2 {
int d, a = 1, b = 2;
d = a++ +++b;
printf("%d %d %d", d, a, b);
}
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int i = 0; int i = 2;
int j = i++ + i; int j = ++i + i;
printf("%d\n", j); printf("%d\n", j);
} }
a) 6
a) 0 b) 5
b) 1 c) 4
c) 2 d) Compile time error
d) Compile time error
Different kinds of operators
main()
{
int a,b,c,d;
a=15;
b=10;
c= ++a-b;
printf(“a =%d b=%d c=%d\n”,a,b,c);
d=b++ +a;
Printf(“a=% b=%d, d=%d\n”, a,b,d);
printf(“a*=b= %d\n”,a*=b);
}
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example of assignment operator
#include<stdio.h>
#include<math.h>
#define N 100
#define A 2
int main()
{
int a;
a=A;
while(a<N)
{
printf("%d\n",a);
a*=a;
}
return 0;
}
Meaning of
Operator Example
C Relational Operators Operator
The logical expression given above is true only if a>b is true and x==10 is true.
If either or both of them are false then the expression becomes false.
Same examples of the usage of logical expression are-
If(age>55 &&salary<1000)
If (number<0 ||number>100)
Precedence of relational and logical operators
Highest !
>>= <<=
== !=
&&
Lowest ||
Ternary operator
#include<stdio.h>
int main()
{
char a= '6';
int b=7;
int result =a+b;
printf("the sum is : %d",result);
return 0;
}
There are two types of Conversion:
1.Implicit Type Conversion
Also known as ‘automatic type conversion’.
A. Done by the compiler on its own, without any external trigger from the user.
B. Generally takes place when in an expression more than one data type is present. In such conditions
type conversion (type promotion) takes place to avoid loss of data.
C. All the data types of the variables are upgraded to the data type of the variable with the largest data
type.
bool -> char -> int -> long -> float -> double -> long double
/ An example of implicit conversion
#include <stdio.h>
int main() x = 107, z = 108.000000
{
int x = 10; // integer x
char y = 'a'; // character c
int main()
{
double x = 1.2;
return 0;
}
#include <stdio.h>
int main() {
float a = 1.5;
int b = (int)a; Output a = 1.500000 b = 1
return 0;
}
Advantages of Type Conversion
• Type safety: Type conversions can be used to ensure that data is being stored and
processed in the correct data type, avoiding potential type mismatches and type
errors.
• Improved code readability: By explicitly converting data between different types,
you can make the intent of your code clearer and easier to understand.
• Improved performance: In some cases, type conversions can be used to optimize
the performance of your code by converting data to a more efficient data type for
processing.
• Improved compatibility: Type conversions can be used to convert data between
different types that are not compatible, allowing you to write code that is
compatible with a wider range of APIs and libraries.
• Improved data manipulation: Type conversions can be used to manipulate data in
various ways, such as converting an integer to a string, converting a string to an
integer, or converting a floating-point number to an integer.
• Improved data storage: Type conversions can be used to store data in a more
compact form, such as converting a large integer value to a smaller integer type, or
converting a large floating-point value to a smaller floating-point type.
ASCII
• ASCII stands for the "American Standard Code for Information Interchange“
• t was designed in the early 60's, as a standard character set for computers and
electronic devices.
• ASCII is a 7-bit character set containing 128 characters.
• It contains the numbers from 0-9, the upper and lower case English letters from A
to Z, and some special characters.
A 65 a 97
B 66 b 98
C 67 c 99
D 68 d 100
E 69 e 101
F 70 f 102
G 71 g 103
H 72 h 104
I 73 i 105
j 106
J 74
k 107
K 75
l 108
L 76
m 109
M 77
n 110
N 78
o 111
O 79
p 112
P 80 q 113
Q 81 r 114
R 82 s 115
S 83 t 116
T 84 u 117
U 85 v 118
V 86 w 119
W 87 x 120
X 88 y 121
Y 89 z 122
Int.c sum.c div.c
Interview question
ENUM variable
Int main()
{
Enum week today;
Today=Wednesday;
Printf(“day %d”,today+1);
}
Why do we nedd enum when we can use #define to assign names to integral
constants
int main()
{
enum day d = thursday;
printf("The day number stored in d is %d", d);
return 0;
}
#include <stdio.h>
enum State {WORKING = 0, FAILED, FREEZED};
enum State currState = 2;
int main() {
(FindState() == WORKING)? printf("WORKING"): printf("NOT
WORKING");
return 0;
}
enum state {working, failed};
enum result {failed, passed};
Output:
Compile Error: 'failed' has a previous declaration
as 'state failed'
Write a program to check is a number is divisible by 2 or not
Tell whether the no. is even or odd
1)What is a variable and what is meant by the “value” of a variable?
2)How do variables and symbolic names differ?
3)State the difference between the declaration of a variable and the definition of
symbolic name
4)What are enumeration variables? How are they declared? what is advantage of
using them in a program?
5) Write a C program to input an integer and print its table
6)Write a c program to print the square of a number.
Interview questions