0% found this document useful (0 votes)
50 views10 pages

C Programming Concepts Explained

The document provides an overview of storage classes in C, detailing their types: auto, register, static, and extern, along with their scope and lifetime. It also includes algorithms for swapping two numbers, checking even or odd numbers, and additional programming concepts such as tokens, constants, operators, and control statements. Furthermore, it discusses the history of C programming, properties of arrays, and rules for naming identifiers.

Uploaded by

ranjeetbhutale
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)
50 views10 pages

C Programming Concepts Explained

The document provides an overview of storage classes in C, detailing their types: auto, register, static, and extern, along with their scope and lifetime. It also includes algorithms for swapping two numbers, checking even or odd numbers, and additional programming concepts such as tokens, constants, operators, and control statements. Furthermore, it discusses the history of C programming, properties of arrays, and rules for naming identifiers.

Uploaded by

ranjeetbhutale
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

Write a short note on storage classes in C.

Write an algorithm and design a flowchart for swapping two


Definition: numbers
A storage class in C defines the scope (visibility), lifetime, and Algorithm to Swap Two Numbers
linkage of variables or functions within a program. It tells the Step 1: Start
compiler where to store the variable, how long to keep it in Step 2: Declare variables a, b, and temp
memory, and who can access it. Step 3: Read the values of a and b
Types of Storage Classes in C: Step 4: Store the value of a in temp → temp = a
There are four types of storage classes in C: Step 5: Assign value of b to a → a = b
1. auto Step 6: Assign value of temp to b → b = temp
• Default storage class for local variables. Step 7: Print the values of a and b (after swapping)
• Scope: Local to the block. Step 8: Stop
• Lifetime: Exists during the execution of the block. Flowchart for Swapping Two Numbers
Example: +---------+
auto int a = 10; | Start |
2. register +---------+
• Suggests storing variable in CPU register instead of |
RAM for faster access. v
• Scope: Local to the block. +-------------------+
• Lifetime: Till the block is active. | Read a and b |
• Cannot use address-of operator (&) with register +-------------------+
variables. |
Example: v
register int count = 0; +-------------------+
3. static | temp = a |
• Retains its value between function calls. +-------------------+
• Scope: |
o Inside a function: Local but retains value. v
o Global: Restricts access to the file. +-------------------+
Example: |a=b |
static int x = 0; +-------------------+
4. extern |
• Used to declare a global variable or function that is v
defined in another file or location. +-------------------+
• Provides external linkage. | b = temp |
Example: +-------------------+
extern int a; |
Conclusion: v
Storage classes control where, how long, and who can use a +-------------------+
variable or function, helping manage memory and program | Print a and b |
structure efficiently. +-------------------+
|
v
+---------+
| Stop |
+---------+
Write program to check whether a number is even or odd.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
return 0;
}
What is a Token in C? Enlist and explain types of tokens. Explain the types of constants in C.
Definition: Definition:
In C, a token is the smallest unit in a program that has A constant in C is a fixed value that does not change during
meaning to the compiler. the execution of a program. Constants can be of different
The compiler breaks the source code into these tokens types based on the kind of data they represent.
during the compilation process. Types of Constants in C:
Types of Tokens in C: Constants in C are mainly classified into the following types:
There are six types of tokens in C: 1. Integer Constants:
1. Keywords • Whole numbers without any fractional part.
o Reserved words with special meaning. • Can be written in:
o Example: int, return, if, else o Decimal: base 10 (e.g., 100)
2. Identifiers o Octal: base 8, starts with 0 (e.g., 075)
o Names given to variables, functions, arrays, o Hexadecimal: base 16, starts with 0x or 0X
etc. (e.g., 0x1A)
o Must begin with a letter or underscore. Example: 10, 0, -55, 0x2F
o Example: total, sum, _count 2. Floating-point Constants:
3. Constants • Numbers with a fractional part or expressed in
o Fixed values that do not change during exponential (scientific) notation.
program execution. Example: 3.14, -0.005, 2.5e3
o Types: Integer (10), Float (3.14), Char ('A') 3. Character Constants:
4. String Literals • A single character enclosed in single quotes.
o Sequence of characters in double quotes. • Represented internally as an integer using ASCII value.
o Example: "Hello", "C Program" Example: 'A', '1', '%'
5. Operators 4. String Constants (String Literals):
o Symbols used to perform operations. • A sequence of characters enclosed in double quotes.
o Example: +, -, ==, && • Ends with a null character '\0'.
6. Special Symbols / Punctuators Example: "Hello", "123", "C Programming"
o Symbols used for syntax and structure. 5. Enumeration Constants:
o Example: ;, {}, (), [] • Used with the enum keyword to define a set of
named integer constants.
What are Relational Operators? Provide examples.
Example:
Definition:
Day { MON, TUE, WED };
Relational operators are used to compare two values or
Here, MON = 0, TUE = 1, WED = 2
variables. They check the relationship between operands
Conclusion:
and return either true (1) or false (0).
Constants are essential in C programming to define fixed
Common Relational Operators in C:
values that improve code readability and maintainability.
Operator Meaning Example
== Equal to a == b
!= Not equal to a != b Constants Variables
> Greater than a>b
< Less than a<b Fixed values that do not Values that can change during
change during execution. execution.
>= Greater than or equal a >= b
<= Less than or equal a <= b Declared using const or Declared using data types like
Example: #define. int, float, etc.
int a = 5, b = 10;
if (a < b) { Used to represent Used to store changing or
unchanging values. dynamic values.
printf("a is less than b");
} Occupy memory only once Occupy memory and can be
In this example, a < b is a relational expression that checks if and remain constant. updated multiple times.
a is less than b.
Example: const int a = 10;, Example: int x = 5;, float
#define PI 3.14 marks = 89.5;
Explain the different types of operators in C with examples. Brief History of C Programming
A C operator can be defined as the symbol that helps us to • C programming language was developed by
perform some specific mathematical, relational, bitwise, Dennis Ritchie at Bell Laboratories (Bell Labs)
conditional, or logical computations on values and variables. between 1972 and 1973.
The values and variables used with operators are • It was designed as a successor to the B language
called operands. So, we can say that the operators are the (which was itself derived from BCPL) to provide a
symbols that perform operations on operands. more efficient and powerful programming
Types of Operators in C language.
C language provides a wide range of built in operators that can • The primary goal was to develop the UNIX
be classified into 6 types based on their functionality: operating system, and C was used to rewrite UNIX,
Operator What it Does (in simple words) which was originally written in assembly language.
• C provided low-level access to memory, simple set
Arithmetic Used to do math like adding, subtracting,
of keywords, and structured programming
Operators multiplying, and dividing.
features, making it ideal for system programming.
Used to join conditions and check if they • The language became widely popular because it is
Logical Operators
are true or false together. portable (can run on different machines with little
Bitwise Used to work with individual bits (0s and modification), efficient, and flexible.
Operators 1s) of numbers. • In 1983, the American National Standards Institute
Relational Used to compare two values to see if one is (ANSI) formed a committee to standardize C. The
Operators bigger, smaller, or equal. resulting standard, known as ANSI C or C89, was
Assignment Used to give values to variables or change published in 1989.
• Later, the ISO (International Organization for
Operators their values.
Standardization) adopted this standard as ISO C.
Example:
• Since then, there have been several updates to the
Operators in C (Short Examples)
language standard: C99, C11, and C18.
• Arithmetic:
• C has heavily influenced many modern
+ (add) → a + b
programming languages such as C++, Java, C#,
- (subtract) → a - b
Objective-C, and many scripting languages.
* (multiply) → a * b
• Today, C remains one of the most widely used
/ (divide) → a / b
programming languages, especially in system
% (modulus) → a % b
software, embedded systems, game development,
• Relational:
and high-performance applications.
== (equal) → a == b
!= (not equal) → a != b
Define an algorithm and write an algorithm for addition
> (greater) → a > b
of two numbers.
< (less) → a < b
• Logical: Definition:
&& (and) → (a > 0 && b > 0)
An algorithm is a step-by-step procedure or set of
|| (or) → (a > 0 || b > 0)
instructions to solve a specific problem or perform a task in
! (not) → !(a == b)
a finite amount of time.
• Assignment:
= (assign) → a = 10 Algorithm to Add Two Numbers:
+= (add and assign) → a += 5
1. Start
• Bitwise:
& (and) → a & b 2. Declare two variables a and b to store numbers,
| (or) → a | b and sum to store result
^ (xor) → a ^ b
~ (not) → ~a 3. Read the value of a
<< (left shift) → a << 1 4. Read the value of b
>> (right shift) → a >> 1
5. Calculate sum = a + b

6. Display the value of sum

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.

2. Character Array Initialization Basic Data Types:


char str2[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Data Size Example
• You manually specify each character including the null Description
Type (typically) Value
terminator \0.
• The size of the array must be enough to hold all Integer values (whole 2 or 4 10, -5,
characters plus \0. int
numbers) bytes 1000

3. Declaring Pointer to a String Literal Single precision floating


float 4 bytes 3.14, -0.5
char *str3 = "Hello"; point
• str3 points to a string literal stored in read-only
memory. Double precision floating 3.14159, -
double 8 bytes
• You should not modify the contents pointed to by point 1.23
str3.
char Single character 1 byte 'A', 'z', '9'
Summary: Represents no value (used
Method Notes void for functions returning 0 bytes N/A
char str[] = Array initialized with string literal, nothing)
"Hello"; modifiable
Array initialized with characters, manual \0
char str[6] = {...};
required
char *str =
Pointer to string literal, usually read-only
"Hello";
Write a program in 'C' for addition of two matrices. What do you mean by operator precedence? Specify the
#include <stdio.h> precedence of arithmetic operators.
int main() { Operator Precedence
int rows, cols; • Operator precedence determines the order in
int matrix1[10][10], matrix2[10][10], sum[10][10]; which operators are evaluated in an expression.
printf("Enter the number of rows and columns: "); • Operators with higher precedence are evaluated
scanf("%d %d", &rows, &cols); before operators with lower precedence.
printf("Enter elements of first matrix:\n"); • If operators have the same precedence,
for (int i = 0; i < rows; i++) { associativity (left to right or right to left) decides
for (int j = 0; j < cols; j++) { the order.
scanf("%d", &matrix1[i][j]); Precedence of Arithmetic Operators in C
} Precedence
} Operators Description Associativity
Level
printf("Enter elements of second matrix:\n");
* (multiplication), Multiply,
for (int i = 0; i < rows; i++) {
1 (Highest) / (division), % divide, Left to right
for (int j = 0; j < cols; j++) {
(modulus) modulus
scanf("%d", &matrix2[i][j]);
+ (addition), - Add and
} 2 Left to right
(subtraction) subtract
}
// Adding two matrices Example:
for (int i = 0; i < rows; i++) { int result = 10 + 20 * 3; // result = 10 + (20 * 3) = 10 + 60 =
for (int j = 0; j < cols; j++) { 70
sum[i][j] = matrix1[i][j] + matrix2[i][j]; • Multiplication * has higher precedence, so it is
} evaluated before addition +.
} Summary:
printf("Sum of the matrices:\n"); • Multiplication, division, and modulus have higher
for (int i = 0; i < rows; i++) { precedence than addition and subtraction.
for (int j = 0; j < cols; j++) { • Operators with the same precedence are evaluated
printf("%d ", sum[i][j]); left to right.
}
printf("\n"); Define an operator and operand in C programming. Give a
} suitable example.
return 0; Operator in C
} • An operator is a symbol that tells the compiler to
perform a specific mathematical, logical, or
Arithmetic Operators in C relational operation.
Arithmetic operators are used to perform basic mathematical • Examples: +, -, *, /, %, ==, <, &&, etc.
operations on variables and values. Operand in C
Operator Meaning Example Result Explanation • An operand is the data or value on which the
operator acts.
+ Addition a+b Adds a and b
• It can be a variable, constant, or expression.
- Subtraction a-b Subtracts b from a Example:
* Multiplication a*b Multiplies a and b int a = 10, b = 5;
/ Division a/b Divides a by b int sum = a + b;
Remainder when a is • Here, + is the operator (addition).
Modulus • a and b are the operands (values to be added).
% a%b divided by b (only for
(remainder)
integers)
Define variable with syntax. Enlist data types.
Answer: A variable is a named memory location used to store
Examples: a value that can change during program execution. Syntax:
int a = 10, b = 3; datatype variable_name; • Example: • int age; • float salary; •
printf("Addition: %d\n", a + b); // Output: 13 char grade; Data Types in C:
printf("Subtraction: %d\n", a - b); // Output: 7 1. Basic Data Types: int, float, char, double.
printf("Multiplication: %d\n", a * b); // Output: 30 2. Derived Data Types: Arrays, pointers, functions.
printf("Division: %d\n", a / b); // Output: 3 3. Enumeration Data Types: enum.
printf("Modulus: %d\n", a % b); // Output: 1 4. Void Data Type: Used for functions with no return value.
Explain user defined function definition with syntax and give Explain function declaration, function definition and
a suitable example. function call. Give a suitable example.
User-Defined Function in C 1. Function Declaration (Prototype)
• A user-defined function is a block of code written by • It tells the compiler about the function’s name,
the programmer to perform a specific task. return type, and parameters before its actual
• It helps in reusing code and makes the program definition.
organized. • Helps the compiler check for correct usage.
• The function is defined once and can be called multiple • Syntax:
times. return_type function_name(parameter_list);
Syntax of Function Definition 2. Function Definition
return_type function_name(parameter_list) { • This is where the actual body of the function is
// body of the function written.
// statements • It contains the code that performs the task.
return value; // if return_type is not void • Syntax:
} return_type function_name(parameter_list) {
• return_type: Data type of value the function returns // function body
(e.g., int, float, or void if no value is returned). }
• function_name: Name of the function. 3. Function Call
• parameter_list: List of input parameters (can be • The process of invoking or executing a function in
empty). the program.
• Function body contains the statements to execute. • When called, the control jumps to the function,
Example: Function to add two numbers executes it, and returns back.
#include <stdio.h> • Syntax:
// Function definition function_name(arguments);
int add(int x, int y) { Example:
int sum = x + y; #include <stdio.h>
return sum;
} // Function declaration
int main() { int add(int, int);
int result = add(5, 7); // Function call
printf("Sum = %d\n", result); int main() {
return 0; int result;
} result = add(10, 20); // Function call
printf("Sum = %d\n", result);
What is a recursion? Explain it with a suitable example. return 0;
• Recursion is a programming technique where a }
function calls itself to solve a smaller instance of the
same problem. // Function definition
• It helps to solve problems that can be broken down int add(int a, int b) {
into similar sub-problems. return a + b;
• Every recursive function has: }
o Base case: Stops the recursion to avoid infinite
Keywords Identifiers
calls.
o Recursive case: The part where the function Reserved words with special Names given to variables, functions,
calls itself. meaning in C arrays, etc.
Example: Factorial of a number
The factorial of a number n (written as n!) is the product of all Examples: int, float, return, if Examples: x, age, sum, totalMarks
positive integers from 1 to n. Fixed and predefined by the
• 0! = 1 (base case) language
User-defined names
• n! = n × (n-1)! (recursive case)
Cannot be modified by the
Can be modified by the programmer
programmer

Used to define structure and Used to identify elements in the


syntax of C program

Must not be used as variable Must follow rules (start with


names letter/underscore)
Differentiate Between Increment and Decrement Operators. Explain If and If-Else Statement in C with Example
In C, the if and if-else statements are used for conditional
Increment Operator (++) Decrement Operator (--)
execution of code. These statements allow the program to
Increases the value of a Decreases the value of a make decisions based on whether a condition is true or false.
variable by 1 variable by 1 1. If Statement:
• Purpose: Executes a block of code only if the given
Syntax: ++variable or Syntax: --variable or condition is true; if the condition is false, the code
variable++ variable-- block is simply skipped.
• Syntax:
++a: Value of a is increased --a: Value of a is decreased if (condition) {
before use before use // Code to execute if condition is true
}
a++: Value of a is increased a--: Value of a is decreased • Example:
after use after use int a = 5;
Example: int a = 5; int b = ++a; Example: int a = 5; int b = -- if (a > 0) {
→ b = 6, a = 6 a; → b = 4, a = 4 printf("a is positive");
}
Example: int a = 5; int b = a++; Example: int a = 5; int b = a-- // Output: a is positive
→ b = 5, a = 6 ; → b = 5, a = 4 Explanation:
In this example, since the condition a > 0 is true, the code
Used when updated value is Used when old value is inside the if block executes and prints "a is positive".
needed immediately needed before update 2. If-Else Statement:
• Purpose: Chooses between two blocks of code,
executing the first if the condition is true, and the
What is an Array? Explain declaration of one-dimensional
second (under else) if the condition is false.
array with an example
• Syntax:
What is an Array in C?
if (condition) {
• An array is a collection of elements of the same data
// Code to execute if condition is true
type stored at contiguous memory locations.
} else {
• It allows storing multiple values under one variable
// Code to execute if condition is false
name.
}
• Each element is accessed using an index (starting
• Example:
from 0).
int a = -5;
One-Dimensional Array if (a > 0) {
• A one-dimensional array stores elements in a single printf("a is positive");
row (linear form). } else {
• It is like a list of elements. printf("a is negative");
Syntax: }
data_type array_name[array_size]; // Output: a is negative
• data_type: Type of data (int, float, char, etc.) Explanation:
• array_name: Variable name of the array In this case, since a > 0 is false (because a is -5), the code in
• array_size: Total number of elements the else block executes and prints "a is negative".
Example Program:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("Element at index %d is: %d\n", i, arr[i]);
}
return 0;
}
Explanation:
• int arr[5] creates an array that can store 5 integers.
• {10, 20, 30, 40, 50} are the initial values of the array.
• The for loop prints each element using its index from
0 to 4.
Write a C program to read n numbers into an array to while Loop do-while Loop
find the largest number
#include <stdio.h> Checks the condition first,
Runs the code once first,
int main() { then runs the code. If
then checks the condition.
condition is false, code does
int n, i, largest; So, code runs at least once.
not run at all.
printf("Enter number of elements: ");
scanf("%d", &n); Can run 0 or more times Runs 1 or more times, no
int arr[n]; // Declare array depending on the condition. matter what.
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++) { Syntax: Syntax:
scanf("%d", &arr[i]); ```c ```c
}
largest = arr[0]; while(condition) { do {
for (i = 1; i < n; i++) {
// code // code
if (arr[i] > largest) {
largest = arr[i]; } } while(condition);
}
} ``` ```
printf("The largest number is: %d\n", largest); Use it when you want the Use it when you want the
return 0; code to run only if the code to run at least once,
} condition is true at the start. even if condition is false.
Output
Enter number of elements: 5 If the condition is false at the The code runs once first,
Enter 5 numbers: start, the code does not run. then checks the condition.
12 43 5 67 23 Example: Show menu
The largest number is: 67 Example: Repeat asking input
options at least once before
only if the input is correct.
asking to repeat.

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]

Break Statement Continue Statement What is an Argument?


• An argument is a value or variable passed to a
Used to skip the current
Used to exit or stop a function when calling it.
iteration of a loop and move
loop immediately. • Arguments provide input data for the function
to the next one.
to work with.
Terminates the loop Skips remaining statements in Formal Arguments Actual Arguments
entirely and control goes the loop body for current Variables declared in the Values or variables
to the statement after the iteration and continues with function definition to passed to the function
loop. the next iteration. receive values. when it is called.
They act as placeholders They are the real data
Works with loops (for,
Works only with loops (for, inside the function. given to the function.
while, do-while) and
while, do-while). Exist only during function Exist in the calling code
switch statements.
execution. where function is called.
Example: Exit loop when a Example: Skip printing even Example: In void fun(int x), Example: In fun(5), 5 is
condition is met. numbers in a loop. x is the formal argument. the actual argument.

You might also like