[go: up one dir, main page]

0% found this document useful (0 votes)
8 views8 pages

C Lecture1

C lacture
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

C Lecture1

C lacture
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C – Notes (Sagar Sir)

✅ 1. History of C Language

 Developed by Dennis Ritchie in 1972 at Bell Labs.


 Designed to develop UNIX Operating System.
 C is a middle-level language: Combines features of both high-level and low-level languages.
 Became extremely popular due to its efficiency and portability.
 Standardized later as ANSI C (1989).

✅ 2. Features of C Language

 Simple and Efficient


 Portable across different machines
 Rich set of built-in operators
 Structured Language (Supports functions & modular programming)
 Low-level memory access (using pointers)
 Fast Execution
 Modular programming using functions

✅ 3. Data Types in C
Data Type Size (bytes) Range (Example for int)
 char 1 -128 to 127
 int 4 -2,147,483,648 to 2,147,483,647
 float 4 ±3.4E-38 to ±3.4E+38
 double 8 ±1.7E-308 to ±1.7E+308

✅ 4. Format Specifiers in C
Data Type Format Specifier
 char %c
 int %d
 float %f
 double %lf
 string (char array) %s

✅ 5. Memory Allocation of Data Types (Typical System)


Data Type Memory Allocated
 char 1 byte
 int 4 bytes
 float 4 bytes
 double 8 bytes

CC CCC C Page 1
C – Notes (Sagar Sir)
(Note: Memory size may vary depending on system architecture)

✅ 6. Variables in C

Variables are names assigned to memory locations to store data.

Declaration Syntax:
data_type variable_name;

Example: int marks;, float price;

✅ Rules for Variable Naming

 Must start with a letter (A-Z, a-z) or underscore (_).


 Can contain letters, digits (0-9), and underscores.
 No special characters allowed (like @, #, $, etc.).
 Cannot use reserved keywords (e.g., int, if, return, etc.).
 Variable names are case-sensitive.
Example: Marks, marks, and MARKS are three different variables.

✅ 7. Control Statements – If and Else


✅ If Statement Syntax
if (condition) {
// Code executed if condition is true
}

✅ If-Else Statement Syntax


if (condition) {
// Executed if condition is true
} else {
// Executed if condition is false
}

✅ Types of If Statements

 Simple if

if (a > b) {
printf("a is greater than b");
}

CC CCC C Page 2
C – Notes (Sagar Sir)
 If-Else

if (a > b) {
printf("a is greater");
} else {
printf("b is greater or equal");
}

 If-Else If Ladder

if (a > b) {
printf("a is greatest");
} else if (b > c) {
printf("b is greatest");
} else {
printf("c is greatest");
}

✅ 8. Examples
✅ Example 1 – Simple If

main() {
int a = 10, b = 5;

if (a > b) {
printf("a is greater than b\n");
}
}

✅ Example 2 – If-Else

main() {
int a = 10, b = 20;

if (a > b) {
printf("a is greater\n");
} else {
printf("b is greater\n");
}
}

✅ Example 3 – If-Else If Ladder

CC CCC C Page 3
C – Notes (Sagar Sir)

main() {
int a = 10, b = 20, c = 15;

if (a > b && a > c) {


printf("a is greatest\n");
} else if (b > c) {
printf("b is greatest\n");
} else {
printf("c is greatest\n");
}
}

✅ Example 4 – Check Even or Odd Number

main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (number % 2 == 0) {
printf("The number is Even.\n");
} else {
printf("The number is Odd.\n");
}
}

✅ Example 5 – Check Positive, Negative, or Zero

main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (num > 0) {
printf("The number is Positive.\n");
} else if (num < 0) {
printf("The number is Negative.\n");
} else {

CC CCC C Page 4
C – Notes (Sagar Sir)
printf("The number is Zero.\n");
}
}

✅ Example 6 – Check Eligibility for Voting

main() {
int age;

printf("Enter your age: ");


scanf("%d", &age);

if (age >= 18) {


printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
}

✅ Example 7 – Find the Largest of Two Numbers

main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

if (num1 > num2) {


printf("%d is greater than %d\n", num1, num2);
} else {
printf("%d is greater than %d\n", num2, num1);
}
}

✅ Example 8 – Check Character is Vowel or Consonant

main() {
char ch;

printf("Enter an alphabet character: ");

CC CCC C Page 5
C – Notes (Sagar Sir)
scanf("%c", &ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||


ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("%c is a Vowel.\n", ch);
} else {
printf("%c is a Consonant.\n", ch);
}
}

✅ Example 9 – Check Leap Year or Not

main() {
int year;

printf("Enter a year: ");


scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


printf("%d is a Leap Year.\n", year);
} else {
printf("%d is not a Leap Year.\n", year);
}
}

✅ Example 10 – Check if Number is Divisible by 7

main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (number % 7 == 0) {
printf("%d is divisible by 7.\n", number);
} else {
printf("%d is not divisible by 7.\n", number);
}
}

✅ Example 11 – Print Number in Words (1 to 10)

CC CCC C Page 6
C – Notes (Sagar Sir)
main() {
int num;

printf("Enter a number (1 to 10): ");


scanf("%d", &num);

if (num == 1) {
printf("One\n");
} else if (num == 2) {
printf("Two\n");
} else if (num == 3) {
printf("Three\n");
} else if (num == 4) {
printf("Four\n");
} else if (num == 5) {
printf("Five\n");
} else if (num == 6) {
printf("Six\n");
} else if (num == 7) {
printf("Seven\n");
} else if (num == 8) {
printf("Eight\n");
} else if (num == 9) {
printf("Nine\n");
} else if (num == 10) {
printf("Ten\n");
} else {
printf("Invalid input! Please enter a number between 1 and 10.\n");
}
}

✅ Example 12 – Print Day of the Week (1 to 7)

main() {
int day;

printf("Enter day number (1 to 7): ");


scanf("%d", &day);

if (day == 1) {
printf("Sunday\n");
} else if (day == 2) {

CC CCC C Page 7
C – Notes (Sagar Sir)
printf("Monday\n");
} else if (day == 3) {
printf("Tuesday\n");
} else if (day == 4) {
printf("Wednesday\n");
} else if (day == 5) {
printf("Thursday\n");
} else if (day == 6) {
printf("Friday\n");
} else if (day == 7) {
printf("Saturday\n");
} else {
printf("Invalid input! Please enter a number between 1 and 7.\n");
}
}

CC CCC C Page 8

You might also like