Showing posts with label Computer. Show all posts
Showing posts with label Computer. Show all posts

Thursday, January 20, 2022

C Programming - Tutorial


C Programming Tutorial

What is Programming?

Computer programming is a medium for us to communicate with computers, just like we use Hindi or English to communicate with each other. Programming is a way for us to deliver our instructions to the computer

What is C programming?

C is a programming language. C is one of the oldest and finest programming languages. C was developed by Dennis Ritchie in 1972.

Uses of C

C is a language that is used to program a wide variety of systems. Some of the uses of C are as follows:

Major parts of Windows, Linux, and other operating systems are written in C.
C is used to write driver programs for devices like Tablets, Printers, etc.
C language is used to program embedded systems where programs need to run faster in limited memory.
C is used to develop games, an area where latency is very important, i.e., a computer has to react quickly to user input.

Chapter 1: Variables, Constants, and Keywords:

Variables

A variable is a container that stores a ‘value.’ In the kitchen, we have containers storing rice, dal, sugar, etc. Similar to that variable in c stores the value of a constant. Example:

a = 3a is assigned “3”
b = 4.7b is assigned “4.7”
c = 'A'c is assigned “A”

Rules for naming variables in C :

1. The first character must be an alphabet or underscore(_).


2. No commas or blanks are allowed.

3. No special symbol other than underscore is allowed

4. Variable names are case sensitive


Constants

An entity whose value doesn’t change is called a constant.
Types of constant

Primarily there are 3 types of constant:

1. Integer Constant-1,6,7,9
2. Real Constant-322.1,2.5,7.0
3. Character Constant‘a’,’$’,’@’(must be enclosed within single inverted commas)

Keywords

These are reserved words whose meaning is already known to the compiler. There are 32 keywords available in C programming:

autodoubleintstruct
breaklongelse switch
casereturn enumtypedef
charregisterexternunion
constshortfloatunsigned
continuesignedforvoid
defaultsizeofgotovolatile
dostaticif while


Our first C program

#include<stdio.h>

int main() {

printf(“Hello World”);
return 0;

}

File : first.c

The basic structure of a C program

All c programs have to follow a basic structure. A c program starts with the main function and executes instructions presents inside it. Each instruction terminated with a semicolon(;)

There are some basic rules which are applicable to all the c programs:

  1. Every program's execution starts from the main function.
  2. All the statements are terminated with a semi-colon.
  3. Instructions are case-sensitive.
  4. Instructions are executed in the same order in which they are written.

Comments

Comments are used to clarify something about the program in plain language. It is a way for us to add notes to our program. There are two types of comments in c:
  1. Single line comment: //This is a comment.
  2. Multi-line comment : /*This is multi-line comment*/

Note: Comments in a C program are not executed and ignored.

Compilation and execution

A compiler is a computer program that converts a c program into machine language so that it can be easily understood by the computer.

A program is written in plain text. This plain text is a combination of instructions in a particular sequence. The compiler performs some basic checks and finally converts the program into an executable.

Library functions

C language has a lot of valuable library functions which is used to carry out a certain task; for instance, printf function is used to print values on the screen.

Types of variables

Integer variablesint a=3;
Real variablesint a=7.7 (wrong as 7.7 is real) ; float a=7.7;
Character variableschar a=’B’;

Chapter 2: Instructions and Operators:

A C-program is a set of instructions. Just like a recipe - which contains instructions to prepare a particular dish.

Types of instructions in C:

1. Type declaration instruction
2. Arithmetic instruction
3. Control instruction

Type of declaration instruction:

int a;
float b;

other variations:

int i = 10; int j = i, int a = 2;
int j1 = a + j - i;

float b = a+3; float a = 1.1; ==>Error! As we are trying to use a before defining it.

int a,b,c,d;

a=b=c=d=30; ==> Value of a,b,c & d will be 30 each.


Arithmetic Instructions



Note:
1.No operator is assumed to be present

int i=ab ( Invalid )
int i=a*b ( valid )

2. There is no operator to perform exponentiation in c however we can use pow(x,y) from <math.h> (More in next chapter).

Type conversion

An Arithmetic operation between

int and int ==> int
int and float ==> float
float and float ==> float

5/2 --> 2 5.0/2 --> 2.5 //IMPORTANT!!
2/5 --> 0 2.0/5 --> 0.4 //IMPORTANT!!

NOTE:
int a = 3.5; //In this case, 3.5 (float) will be denoted to a 3 (int) because a cannot store floats.

float a = 8; // a will store 8.0 [8-->8.0(Promotion to float)]

Quick Quiz:

Question - int k=3.0/9 value of k? and why?

Solution - 3.0/9=0.333, but since k is an int, it cannot store floats & value 0.33 is demoted to 0.

Operator Precedence in C

3*x-8y is (3x)-(8y) or 3(x-8y)?

In the c language, simple mathematical rules like BODMAS no longer apply.

The answer to the above question is provided by operator precedence & associativity.

Operator precedence

The following table list the operator priority in C.

PriorityOperators
1st  * / %
2nd+   -
3rd=

Operators of higher priority are evaluated first in the absence of parenthesis.

Popular Posts

Mobile Phones News

Digital Trends