This Blog is for New Computer Prgrammers

Tuesday 13 December 2016

Output from C Language


Output from C - Language.

It is very important to get output from any computer language, there is a built in function in c language printf that is used to get output from c language. This function is defined in stdio.h header file.
Its syntax is as follows

printf("Formatted String",var1,var2 ...);

It is very important to understand what "Formatted String" is.

This string basically guides the printf function how to display the output on user screen, This string has three basic things

  • Text
  • Format Specifiers
  • Escape Sequences

Text means what ever you type alphabets, numbers etc will be printed on user screen as it is, if you want to show value of any variable you have to tell the format (type) of that variable in this Format String 

Here is the list of common Format Specifiers in C - Language.

Format Specifiers in C
%d for int
%f  for float
%c for character
%s for string
%u for unsigned int
%ld for long int

%lf  for double
%o  for octal int
%x  for hexadecimal int and for Addresses too

Escape Sequences in C

for special instructions inside Format String of printf and to print some of the characters which are not legally allowed to print by printf like " inverted commas, \ backslash etc we use some special code with \ inside printf this is called escape sequence

List of Escape Sequences in C Language

\'     single quote
\"     double quote
\?     question mark
\\     backslash
\a     audible bell
\b     backspace
\f     form feed 
\n     line feed
\r     carriage return
\t     horizontal tab

Some Examples for printf function

#include<stdio.h>
void main(void)
{
  printf("Hello C Language");
}

Output:
Hello C Language

#include<stdio.h>
#include<conio.h>

void main(void)
{
int x;
x=10;
printf("Value of x is:%d",x);
}

Output:
Value of x is10

#include<stdio.h>
#include<conio.h>
void main(void)
{
  int x,y;
 float  f;
x=5;
y=6;
f=4.5;
printf("Value of x is:%d\n",x);
printf("Value of y is:%d\n",y);
printf("Value of f:%f",f);
}

Output:
Value of x is:5
Value of y is:6
Value of f is:4.50000



Share:

Saturday 10 December 2016

Data Types in C - Language

What is Data Type?

There are some predefined keywords in C - Language that defines what kind of data would be stored in a variable, what operations can be applied on this data, how much memory will be occupied, and what will be the value range +ve and -ve to store in variable.

Common Data types in C - Language.
There are three main data types
  • Integral 
  • Floating Point
  • Character
Integral Data types.
  • int 
  • long
Floating Point Data types.
  • float
  • double
Character Data types.
  • char
Attributes of Integral data types.
  • signed
  • unsigned
When we use "unsigned" keyword with data type it blocks the input of -ve numbers and doubles the value range for this variable.

When we use "singed" or don't use this, -ve and +ve both type of values can be stored in the variable.

Integral Data Types in C.

TypeStorage sizeValue range
char1 byte-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2  bytes-32,768 to 32,767 
unsigned int2  bytes0 to 65,535
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long4 bytes-2,147,483,648 to 2,147,483,647
unsigned long4 bytes0 to 4,294,967,295
#include <stdio.h>
#include <conio.h>

int main() {

   printf("Storage size for int : %d \n", sizeof(int));
   
   return 0;
}
Here above is a code snippet to check the actual size of any Data type, use have to replace required data type with (int).

Note: in char type we can store any one character using single quots around it.


Floating Point Data Types

TypeStorage sizeValue rangePrecision
float4 byte1.2E-38 to 3.4E+386 decimal places
double8 byte2.3E-308 to 1.7E+30815 decimal places
long double10 byte3.4E-4932 to 1.1E+493219 decimal places
Note: we cannot use "signed" and "unsigned" attributes with Floating Point Type of values.







Share:

Friday 9 December 2016

Variables in C - Language


Variables
Variables are named memory locations which are used to store program's input data and its computational Results during program execution.

The Variables are created in the RAM, therefore the data stored in variables is temporarily saved. Every variables has following attributes.



  • Variable Name
  • Address
  • Value
  • Data Type

Variable Name.

Variable name could be any name that follows the C - Language Variable Naming Rules
  • A variable name can consists of letters, digits and underscore character.
  • The first character of the name must be letter (Alphabet), The underscore is also allowed but not recommended
  • C - Language is case sensitive, so be careful in naming variables, C understands Capital A and small a differently.
  • C Keywords can't be used as variable names.
  • For many compilers, a variable name must not exceed from 31 letters, if it is so, first 31 letters are accepted and others are ignored.
  • Blank spaces, or special characters are not allowed except underscore.
  • Variable name in a module must be identical.
Address.
Every variable is assigned a unique memory address composed with Hexa Decimal Numbers you can display the address of variable using following printf command

printf("%x",&varibalename);

Value.
You can store any one value from the variable's permitted range, that is told when we create variable (will discuss later), Only one value can be stored in a variable at one time, but it could be changed during the program execution.

Data Type.
Data type means set of values and a set of operations on those values (will discuss later).

Declaring a Varibale in C - Language.

C is a strongly typed language so all variables must be declared before using them, Varibale declaration means you are telling the compiler that i am going to use this number of variables, with these names and respective data types.

datatype variablename;

Defining a Variable.

Remember declaration means only data type and variable name, but no memory assigned to this variable, but When we assign any value to a variable it takes space in  memory, it is called Defining the variable.

Note: in C - Language when we declare a variable C automatically define it, and some garbage is stored in that variable.

Initialization of Variable.
We can declare and define variable in a single statement, this is called initialization of variable

datatype variablename=value;



Share:

Tuesday 6 December 2016

C - Language Terminology



There are some specific Terms every one have to memorize who want to learn C Language

KeyWords

In C Language some words are predefined means have special meanings for C - Language and Compiler of C Language recognize these words and behaves accordingly.

  • There are 32 Keywords in C
  • C Keywords are also called as Reserved words .

32 Keywords in C Programming Language

autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile

Constants.
Constants are basically values we use in Our Program. There are four types of constants are available in C-Language,

  • Character Constants
  • String Constants
  • Integer Constants
  • Floating Point Constants

Character Constants.

Only one character from ASCII Character Set enclosed within Single Quotes (Apostrophes signs) is called Character Constant, There is a special situation when two characters are enclosed in Single Quotes '\0'.

'A', '1', '#' are all valid examples of Character Constants.

String Constants.
String constants means series of characters one or more enclosed in Double Quotes (Inverted Commas) ,
here are some examples

"A" , "Asif", "I Love Pakistan" , "House # 45", "12345" are all valid string examples.

Integer Constants.

Integer Constant means the Numbers without Decimal Point, like 100, 234, 56

Floating Point Constants.

Floating Point Constants means Numbers with Decimal Points, like 23.45, 0.5, 4.555;






Share:

Monday 5 December 2016

Common Programming Errors

Common Programming Errors

The Programmer may come across errors while writing a computer program. In Programming languages these errors are called "bugs", and the mechanism to find and fix these errors is called "debugging", Some errors are caught by the compiler and some or not, There are common three types of errors normally occur while programming.

Syntax Error
These errors occur when we disobey the rules of writing in a language, means we violates the grammar rules of C Language, Compiler detects this kind of errors and stops the compilation process and shows a message box indicating what mistake has been done by the programmer.

Syntax are caught by the compiler.

Run time Errors.
This kind of errors occur due to environment of execution of the program or performing some illegal operation in the program such as dividing a number by Zero.

Run time errors are detected by the computer while executing the program and program is crashed if this kind of errors are not handled in the program logic.

Logical Errors.
This kind of errors are very hard to find, because every thing is good, Syntax is good, Environment is good, but the program is not behaving like we want, means not given the results we were expecting, to check this kind of errors we need to look into the program very carefully, check the values of variables on every step and then may be you would be able to find such errors, these errors are not detected by the compiler and not even by the environment, this is totally effort of the programmer to find and remove such type of errors.

Programming Languages
Computer programming languages are used to write computer programs, There are two major categories of programming languages i-e, Low Level Languages and High Level Languages.

Low Level Languages.

Low level languages are divided into two main categories i-e, Machine Language and Assembly Language. Machine language is the native language of the computer,  The computer does not need any translator to understand this language. Program written in any other language must be converted to machine language so that the computer can understand them. Machine language is consist on series of Binary digits 0's and 1's. As it is very difficult for human beings to remember long sequences of 0's and 1's, so writing programs in machine language could lead to full of errors, So it was thought to replace the long sequences of 0s and 1s with English like words, This idea gave the basis of the development of Assembly Language.

Assembly Language.
In Assembly Language, machine language instructions are replaced with English like words known as mnemonics. An Assembler is used to translate an assembly language programs into machine language.

High Level Language.
Programming languages whose instructions resemble the English language are called high level languages.  Every high level language defines a set of rules for writing programs called syntax of the language, Every instruction in the high level language must confirm  its syntax. If there is a syntax error in the program, it is reported by the language translator(Compiler or Interpreter),  

Common High level languages are C, C++, Java, Pascal, FORTRAN, BASIC, COBOL etc.

Share:

Saturday 3 December 2016

Hello World in C - Language

So dear ones Time to jump into the wonderland of C-Language, Going to describe you the very First Program in C - Language, that is must to enter otherwise there is no program in C - Language at all.

#include<stdio.h>
#include<conio.h>

void main(void)
{
clrscr();
pritnf("Hello World");
getch();
}

Here is the details about above mentioned program

Header Files
Remember one thing that all functionality of C - Language is stored inside some special Files called Header Files. Header Files have extension .h and are stored in include folder inside TC Folder when you install C Compiler.

Preprocessor Directives

# sign indicates the preprocessor directive, we have two preprocessor directives in C - Lanugage

  • include 
  • define


Include is used to add Header Files functionality in your program, and define is used to define Constants in C - Language

every C Program must start with preprocessor directive statements.

Main Function

Every Language starts its program from somewhere, so is the C Language, C Language Starts its Program from main function, the whole program is controlled from main function

Function Body
{ } Braces define the body of the function

inside the Body of the function we write our statements, 

clrscr() ; 
this statement clears the output screen

printf();
this statement writes the text given in double quotes on output screen

getch();
this statement is used to pause of output screen, it stays there until user presses any key from keyboard.


Role of Semicolon.
Semicolon is called statement terminator, so every C Language statement must end at Semicolon.

Share:

C - Development Environment


C - Development Environment

There are some basic Steps you have to keep in mind while working in C - Language Environment,  Remember one more thing i am using TURBOC Compiler for my this blog.

Writing your Programs.

First of all you have to write your program, for this you will have to invoke TurboC IDE Blue Screen by going 
c:\TC\Tc.exe

when your IDE loads you will see a blue screen if not so, FILE - NEW, all done.

write your program instructions here and save your file (FILE - SAVE or F2) with the extension .c anywhere in your computer but the best place is c:\tc\bin

Compiling your Program.

After writing the program, time to check either my program has some SYNTAX Errors or not, to do this we have to compile our program by Pressing ALT + F9, it will compile your program, if program has errors or warnings it will show you by popup a message box, and if everything is Ok it will create a new file .obj with the same name as you saved in previous step.

Source Program / Object Program

The Program you save in C Language with F2 is called your source program and the program file created by the compiler is called Object Program.

Linking your Program.

When the object program is created by the compiler, it is not executable, to make your Object program as Executable program you have to LINK some files with your object program this is done with a special process called Linking in C, to Invoke the Linker you can F9, it will add all required files with your object program and will create an Executable File with extension .EXE,

Running the Program.

When All is done so for, Time to Load the program in the Computer memory and let the Operating System to Execute your Program, This you can achieve by using CTRL + F9.


Share:

Total Pageviews