Reading:  

Getting started with C Language


How to write your first program

Program

Programs are the set of instructions that given to the computer using any programming language to perform any specific task. The program explains in detail what to perform and how it can be performed. It consists of many specific operations along with operators and operands. The programs are written in high level language and there will be an interpreter in between these high level language and computer language. Here it’s C compiler which interprets the c language to the computer language. Other than any other interpreters’ compiler interprets the language and then finds the errors in the program and gives the error message along with reason and the number of line where the error occupies. All the errors are not detected but definitely the syntax errors.

Header files

The header files are used to use the built in functions, as they are defined in the header files. There are many header files in C program to use any kind of operations. To use those header files we use ‘#include’ the keyword and the file name in angle brackets ‘<>’. To denote that it is a header file we use ‘.h’ extension.  

Syntax :

#include<filename.h>

Example :

#include<stdio.h>    

Stdio.h the standard input output file is used for input output operations and functions like printf(),scanf().

 

Main function

As C is a procedure oriented language the important step in a program is the ‘main()’ function. The execution of a program begins from the main() function.

void main() {
Body of the program;
}

If we are using void main() function then we don’t want to return anything. And there is another option int main() where we have to return any value or have to return a ‘0’ value as return 0 statement.

int main() {
Body of the program;
return 0;
}

The ‘{}’

The ‘{‘ and ‘}’ are the two boundaries of a function body. Any program code in a function body is written between these two brackets.

Declaration

Declaration of a variable means allocating the memory for each variables.

Types of declaration

There are two types of declaration. The classification is by the scope of variable. A variable can be declared as a global variable and local variable. If we declare the variable before the main function  then it will be a global variable and we can access that variable from anywhere within the program. If we declare it within the function then the scope will be only within the function.

Syntax:

data type variables list;

Example:

int a,b;

which means it allocates the space for a and b.

Initialization of a variable

Initialization means assigning the initial values to the variable. For initialization we use assignment operator. We can assign the values to the variables at run time.

Syntax:

datatype variable=value;

Or

datatype variable;

variable=value;

Example :

int a =10;
// or
int a;
a= 10;

which means a now has value 10 assigned to it.

Input output functions

In computer language inputs are the data or information given to the computer. With the set of instructions (that is programs) and the inputs given, computer generates the result which we called outputs.

In C language we use functions for taking inputs and displaying output. The most common functions used are printf() and scanf() functions. The scanf() is a built in function defined within compiler to access the input to the program.

Syntax :

scanf(“%---”,&variable);

Example :

int a;

scanf(“%d”,&a);

here ‘%d’ is used because the variable a is an integer. The letter with % varies to different data types. %c is used for character , %f for float etc .

Sample programs

Write a program to display the value which is entered using the keyboard.

#include<stdio.h>
void main()
{
int  a;
printf(“Enter the value to be displayed “);
scanf(“%d”,&a);
printf(“The value entered is%d”, a);
}

Here the variable used is a, and at the  run time value(any integer) will be given to that variable By using scanf(), when the message ‘Enter the value to be displayed’ is displayed on the screen.

Program to add two numbers and store into another variable

#include<stdio.h>
void main()
{
int  a,b,c;
printf(“Enter the values to be added “);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“The sum is %d”, c);
}

This program accepts two values to a and b. then using arithmetic operator + those values are added and then saved in to the variable c. Then this program displays the value of c. in a single step ‘c=a+b;’ we use assignment operator and arithmetic operators together. As here we use ‘+’ ( addition ) likewise we can use  ‘-‘ ( subtraction ) ‘/’ ( division ) and  ‘%’ ( modulo ) operations like the same

Program to find largest among two numbers

#include<stdio.h>
void main()
{
int  a,b,c;
printf(“Enter two values “);
scanf(“%d%d”,&a,&b);
a>b?c=a:c=b;
printf(“The greater value is %d”, c);
}

Here we use relational operator ,assignment operator  as well conditional operators are used in the line ‘a>b?c=a:c=b;’.

 

 

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!