0% found this document useful (0 votes)
5 views

C Language Tutorials

The document provides an introduction to the C programming language. It discusses installing editors and compilers needed for C development, describes the structure of a basic C program including main functions and header files, covers various C language concepts like variables, data types, operators, and input/output, and provides a sample program to demonstrate variable declaration.

Uploaded by

schoolhelpmentor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Language Tutorials

The document provides an introduction to the C programming language. It discusses installing editors and compilers needed for C development, describes the structure of a basic C program including main functions and header files, covers various C language concepts like variables, data types, operators, and input/output, and provides a sample program to demonstrate variable declaration.

Uploaded by

schoolhelpmentor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 63

C Language

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>

First int main(){

Program In printf("Hello World");


return 0;
C }

Output :
Header File
#include <stdio.h>
Main Program

int main() Instruction / Code


{ In Program

printf("Hello World");
return 0;
}

Understand End of program

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

• Database systems / Gaming / Scientific • Portability

applications / Financial applications • Easy to Extend


Low Level Language
• Machine language: This is the lowest level of • Assembly language: This is a slightly higher level of

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

Output file is converted into assembly-


Compiler level instructions
How
Compiling
Works ? Assembler
Output file is converted into hardware-
level instructions

File from Assembler is converted into


Linker executable file
Comments in C Multi-line comment
/* C program to illustrate
Single-line comment use of multi-line comment */
// C program to illustrate #include <stdio.h>
// use of single-line comment int main(void)
#include <stdio.h> {
/* This is a multi-line comment */
int main(void)
{ /* This comment contains some code
// This is a single-line comment which will not be executed.
printf("Welcome to printf("Code enclosed in Comment");
GeeksforGeeks"); */
return 0; printf("Welcome to GeeksforGeeks");
} return 0;
}
• These fundamental building blocks come together to form
Keywords the essence of a C program, much like individual bricks
construct a sturdy structure.

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

The keywords are pre- Identifier is a unique name


defined or reserved words given to an entity (such as a
in a programming variable, function, or array)
language. in a program.
TOKENS Each keyword performs Identifiers are user-defined
specific function in a words used for naming
program. various program elements.
C language has 32 Examples: Structure names,
keywords function names, class names.
Variables

Variable is the name of a memory #include <stdio.h>


location which stores some data.
int main()
{
int num = 10;
Internal Memory char name;
x1 x4 x5 float pi = 3.14;
1 10 17
return 0;
}
Rule for Declaring Variables

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;

• They are like normal variables but num = 15


with the difference that their values printf(num)
can not be modified in the program
once they are defined.
Strings
• Strings are nothing but an array of char string1[20] = "hello world"
characters ended with a null
char string2[20] = "hello world2"
character (‘\0’).
printf(string1)
• This null character indicates the end
of the string. printf(string2)
• Strings are always enclosed in double
quotes.
Special Symbols Operators

Brackets[] - used for array declaration Arithmetic operators


Parentheses() - Used for Relational Operators
function declaration Logical Operators
Braces{} - Used for main program block Assignment Operators
code Bitwise Operator
Comma (, )
Semicolon(;)
Pre-processor (#)
Test Program
#include <stdio.h> #include <stdio.h>
// This program is to let user /* This comment can handle
understand variable paragraph */
declaration.
int main()
int main() {
{ int _ num = 10;
int _ num = 10; char 1,name;
char 1,name; float @pi = 3.14;
float @pi = 3.14; return 0;
return 0; }
}
I/O Process - Output
Print Output Characters : Print Characters with New Line :

#include <stdio.h> #include <stdio.h>

int main() int main()


{ {
printf("Hello World"); printf("Hello World \n");
printf("Hello World"); printf("Hello World \n");
printf("Hello World"); printf("Hello World \n");
return 0; return 0;
} }
I/O Process - Output
Printing integer value 1. Characters
o printf(" A hold mentioned
1. Printing Integer Value character value : %c ", abc)
o printf(" value of a is %d", a);
o To print value of integer we need
to use %d.

2. Printing Real Value


o printf(" value of a is %f ", a);
o To print value of real
number we need to use %f.
Program :

I/O Process - Input


#include <stdio.h>
Input
int main(){
scanf("%d", &age);
int age;
printf("Enter your age : ");
• %d : Input type –
int/char/float scanf("%d",&age);
• &age : Address in Memory printf("The age entered : %d", age);
return 0;
}
Q : WAP to add 2 number and share sum.
#include <stdio.h> #include <stdio.h>

int main() int main()

{ {

int a,b,sum; int a,b,sum;

printf("Enter number 1 : "); printf("Enter number 1 : ");

scanf("%d",&a); scanf("%d",&a);

printf("Enter number 2 : "); printf("Enter number 2 : ");

scanf("%d",&b); scanf("%d",&b);

sum = a + b; printf("The age entered : %d", a+b);

printf("The age entered : %d", sum); return 0;

return 0; }

}
Question -

Q : Write a program to calculate area of a square. (Area = side*side)


Q : Write a program to calculate area of circle. (Area = Pi*r(sqr))
Q : Write a program to calculate perimeter of rectangle. Take sides
as a,b from user.
Q : Write a program to calculate cube. Cube = a*a*a
Q : Write a program to calculate area of Scalene Triangle (All three sides of a scalene
triangle have different lengths, i.e. A = ½ (b*h) ).
Q : Write a program to calculate area of Isosceles Triangle (two sides are equal in length,
i.e. A = ½ a(sqr) )
Q : Write a program to take input in Fahrenheit and output in Celsius
Instructions Statements in Program to perform task.

• Type Declaration Instructions • Type Declaration Instructions


o Variable needs to be declared in there correct
• Arithmetic Instructions
data type.
• Control Instructions o Declare variable before using it.

Valid Declaration InValid Declaration

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

Valid Declaration InValid Declaration

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

• Sequence Control Instructions process take as per sequence

• Decision Control If else. Conditions based on input given

• Loop Control Loops Condition : To process same instruction multiple times

• 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 + , - , / , * , %

• Relational Operator = = , It tell relation between 2 operands

• Logical Operator It tells logical relation between 2 operand

• Bitwise Operators If operation take place on 0 & 1.

• Assignment Operators Assignment of value

• Ternary Operator Conditional Operator, same as if else


Relation Operators Logical Operators
• = = If LHS is equal to RHS, 1 == 2 • & & And Operator

• > , >= Greater than operator • || OR Operator

• < , <= Less than operator • ! Not

• != Not Operator

Question

• !((5>1) && (3>4))


Assignment Operators
• = • WAP to check if a number is divisible by 2
or not. (Take input by user).
• += a = a + b; -> a += b; • WAP to check if a number is odd or even.
• -= a = a - b; -> a -= b; • int x; int y=x;

• *= a = a * b; -> a *= b; • int x, y=x;

• 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.

• WAP to print the average of 3 number.

• WAP to check if given character is digit or


not.

• WAP to print smallest number.


Chp : 3 - Conditional Statements
• First Types of If Conditional Statement 2nd Types of If Conditional Statement

if else if elseif else

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){

/* WAP to check if age is between 13 – 18 printf("User is Student.");


: Student, if over 18 : can vote. */
}
int main()
else{
{
printf("User is a child.");
int age;
}
printf("Enter your age : ");
return 0;
scanf("%d", &age);
}
if (age>=18){

printf("The age : %d, you can


vote.", age);

}
Chp : 3 - Conditional Statements – Ternary
Condition ? Perfrm_action if TRUE : perfrm_action if FALSE ;

#include<stdio.h> age >= 18 ? printf("Vote : True \


n") : printf("Vote : False \n");
/* WAP to check if age 18 : can vote. */
return 0;
int main()
}
{

int age;

printf("Enter your age : ");

scanf("%d", &age);
Chp : 3 - Conditional Statements – Switch
#include <stdio.h> case 8:
Switch Operator
int main() { printf("Grade B\n");

switch(number) int num; break;

// Accept user input case 7:


case A1 : // perform action
printf("Enter the number: "); printf("Grade C\n");
break; scanf("%d", &num); break;
case A2 : // perform action // Using switch to determine default:
grade
break; printf("Fail\n");
switch (num/10) {
default : break;
// perform action case 10:
}
case 9:
return 0;
printf("Grade A\n");
}
break;
Chp : 3 - Conditional Statements
Questions –

1. WAP to enter day of week in number and


respond with name of day using switch.

2. WAP taking first letter of week as input


from user, respond with name of day.

3. While purchasing certain items, a


discount of 10% is offered if the
quantity purchased is more than 1000. If
quantity and price per item are input
through the keyboard, write a program to
calculate the total expenses. (Item and
rate of items.)
Chp : 3 - Nested Conditional Statements
printf("Number is positive. \n");
a. Cases can be in any order
if(num%2 == 0){
b. Nested Condition
printf("Number is Even. \n");

#include<stdio.h> }

int main(){ else{

int num; printf("Number is Odd. \n");

// Input by user. }

}
printf("Enter Number : ");
else{
scanf("%d", &num);
printf("Number is Negative. \n");
// checking if number is positive
and even or odd. }

if (num >=0){ return 0;

}
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

1) It provides code reusability. Loop


2) Using loops, we do not need to write the
same code again and again.
for loop while do while
Chp : 4 - for Loops
Syntax – for(initialization; condition; updation){

//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>

int main() int main()

{ {

printf("Hellow World.\n"); int a;

printf("Hellow World.\n"); for(a=1;a<=5;a++){

printf("Hellow World.\n"); printf("%d\n",a);

printf("Hellow World.\n"); }

printf("Hellow World.\n"); return 0;


return 0;
} }
Chp : 4 - for Loops
WAP to print character values corresponding WAP to print Capital A to D character values
to numeric value. corresponding to there numeric value using
for loop.
#include<stdio.h>

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 main() int main()

{ {

int x = 5; int x = 5;

printf("%d\n", x); printf("%d\n", x);

int y = ++x; int y = x++;

printf("%d\n", x); printf("%d\n", x);

printf("%d\n", y); printf("%d\n", y);

return 0; return 0;

} }
Chp : 4 - Increment Operator
Pre-Decrement Operator Post-Decrement Operator

#include<stdio.h> #include<stdio.h>

int main() int main()

{ {

int x = 5; int x = 5;

printf("%d\n", x); printf("%d\n", x);

int y = --x; int y = x--;

printf("%d\n", x); printf("%d\n", x);

printf("%d\n", y); printf("%d\n", y);

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.

Includes uppercase and lowercase


ASCII - Developed in the 1960s. letters, digits, punctuation marks,
control characters, and special symbols.
Created to standardize the
representation of text in computers and
other devices.
Chp : 4 - while loop
while (condition) { // Statements // Increment /
Decrement }
#include<stdio.h>
• In a while loop, initialization and
increment/decrement of loop control int main()
variables must be done explicitly {
before and/or within the loop. initialize loop counter ;
while ( condition )
• The syntax for a while loop is simpler {
and more flexible compared to a for
loop. task ;
loop increment counter ;
• while loops are generally used when }
the number of iterations is not known return 0;
beforehand or when the loop condition }
is complex and needs to be evaluated
separately.
Chp : 4 - while loop
WAP to print number using while loop. WAP to print number using while loop.

#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 –

1. WAP to print table of number entered by


user.

2. WAP to print table of 2.

3. Calculate sum of all number between 5


and 50 using loop.
Chp : 4 - do while loop
The do-while loop executes a block of The statements within the do block are
statements repeatedly until the specified executed at least once before the condition
condition becomes false. is evaluated.

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;

} while ( this condition is true ) ;


Chp : 4 - do while loop
WAP to determine whether a number is positive integer.

#include <stdio.h> } while (number <= 0);

int main() { printf("Number entered : %d is a


int number; positive number.\n", number);

do { return 0;
printf("Enter a integer: "); }
scanf("%d", &number);

if (number <= 0) {
printf("Invalid input. Please
enter a positive integer.\n");
}

You might also like