Podcast
Questions and Answers
What is the primary purpose of a dearness allowance in salary calculations?
What is the primary purpose of a dearness allowance in salary calculations?
- To provide health insurance
- To cover transportation costs
- To reduce loan repayment amounts
- To offset inflation effects (correct)
Which of the following is NOT a data type in C programming?
Which of the following is NOT a data type in C programming?
- int
- double
- string (correct)
- float
What is the correct use of the continue statement in C?
What is the correct use of the continue statement in C?
- To restart a loop from the beginning
- To end the loop immediately
- To jump to the end of a program
- To skip the current iteration of a loop (correct)
What is the output of a program that calculates the area of a rectangle with length 5 and breadth 10?
What is the output of a program that calculates the area of a rectangle with length 5 and breadth 10?
In C programming, which of the following is a characteristic of a token?
In C programming, which of the following is a characteristic of a token?
Which of the following best describes a loop in C programming?
Which of the following best describes a loop in C programming?
When converting Fahrenheit to Centigrade, which formula is used?
When converting Fahrenheit to Centigrade, which formula is used?
Which keyword is used to declare a variable in C programming?
Which keyword is used to declare a variable in C programming?
Flashcards are hidden until you start studying
Study Notes
C Programming Language Characteristics
- C is a structured programming language, allowing code organization and modularity.
- It is a procedural language, focusing on steps and algorithms.
- C supports various data types, including integers, floats, characters, and arrays.
- The language is portable, meaning it can be run on different platforms.
- C offers a wide range of operators, including arithmetic, relational, logical, and bitwise.
- C is a powerful and versatile language with a rich set of built-in functions and libraries.
Structure of a C Program
- A C program typically consists of a set of functions, including a main function that serves as the program's entry point.
- Functions are blocks of code that perform specific tasks.
- Comments are used to explain code and are ignored by the compiler.
- Preprocessing directives, starting with
#
(e.g.,#include <stdio.h>
) include headers and configure the compiler. - Variable declarations define the data types and names of variables used in the program.
- Statements are instructions executed by the program in sequential order.
- The
return
statement is used to return a value from a function.
Constants in C
- Constants are fixed values that cannot be changed during program execution.
- Integer constants: Whole numbers (e.g.,
10
,-5
,0
). - Floating-point constants: Numbers with decimal points (e.g.,
3.14
,-2.5
). - Character constants: Single characters enclosed in single quotes (e.g.,
'A'
,'%'
). - String constants: Sequences of characters enclosed in double quotes (e.g.,
"Hello"
).
Tokens in C
- Tokens are the basic building blocks of a C program, recognized by the compiler.
- Example of each:
- Keywords:
if
,else
,for
,while
- Identifiers:
name
,age
,sum
- Constants:
100
,3.14
,'a'
,"Hello"
- Operators:
+
,-
,*
,/
,%
,<
,>
,==
,!=
- Special Symbols:
{
,}
,(
,)
,;
,#
- Keywords:
Keywords and Identifiers
- Keywords: Reserved words with special meaning in the C compiler.
- Identifiers: User-defined names for variables, functions, arrays, etc.
- Key Differences:
- Purpose: Keywords have predefined roles, while identifiers are user-created for specific entities.
- Case-sensitivity: Keywords are often case-insensitive, while identifiers may be case-sensitive depending on your compiler.
Data Types in C
- Data types define the kind of data a variable can hold and the operations it can perform.
- Basic Data Types:
int
: for integers (whole numbers)float
: for floating-point numbers (numbers with decimal points)char
: for single charactersdouble
: for larger floating-point numbers with higher precision
- Derived Data Types:
- Arrays: Collections of elements of the same data type.
- Structures: User-defined data types that group variables of different types.
- Unions: Memory spaces that can hold different data types at different times.
- Pointers: Variables that store memory addresses.
Naming Conventions for Variables
- Start with a Letter or underscore:
counter
,_radius
- Use Alphanumeric Characters:
total_cost
,score_1
- Case-Sensitive:
totalCost
is different fromtotalcost
- No Spaces or Special Characters: Use underscores instead of spaces (e.g.,
balance_due
). - Descriptive Names: Choose names that clearly explain the variable's purpose.
Program Examples
- Ramesh's Gross Salary:
#include <stdio.h>
int main() {
float basicSalary, dearnessAllowance, houseRentAllowance, grossSalary;
printf("Enter Ramesh's basic salary: ");
scanf("%f", &basicSalary);
dearnessAllowance = 0.40 * basicSalary;
houseRentAllowance = 0.20 * basicSalary;
grossSalary = basicSalary + dearnessAllowance + houseRentAllowance;
printf("Ramesh's gross salary is: %.2f\n", grossSalary);
return 0;
}
- Distance Conversion:
#include <stdio.h>
int main() {
int distanceKm, distanceMeters, distanceCentimeters, distanceInches, distanceFeet;
printf("Enter the distance between two cities in kilometers: ");
scanf("%d", &distanceKm);
distanceMeters = distanceKm * 1000;
distanceCentimeters = distanceMeters * 100;
distanceInches = distanceCentimeters / 2.54 ;
distanceFeet = distanceInches / 12;
printf("Distance in meters: %d\n", distanceMeters);
printf("Distance in centimeters: %d\n", distanceCentimeters);
printf("Distance in inches: %d\n", distanceInches);
printf("Distance in feet: %d\n", distanceFeet);
return 0;
}
- Student Marks and Percentage:
#include <stdio.h>
int main() {
int subject1, subject2, subject3, subject4, subject5, totalMarks, percentage;
printf("Enter marks for subject 1: ");
scanf("%d", &subject1);
printf("Enter marks for subject 2: ");
scanf("%d", &subject2);
printf("Enter marks for subject 3: ");
scanf("%d", &subject3);
printf("Enter marks for subject 4: ");
scanf("%d", &subject4);
printf("Enter marks for subject 5: ");
scanf("%d", &subject5);
totalMarks = subject1 + subject2 + subject3 + subject4 + subject5;
percentage = (totalMarks * 100) / 500;
printf("Total Marks: %d\n", totalMarks);
printf("Percentage: %d%%\n", percentage);
return 0;
}
- Fahrenheit to Centigrade Conversion:
#include <stdio.h>
int main() {
float fahrenheit, centigrade;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
centigrade = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Centigrade: %.2f\n", centigrade);
return 0;
}
- Rectangle and Circle Calculations:
#include <stdio.h>
int main() {
float length, breadth, radius, rectangleArea, rectanglePerimeter, circleArea, circleCircumference;
printf("Enter length of the rectangle: ");
scanf("%f", &length);
printf("Enter breadth of the rectangle: ");
scanf("%f", &breadth);
printf("Enter radius of the circle: ");
scanf("%f", &radius);
rectangleArea = length * breadth;
rectanglePerimeter = 2 * (length + breadth);
circleArea = 3.14159 * radius * radius;
circleCircumference = 2 * 3.14159 * radius;
printf("Rectangle Area: %.2f\n", rectangleArea);
printf("Rectangle Perimeter: %.2f\n", rectanglePerimeter);
printf("Circle Area: %.2f\n", circleArea);
printf("Circle Circumference: %.2f\n", circleCircumference);
return 0;
}
- Swapping Two Numbers:
#include <stdio.h>
int main() {
int c, d, temp;
printf("Enter the value of C: ");
scanf("%d", &c);
printf("Enter the value of D: ");
scanf("%d", &d);
printf("Before swapping: C = %d, D = %d\n", c, d);
temp = c;
c = d;
d = temp;
printf("After swapping: C = %d, D = %d\n", c, d);
return 0;
}
Branching and Looping in C
- Branching: Used to alter the program's execution flow based on conditions.
if
statement: Executes a block of code if a condition is true.else
statement: Executes a different block of code if theif
condition is false.if-else if-else
chain: Allows for multiple conditions to be evaluated.
- Looping: Repeats a block of code multiple times until a certain condition is met.
for
loop: Executes a block of code a specific number of times.while
loop: Executes a block of code as long as a condition remains true.do-while
loop: Executes a block of code at least once, then checks a condition to continue looping.
- Switch Statement: Evaluates an expression against a set of constant integer values, and executes the corresponding code block.
- Goto Statement: Unconditionally jumps to a labeled statement within the same function, often avoided due to potential code complexity.
- Comma Operator: Evaluates multiple expressions, with the last expression's value being returned. Used to combine statements that may be executed independently.
Operator Precedence in Expression Evaluation:
- Operators have different priorities when evaluating expressions.
- The order of operations is used to determine which operations are performed first:
- Parentheses: Expressions inside parentheses are evaluated first.
- Unary operators: Operators acting on single operands (e.g.,
!
,-
) are evaluated next. - Multiplicative operators:
*
,/
,%
- Additive operators:
+
,-
- Relational operators:
==
,!=
,<
,>
,<=
,>=
- Logical operators:
&&
,||
- Assignment operator:
=
Prime Number Algorithm, Flowchart, and Code
- Algorithm:
- Input an integer.
- If the number is less than 2, it's not a prime number.
- Iterate from 2 to the square root of the number.
- Check if the number is divisible by any of these values.
- If divisible, it's not a prime number; otherwise, it is.
- Flowchart: (To be visually drawn but can't be included here - would visually show the steps described in the algorithm).
- C Code:
#include <stdio.h>
#include <math.h>
int main() {
int number, i, isPrime = 1;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= sqrt(number); i++) {
if (number % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}
return 0;
}
Continue Statement
- The
continue
statement is used to skip the current iteration of a loop without terminating the loop entirely. - It jumps to the next iteration of the loop.
Switch Statement in C
- Syntax:
switch (expression) {
case constant_value1:
// Code to be executed if expression matches constant_value1
break;
case constant_value2:
// Code to be executed if expression matches constant_value2
break;
// ... more cases
default:
// Code to be executed if no case matches
}
- Example:
#include <stdio.h>
int main() {
int day;
printf("Enter a day of the week (1-7): ");
scanf("%d", &day);
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day number.\n");
}
return 0;
}
Loops and Their Types
- Loops are used to repeat blocks of code as long as a certain condition is met.
- For Loop: Repeats a block of code a specific number of times. It consists of three parts:
- Initialization: Executes once before the loop starts (e.g., setting a loop counter).
- Condition: Checked before each iteration. If true, the loop body executes; if false, the loop terminates.
- Increment/Decrement: Executed after each iteration (e.g., updating the loop counter).
- While Loop: Keeps executing a block of code as long as a specified condition remains true. The condition is checked at the beginning of each iteration.
- Do-While Loop: Similar to a while loop, but the condition is checked at the end of each iteration. This ensures the loop body executes at least once.
Conditional Statements in C
- Conditional statements allow the program to make decisions based on conditions.
- If Statement: Executes a block of code if a specified condition is true.
- Syntax:
if (condition) { // Code to be executedif condition is true }
- Else Statement: Executes a block of code if the
if
statement's condition is false.- Syntax:
if (condition) { // Code to be executed if condition is true } else { // Code to be executed if condition is false }
- Else If Statement: Provides alternative conditions to be checked if previous
if
orelse if
conditions are false.- Syntax:
if (condition1) { // Code for condition1 } else if (condition2) { // Code for condition2 } else if (condition3) { // Code for condition3 } else { // Code for no matching condition }
Factorial Calculation using Recursion
- Recursion: A function calling itself within its own definition.
- Factorial: The product of all positive integers less than or equal to a given number.
- Factorial of 5 (denoted as 5!) is 5 * 4 * 3 * 2 * 1 = 120.
- C Code:
#include <stdio.h>
int factorial(int n);
int main() {
int number, result;
printf("Enter a non-negative integer: ");
scanf("%d", &number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
result = factorial(number);
printf("Factorial of %d is %d\n", number, result);
}
return 0;
}
int factorial(int n) {
if (n == 0) {
return 1; // Base case for recursion
} else {
return n * factorial(n - 1); // Recursive step
}
}
Arrays in C
- Array: A collection of elements of the same data type, stored in contiguous memory locations.
- Initialization:
- Direct initialization: Provide values directly during declaration (e.g.,
int numbers[5] = {1, 2, 3, 4, 5};
) - Partial initialization: Provide some values, others are assigned to 0 (e.g.,
int ages[10] = {20, 25, 30};
) The remaining elements get 0. - Indirect initialization: Assign values to elements individually after declaration (e.g.,
numbers[0] = 10; numbers[1] = 20;
).
- Direct initialization: Provide values directly during declaration (e.g.,
String Handling Functions in C
-
String: A sequence of characters terminated by a null character (
\0
). -
Functions: (Five examples along with descriptions):
strcpy(destination, source)
: Copies a string fromsource
todestination
.strcat(destination, source)
: Concatenates (joins) two strings.strlen(string)
: Returns the length of a string (excluding the null terminator).strcmp(string1, string2)
: Compares two strings lexicographically and returns an integer:- A negative value if
string1
is alphabetically smaller thanstring2
. - Zero if
string1
andstring2
are equal. - A positive value if
string1
is alphabetically larger thanstring2
.
- A negative value if
strchr(string, character)
: Finds the first occurrence of a specified character in a string and returns a pointer to that character.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.