This Blog is for New Computer Prgrammers

Monday 2 October 2017

Control Structures in C language

Control Structures in C language

A Computer Table

Most of the time we have to take Decisions in the Program...
before going to start this topic please visit my this Post also



Share:

Thursday 28 September 2017

Header Files in C language

Header Files


The final mystery of C that needs to be discussed is the header file. This started off as a simple idea, a convenience to make programming easier. If you have a standard set of instructions that you want to insert in a lot of programs that you are writing then you can do it using the #include statement.
The # symbol at the start indicates that this isn't a C statement but one for the C pre-processor which looks at the text file before the compiler gets it. The #include tells the pre-processor to read in a text file and treat it as if it was part of the program's text. For example:
#include "copy.txt"
could be used to include a copyright notice stored in the file copy.txt. However the most common use of the #include is to define constants and macros. The C pre-processor is almost a language in its own right For example, if you define the identifier NULL as:
#define NULL 0
then whenever you use NULL in your program the pre-processor substitutes 0. In most cases you want these definitions to be included in all your programs and so the obvious thing to do is to create a separate file that you can #include.
This idea of using standard include files has spiralled out of all proportions. Now such include files are called header files and they are distinguished by ending in the extension .h. A header file is generally used to define all of the functions, variables and constants contained in any function library that you might want to use. The header file stdio.h should be used if you want to use the two standard I/O functions printf and scanf. The standard libraries have been covered in a previous section.
This sort of use of header files is simple enough but over time more and more standard elements of the C environment have been moved into header files. The result is that header files become increasingly mysterious to the beginner. Perhaps they reach their ultimate in complexity as part of the Windows development environment So many constants and macros are defined in the Windows header files that they amount to hundreds of lines! As another example of how you could use a header file consider the complex structure defined earlier. At the moment it looks messy to declare a new complex variable as:
Share:

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:

Total Pageviews