C Language Tutorials
C Language Tutorials
Tutorial
Installing Editor
Installing
IDE -
VS Code
Installing
GCC For
Window's
Setting Environment
Setting
Environment
C Extension
In VS Code
Hello World :
#include <stdio.h>
Output :
Header File
#include <stdio.h>
Main Program
printf("Hello World");
return 0;
}
Programming
Structure
Header Files Common Header Files
• stdio.h - standard input and output operations
• stdlib.h - provides general utility functions such
as memory allocation and deallocation
Header file is a To tell compiler to include These library contains • math.h - file contains mathematical functions
preprocessor directive. the contents of header file. definition of functions
used in programs.
• string.h – offers string manipulation functions
• time.h – offers time & date manipulation
functions
stdio.h - standard input- This library contains These are used for input
output library functions like printf() and and output operations in a
scanf(). program.
Basics In C
Application Of C Features of the C Language
• Procedural Language
• OS: C is widely used for developing operating
• Fast and Efficient
systems such as Unix, Linux, and Windows.
• General-Purpose Language
• System Software : Driver for devices
• Rich set of built-in Operators
• Networking: C is widely used for developing
• Libraries with Rich Functions
networking applications such as web servers,
network protocols, and network drivers. • Middle-Level Language
programming language, consisting of binary code programming language, using symbolic names and
that the CPU can execute directly. codes to represent the basic operations that the CPU
can perform.
#include <stdio.h>
int main()
Program {
Structure In printf("Hello World");
C return 0;
}
#include <stdio.h>
int main()
Program {
Structure In printf("Hello World");
C return 1;
}
Header file is included into main
Source File Pre-Processor program as Output file
Operators Identifiers
Tokens in C
Tokens in C can be defined
as the tiniest meaningful
Special
Symbols
Constants unit that the compiler
recognizes.
Strings
Keywords Identifiers
Rules
#include <stdio.h>
• Variables are case sensitives
• 1st character needs to be alphabet or int main()
can start with ' _ ' (underscore)
{
• No comma or blank space
int _ num = 10;
• No symbol
char 1,name;
float @pi = 3.14;
return 0;
}
Note : While declaring variable, there name should correspond to its
action/task performing. For :- final_value , age
Data Types Size in Bytes
Char or signed char 1
Int or signed int 2 Data Types
Float 4
Double 8
Constants
• The constants refer to the variables const int num =
with fixed values. 10;
{ {
scanf("%d",&a); scanf("%d",&a);
scanf("%d",&b); scanf("%d",&b);
return 0; }
}
Question -
Int a = 2; Int a = 2;
Int b = a * 2; Int b = a*c; <- C is not declared
Int c = b; Int c = 3;
Int d=1,e;
int x = y = z = 4; <- As per compiler
Int a,b,c; use and declare cannot be used
a=b=c=1; together.
Arithmetic Instructions
int a = 1, b = 2;
• a + b -> a, b is Operand and + is
Operator int x, y = a*b;
• Modulus Operator % : 3 % 2 = 1 , 5 %
3 = 2. This only works on int data
type value
a = b + c b + c = a
a = b * c a = bc
a = b / c
Arithmetic Instructions
• Type Conversion – converting value #include <stdio.h>
into different data type.
int main()
• Int * int -> int
{
• int * float -> float
printf("%f",3.0*2);
• Float * double -> double
return 0;
}
2 * 2 = 4
2 * 2 = 4.0
2 * 2 = 4.0
Arithmetic Instructions
• Operator Priority/Precedence – #include <stdio.h>
• * , / , %
int main()
• + , -
• = {
float x;
• * , / , % x = 4*3/6*2;
• Left to right priority
printf("%f",x);
return 0;
}
Arithmetic Instructions Q's
x = 2 + 4 * 5 #include <stdio.h>
x = 4 * 3 / 6 * 2 int main()
x = 3 * 2 – 2 * 1 {
x = 5 * 2 / 2 * 3 float x;
x = 5 * (4 / 4)* 2
x = 4*3/6*2;
x = 5 + 2 / 2 * 3
printf("%f",x);
return 0;
}
Flow of Control / Control Instructions
• Case Control Process take place as per case, if between Mon-Fri : Need to goto office. If
Sat-Sun : Leave.
Operators
• Arithmetic Operator It includes + , - , / , * , %
• != Not Operator
Question
• char star=‘aa’;
• /= a = a / b; -> a /= b;
• If number is greater than 15 and less than
20 then print true.
Question
• a = 1, b = 2;
• a +=b;
Assignment
• If number is greater than 9 and less than
100 then print true. Also check number is 2
digit.
If(Condition) If(Condition)
{ {
// perform statements if true; // perform statements if true;
} }
else{ elseif(Condition){
// perform this statement if false // perform this statement if false
} }
else{
// perform this statement if false
Questions }
WAP to check if user is over 18 to be able to vote.
Chp : 3 - Conditional Statements – If Else
#include<stdio.h> // using if else condition to check if
user is over 18.
/* WAP to check if user is over 18 to
be able to vote using if else if (a>18){
condition. */
printf("The user is Adult,
int main() therefor they can vote.");
{ }
int a; else{
// taking age input from end user. printf("Sorry ! You can not
vote");
printf("Enter age if user can vote
}
or not :");
return 0;
scanf("%d", &a);
}
Chp : 3 - Conditional Statements – if elseif else
#include<stdio.h> else if (age>13 && age<18){
}
Chp : 3 - Conditional Statements – Ternary
Condition ? Perfrm_action if TRUE : perfrm_action if FALSE ;
int age;
scanf("%d", &age);
Chp : 3 - Conditional Statements – Switch
#include <stdio.h> case 8:
Switch Operator
int main() { printf("Grade B\n");
#include<stdio.h> }
// Input by user. }
}
printf("Enter Number : ");
else{
scanf("%d", &num);
printf("Number is Negative. \n");
// checking if number is positive
and even or odd. }
}
Question’s
# include <stdio.h> printf ( "You entered something other
than 5\n" ) ;
int main( )
return 0 ;
{
}
int i ;
printf ( "Enter value of i " ) ;
scanf ( "%d", &i ) ;
if ( i = 5 )
printf ( "You entered 5\n" ) ;
else
Chp : 3 - Conditional Statements
Questions – 1. In a company an employee is paid as
under: If his basic salary is less than
1. WAP to input number, and check if number Rs. 1500, then HRA = 10% of basic salary
is positive and if number is odd or and DA = 90% of basic salary. If his
even. salary is either equal to or above Rs.
1500, then HRA = Rs. 500 and DA = 98% of
2. WAP to check if a student passed or basic salary. If the employee’s salary
failed. Also if passed, which grade is input through the keyboard write a
student is in. program to find his gross salary. (gs =
bs + hra + da).
3. The current year and the year in which
the employee joined the organization are 2. If cost price and selling price of an
entered through the keyboard. If the item are input through the keyboard,
number of years for which the employee write a program to determine whether the
has served the organization is greater seller has made profit or incurred loss.
than 3, then a bonus of Rs. 2500/- is Also determine how much profit he made
given to the employee. If the years of or loss he incurred.
service are not greater than 3, then the
program should do nothing.
(yearsOfService = currentYear - joinYear)
Chp : 3 - Conditional Statements
Questions – 1. A company insures its drivers in the
following cases:
1. The marks obtained by a student in 5
1. If the driver is married.
different subjects are input through the
keyboard. The student gets a division as 2. If the driver is unmarried, male &
per the following rules: above 30 years of age.
1. Percentage above or equal to 60 - First 3. If the driver is unmarried, female &
division above 25 years of age.
2. Percentage between 50 and 59 - Second In all other cases, the driver is not
division insured. If the marital status, sex and
3. Percentage between 40 and 49 - Third age of the driver are the inputs, write a
division program to determine whether the driver
should be insured or not.
4. Percentage less than 40 – Fail
Write a program to calculate the division
obtained by the student.
Chp : 4 - Loops
The looping can be defined as repeating the Types of C Loops
same process multiple times until a
specific condition satisfies. There are three types of loops in C
language that is given below:
Advantage of loops in C
//action;
int i = 1 i=i+1
Initialization Statement Updation Statement
int i = 1 i=i+2
i >= 5
Conditional Statement
i<=5
Chp : 4 - for Loops
#include<stdio.h> #include<stdio.h>
{ {
printf("Hellow World.\n"); }
int main()
WAP to print a to d character values using
{ for loop.
int a;
for(a=1;a<=5;a++){
printf("%c\n",a);
return 0;
}
Chp : 4 - Increment Operator
Pre-Increment Operator Post-Increment Operator
#include<stdio.h> #include<stdio.h>
{ {
int x = 5; int x = 5;
return 0; return 0;
} }
Chp : 4 - Increment Operator
Pre-Decrement Operator Post-Decrement Operator
#include<stdio.h> #include<stdio.h>
{ {
int x = 5; int x = 5;
return 0; return 0;
} }
ASCII - American Standard Code
ASCII - American Standard Code for ASCII - American Standard Code for
Information Interchange. Information Interchange.
It's a character encoding standard for ASCII encodes 128 characters into 7-bit
electronic communication. binary integers.
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i=1;
int i=1;
int n;
while ( i <= 10 ) printf("Enter loop counter number : ");
{ scanf("%d", &n);
printf ( "%d \n", i ) ; while ( i <= n )
i++ ; {
} printf ( "%d \n", i ) ;
return 0; i++ ;
} }
return 0;
}
Chp : 4 - while loop
WAP to find the factorial value of factorial value of any number entered
number entered using for loop.
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i, fact, num;
int i, fact, num;
i = 1; fact = 1;
printf("Enter the number for factorial : "); fact = 1;
scanf("%d",&num); printf("Enter the number for
while (i<=num) factorial : ");
{ scanf("%d",&num);
fact = fact*i; for(i=1;i<=num;i++)
i++; {
} fact = fact*i;
printf("Factorial of Number : %d is : %d",
}
num, fact);
return 0; printf("Factorial of Number : %d is :
} %d", num, fact);
return 0;
}
Chp : 4 - while loop
Questions –
Unlike the while loop, which checks the If the condition is true, the loop
condition before the execution of the continues to execute; otherwise, the loop
block, the do-while loop checks the terminates.
condition after the execution of the block.
do{
//Statements to be executed;
do { return 0;
printf("Enter a integer: "); }
scanf("%d", &number);
if (number <= 0) {
printf("Invalid input. Please
enter a positive integer.\n");
}