Skip to content

Commit 18553f7

Browse files
committed
Initial commit
0 parents  commit 18553f7

13 files changed

+215
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.exe

README.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Learning C
2+
3+
this is repo containing small code snippets written to explain basic programming
4+
concepts with c programming language
5+
6+
## Usage
7+
- Any and all code improvements are whole-heartedly accepted
8+
- The codes provided in this repo are under no restriction in terms of usage
9+
- Use the codes here wisely, as there were written by a budding c programmer
10+
- Any Attribution to the repo is also welcomed
11+

add_2_integers.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// program to add 2 integers in the C programming language
2+
#include <stdio.h>
3+
4+
// start of the main function
5+
int main(void)
6+
{
7+
printf("Starting the program here: \n");
8+
9+
int integer1, integer2; // first and second number to be entered by the user
10+
// int integer2; // second number to be entered by the user
11+
12+
printf("Enter the first Integer:\n"); //prompt
13+
scanf("%d", &integer1); // read the integer
14+
15+
printf("Enter the second Integer:\n"); //prompt
16+
scanf("%d", &integer2); // read the second integer
17+
18+
int sum = integer1 + integer2; // assign total to sum
19+
printf("Sum is %d\n", sum);
20+
21+
}// end of the main function

bitwise.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* program to study the bitwise operation of the c programming language */
2+
#include <stdio.h>
3+
4+
/* function main begin program execution */
5+
int main(void)
6+
{
7+
char b1 = 0b00000010;
8+
char b2 = 0b00000011;
9+
char b30, b31, b32;
10+
11+
b30 = 0 & b2; /* bitwise OR*/
12+
printf("the value of b30 is %d\n", b30);
13+
14+
b31 = (b2 >> 2) & 0b00000001;
15+
printf("the value of b31 is %d\n", b31);
16+
17+
b32 = (b2 >> 1) & 0b00000001;
18+
printf("the value of b32 is %d\n", b32);
19+
20+
}/* end of the main function */

comma_operator.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* program to test the operation of comma in assignment expression */
2+
#include <stdio.h>
3+
4+
/* function main starts program execution */
5+
int main(void)
6+
{
7+
int a;
8+
a = 89, 1336;
9+
printf("%d\n", a);
10+
}

for_loop.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* program to demonstrate the operation of the for loop in c programming language */
2+
#include <stdio.h>
3+
4+
/* function main starts program execution */
5+
int main(void)
6+
{
7+
int my_array[100];
8+
int i;
9+
10+
for (i=0; i<100; i++){
11+
my_array[i] = 1337;
12+
}
13+
14+
printf("The value of my_array is %a", my_array);
15+
16+
}/* end of the main function */

if_conditions.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* this program would make decision from a condition to execute part of the program */
2+
#include <stdio.h>
3+
4+
/* function main begin program execution */
5+
int main(void)
6+
{
7+
puts("Enter a value for the two integers and i will tell you");
8+
printf("%s", "the relation they satisfy: ");
9+
10+
int num1, num2; /* first and second number to be read from the user */
11+
12+
scanf("%d %d", &num1, &num2); /* read the two numbers from the user */
13+
14+
if (num1 == num2){
15+
printf("%d is equal to %d\n", num1, num2);
16+
} /* end if */
17+
18+
if (num1 != num2) {
19+
printf( "%d is not equal to %d\n", num1, num2 );
20+
} /* end if */
21+
22+
if (num1 < num2) {
23+
printf( "%d is less than %d\n", num1, num2 );
24+
} /* end if */
25+
26+
if (num1 > num2) {
27+
printf( "%d is greater than %d\n", num1, num2 );
28+
} /* end if */
29+
30+
if (num1 <= num2) {
31+
printf( "%d is less than or equal to %d\n", num1, num2 );
32+
} /* end if */
33+
34+
if (num1 >= num2) {
35+
printf( "%d is greater than or equal to %d\n", num1, num2 );
36+
} /* end if */
37+
38+
} /* end function main */

if_else_block.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* program to display the decision making process with a if-else blocks and conditions to check against */
2+
#include <stdio.h>
3+
4+
/* function main starts program execution */
5+
int main(void)
6+
{
7+
int age; /*declare a variable to hold the age value */
8+
9+
printf("Enter your age: "); /* prompt */
10+
scanf("%d", &age); /* gets the age value from the user*/
11+
12+
if (age > 18){
13+
puts("Hurray! you are an Adults"); /* block to be executed if the if block condition is met*/
14+
}
15+
else {
16+
puts("Awwwn! you are still a child"); /* block to be executed if the if block's condition is not satistied /*/
17+
}
18+
19+
20+
}/* end of main function */

index.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// a first program
2+
#include <stdio.h>
3+
4+
// function main begins program execution
5+
int main(void)
6+
{
7+
printf("Welcome to C!\n");
8+
} // function end

return_statement.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* c program to show the usage of the return statement in c functions */
2+
#include <stdio.h>
3+
4+
/* user defined function to carry out addition on 3 integers and return the result */
5+
int add(int a, int b, int c){
6+
int result; /* declare the result variable */
7+
8+
result = a + b + c; /* perform the addition in a single expression */
9+
return (result); /* return the result to the caller of the function */
10+
}
11+
12+
/* function main starts program execution */
13+
int main(void){
14+
int a;
15+
int b;
16+
17+
b = 98;
18+
a = 1 + 2 + 3 + 4;
19+
b = 23 + add(32, a, a + b); /* calls the function add */
20+
return (0); /* using return in the main function ends the program */
21+
22+
a = 999999999; /* anythinh after the return is not executed */
23+
}

test_addition_of_int_to_char.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* program to test the addition of a integer type to char type */
2+
#include <stdio.h>
3+
4+
/* function main starts program execution */
5+
int main(void)
6+
{
7+
int a = 15;
8+
char b = 'a';
9+
10+
printf("The sum of a and b is %d\n", a+b); /* displat the result of the summation as a formated string */
11+
12+
printf("The value of a is %d\n", a);
13+
printf("The value of b is %d\n", b);
14+
15+
}/* end of main function */

unary_operators.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* demonstrate use of a unary operator to check the size of an type or varaible in memory */
2+
#include <stdio.h>
3+
4+
/* function main starts program execution */
5+
int main(void)
6+
{
7+
int a; /* declare a integer variable a*/
8+
int size; /* declare a integer variable size */
9+
10+
a = 10000;
11+
size = sizeof(a);
12+
printf("The size of variable 'a' is %d Byte(s)\n", size);
13+
14+
int int_size = sizeof(int); /* checks the size of the int type*/
15+
int char_size = sizeof(char); /* checks the size of the char type*/
16+
17+
printf("The size of the 'int' type is %d Byte(s)\nThe size of the 'char' type is %d\n", int_size, char_size);
18+
19+
}/* end of the main function */

while_loop.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* program to display the syntax and use of the while loop in the c programming language */
2+
#include <stdio.h>
3+
4+
/* function main starts program execution */
5+
int main(void)
6+
{
7+
int a = 0;
8+
9+
while (a < 100){
10+
printf("A is currently = %d\n", a);
11+
a += 1;
12+
}
13+
}/* end of the main function */

0 commit comments

Comments
 (0)