Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Programming Fundamentals: A Comprehensive Guide with Examples, Schemes and Mind Maps of Computer Applications

A comprehensive introduction to c programming, covering fundamental concepts such as variables, data types, operators, control flow, functions, arrays, strings, pointers, and file handling. It includes numerous examples and explanations to illustrate key concepts and enhance understanding. Suitable for beginners and those seeking to solidify their understanding of c programming.

Typology: Schemes and Mind Maps

2024/2025

Available from 04/14/2025

vijaya-kumari-2
vijaya-kumari-2 🇮🇳

2 documents

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
11.a)
Definition of Symbolic Constant
A symbolic constant is a named constant that cannot change throughout the program and is
defined using the #define directive.
Syntax
#define CONSTANT_NAME value
Here:
CONSTANT_NAME is the name of the constant.
value is the constant value assigned to CONSTANT_NAME.
Example Program
#include <stdio.h>
#define PI 3.14159 // Symbolic constant for the value of Pi
#define RADIUS 5 // Symbolic constant for the radius
int main() {
// Calculate the area of a circle
double area = PI * RADIUS * RADIUS;
printf("The area of a circle with radius %d is: %.2f\n", RADIUS, area);
return 0;
}
Output
The area of a circle with radius 5 is: 78.54
b) Tokens in C
In C programming, tokens are the smallest individual units in the source code. A C program is
composed of various types of tokens, which can be categorized as follows:
Types of Tokens in C
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download C Programming Fundamentals: A Comprehensive Guide with Examples and more Schemes and Mind Maps Computer Applications in PDF only on Docsity!

11.a)

Definition of Symbolic Constant

A symbolic constant is a named constant that cannot change throughout the program and is defined using the #define directive.

Syntax

#define CONSTANT_NAME value Here:CONSTANT_NAME is the name of the constant.value is the constant value assigned to CONSTANT_NAME.

Example Program

#include <stdio.h> #define PI 3.14159 // Symbolic constant for the value of Pi #define RADIUS 5 // Symbolic constant for the radius int main() { // Calculate the area of a circle double area = PI * RADIUS * RADIUS; printf("The area of a circle with radius %d is: %.2f\n", RADIUS, area); return 0; }

Output

The area of a circle with radius 5 is: 78.

b) Tokens in C

In C programming, tokens are the smallest individual units in the source code. A C program is composed of various types of tokens, which can be categorized as follows:

Types of Tokens in C

1. Keywords:Reserved words in C that have special meaning.Examples: int, return, if, while, for, void, break, continue, switch, case, default, **do, else, etc.

  1. Identifiers:** ○ Names given to entities such as variables, functions, arrays, etc.Identifiers must begin with a letter (uppercase or lowercase) or an underscore (_), followed by letters, digits, or underscores. ○ **Example: myVariable, count, sum_of_numbers, MAX_VALUE.
  2. Constants:** ○ Fixed values that do not change during program execution.Types include:Integer Constants: Whole numbers (e.g., 100, - 42).Floating-Point Constants: Numbers with decimal points (e.g., 3.14, - 0.001).Character Constants: Single characters enclosed in single quotes (e.g., 'a', '\n').String Constants: Sequences of characters enclosed in double quotes **(e.g., "Hello, World!").
  3. Operators:** ○ Symbols that perform operations on variables and values.Examples: ■ *Arithmetic Operators: +, - , , /, %.Relational Operators: ==, !=, >, <, >=, <=.Logical Operators: &&, ||, !. ■ **Bitwise Operators: &, |, ^, ~, <<, >>.
  4. Punctuators (Delimiters):** ○ Symbols that help in structuring the code.Examples:Commas: ,

10 is positive. Enter a number: - 5

- 5 is negative. Enter a number: 0 0 is zero. C Switch Statement

The switch statement in C is an alternate to if-else-if ladder statement

which allows us to execute multiple operations for the different

possibles values of a single variable called switch variable.

Syntax:

switch ( expression )

case x:

// code block

break;

case y:

// code block

break;

default:

// code block

Example: #include <stdio.h> int main() { int day = 4; // Initialize the day variable 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! Please enter a number between 1 and 7.\n"); } return 0; // Exit program } Output Thursday 12)b

Loops

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code

more readable.

While Loop

The while loop loops through a block of code as long as a specified condition is

true:

Syntax

while ( condition ) {

// code block to be executed

In the example below, the code in the loop will run, over and over again, as long

as a variable (i) is less than 5:

Example

#include <stdio.h>

int main() {

int i = 0; // Initialize i to 0

// While loop to print numbers from 0 to 4

printf("%d\n", i);

i++;

} while (i < 5);

return 0;

OUTPUT

13)a)

ARRAY IN C

Arrays in C are a fundamental data structure that allows you to store

a fixed-size sequence of elements of the same type.

Array Declaration and Initialization:

int numbers[5] = {10, 20, 30, 40, 50};

Here, we declare an array named numbers that can hold 5 integers.

The array is initialized with the values 10, 20, 30, 40, and 50.

Types of Array in C

There are two types of arrays based on the number of

dimensions it has. They are as follows:

1. One Dimensional Arrays (1D Array)

2. Multidimensional Arrays

1. One Dimensional Array in C

The One-dimensional arrays, also known as 1 - D arrays in C

are those arrays that have only one dimension.

Syntax of 1D Array in C

array_name [size];

2. Multidimensional Array in C

printf("Elements of the array:\n"); for (int i = 0; i < 5; i++) { printf("numbers[%d] = %d\n", i, numbers[i]); } return 0; // Exit the program } OUTPUT Elements of the array: numbers[0] = 10 numbers[1] = 20 numbers[2] = 30 numbers[3] = 40 numbers[4] = 50 b) STRING FUNCTIONS IN C

1. strlen(string_name)

Description: Returns the length of the string string_name (excluding the null character). Example: #include <stdio.h> #include <string.h> int main() { char str[] = "Hello, World!"; printf("Length of string: %zu\n", strlen(str)); // Output: 13

return 0; } Length of string: 13//output

2. strcpy(destination, source)

Description: Copies the contents of the source string to the destination string. Example: #include <stdio.h> #include <string.h> int main() { char src[] = "Hello"; char dest[20]; // Ensure enough space in destination strcpy(dest, src); printf("Copied string: %s\n", dest); // Output: Hello return 0; } Copied string: Hello // output

3. strcat(first_string, second_string)

Description: Concatenates (joins) second_string to first_string. The result is stored in first_string. Example: #include <stdio.h> #include <string.h> int main() { char str1[20] = "Hello"; char str2[] = " World!"; strcat(str1, str2);

int n = strlen(str); for (int i = 0; i < n / 2; i++) { char temp = str[i]; str[i] = str[n - i - 1]; str[n - i - 1] = temp; } } int main() { char str[] = "Hello"; strrev(str); printf("Reversed string: %s\n", str); // Output: olleH return 0; } Reversed string: olleH // output

6. strlwr(string)

Description: Converts all uppercase characters in string to lowercase. Example: #include <stdio.h> #include <ctype.h> void strlwr(char *str) { for (int i = 0; str[i]; i++) { str[i] = tolower(str[i]); }

int main() { char str[] = "HELLO"; strlwr(str); printf("Lowercase string: %s\n", str); // Output: hello return 0; } Lowercase string: hello // output

7. strupr(string)

Description: Converts all lowercase characters in string to uppercase. Example: #include <stdio.h> #include <ctype.h> void strupr(char *str) { for (int i = 0; str[i]; i++) { str[i] = toupper(str[i]); } } int main() { char str[] = "hello"; strupr(str); printf("Uppercase string: %s\n", str); // Output: HELLO

if (n==0) { return 0; } else if ( n == 1) { return 1; } else { return n*fact(n-1); } } OUTPUT Enter the number whose factorial you want to calculate? factorial = 120 b)structure differ from union

Difference Between Structure and Union in C

Parameter Structure Union Keyword A user can deploy the keyword struct to define a Structure. A user can deploy the keyword union to define a Union.

Internal Implementation The implementation of Structure in C occurs internally- because it contains separate memory locations allotted to every input member. In the case of a Union, the memory allocation occurs for only one member with the largest size among all the input variables. It shares the same location among all these members/objects. Accessing Members A user can access individual members at a given time. A user can access only one member at a given time. Syntax The Syntax of declaring a Structure in C is:

struct [structure name]

type element_1;

type element_2;

} variable_1, variable_2, …;

The Syntax of declaring a Union in C is:

union [union name]

type element_1;

type element_2;

} variable_1, variable_2, …;

Size A Structure does not have a shared location for all of its members. It makes the size of a Structure to be greater than or equal to the sum of the size of its data members. A Union does not have a separate location for every member in it. It makes its size equal to the size of the largest member among all the data members. Value Altering Altering the values of a single member does not affect the other members of a Structure. When you alter the values of a single member, it affects the values of other members.

9 fgetw() reads an integer from file 10 ftell() returns current position 11 rewind() sets the file pointer to the beginning of the file 15)b In C, variable declaration and pointer declaration are fundamental concepts. Here’s an easy explanation of the differences between them, including examples.

Variable Declaration

A variable is a storage location identified by a name that holds a value. When you declare a variable, you specify its type, which determines the kind of data it can hold. Example:

int num; // Declaration of an integer variable num = 5; // Assigning a value to the variable 

Pointer Declaration

A pointer is a variable that stores the address of another variable. When you declare a pointer, you use an asterisk (*) to indicate that it is a pointer type, along with the type of the variable it points to. Example:

int *ptr; // Declaration of a pointer to an integer int num = 5; // Declare an integer variable ptr = &num; // Assign the address of num to ptr 

Key Differences

Example #include <stdio.h> int main() { int num = 10; int *ptr; ptr = # printf("Value of num: %d\n", num); printf("Value via pointer: %d\n", *ptr);