Programming Languages
Basics of Computer Programming For Beginners
Be it any programming language in which you want to grow your career, it's very important to learn
the fundamentals first. Before having a good command over the basic concepts of programming, you
cannot imagine the growth in that particular career. Hence, this article will talk about all the basic
concepts of programming.
Also, if you're a beginner aiming to be a software engineer? Students often seek coding homework
assistance from experts when they are beginners or stuck with their difficult programming tasks. Then
you have landed on the right article. This article is specially designed to give you a glimpse into
programming and take a deep dive into the fundamentals of programming that most neglect but yet
are most important to know.
What is a Program (Code)?
Suppose I give you 10 numbers and tell you to find the average of the given 10 numbers, then how
do you find the average? You add all those numbers and then divide the sum of the numbers by the
total numbers given.
Easy task yeah. Now, if 10 sets and each set contain 10 numbers then what would you do?
For this problem, there are two solutions to solve the problem.
1. You take one set, add each number then divide it by the total numbers. If you are thinking to
solve this problem by this procedure, then it is right to solve by this but if you are going to be a
software engineer then solving the problem through this approach is not preferred.
2. You can write a program to solve the problem. Just you need to write a program in which the
computer takes input from the user and then it uses a procedure to find the average.
I have used the word procedure many times, what does it mean?
We define a procedure by giving steps one by one to our computer system and we call it a program.
Like, taking numbers from a user, adding them, and dividing the sum by the total number is a
procedure.
In short, a program is a set of instructions.
Top Most Popular Programming Languages
Learning a programming language before learning any technology is a must, hence, it's very
important to have full command of anyone programming language. Choosing anyone programming
language instead of going for many is better. Try working on basic problems using different
programming languages, and whichever seems easy to you, you can prefer working on that.
C/C++
Java
JavaScript
Python
Kotlin
Swift
R
Ruby
Scala
TypeScript
C Language Introduction
C is a general-purpose procedural programming language initially developed by Dennis
Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system programming
language to write the UNIX operating system.
Main features of C
Why Learn C?
C is considered mother of all programming languages as many later languages like Java, PHP
and JavaScript have borrowed syntax/features directly or indirectly from the C.
If a person learns C programming first, it helps to learn any modern programming language as it
provide a deeper understanding of the fundamentals of programming and underlying architecture
of the operating system like pointers, working with memory locations etc.
C is widely used in operating systems, embedded systems, compilers, databases, networking,
game engines, and real-time systems for its efficiency to work in low resource environment and
hardware-level support.
Writing First Program in C Language
This simple program demonstrates the basic structure of a C program. It will also help us understand
the basic syntax of a C program.
#include <stdio.h>
int main(void)
{
// This prints "Hello World"
printf("Hello World");
return 0;
}
Output
Hello World
Let us analyse the structure of our program line by line.
Structure of the C program
After the above discussion, we can formally assess the basic structure of a C program. By structure,
it is meant that any program can be written in this structure only. Writing a C program in any other
structure will lead to a Compilation Error. The structure of a C program is as follows:
Header Files Inclusion - Line 1 [#include <stdio.h>]
The first component is the Header files in a C program. A header file is a file with extension .h which
contains C function declarations and macro definitions to be shared between several source files. All
lines that start with # are processed by a preprocessor which is a program invoked by the compiler. In
the above example, the preprocessor copies the preprocesses code of stdio.h to our file. The .h files
are called header files in C.
Some of the C Header files:
stddef.h - Defines several useful types and macros.
stdint.h - Defines exact width integer types.
stdio.h - Defines core input and output functions
stdlib.h - Defines numeric conversion functions, pseudo-random number generator, and memory
allocation
string.h - Defines string handling functions
math.h - Defines common mathematical functions.
Main Method Declaration - Line 2 [int main()]
The next part of a C program is the main() function. It is the entry point of a C program and the
execution typically begins with the first line of the main(). The empty brackets indicate that the main
doesn't take any parameter. The int that was written before the main indicates the return type of
main(). The value returned by the main indicates the status of program termination.
Body of Main Method - Line 3 to Line 6 [enclosed in {}]
The body of the main method in the C program refers to statements that are a part of the main
function. It can be anything like manipulations, searching, sorting, printing, etc. A pair of curly
brackets define the body of a function. All functions must start and end with curly brackets.
Comment - Line 7[// This prints "Hello World"]
The comments are used for the documentation of the code or to add notes in your program that are
ignored by the compiler and are not the part of executable program .
Statement - Line 4 [printf("Hello World");]
Statements are the instructions given to the compiler. In C, a statement is always terminated by
a semicolon (;). In this particular case, we use printf() function to instruct the compiler to display
"Hello World" text on the screen.
Return Statement - Line 5 [return 0;]
The last part of any C function is the return statement. The return statement refers to the return
values from a function. This return statement and return value depend upon the return type of the
function. The return statement in our program returns the value from main(). The returned value may
be used by an operating system to know the termination status of your program. The value 0 typically
means successful termination.
Execute C Programs
In order to execute the above program, we need to first compile it using a compiler and then we can
run the generated executable. There are online IDEs available for free like GeeksforGeeksIDE, that
can be used to start development in C without installing a compiler.
If you want to run the programs in your computer, then you will have to create a development
environment for C. A development environment consists mainly consists of a text editor, which is
used to write the code, and a compiler that converts the written code into the executable file. Refer to
this article - Setting Up C Development Environment
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable can
store like integer, character, floating, double, etc.
Example:
int number;
The above statement declares a variable with name number that can store integer values.
C is a statically type language where each variable's type must be specified at the declaration and
once specified, it cannot be changed. This is because each data type requires different amounts of
memory and allows type specific operations.
The data types in C can be classified as follows:
In this article, we will discuss the basic (primary) data types in C.
Integer Data Type
The integer datatype in C is used to store the integer numbers (any number including positive,
negative and zero without decimal part). Octal values, hexadecimal values, and decimal values can
also be stored in int data type in C.
Range: -2,147,483,648 to 2,147,483,647
Size: 4 bytes
Format Specifier: %d
Format specifiers are the symbols that are used for printing and scanning values of given data types.
Example:
We use int keyword to declare the integer variable:
int val;
We can store the integer values (literals) in this variable.
#include <stdio.h>
int main() {
int var = 22;
printf("var = %d", var);
return 0;
}
Output
var = 22
A variable of given data type can only contains the values of the same type. So, var can only store
numbers, not text or anything else.
The integer data type can also be used as:
unsigned int: It can store the data values from zero to positive numbers, but it can’t store negative
values
short int: It is lesser in size than the int by 2 bytes so can only store values from -32,768 to
32,767.
long int: Larger version of the int datatype so can store values greater than int.
unsigned short int: Similar in relationship with short int as unsigned int with int.
Note: The size of an integer data type is compiler dependent. We can use sizeof operator to check
the actual size of any data type. In this article, we are discussing the sizes according to 64-bit
compilers.
Character Data Type
Character data type allows its variable to store only a single character. The size of the character is 1
byte. It is the most basic data type in C. It stores a single character and requires a single byte of
memory in almost all compilers.
Range: (-128 to 127) or (0 to 255)
Size: 1 byte
Format Specifier: %c
Example:
#include <stdio.h>
int main() {
char ch = 'A';
printf("ch = %c", ch);
return 0;
}
Output
ch = A
Float Data Type
In C programming, float data type is used to store single precision floating-point values. These values
are decimal and exponential numbers.
Range: 1.2E-38 to 3.4E+38
Size: 4 bytes
Format Specifier: %f
Example:
#include <stdio.h>
int main() {
float val = 12.45;
printf("val = %f", val);
return 0;
}
Output
val = 12.450000
Double Data Type
The double data type in C is used to store decimal numbers (numbers with floating point values) with
double precision. It can easily accommodate about 16 to 17 digits after or before a decimal point.
Range: 1.7E-308 to 1.7E+308
Size: 8 bytes
Format Specifier: %lf
Example:
#include <stdio.h>
int main() {
double val = 1.4521;
printf("val = %lf", val);
return 0;
}
Output
val = 1.452100
Void Data Type
The void data type in C is used to indicate the absence of a value. Variables of void data type are not
allowed. It can only be used for pointers and function return type and parameters.
Example:
void fun(int a, int b)
{ // function body
}
where function fun is a void type of function means it doesn't return any value.
Size of Data Types in C
The size of the data types in C is dependent on the size of the architecture, so we cannot define the
universal size of the data types. For that, the C language provides the sizeof() operator to check the
size of the data types.
Example
#include <stdio.h>
int main(){
// Use sizeof() to know size
// the data types
printf("The size of int: %d\n",
sizeof(int));
printf("The size of char: %d\n",
sizeof(char));
printf("The size of float: %d\n",
sizeof(float));
printf("The size of double: %d",
sizeof(double));
return 0;
}
Output
The size of int: 4
The size of char: 1
The size of float: 4
The size of double: 8
Different data types also have different ranges up to which can vary from compiler to compiler. Below
is a list of ranges along with the memory requirement and format specifiers on the 64-bit GCC
compiler.
Data Type Size (bytes) Range Format Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long
8 0 to 18,446,744,073,709,551,615 %llu
int
signed char 1 -128 to 127 %c
Data Type Size (bytes) Range Format Specifier
unsigned char 1 0 to 255 %c
float 4 1.2E-38 to 3.4E+38 %f
double 8 1.7E-308 to 1.7E+308 %lf
long double 16 3.4E-4932 to 1.1E+4932 %Lf
Note: The long, short, signed and unsigned are datatype modifier that can be used with some
primitive data types to change the size or length of the datatype.
Literals in C
In C, literals are constant values assigned to variables. They represent fixed values that cannot be
changed. Literals occupy memory but do not have references like variables. Often, the terms
constants and literals are used interchangeably.
Type Conversion
In C, type conversion is the process of changing one data type into another. This can happen
automatically by the compiler or manually by the programmer. Type conversion is only performed
between data types where such a conversion is possible.
Derived data types in C: are complex data types built upon or derived from the fundamental (or
primitive) data types like int, char, float, and double. They allow for the organization and manipulation
of data in more structured and powerful ways.
The main derived data types in C are:
Arrays: An array is a collection of elements of the same data type, stored in contiguous
memory locations. Elements are accessed using an index.
int numbers[5]; // An array of 5 integers
char name[20]; // An array of 20 characters (a string)
Pointers: A pointer is a variable that stores the memory address of another variable. Pointers
are fundamental for dynamic memory allocation, efficient array manipulation, and working with
functions.
int *ptr; // A pointer to an integer
int num = 10;
ptr = # // ptr now stores the address of num
Functions: While functions are not data storage types in the same way as arrays or
structures, they are considered derived data types in the context of C's type system because
they have a return type and parameter types, effectively defining a "type" of operation.
Structures (struct): A structure is a user-defined data type that groups together variables of
different data types under a single name. This allows for the creation of complex data records.
struct Student {
char name[50];
int roll_number;
float marks;
};
Unions (union): A union is similar to a structure, but all its members share the same memory
location. Only one member of a union can hold a value at any given time.
union Data {
int i;
float f;
char str[20];
};
Enum: an enum (enumerated type) is a user-defined data type that consists of a set of named
integer constants. It provides a way to assign meaningful names to integral values, enhancing
code readability and maintainability.
Syntax:
An enum is declared using the enum keyword, followed by the enum's name and a list of
enumerators enclosed in curly braces.
enum enum_name {
enumerator1,
enumerator2,
// ...
enumeratorN
};