ALITS_C&DS_Online Training
ALITS_C&DS_Online Training
Operators
Strings
User Inputs
Conditional Statements
Loops
Arrays
Functions in C
C is a programming language created by Dennis Ritchie at Bell Labs in the 1970s. It's important
because many well-known operating systems like Unix, Microsoft Windows, Mac OS X, and
GNU/Linux are written in C.
Besides operating systems, many other popular programming languages like Perl, PHP, Python, R,
Matlab, and Mathematica are based on or influenced by C.
As of July 2012, C has been ranked as the most popular programming language according to the
TIOBE index - a well known website which ranks the programming langauages. This shows that C is
trusted by many programmers and is still widely used today.
Operating Systems: Many major operating systems are written in C, which means it's
fundamental to how computers work.
Influence on Other Languages: C's concepts and syntax have influenced many other popular
programming languages, making it a foundational language for many programmers.
Trust and Usage: The TIOBE index ranking shows that C is still widely used and trusted by
programmers today.
#include <stdio.h>
int main() {
rintf("%d", 12);
1. #include is a preprocessor directive that tells the compiler to include the contents of another
file before compiling the program.
2. <stdio.h> stands for Standard Input Output Header. This file contains functions that allow us
to perform input and output operations, such as printf() (for displaying output)
and scanf() (for taking user input).
3. main() is the starting point of any C program. The execution of the program begins from this
function.
4. int before main() means that the function returns an integer value. A return value of 0
generally indicates successful execution.
5. printf: This is a function in C used for printing or displaying information on the screen.
6. "%d": This is a format specifier. It tells printf how to interpret the data it's given. In this
case, %d is used for integers (whole numbers).
7. 12: This is the actual data you want to print. Here, it's the number 12.
So, when you write printf("%d", 12);, it tells the computer to print the integer 12 on the screen.
Note that this line contains a (;) semicolon at the end. Semicolon is mandatory at the end of
statements in C.
Click on Submit below the IDE to know the result. Then click on next to continue.
Common Doubts?
Printing a number
#include <stdio.h>
int main() {
printf("%d", 20);
}
Arithmetic Operations
We can also perform mathematical operations (like addition, subtraction etc) with printf().
++ for addition
−− for subtraction
∗∗ for multiplication
// for division
printf("%d", 21 + 40);
Again, printf is the function we use when we want to print. "%d" tells the compiler that we want to
print a number. And writing 21 + 40 tells the compiler to add these two number and print the sum.
Task
Try to add 21 and 40 in code and print the result. Remember, we don't need "" (double quotes) when
printing numbers.
#include <stdio.h>
int main() {
printf("%d", 21+40);
Common doubts:
Comments:
In last section, you might have noticed this symbol // and some text instruction in the code editor.
That was a comment.
Comments in C are notes added to code to explain what the code does or any other kind of
information about the code. They are helpful for making the code easier to understand.
Types of Comments in C
o Example:
int main() { printf("%d", 12); // This is a single-line comment. the printf statement
prints 12. }
2. Multi-Line Comments:
Syntax: /* Your multi-line comment here *
Example:
/* This is a multi-line comment
spanning multiple lines */
int main()
{ printf("%d", 12); }
Multi-line comments start with /* and end with */. They can span multiple lines and are
typically used for longer explanations or for commenting out large blocks of code.
Best Practices for Using Comments in C
Be Descriptive: Always strive to write clear and descriptive comments that explain the
purpose, functionality, or reasoning behind the code.
Update Regularly: Remember to update comments when you modify the code to ensure
they remain accurate and relevant.
Avoid Redundancy: Avoid writing comments that only restate the obvious. Comments
should provide additional information or insights that are not immediately evident from the
code itself.
Printing text:
#include <stdio.h>
int main() {
Task
Note - Notice that in the output they are printed together without any space between them.
Expected Output
73
Data Types
Lets look at some common data types found in programming problems for beginners
Integers
o Integers are whole numbers that can be negative, zero or positive. Examples
- 15, 0, -5
Float
Strings
o Note that C does not contain a string datatype we can declare a string using a char
array.
Arrays
o Arrays are used to store multiple variables or input types. Examples of arrays
o Note that the strings in the array were denoted as "abcd" - we will come to this at
a later stage
Bool
o They are used to obtain true or false output from the code
#include <stdio.h>
#include <stdbool.h>
int main() {
// Integer
int a = -5;
printf("%d\n", a);
// Float
float b = 4.0;
printf("%f\n", b);
// Character
char c = 'a';
printf("%c\n", c);
printf("%s\n", string);
// Integer array
printf("\n");
// Boolean
printf("%d\n", boolean);
return 0;
In the previous problem, we saw common data types found in programming problems for
beginners. Now lets try to take an integer as input.
Task
Let us define a variable nn that accepts integer inputs.
Output the same variable nn.
Code the solution in the IDE and then click Submit to continue.
#include <stdio.h>
int main() {
___ n;
_____("%_", &n );
printf("%_", n );
return 0;
#include <stdio.h>
int main() {
int n;
scanf("%d", &n );
printf("%d", n );
return 0;
Accept 3 space separated integers given in a line into 3 variables - AA, BB and CC
#include <stdio.h>
int main() {
int A, B, C;
return 0;
Task
Prints out 99 space separated strings as output in a single line to the console
Remember that the scanf()scanf() function necessarily takes the parameters to be strings.
Solve the problem in the IDE and then click on Submit to proceed.
Sample 1:
abc cde
fg hi jk
lmno
output:
abc cde fg hi jk l m n o
Solution:
#include <stdio.h>
int main() {
scanf("%s%s", A , B );
scanf("%s%s%s", C, D, E );
scanf("%s%s%s%s", F , G, H, I );
printf("%s %s %s %s %s %s %s %s %s ", A , B , C , D , E , F , G , H , I );
return 0;
Now let's look at how to convert input into output and display the same.
Most algorithmic programming problems will need you to generate the following types of output
Integers or Floats
Strings
#include <stdio.h>
int main() {
int N = 5;
printf("%d\n", N);
return 0;
Input mirror
Now let's write a simple program - the Input mirror. Your program needs to do the following
Accept an integer input from the console and store it in the variable NN
Accept a string input from the console and store it in the variable SS
Solve the problem in the IDE and then click on click on Submit to proceed.
Sample 1:Input
Abcde
Output:
5 abcde
#include <stdio.h>
int main() {
int N;
return 0;
}
In the previous module, we practiced simple methods of input and output used in programming
problems.
Let's take the next small step and learn about test cases.
You will find the concept of 'test cases' on various programming platforms such as Leetcode,
Codeforces, CodeChef, Hackerrank. What are test cases?
Test cases are multiple Inputs - multiple instances of the same problem, all of which have
to be solved by your code correctly.
Task
For each test cases, prints out the integer N to console on a separate line (our Input
mirror problem)
Sample1:
Input:
11
13
Output:
11
13
#include <stdio.h>
int main() {
int A, B, C, D, E;
printf("%d \n", A );
printf("%d \n", D );
printf("%d \n", E );
In the previous problem - we wrote the program to accept 5 inputs on 5 separate lines.
Task
Accepts the count of test cases - t - as an integer input given in the 1st line.
This is followed by t lines - Each line contains an integer N
For each test cases, prints out the integer N to console on a separate line (our Input
mirror problem)
Sample1:
Input:
22
33
Output:
22
33
#include <stdio.h>
int main() {
int t,n;
int i = 1;
scanf("%d", &t );
while ( i <= t) {
scanf("%d", &n );
printf("%d \n", n );
i = i+1;
return 0;
In the previous problem, we had t test cases and each test case had 1 line of input.
However, each test case can have multiple lines of input as well.
Task
o The 1st line of input has 2 space separated integers - accept them
as variables A and B.
o The 2nd line of input has 3 space separated integers - accept them
as variables C, D and E.
Sample 1:
Input:
3
12
345
11 22
33 44 55
1 23
Output:
12345
11 22 33 44 55
#include <stdio.h>
int main() {
int t;
int A, B, C, D, E;
int i = 1;
_____("%d", &t );
_____ (i <= t) {
i = i + _;
#include <stdio.h>
int main() {
int t;
int A, B, C, D, E;
int i = 1;
scanf("%d", &t );
while (i <= t) {
i = i + 1;
In the previous problem, each testcase had 2 lines of input - each consisting of integers.
Test cases can also contain a combination of integers and strings.
Task
For each test case, output on a single line the 2 integers followed by the string.
Sample 1:
Input:
12
abcde
34 567
A1B2C3
Output:
1 2 abcde
34 567 A1B2C3
// Update the blanks below to solve the problem
#include <stdio.h>
int main() {
int t;
___ A, B;
____ C[30];
int i = 1;
_____("%d", &t);
_____ (i <= t) {
_____("%_", &C);
i = i+_;
#include <stdio.h>
int main() {
int t;
int A, B;
char C[30];
int i = 1;
scanf("%d", &t);
while (i <= t) {
scanf("%s", C); // No '&' needed for scanning a string into an array name
i = i + 1;
}
return 0; // Added return 0 for good practice
o far we dealt with test files and test cases which were visible to you.
However, programming platforms typically have hidden or private test files and test cases. What
are these?
o Test your code for handling scale -> does your program solve within time and space
constraints?
o Your code is run on each test case within the private test file
You are able to see the output generated by your code for inputs defined by you
Note: The Sample 1 input values are pre-populated in the Custom inputs below the IDE
Task
Replace the Custom inputs with your own created inputs and click Run to check the result.
You can experiment a few more options.
Once done, click on Submit to test your code against the Private test files
Note - Do not forget that the 1st integer in the custom inputs has to be t - the number
of test cases
//Click on 'Submit' once you have tried out to proceed to the next problem
#include <stdio.h>
int main() {
int t;
int n;
int i = 1;
scanf("%d\n", &t);
while (i <= t) {
scanf("%d", &n);
printf("%d\n", n + 1);
i = i + 1;
In the IDE - the text after '//' is comments which doesn't affect the code and will give you
hints about what you need to do
The Solution tab also has '//' - comments which give you helpful information
Task
Sample1:
Input:
-4
-5
Output:
-1
-2
-3
#include <stdio.h>
int main() {
int t;
int N;
int i = 1;
_____("%d\n", &t);
_____ (i <= t) {
scanf("%_", &N);
i = i + 1;
#include <stdio.h>
int main() {
int t;
int N;
int i = 1;
scanf("%d\n", &t);
while (i <= t) {
scanf("%d", &N);
printf("%d\n", N * -1);
i = i + 1;
o Create a variable X which contains the string S concatenated with the string S
Sample1:
Input:
3
ab
bc
cd
Output:
abab
bcbc
cdcd
#include <stdio.h>
#include <string.h>
int main() {
int t;
scanf("%d", &t);
char X[MAX_LEN * 2]; // declare a character array to store the concatenated string
int j;
int k;
}
X[j+k] = '\0'; // terminate the string
#include <stdio.h>
#include <string.h>
int main() {
int t;
scanf("%d", &t);
char X[MAX_LEN * 2]; // declare a character array to store the concatenated string
int j;
int k;
o as you solve programming problems - you will need to debug and find errors in your own code.
Task
For each test case, output to the console the value that is double the integer N.
Can you try and debug / fix the error in the given program?
Sample1:
Input:
Output:
#include <stdio.h>
int main() {
int t;
int N;
int i = 1;
scanf("%d", &t);
while (i <= t) {
scanf("%s", &N);
printf("%s\n", 2 * N);
i = i + 1;
#include <stdio.h>
int main() {
int t;
int N;
int i = 1;
scanf("%d", &t);
while (i <= t) {
i = i + 1;
#include <stdio.h>
int main() {
int t;
int N;
int i = 1;
scanf("%d", &t);
while (i <= t)
scanf("%d", &N);
printf("%d\n", 2 * N);
i = i+1
#include <stdio.h>
int main() {
int t;
int N;
int i = 1;
scanf("%d", &t);
scanf("%d", &N);
printf("%d\n", 2 * N);