C Programming Concepts Explained
C Programming Concepts Explained
7. Stop
Break and Continue Statements in C Write a program for leap year using function.
Break Statement: #include <stdio.h>
• The break statement is used to exit or terminate a // Function to check leap year
loop or switch immediately. int isLeapYear(int year) {
• When break is executed inside a loop (for, while, do- if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
while), it stops the loop and control moves to the return 1; // Leap year
statement after the loop. else
Example of break: return 0; // Not a leap year
for(int i = 1; i <= 5; i++) { }
if(i == 3) { int main() {
break; // Loop stops when i is 3 int year;
} printf("Enter a year: ");
printf("%d ", i); scanf("%d", &year);
} if (isLeapYear(year))
// Output: 1 2 printf("%d is a leap year.\n", year);
Continue Statement: else
• The continue statement is used to skip the current printf("%d is not a leap year.\n", year);
iteration of a loop and move to the next iteration. return 0;
• It does not stop the loop but skips the rest of the }
code inside the loop for the current cycle.
Write a program for prime number using function.
Example of continue:
#include <stdio.h>
for(int i = 1; i <= 5; i++) {
// Function to check prime
if(i == 3) {
int isPrime(int num) {
continue; // Skip printing 3
if (num <= 1)
}
return 0; // Not prime
printf("%d ", i);
for (int i = 2; i <= num / 2; i++) {
}
if (num % i == 0)
// Output: 1 2 4 5
return 0; // Not prime
Summary:
}
Statement Effect inside loop return 1; // Prime
break Exit the loop immediately }
continue Skip current iteration, continue with next int main() {
int number;
Properties of Arrays in C printf("Enter a number: ");
1. Collection of elements: scanf("%d", &number);
An array stores multiple elements of the same data if (isPrime(number))
type in a single variable. printf("%d is a prime number.\n", number);
2. Fixed size: else
The size of an array is fixed when declared and printf("%d is not a prime number.\n", number);
cannot be changed during program execution. return 0;
3. Contiguous memory allocation: }
Array elements are stored in continuous memory
Write a program to count number of characters in a string
locations, allowing efficient access.
using a looping statement.
4. Indexed elements:
#include <stdio.h>
Each element is accessed by an index, starting from 0
int main() {
(zero-based indexing).
char str[100];
5. Homogeneous elements:
int count = 0;
All elements in an array must be of the same type
printf("Enter a string: ");
(e.g., all int, all float).
fgets(str, sizeof(str), stdin); // Read string with spaces
6. Random access:
// Count characters until null character '\0' or newline '\n'
Elements can be accessed directly using their index
for (int i = 0; str[i] != '\0' && str[i] != '\n'; i++) {
without traversing the whole array.
count++;
7. Single name with multiple values:
}
An array variable refers to the whole collection, but
printf("Number of characters: %d\n", count);
individual elements are accessed by their index.
return 0;
}
Cast Operator in C Definition of Identifier in C
• The cast operator is used to convert a variable from An identifier is the name given to variables, functions, arrays,
one data type to another explicitly. or other user-defined objects in a program. It is used to
• It tells the compiler to treat a variable as a different identify these entities uniquely in the code.
type temporarily.
Rules for Naming an Identifier
• The syntax for casting is to place the desired type in
parentheses before the variable or expression. 1. Allowed Characters:
Syntax: An identifier can contain letters (A-Z, a-z), digits (0-
(type) expression 9), and underscore (_).
Example: 2. Must Start with Letter or Underscore:
int a = 10;
float b; The first character of an identifier must be a letter or
b = (float) a; // Casting integer 'a' to float an underscore. It cannot start with a digit.
before assignment
3. No Spaces or Special Characters:
Why use cast operator?
Identifiers cannot contain spaces, or special
• To force type conversion when automatic conversion
does not happen. characters like @, #, $, %, etc.
• To avoid loss of data or precision issues. 4. Case Sensitive:
• To make expressions compatible with certain Identifiers are case sensitive. For example, Var and
operations or functions. var are different identifiers.
Summary: 5. Cannot Use Keywords:
Term Explanation Identifiers cannot be the same as C reserved
Cast Explicitly converts a value to another data keywords like int, for, while, return, etc.
Operator type 6. Length Limit:
Although modern compilers allow long names, it’s
good practice to keep identifiers meaningful and
reasonably short.
Different Ways of String Initialization in C
1. Using String Literal during Declaration Explain data types in C.
char str1[] = "Hello";
• The string "Hello" is automatically stored with a null Data Types in C
character \0 at the end. A data type in C defines the type of data a variable can hold
• The array size is determined by the compiler (6 in this and the amount of memory it occupies. It tells the compiler
case, including \0). how to interpret the bits stored in memory.
What is a String in C?
• A string in C is a sequence (array) of characters stored What is Associativity?
in contiguous memory locations. • Associativity in C programming tells us the
• It ends with a special null character \0 which indicates
order in which operators of the same
the end of the string.
precedence are evaluated in an expression.
• Strings are used to represent text like names,
• It can be left to right or right to left.
sentences, etc.
• This helps decide how expressions with
Declaration of a String
char str[20]; multiple operators of the same precedence are
• Declares a character array named str that can hold up grouped.
to 19 characters plus the null character. Associativity of Unary Operators
• Unary operators include: ++ (increment), --
Initialization of a String (decrement), + (unary plus), - (unary minus), !
1. Using double quotes (automatic null terminator): (logical NOT), ~ (bitwise NOT), and & (address
char str[] = "Hello"; of).
2. Using character array with manual null terminator: • The associativity of unary operators is right to
char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
left.
Example:
• This means if you have multiple unary
#include <stdio.h>
int main() { operators, they are applied from right to left.
char greeting[] = "Hello, World!";
printf("%s\n", greeting); Example:
return 0; int a = 5;
} int b = -++a; // First ++a is applied, then unary minus
(-)
Algorithm to Print First N Fibonacci Numbers Write an Algorithm and Flowchart to find largest of
1. Start three numbers..
2. Input N (number of Fibonacci numbers to print) Algorithm to Find Largest of Three Numbers
3. Initialize: 1. Start
o a = 0 (first Fibonacci number) 2. Input three numbers: A, B, C
o b = 1 (second Fibonacci number) 3. If A > B and A > C then
o count = 2 (since first two numbers are known) Print "A is the largest"
4. Print a and b 4. Else if B > C then
5. While count < N do: Print "B is the largest"
o next = a + b 5. Else
o Print next Print "C is the largest"
o Update a = b 6. End
o Update b = next Flowchart Description (Boxes)
o Increment count by 1 Step Operation Box Type
6. End
1 Start Terminator
Flowchart (Visual Description)
2 Input A, B, C Input/Output
[Start]
↓ 3 Is A > B and A > C? Decision
[Input N] 4 Print "A is largest" Output
↓ 5 Is B > C? Decision
[Initialize a=0, b=1, count=2]
6 Print "B is largest" Output
↓
[Print a and b] 7 Print "C is largest" Output
↓ 8 End Terminator
[Is count < N?] —No→ [End] Flowchart (Visual Description)
↓Yes [Start]
[Calculate next = a + b] ↓
↓ [Input A, B, C]
[Print next] ↓
↓ [Is A > B and A > C?] —No→
[Update a = b, b = next] ↓Yes [Is B > C?]
↓ [Print A] —No→ ↓Yes
[Increment count] ↓ [Print C] [Print B]
↓ [End] ↓ ↓
(Loop back to "Is count < N?") [End] [End]