Category Archives: C++

C Language Reference| set(1)

Introduction to C:

The programming language C was developed in the 1970s by Dennis Ritchie at Bell Labs (Murray Hill, New Jersey) in the process of implementing the Unix operating system on a DEC PDP-11 computer. C has its origins in the typeless programming language BCPL (Basic Combined Programming Language, developed by M. Richards) and in B (developed by K. Thompson). In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.
C is a highly portable language oriented towards the architecture of today’s computers. The actual language itself is relatively small and contains few hardware-specific elements. It includes no input/output statements or memory management techniques, for example. Functions to address these tasks are available in the extensive C standard
library.
C’s design has significant advantages:
i. Source code is highly portable
ii. Machine code is efficient
iii. C compilers are available for all current systems

Functions:

A C program consists of individual building blocks called functions, which can invoke one another. Each function performs a certain task. Ready-made functions are available in the standard library; other functions are written by the programmer as necessary. A special function name is main( ): this designates the first function invoked when a program starts. All other functions are subroutines.

Character Sets:

ANSI C defines two character sets. The first is the source character set, which is the set of characters that may be used in a source file. The second is the execution character set, which consists of all the characters that are interpreted during the execution of the program, such as the characters in a string constant. Each of these character sets contains a basic character set, which includes the following:
The 52 upper- and lower-case letters of the Latin alphabet:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
The ten decimal digits (where the value of each character after 0 is one greater than the previous digit):       0 1 2 3 4 5 6 7 8 9
The following 29 graphic characters:
· ! ” # % & ‘ ( ) * + , – . / : ;
< = > ? [ \ ] ^ _ { | } ~
The five whitespace characters:
space, horizontal tab, vertical tab, newline, form feed
In addition, the basic execution character set contains the following:
i.The null character \0, which terminates a character string
ii. The control characters represented by simple escape sequences, for controlling output devices such as terminals or printers.

Identifiers:

Identifiers are names of variables, functions, macros, types, etc. Identifiers are subject to the following formative rules:
1.An identifier consists of a sequence of letters (A to Z, a to z), digits (0 to 9), and underscores (_).
2.The first character of an identifier must not be a digit.
3. Identifiers are case-sensitive.
4. There is no restriction on the length of an identifier. However, only the first 31 characters are generally significant.
Keywords are reserved and must not be used as identifiers. Following is a list of keywords:

auto      enum       restrict(*)       unsigned
break     extern     return            void
case      float      short             volatile
char      for        signed            while
const     goto       sizeof           _Bool(*)
continue  if         static           _Complex(*)
default  inline(*)   struct           _Imaginary(*)
do       int         switch
double   long        typedef
else     register    union

Some examples of identifiers are:
Valid: a, DM, dm, FLOAT, _var1, topOfWindow,temp,n
Invalid: do, zähler, total-amount, 123_num

Categories and Scope of Identifiers:

Each identifier belongs to exactly one of the following four categories:
1. Label names
2. The tags of structures, unions, and enumerations. These are identifiers that follow one of the keywords struct, union, or enum.
3.Names of structure or union members. Each structure or union type has a separate name space for its members.
4. All other identifiers, called ordinary identifiers.
Identifiers of different categories may be identical. For example, a label name may also be used as a function name. Such re-use occurs most often with structures: the same string can be used to identify a structure type, oneof its members, and a variable;

for example:

struct student {char *student; /*...*/} student;

The same names can also be used for members of different structures.
Each identifier in the source code has a scope . The scope is that portion of the program in which the identifier can be used. The four possible scopes are:

Function prototype:

Identifiers in the list of parameter declarations of a function prototype (not a function definition) have function prototype scope . Because these identifiers have no meaning outside the prototype itself, they are little more than comments.

Function:

Only label names have function scope. Their use is limited to the function block in which the label is defined. Label names must also be unique within the function. The goto statement causes a jump to a labelled statement within the same function.

Block:

Identifiers declared in a block that are not labels have block scope. The parameters in a function definition also have block scope. Block scope begins with the declaration of the identifier and ends with the closing brace (}) of the block.

File:

Identifiers declared outside all blocks and parameter lists have file scope. File scope begins with the declaration of the identifier and extends to the end of the source file.
An identifier that is not a label name is not necessarily visible throughout its scope. If an identifier with the same category as an existing identifier is declared in a nested block, for example, the outer declaration is temporarily hidden. The outer declaration becomes visible again when the scope of the inner declaration ends.

Basic Types:

The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted. Similarly, the type of a function determines how its return value is to be interpreted. Types can be either predefined or derived. The predefined types in C are the basic types and the type void. The basic types consist of the integer types and the floating types.
1 Integer Types:
There are five signed integer types: signed char, short int (or short), int, long int (or
long), and long long int(*) (or long long(*)). For each of these types there is a corresponding unsigned integer type with the same storage size. The unsigned type is designated by the prefix unsigned in the type specifier, as in unsigned int.
The types char, signed char, and unsigned char are formally different. Depending on the compiler settings, however, char is equivalent either to signed char or to unsigned char. The prefix signed has no meaning for the types short, int, long, and long long(*), however, since they are always considered to be signed. Thus short and signed short specify the same type. The storage size of the integer types is not defined; however, their width is ranked in the following order: char <= short <= int <= long <= long long(*). Furthermore, the size of type short is at least 2 bytes, long at least 4 bytes, and long long at least 8 bytes. Their value ranges for a given implementation are found in the header file limits.h. ANSI C99 also introduces the type _Bool to represent Boolean values. The Boolean value true is represented by 1 and false by 0. If the header file stdbool.h has been included, then bool can be used as a synonym for _Bool and the macros true and false for the integer constants 1 and 0.Three types are defined to represent non-integer real numbers: float, double, and long double. These three types are called the real floating types.

Internal representation of a real floating-point number:
The representation of a floating-point number x is always composed of a sign s, a mantissa m, and an exponent exp to base 2:
x = s * m * 2exp, where 1.0 <= m < 2 or m = 0
The precision of a floating type is determined by the number of bits used to store the mantissa. The value range is determined by the number of bits used for the exponent.For example, the number -2.5 = -1 * 1.25 * 21 is stored as:
S = 1, Exponent = 1+127 = 128, Mantissa = 0.25

 

C program to shift inputed data by two bits to the left

Parenthesis Checker(java implementation)

 

 

 

 

 

Advertisements

C program to shift inputed data by two bits to the left

This C program shift inputed data by two bits to the left


// C program to shift inputed data by two bits to the left
//Author: Sani Kamal
//Date:04-06-2015

int main(){
int num,temp;
printf("Enter any integer:");
scanf("%d",&num);
num<<=2;
temp=num;
printf("\nThe left shifted data is:%d",temp);
return 0;
}

/*
INPUT:
Enter any integer:5
OUTPUT:
The left shifted data is:20

*/

INPUT:
Enter any integer:2
OUTPUT:
The left shifted data is:8

Life, the Universe, and Everything

C program to find entered year is leap year or not

ScreenShots:

Untitled

C Program to find whether given number is even or odd

This c program check whether number is even or odd and print the result to the console.


// C program to find whether given number is even or odd

//Author:Sani kamal
//date: 24-08-2017

#include<stdio.h>

int main(){
int num;
printf("Enter any integer:\n");
scanf("%d",&num);
if(num%2==0)
printf("number is even");
else
printf("number is odd");

&nbsp;

return 0;

}
/*

INPUT:
Enter any integer:10
OUTPUT:
number is even

*/

INPUT:
Enter any integer:13
OUTPUT:
number is odd

C Language Decision Statements

C Program to find sum of two number

 

Screen shots:Untitled.png

C program to find entered year is leap year or not

This C program to find entered year is leap year or not. This program use if else condition.


// C program to find entered year is leap year or not
//Author:Sani kamal
//date: 24-08-2017

#include<stdio.h>

int main(){
int yr;
printf("Enter any year:\n");
scanf("%d",&yr);
if(yr%4==0)
printf("year is leap year");
else
printf("year is not leap year");

&nbsp;

return 0;

}
/*
Output:
Enter any year:1947
year is not leap year

*/

Output:
Enter any year:2012
year is leap year

 

Introduction to Java

HTML Elements   Java variables

Java program to swap first name and last name

C program to show the use of conditional operator

C program to show the use of conditional operator

This C program show the use of conditional operator. This program find greatest among two number


// C program to show the use of conditional operator
//This C program find greatest among two numbers
//Author:Sani kamal
//date: 24-08-2017

#include<stdio.h>

int main(){
int num1,num2;
printf("Enter the value for num1 and num2:\n");
scanf("%d%d",&num1,&num2);
(num1>num2) ? printf("num1 is greatest"): printf("num2 is greatest");

&nbsp;

return 0;

}
/*
Output:
Enter the value for num1 and num2:
12
34

num2 is greatest

*/

 

Output:
Enter the value for num1 and num2:
2
4

num2 is greatest

 

 

C program to find greatest of three number

C program to reverse a given numberC program to show swap of two number without using third variables

C program to find greatest of three number

This C program find greatest of three number using if conditional statement.


// C program to find greatest in three numbers
//Author:Sani kamal
//date: 24-08-2017

#include<stdio.h>

int main(){
int x,y,z;
printf("Enter the value for x,y& z:\n");
scanf("%d%d%d",&x,&y,&z);
if ((x>y)&&(x>z))
printf("x is greatest");
if((y>z)&&(y>x))
printf("y is greatest");
if ((z>x)&&(z>y))
printf("z is greatest");

&nbsp;

return 0;

}
/*
Output:
Enter the value for x,y & z:
12
34
56
z is greatest

*/

C program to reverse a given number

C program to calculate sum of 5 subjects and find percentage

C program to reverse a given number

This c program reverse a number given by user.


// C program to reverse a given number
//Author:Sani kamal
//date: 24-08-2017

#include<stdio.h>

int main(){
int num,a ,rev=0;
printf("Enter any integer:\n");
scanf("%d",&num);
while(num>=1){
a=num%10;
rev=rev*10+a;
num=num/10;

}
printf("Reverse:%d",rev);

&nbsp;

return 0;

}

Oputput:

Enter any integers:123

Reverse:321

 

C program to show swap of two number without using third variables

Algorithm paradigms (Introduction)

C program to show swap of two number without using third variables

This c program show swap of two number without using third variables.


// C program to show swap of two number without using third variables
//Author:Sani kamal
//date: 24-08-2017

#include<stdio.h>

int main(){
int num1,num2;
printf("Enter the value for num1 and num2:\n");
scanf("%d%d",&num1,&num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("After swapping the value of num1 and num2:%d %d",num1,num2);

&nbsp;

return 0;

}

Output:

Enter the value for num1 and num2:

3

5

After swapping the value of num1 and num2 :5  3

 

 

 

C program to calculate sum of 5 subjects and find percentage

C program to convert temperature degree centigrade to fahrenheit

C program to calculate sum of 5 subjects and find percentage

This C program calculate sum of five subjects and find percentage


// C program to calculate sum of five subject and find percentage
//Author:Sani kamal
//date: 24-08-2017

#include<stdio.h>

int main(){
int s1,s2,s3,s4,s5,sum, total=500;
float per;
printf("Enter marks of five subject:");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf("Total marks::%d",sum);
per=sum*100/total;
printf("Average marks:%f",per);

return 0;

}

Oputput:

Enter marks of five subjects:

50

70

65

75

85

Total mark:345

Average mark:69.0000

 

C program to find simple interest

C program to find area and circumference of a circle

Overview of C language

C program to find simple interest

This c program calculate simple interest from given principle amount, rate of interest and time .


// C program to calculate simple interest
//Author:Sani kamal
//date: 24-08-2017

#include<stdio.h>

int main(){
int p, r,t,si;
printf("Enter principle,rate of interest and time to calculate simple interest:");
scanf("%d%d%d",&p,&r,&t);
si=(p*r*t)/100;
printf("Simple interest:%d",si);

return 0;

}

Output:

Enter principle ,rate of interest and time to calculate simple interest:

500

5

10

Simple interest:250

 

C program to find area and circumference of a circle