Category: C for Beginners

C is a programming language, developed in “AT&T’s Bell Laboratories” to develop unix operating systems.It Was Written by Dennis Ritche.

Do you know why it was named as “C”?. As many of this feature is derived from “B” Language, it was named as “C”. BCPL and CPL are the earlier ancestors of “B” Language.

We don’t like to give you more introduction and history Since it won’t be useful to improve your skills in C Programming. At the same time, it will be interesting if you know the real-time use of C Programming Language.

1.C Language is used for creating system tool that acts as a major part of Operating systems.
2.Used in Embedding Softwares.
3.Unix is Completely Developed in C Language.

EXECUTION OF C PROGRAMMING:

Before start learning Programming Language, We need to know some important topics, which should be more helpful to understand

1.Coding :

First step in Programming , we need to choose editors to code our logic.Turbo C is the simple and mostly used editor to learn C Language

2.Compiling:

Second step in execution , before to run the code , we must compile it , it converts our written code into Machine Instruction.Compiler checks for errors, if your code is error free, then it is ready to run.

3.Compile Time Error Correction:

During Compile time, Compiler compile our code , and if it finds any error or issues , It will alert the user for the error occurred at compile time.

4.Linking:

This step Links our Libraries with programs, here also editor checks for error. This error will be called as Linker error

5.Run:
After Finishing all the above step, we can see our output by executing our program. There are chances for getting error in run time also, So again we need to review the code.

In C Programming Language DataType refers to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.

example:

int a;

a is variable.

Here int is a Datatype, which tells the compiler that how much space needs for this integer variable.

This program is not such a complex one, this is same as Hello World. But the difference is, it prints as ‘The number is 5’.

Now we need to print 10 instead of 5, not at compile time, it should be at run time. And we should not edit the code again and again to get the desired output.
Hope that now you understand the requirement, just think of it. Are you ready? can we achieve this? Yes sure. So, For that we are introducing an important concept in C Programming, Can you guess that? yep, that is Variable

Variable is a container which stores the data,Variable may have different DATA TYPES (int,char, float like that).

syntax:

int numberToPrint;

numberToPrint is the name of a variable
int is DATA TYPE of a variable
This is an integer variable as its data type is int, And as it is int variable, we can store integer values only.

Note : VARIABLE IS A NAME GIVEN TO THE MEMORY LOCATION WHERE THE ACTUAL DATA IS STORED.

Lets come back to our task, we need to change the value getting printed dynamically. We need to give input (a number that needs to be printed).
Here we add our variable into our program

From the above explanation , you can understand the usage of variable easily. Then Lets see how Variable works.

"VARIABLE IS A NAME GIVEN TO THE MEMORY LOCATION WHERE THE ACTUAL DATA IS STORED".

Can you understand the meaning of that Line above?
Lets say,int a;
Now, we have declared a variable.
Compiler checks the data type of that variable and allocates memory depending on that data type.
Initially no value is assigned to that variable , so the garbage will persist in that memory location.
Now, we are assigning a value to that variable like below

a = 10;

instead of garbage value, 10 is stored in that memory location.
Again, i am going to change the value of that variable like below,

a = 5;

Now, 10 is replaced by 5, but the memory address is same.Hope you understand the definition of variable:
To print the address of a variable , you can use like this ‘&var_name’.

Now lets back to our task.Now you can understand the program and you can enter your desired number at run time using scanf() function. It gets input and assign it in address of that variable.
printf() function prints the number what we have entered.

Lvalues and Rvalues in C:

There are two kinds of expressions in C:
lvalue : An expression that is a lvalue may appear as either the left-hand or right-hand side of an assignment.

rvalue : An expression that is a rvalue may appear on the right- but not left-hand side of an assignment.

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side.
int var = 100;

Constants:

The constant refer to a fixed value and the program can not alter it during its execution. These are fixed values and called as literals.
The constants are treated just like regular variables except that their values cannot be modified after their declaration.

Types of constants:

integer constants.

float constants.

character constants.

string constants.

Declaration:

const int a = 10;

In the above line, value of ‘a’ can not be changed during run time, since it is readonly.

Variables are basic and important concept before Datatypes, if you have not been gone through variables, please click here to know about variables in C. We know that variable should be declared before using it. Data types are the keywords, which are used for assigning a type to the variable.

The size of variable and constant are identified by data type.Data types are classified into two types.

Fundamental data type:

integer data type.

floating data type.

character data type.

Derived Data type:

Arrays

Structures

Enumeration.

Pointers.

syntax:data_type variable_name;

example:
int a;
here, int is a data type
a is the variable name

Integer data type:

“int” is a keyword used to refer integer data type.it allows a variable to store numeric values. int allocates 2 bytes of memory for that variable.

int a = 1;

It can store a value from -32768 to 32767.

long int data type allocates 4 bytes in memory,it can store value from -2,147,483,648 to +2,147,483,647.

Floating data type:

floating data type consists of a float and double.

“float” is a key word used to refer float data type. It allows a variable to store decimal numbers. Float allocates 4 bytes of memory for that variable.

float a = 1.5;

the range for the double data type is from 1E-37 to 1E+37.

Character data type:

“char” keyword is used to refer character data type, it allows to store only one character, Default storage size for character data type is 1 byte.

char variable_name= ‘A’;

note:sizeof() used to find the memory space for all data types.

Qualifiers:

Qualifiers alter the meaning of fundamental data types.There are three Qualifiers in C

sign Qualifiers:

Whether a variable can hold only positive values or both, can be specified by sign qualifiers.

‘signed‘ and ‘unsigned‘ are sign QualThe default is signed Qualifier.

example: unsigned int a;

here variable a can store only positive numbers.

size Qualifier:

These keywords are to alter the size of fundamental keywords. ‘long’ and ‘short’ are two keywords.

The size of ‘int’ variable is 2 bytes, if we want to store large values, ‘we will use ‘long’ (like long int a )

short can be used for the smaller size of a variable.

Constant Qualifiers:

if a variable has const as a prefix , then the value of that variable can not be changed.

const int a = 10;

Enumerator data type:

Enumeration data type consists of named integer constants as a list.
It starts with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list.
Enum syntax in C:
enum identifier {

there is a usage difference between increment (++) operator and (–) Decrement operator and these operators can be used as prefix or postfix, like below
let a is variable
a++;
++a;
a–;
–a;
When ++ is used as a refix, ++a will increment the value of a by 1 and then return it, if ++ is used as postfix(a++), operator will return the value of operand first and then increment it.

Logical (&) Relational operators:

Relational operators always checks relationship between two operands, it always gives binary result like 1 or 0 always,

example : a > b , where > is relational operator , if a is greater than b , it gives 1 else 0.

Logical operators are used to combine expressions containing relation operators.

nested if else statement:

if… else can be used in nested forms when serious of decisions are involved.

syntax:

if (condition)
statements to be executed if test expression is true;
else if(condition)
statements to be executed if test expressions 1 is true;
else if (condition)
. . .
else
statements to be executed if all test expressions are false;

In decision making, we are executing a block of code only one time when certain condition meets. We might come across a situation to execute a block of code n number of times. Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out from the loop statements when the condition becomes false.

There are 3 types of loop control statements in C language. They are,

for

while

do-while

for loop:

Execute a sequence of statements multiple times and abbreviate the code that manages the loop variable.It is entry controlled loop.

In this tutorial, we are going to talk about arrays. C programming language provides a special data structure called the array. An array lets you declare and work with a collection of values of the same data type.
Let’s say you want to declare 5 integers and assume that we don’t have array concepts. We will write like below.

int a,b,c,d,e;

now you want to declare 1000 variables of same data type.Are you ready to type a thousand variables like above? With the help of array, we can easily declare our 5 variable like this.

int a[5];

Array classified into two categories.

Single Dimensional Array

Multi-Dimensional Array.

Declaration:

syntax:

data_type variable_name[array_size];

This type of array is called as single dimensional array.

Array size should be an integer and greater than zero.

Initializing Arrays:

You can declare an array one by one or in a statement.

int age[3];

age[0] = 10;

age[1] = 20;

age[2] = 30;

(OR)

int age[3] = {10,20,30}; // the number of values inside the {,} can not be greater than array size declared with in [].

A function is a group of statements that together performs a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

You can divide up your code into separate functions for better understanding and readability. Dividing up your code among different functions is up to you, but logically the division usually is to each function will perform a specific task.
A function declaration tells the compiler about a function’s name, return type, and para meters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can call. Few example for built-in functions are strcat(), memcpy(), function strcat() is to concatenated two strings, function memcpy() is to copy one memory location to another location and many more functions.

Parts of a function:

Return Type:

A function may return a value or may not. The return_type is the data type of the value that function returns. Some functions perform the desired operations without returning a value. In this case, the return_type should be void.

Function Name:

This is the actual name of the function. The function name and the parameter list together constitute the function signature.

Arguments:
An Argument is like a placeholder. When a function is invoked, you pass a value as the Argument. This value is referred as actual parameter or argument. The Argument list refers to the type, order, and number of the parameters a function has.
Arguments are optional; that is, a function may contain parameters or not.

Function Body:

The function body contains a collection of statements that describes what the function does.Example:
Hope you understand , now lets write a simple function for sum of two numbers. This function has two numbers as arguments ,and result should be returned,

Function Declaration:

A function declaration tells the compiler about a function name and how to invoke the function name.

syntax:

return_type function_name( arguments );

example:int sumOfTwoNumbers(int num1,int num2);

Function calling:

We know what is function, and how to declare it , now we need to know , how can we use this function to achieve our desired task.

To invoke a function, you will have to call the function to perform the defined task . When a program calls a function, program control is transferred to the called function. A called function performs a defined task un till return statement is executed or its function-ending closing brace is reached, and it returns the program control back to the main program.

To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you have to store the returned value.

Function arguments:

Arguments can behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

While calling a function, there are two ways to pass the arguments to a function:

Call by value

Call by reference

Call by value:

This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.Program:void swap(int x, int y) { int z; z = x; x = y; y = z; printf("Swapped values are a = %d and b = %d", x, y); }

Sometimes it is useful to assign a pointer to a structure. Declaring pointers to structures is basically the same as declaring a normal pointer.struct customer *customer1;
To reference, you can use the infix operator: ->.
printf(“%s\n”, customer1->name)