- gcc <fileName>.c
- - ./a.exe
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}
Variables:
Variable is the name of a memory location which stores some data.Data type Size in bytes
Char or signed char 1
Unsigned char 1
int or signed int 2
Unsigned int 2
Short int or Unsigned short int 2
Signed short int 2
Long int or Signed long int 4
Unsigned long int 4
float 4
double 8
Long double 10
Keywords:
- 32 Reserved Keywords words in C that have special meaning to the compiler.
#include <stdio.h>
int main() {
// Basic Calculator
int a,b;
printf("Enter first number ");
scanf("%d", &a);
printf("Enter second number ");
scanf("%d", &b);
printf("Addtition of %d and %d : %d\n", a,b,a+b);
printf("Subraction of %d and %d : %d\n", a,b,a-b);
printf("Multiplication of %d and %d : %d\n", a,b,a*b);
printf("Division of %d and %d : %d\n", a,b,a/b);
return 0;
}
output Case:
1. Integers: printf("age is %d " age);
2. Real numbers: printf("Value of pi is %f ", pi);
3. characters: pirintf("star looks like this %cc ", star);
Format specifier:
- %d, %f, %c
Compilation: A computer program that translates C code into machine code.
Type declaration instructions:
#include <stdio.h>
int main() {
// Valid
int a = 1;
int b = a;
int c = b * 4;
int d = 1, e;
int x,y,z;
x = y = z = 1;
// Invalid
// int oldAge = 22;
// int newAge = oldAge + years;
// int years = 2
// int _x = _y = _z = 1;
return 0;
}
Arithmetic Instructions:
#include <stdio.h>
int main() {
// Valid
int a = 1;
int b = a;
int c = b * a;
// Invalid
// int b + c = a;
// int a = bc;
return 0;
}
- Modulo returns positive and negative values.
Type conversions:
- int op int -> int
- int op float -> float
- float op float -> float
Conversion:
1. Implicit: compiler can do conversion.
2. Explicit: developers can do conversion.
Q: Guess the output:
#include <stdio.h>
int main() {
// Valid
int a = 1.9999;
printf("%d\n", a);
int b = (int) 1.9999;
printf("%d", b);
return 0;
}
Associativity rule: Follow left to right.
a. 5*2-2*3
b. 5*2/2*3
5*(2/2) * 3
d. 5+2 / 2*3
#include <stdio.h>
int main() {
// Valid
int a = 4 * 3 / 6 * 2;
printf("%d", a);
return 0;
}
Control Instructions: Used to determine flow of program.
- Sequence control
- Decision control
- Loop
- Case
Operators:
1. Arithmetic
2. Relational
3. Logical
4. Bitwise
5. Assignment
6. Ternary
Relational Operators:
#include <stdio.h>
int main() {
printf("%d \n", 4==4);
printf("%d \n", 4<3);
printf("%d \n", 3<4);
printf("%d \n", 4<4);
printf("%d \n", 4<=4);
printf("%d \n", 4>3);
printf("%d \n", 3>4);
printf("%d \n", 4>4);
printf("%d \n", 4>=4);
printf("%d \n", 4!=4);
printf("%d \n", 3!=4);
return 0;
}
Note:
- True and False does exists in C Language only exists 0 and 1.
- In C, array string is terminated by NULL character.
- In C non-zero values are True.
#include <stdio.h>
int main() {
if(1){
printf("I am 1\n");
}
if(243) {
printf("I am 243\n");
}
if('c') {
printf("I am c\n");
}
if(0) {
printf("I am zero(0)\n");
}
return 0;
}
Operator Precedence:
Priority Operator
1 !
2 *, /, %
3 +,
4 <, <=, >, >=
5 ==, !=
6 &&
7 ||
8 =
Logical Operator:
#include <stdio.h>
int main() {
printf("%d \n", 3<4 && 3<5);
printf("%d \n", 3<4 && 5<4);
printf("%d \n", 3<4 && 5<4);
printf("%d \n", 3>4 && 5>4);
printf("%d \n", 3<4 && 3<5);
printf("%d \n", !(3<4 && 3<5));
printf("%d \n", !(4<3 || 5<3));
return 0;
}
Assignment Operators:
#include <stdio.h>
int main() {
int a = 10;
a += 10;
printf("a+10 = %d \n", a);
a-= 10;
printf("a-10 = %d \n", a);
a *= 10;
printf("a*10 = %d \n", a);
a /= 10;
printf("a/10 = %d \n", a);
a %= 10;
printf("a%c10 = %d \n", '%', a);
return 0;
}
For loop:
- pre increment and post increment.
New Syntax
#include <stdio.h>
#include <math.h>
int main() {
int sum = 0;
for(int i=1, j=5; i<=5 && j>=1; i++, j--){
sum += i;
printf("%d\n", j);
}
printf("Sum is %d \n", sum);
return 0;
}
Functions in C:
- Execution always starts from main.
- A function gets called directly or indirectly from main.
- There can be multiple functions in a program.
Function Types:
- Library function - Special functions inbuilt in C (like scanf(), printf()).
- User defined - Declared and defined by programmer.
#include <stdio.h>
void sayHello();
int main() {
sayHello();
sayHello();
sayHello();
return 0;
}
void sayHello(){
printf("Hello!\n");
}
Example:
#include <stdio.h>
void sayHello();
void sayGoodBye();
int main() {
sayHello();
sayGoodBye();
sayHello();
return 0;
}
void sayHello(){
printf("Hello!\n");
}
void sayGoodBye(){
printf("Good Bye!\n");
}
Recursion:
Recursion in maths:
Q: f(x) = x^2 find f{f[f(x)]} for x = 2.
Solve: f(x) = x^2, x = 2
f(x) = 2^2 = 4
f(x) = 4 ^2 = 16
f(x) = 16 ^2 = 256
#include <stdio.h>
#include <math.h>>
// Using Reccursion
void fxValue(int count, int value);
int main(){
fxValue(3, 2);
return 0;
}
void fxValue(int count, int value){
if(count == 0){
return;
}
double res = pow(value, 2);
printf("%f \n", res);
fxValue(count - 1, res);
}
//OR
#include <stdio.h>
int f(int x) {
return x * x;
}
int recursive_f(int x, int depth) {
if (depth == 0)
return x;
return f(recursive_f(x, depth - 1));
}
int main() {
int x = 2;
int depth = 3; // Since we want f(f(f(x))), depth is 3
int result = recursive_f(x, depth);
printf("f(f(f(%d))) = %d\n", x, result);
return 0;
}
//OR
#include <stdio.h>
int f(int x) {
return x * x;
}
int main() {
int x = 2;
int result = f(f(f(x)));
printf("f{f[f(x)]} for x = 2 is: %d\n", result);
return 0;
}
Pointer:
#include <stdio.h>
// Pointer
int main(){
int age = 22;
int *ptr = &age;
int _age = *ptr;
// printf("age is %d\n", _age); //22
// printf("age is %p\n", &age); //hexadecimal value
// printf("age is %u\n", &age); //Type cast hexadecimal to integer value
// printf("age is %u\n", ptr); //Value of &age and ptr will same
// printf("age is %u\n", &ptr);
// printf("age is %d\n", *ptr); // 22
// printf("age is %d\n", *(&age)); // 22
return 0;
}
Pointer Arithmetic:
- Pointer can be incremented and decremented.
Case 1:
#include <stdio.h>
int main(){
int age = 20;
int *ptr = &age;
printf("ptr: %d\u", ptr);
ptr++;
printf("ptr: %d\u", ptr);
ptr = ptr - 2;
printf("ptr: %d\u", ptr);
return 0;
}
Case 2:
#include <stdio.h>
int main(){
float price = 20.00;
float *ptr = &price;
printf("ptr: %u\n", ptr);
ptr++;
printf("ptr: %u\n", ptr);
ptr = ptr - 2;
printf("ptr: %u\n", ptr);
return 0;
}
Case 3:
#include <stdio.h>
int main(){
char star = '*';
char *ptr = ☆
printf("ptr: %u\n", ptr);
ptr++;
printf("ptr: %u\n", ptr);
ptr = ptr - 2;
printf("ptr: %u\n", ptr);
return 0;
}
Arrays:
#include <stdio.h>
int main(){
int marks[] = {23, 28, 45, 65};
// int* ptr = &marks[0];
int* ptr = marks; // Same as int* ptr = &marks[0];
for(int i=0; i<4; i++){
// printf("The marks at index %d %d\n", i, marks[i]);
printf("The marks at index %d is %d\n", i, *ptr);
ptr++;
}
return 0;
}
Structure:
#include <stdio.h>
#include <string.h>>
struct student {
char name1[100]; // using array
char *name2; // using pointer
int roll;
float cgpa;
};
int main() {
struct student s1;
// s1.name1 = "Hello" // Wrong
strcpy(s1.name1, "Hello");
s1.name2 = "World";
s1.roll = 123;
s1.cgpa = 9.5;
printf("%s\n", s1.name1);
printf("%s\n", s1.name2);
printf("%d\n", s1.roll);
printf("%f\n", s1.cgpa);
return 0;
}
File Input Output:
#include <stdio.h>
int main() {
FILE *fptr;
char ch;
fptr = fopen("test.txt", "r"); //Reading a file
if(fptr == NULL){
printf("File does not exists");
}
else{
fscanf(fptr, "%c", &ch); // scan only one character
printf("%c", ch);
fscanf(fptr, "%c", &ch); // scan second character
printf("%c", ch);
fscanf(fptr, "%c", &ch);
printf("%c", ch);
fscanf(fptr, "%c", &ch);
printf("%c", ch);
fscanf(fptr, "%c", &ch);
printf("%c", ch);
fscanf(fptr, "%c", &ch);
printf("%c", ch);
fscanf(fptr, "%c", &ch);
printf("%c", ch);
fclose(fptr);
}
// Writting a file
ch = 'M';
fptr = fopen("test.txt", "w");
fprintf(fptr, "%cang", ch);
fprintf(fptr, "%c", 'o');
fclose(fptr);
// Append a text in a file
ch = 'A';
fptr = fopen("test.txt", "a");
fprintf(fptr, "%cpple", ch);
fclose(fptr);
// Read and Write a char
// 1. fgetc
printf("\n");
fptr = fopen("test.txt", "r");
printf("%c", fgetc(fptr));
printf("%c", fgetc(fptr));
fclose(fptr);
// 2. fputc
fptr = fopen("test.txt", "w");
fputc('Z', fptr);
fputc('O', fptr);
fputc('O', fptr);
fclose(fptr);
// Read the full file (EOF)
// fptr = fopen("test2.txt", "a");
// fputc('.', fptr);
// fclose(fptr);
fptr = fopen("test2.txt", "r");
ch = fgetc(fptr);
printf("\n");
while(ch != EOF){
printf("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
return 0;
}
Dynamic Memory Allocation(DMA):
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Allocate memory for n integers
int *ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &ptr[i]); // Correctly use ptr instead of arr
}
printf("You entered:\n");
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
// Free the allocated memory
free(ptr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
int arr[n]; // Not allowed is c
printf("%d %d \n", n, sizeof(arr));
for(int i=0; i<n; i++) {
scanf("%d", arr[i]);
}
for(int i=0; i<n; i++) {
printf("%d", arr[i]);
}
return 0;
}
Q: write a program to allocate memory to store 5 process?
#include <stdio.h>
#include <stdlib.h>>
int main() {
float *ptr;
ptr = (float *) malloc(5 * sizeof(float));
ptr[0] = 1;
ptr[1] = 2;
ptr[2] = 3;
ptr[3] = 4;
ptr[4] = 5;
for(int i=0; i<5; i++){
printf("%f\n", ptr[i]);
}
return 0;
}
calloc:
#include <stdio.h>
#include <stdlib.h>>
int main() {
int *ptr;
ptr = (int *) calloc(5, sizeof(int));
for(int i=0; i<5; i++){
printf("%d\n", ptr[i]);
}
return 0;
}
Free:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr, n;
printf("Enter number: ");
scanf("%d", &n);
ptr = (int *) malloc(n * sizeof(int));
for(int i=0; i<n; i++){
printf("%d\n", ptr[i]);
}
free(ptr);
ptr = (int *) calloc(2, sizeof(int));
for(int i=0; i<2; i++){
printf("%d\n", ptr[i]);
}
return 0;
}
-----------
#include <stdio.h>
#include<math.h>
int main(){
// int a;
// printf("Enter number: ");
// scanf("%d", &a);
// scanf("%d", &b);
// scanf("%f", &c);
// for(int i=0; i<a; )
// int temp = a/100;
// int sum += pow(temp,3);
//Prime number in range of numbers
// char isPrime = 1;
// for(int i=1; i<a; i++){
// for(int j=2; j<i; j++){
// if(i%j == 0){
// continue;
// }
// }
// printf("%d\n", a);
// }
// if(isPrime){
// printf("Prime number");
// }
// else{
// printf("composite number");
// }
//Armstrong number
// int n = 120;
// int temp = n;
// int sum = 0;
// while(n != 0){
// sum += pow(n % 10, 3);
// n /= 10;
// }
// if(temp == sum){
// printf("Yes it is Armstrong number");
// }
// else{
// printf("No, it is not an Armstrong number");
// }
return 0;
}
Fibonacci series.
#include <stdio.h>
#include <stdlib.h>
// Function to generate Fibonacci series iteratively
void fibonacci_iterative(int n) {
long long a = 0, b = 1, c;
printf("Fibonacci series (Iterative): ");
for (int i = 1; i <= n; i++) {
printf("%lld ", a);
c = a + b;
a = b;
b = c;
}
printf("\n");
}
// Function to generate Fibonacci series recursively
long long fibonacci_recursive(int n) {
if (n <= 1)
return n;
return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2);
}
// Function to print Fibonacci series using recursion
void print_fibonacci_recursive(int n) {
printf("Fibonacci series (Recursive): ");
for (int i = 0; i < n; i++) {
printf("%lld ", fibonacci_recursive(i));
}
printf("\n");
}
// Function to validate user input
int validate_input() {
int n;
while (1) {
printf("Enter the number of terms (positive integer): ");
if (scanf("%d", &n) == 1 && n > 0) {
return n;
} else {
printf("Invalid input! Please enter a positive integer.\n");
while (getchar() != '\n'); // Clear input buffer
}
}
}
int main() {
printf("Fibonacci Series Generator\n");
printf("--------------------------\n");
// Get number of terms from the user
int n = validate_input();
// Display Fibonacci series using iterative method
fibonacci_iterative(n);
// Display Fibonacci series using recursive method
print_fibonacci_recursive(n);
// Optional: Save to a file
FILE *file = fopen("fibonacci_series.txt", "w");
if (file) {
fprintf(file, "Fibonacci series (Iterative): ");
long long a = 0, b = 1, c;
for (int i = 1; i <= n; i++) {
fprintf(file, "%lld ", a);
c = a + b;
a = b;
b = c;
}
fprintf(file, "\n");
fclose(file);
printf("The series has also been saved to 'fibonacci_series.txt'.\n");
} else {
printf("Failed to save series to file.\n");
}
return 0;
}
------------incomplete
0 Comments