




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 44
This page cannot be seen from the preview
Don't miss anything!
A symbolic constant is a named constant that cannot change throughout the program and is defined using the #define directive.
#define CONSTANT_NAME value Here: ● CONSTANT_NAME is the name of the constant. ● value is the constant value assigned to CONSTANT_NAME.
#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; }
The area of a circle with radius 5 is: 78.
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:
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.
10 is positive. Enter a number: - 5
- 5 is negative. Enter a number: 0 0 is zero. C Switch Statement
Syntax:
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
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
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
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
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
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
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
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:
The Syntax of declaring a Union in C is:
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.
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
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 = # // Assign the address of num to ptr
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);