Skip to main content

C Programming Basics

C PROGRAMMING BASICS:


1. Introduction to C language:

C language was invented in Bell Labs by Dennis Ritchie between 1972-73. C language is a case sensitive language i.e the input given to it varies according to upper and lower case letters. It is executed using binary form data such as (0,1). By using binary data the speed of executing a code increases.

1.1 Header Files:


Header files are those files which contain some predefined functions in it which we are going to use in our code. Every header file is accessed by appending an extension ".h".

Example:
              #include<stdio.h>
                               ↓                   ⇡
         Tells computer to                  header file with 
         include the header                 extension .h
         file                             
     There are many header files for different functions such as the following:        

  • <conio.h>
  • <math.h>
  • <string.h>
Etc.

1.2 Starting of code and basic Syntax:


void main() or int main()


This tells the computer from where the code is starting. It is written after the header files are specified.

Example:


                  #include<stdio.h>
                  #include<conio.h>
                  void main()
                  { Code to be written here}

Note: 

The code should be written inside the curly braces.

1.3 Step Sequence:

"\n" - used for going to a new line.
"\t"  - used for giving space of tab index.
"\v" - used for giving vertical space.
"\b" - used for backspace.

1.4 Data Types:

This is a keyword to specify the form of data which you will be using in your code. For integer it is ‘int’ for character it is ‘char’.

List of some important data types

Data Type
Value
Storage
int
1233456
2 bytes
long int
123456789
4 bytes
char
a
1 byte
float
4.3
4 bytes
double
456789.56
8 bytes
long double
123456.4567
10 bytes

1.5 Format Specifier:

It is a symbol which is used with the printf and scanf statements in your code to inform the computer which data type (int, char, float) is to be printed or being scanned. 

List of some important format specifiers:

Data Type
Format Specifier
int
%d
long int
%ld
long long int
%lld
float
%f
double
%lf
char
%c
string
%s

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter the value of a”);
scanf(“%d”,&a);
printf(“\n the value of a is %d”,a);
 getch();
}
                  
The above image code is written in code blocks
Output:
enter the value of a
10
The value of a is 10

Note:
  • clrscr() and getch() should be written if using turbo C++.
  • printf and scanf functions are explained further.
1.6 Airthmetic Operator

(+)  - for adding two values
(-)   - for subtraction
(*)  - for multiplication
(/)   - for division
(%) - modulus for calculating remainder

1.7 How to  declare a variable:

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 9000;
}

Note:
  • You can use any name in place of a such as num, no etc any name you want. 
  • semicolon should be present at end of every line in your code except some which you will learn further.
  • You cannot use name such as float etc in place of 'a' because it is already reserved for other function in the language.
1.8 Declaring multiple variables:

We can also declare multiple variables in the same line by seperating them with a (,) comma.

Example:

int a,b;

The above statement has declared two integers a and b in the single line.

1.9.1 printf function:

It is used to print the statement we want to display on the screen of our computer.

Example Code:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\n welcome to coding ninja”);
getch();
}

               
Output:
welcome to coding ninjaa


1.9.2 scanf Function:

It is very useful functionused to take input values from the user.

Example Code: 

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter the number”);
scanf(“%d”,&a);
printf(“\n the number is %d”,a);
getch();
}


Output:

Enter the number
54
The number is 54


Explaination of the above code:

In the above code the scanf function first specifies the data type which is going to be used in this case integer using a format specifier and the statement ‘&a’ tells the computer its storage location i.e ‘a’ in this case.

2. A code for calculating sum of two numbers:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
clrscr();
printf("\nEnter 1st number");
scanf("%d",&a);
printf("\nEnter 2 nd number");
scanf("%d",&b);
sum=a+b;
printf("\n the addition of two numbers is %d",sum);
getch();
}
   
              


Output:

Enter 1st number
10
Enter 2nd number
5
the sum of two numbers is 15


Conclusion:

So now you know the basics fundamentals of c language and you can now write a code for adding subtracting division and multiplication of numbers in the similar way.

If you want to learn the further concept of conditional statements you can click on the link given below

Comments

Popular posts from this blog

How to start programming for beginners

HOW TO START PROGRAMMING:
1. What is Programming?
Programming is a way of communicating with the computer and giving it set of instructions to do the work as per our needs.
Example: While calculating something with a calculator what has to be done according to the input given to it is regulated by a program.Programming can also be used to build a software to determine whether a particular number is prime or not.2. Can anyone learn Programming ?The answer to this question is simple YES anyone with basic knowledge of maths and some common sense can master programming. The most amazing example of this is Sundar Pichai the CEO of Google he Studied metallurgical engineering at IIT Kharagpur. So if you love it you can do it. 3. Why Should I learn Programming?If you have that passion to solve a problem and enjoy twisting your mind then this is for you else you should leave programming right now.The other important Factor is Money if you become a master in programming then there is no limit on how…

Basic C Programming Questions

Basic Coding Questions:Exercise 1:Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth.
Ans:
#include<stdio.h> #include<conio.h> void main() { float a,p,l,b; clrscr(); printf("\n enter the length and breadth of rectangle"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); printf("\n the area of rectangle is %f and the perimeter is %f",a,p); getch(); }

The above image code is written in code blocks
Output:
enter the length and breadth of rectangle 20(press enter) 10(press enter) the area of rectangle is 200 and the perimeter is 60


Explanation of the above code: Declaring of variables in data type float since length and breadth cannot always be integers.Taking input of length and breadth together by using scanf function.Substituting the values of length and breadth stored in l and b to find out area and perimeter.Displaying the result.
Q2. Write a programme to calculate average of 5 numbers by taking input from user.
Ans: