c Programing[1]
c Programing[1]
INDEX
Sr. PARTICULAR PAGE
NO.
1. A VARIABLE IN C PROsGRAMMING 3
4. C PROGRAMMING “LOOPS” 7
7. CONCEPT OF ARRAYS IN C 11
8. “SIZEOF” OPERATOR IN C 12
9. ROLE OF POINTERS IN C 14
PROGRAMMING
10. WHAT ARE FUNCTIONS IN C 16
Page | 2
1. What is a variable in C programming, and how do
you declare one?
In C programming, a variable is a named memory location used to store data
of different types. Variables serve as the basic building blocks of C
programs, allowing us to manipulate and retrieve values. Here’s how you
declare a variable in C:
1. Variable Declaration:
2. Variable Definition:
Page | 3
int count = 10; // Integer variable with initial value
char grade = 'A'; // Character variable with initial value
float temperature = 25.5; // Float variable with initial value
3. Variable Initialization:
• Example:
int score; // Variable definition
score = 100; // Initialization
2. Explain the difference between syntax and
semantics in programming languages?
Certainly! In programming languages, syntax and semantics play distinct
roles:
1. Syntax:
• Definition: Syntax refers to the rules and regulations for
writing statements in a programming language (e.g., C/C++).
• Focus: It deals with the structure and grammar of the
language.
• Validity: A statement is syntactically valid if it adheres to all
the rules.
• Errors: Syntax errors occur during compile time when a
statement violates language grammar (e.g., missing semicolons,
undeclared variables).
• Example: Consider a C++ program with a return 0; statement
before cout << "GFG!";. Although syntactically correct, it won’t
print anything due to the misplaced return statement—a
semantic error.
2. Semantics:
Page | 4
• Definition: Semantics refers to the meaning associated with
statements in a programming language.
• Focus: It interprets the program’s behavior based on the
statement’s meaning.
• Errors: Semantic errors occur at runtime when a statement,
though syntactically valid, doesn’t behave as intended (e.g.,
incorrect logic).
• Example: A program that terminates before printing anything
due to a misplaced return statement illustrates a semantic error.
3. How do you write a simple "Hello, World!"
program in C?
#include<stdio.h>
void main()
{
printf("\n Hello");
}
1. For Loop:
• Syntax: for (initialization; testExpression; updateStatement) {
/* code */ }
• How it works:
▪ The initialization statement runs once.
Page | 5
▪ The test expression is evaluated. If false, the loop
terminates; otherwise, the body executes.
▪ The update statement runs, and the test expression is re-
evaluated.
▪ This process continues until the test expression becomes
false.
• Example:
// Print numbers from 1 to 10
#include <stdio.h>
int main()
{
for (int i = 1; i <= 10; ++i)
{
printf("%d ", i);
}
return 0;
}
// Output: 1 2 3 4 5 6 7 8 9 10
2. While Loop:
• Syntax: while (testExpression) { /* code */ }
• How it works:
▪ The test expression is evaluated initially.
▪ If true, the body executes, and the test expression is re-
evaluated.
▪ This continues until the test expression becomes false.
• Example:
// Calculate the sum of first n natural numbers
#include <stdio.h>
int main()
Page | 6
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
while (count <= num)
{
sum += count;
++count;
}
printf("Sum = %d", sum);
return 0;
}
// Output (for input 10): Sum = 55
3. Do…While Loop:
• Syntax: do { /* code */ } while (testExpression);
• How it works:
▪ The body executes at least once.
▪ Then, the test expression is evaluated.
▪ If true, the body continues to execute; otherwise, the loop
terminates.
• Example:
// Print numbers from 1 to 5 using do...while loop
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d ", i);
++i;
Page | 7
}
while (i <= 5);
return 0;
}
// Output: 1 2 3 4 5
5. Describe the difference between pass by value and
pass by reference in function arguments?
The distinction between pass by value and pass by reference lies in how
function arguments are handled:
1. Pass by Value:
• When you pass arguments by value, the function receives copies
of the original values.
• Modifications made to these copies within the function do not
affect the original values outside the function.
• Common in languages like C++, Java, and Python (for
immutable types).
• Example: If you pass an integer x to a function, the function
operates on a copy of x, leaving the original x unchanged.
2. Pass by Reference:
• When you pass arguments by reference, the function receives
references (or memory addresses) to the original values.
• Changes made to these references within the function directly
affect the original values.
• Common in languages like JavaScript (for objects) and Python
(for mutable types like lists and dictionaries).
• Example: Passing an array to a function allows modifying the
original array directly.
1. If Statement:
• The most basic form of a conditional statement.
• Checks if a condition is true.
• If true, executes a block of code.
• Example:
int x = 10;
if (x > 0) {
printf("x is positive");
}
// Output: x is positive
2. If-Else Statement:
• Extends the if statement by adding an else clause.
• If the condition is false, executes the code in the else block.
3. If-Else If Statement:
• Allows handling multiple conditions sequentially.
• If the first condition is false, checks additional conditions using
else if.
4. Switch Statement:
• Useful for handling multiple cases.
• Compares an expression against various constant values.
• Executes code corresponding to the matched case.
5. Ternary Expression:
• A concise way to assign a value based on a condition.
• Syntax: condition ? value_if_true : value_if_false.
Page | 9
7. Explain the concept of arrays in C programming,
and how to declare and access elements in an array?
Let’s dive into the world of arrays in C programming.
What is an Array in C?
An array in C is a fixed-size collection of similar data items stored in
contiguous memory locations. It allows you to store multiple values under a
single name. Arrays can hold primitive data types (such as int, char, float,
etc.) as well as derived and user-defined data types (like pointers and
structures).
Declaring an Array:
To declare an array in C, follow this syntax:
data_type array_name[size];
• data_type: The type of elements in the array (e.g., int, char, etc.).
• array_name: The name you give to the array.
• size: The number of elements the array can hold.
For example:
int arr_int[5]; // Declares an integer array with 5 elements
char arr_char[5]; // Declares a character array with 5 elements
Initializing an Array:
1. Static Initialization (during declaration):
• Initialize the array along with its declaration using an initializer
list:
• int arr[] = {10, 20, 30, 40, 50}; // Compiler deduces size as 5
2. Dynamic Initialization (after declaration):
Page | 10
• Assign values to each element individually using loops (e.g., for,
while, or do-while).
▪ Pointer types
2. Usage Examples:
• When the operand is a data type, sizeof returns the memory
allocated for that type. For example:
printf("%lu\n", sizeof(char)); // Output: 1
printf("%lu\n", sizeof(int)); // Output: 4
printf("%lu\n", sizeof(float)); // Output: 4
printf("%lu\n", sizeof(double));// Output: 8
Page | 11
• When the operand is an expression, sizeof calculates the size of
the expression. For instance:
int a = 0;
double d = 10.21;
printf("%lu\n", sizeof(a + d)); // Output: 8
3. Compile-Time Operator:
• sizeof operates at compile time, meaning it doesn’t execute code
inside parentheses.
int y;
int x = 11;
y = sizeof(x++);
printf("%i %i", y, x); // Output: 4 11
4. Use Cases:
• Array Elements Count:
Page | 12
Pointers are fundamental in C programming, allowing you to work with
memory addresses directly. Here’s a concise explanation and an example:
1. Role of Pointers:
• A pointer is a variable that stores the memory address of another
variable or function.
• Pointers enable low-level memory access, dynamic memory
allocation, and interaction with the operating system.
• They’re essential for creating complex data structures and
simulating call-by-reference.
2. Example:
#include <stdio.h>
int main()
{
int var = 5;
int* ptr = &var; // Declare a pointer and assign the address of 'var'
return 0;
}
• In this example:
Page | 13
• ptr stores the address of var.
• *ptr gives us the value stored at that address.
• Modifying *ptr also changes the value of var.
Example:
#include <stdio.h>
// Function declaration (prototype)
int sum(int a, int b);
int main()
{
// Function call
Page | 14
int result = sum(10, 30);
printf("Sum is: %d", result);
return 0;
}
// Function definition
int sum(int a, int b)
{
return a + b;
}
• In this example:
• We declare the sum function with its return type (int) and parameters
(int a and int b).
• Inside main(), we call sum(10, 30) to compute the sum of 10 and 30.
• The function definition for sum calculates the sum and returns the
result.
Page | 15