Friday , June 23 2017
Home / Programming / Learn Full C Programming Basics With Examples

Learn Full C Programming Basics With Examples

As previously we discussed about how we can execute c program and similarly today we will learn C programming basics . Mean you can learn anything come between Basics of C programming language and this is my first long and big post ever that I am going to share with you and read it carefully and start practise on learning c language program because its very important language once you learned it then your future will be bright and shines . I wrote this huge article with example aswell so lets get started

★ C CHARACTER SET

C character set consists of Numeric characters, Alphabetic characters, Special characters, Escape characters.

Numeric Character : Set  Numeric characters are 0,1,2,3,4,5,6,7,8 and 9.

Alphabetic Character : Set Alphabetic characters are upper and lower-case letter of the English language (A,B,C,……………Z, a,b,c,…………………..z).

Special Character : Set The special characters are period (.), Comma (,), Semicolon (;), Colon(:), Single & double quotes( ‘ & ”), Plus & Minus sign (+ -), Asterisk(*), Slash(/), Back Slash (\), Equal Sign (=), Parentheses ( ), Percent sign (%), Less and Greater sign (< >), Hash or number sign (#), Ampersand (&), Question Mark (?), Opening and Closing braces ({ }), and other special characters including the blank character which is equivalent to a space on the keyboard.

Escape Character : Set In C/C++ language, some special characters are used for formatting. For this purpose, backslash (called the Escape character) is used in the statement, followed by the character. For example, to insert a new line in your program output, you would simply enter a single quotation mark, Backslash , the letter n and then a closing single quotation mark (\n) in your program code. Any character interpreted with a backslash (\), is known as escape sequence or escape character.

Escape Char Meaning Escape Char Meaning
\a Alarm \’ Single Quote
\b Back space \? Question Mark
\t Tab \\ Back slah
\n New line \r Carriage Return
\” Double Coute \v Verticle Tab
\0 The null zero \f Form feed

For example to print two names on two different lines, simple include \n between them, like:

  • printf( “Saeed \n Rehman”);
  • The above statement would print the output on screen as:
  • Saeed
    Rehman

★C RESERVED WORDS

Reserved words or Keywords are those words that are reserved for the specific purpose and cannot be used for other purposes. They have special meanings and compiler knows their meanings. In C, all keywords are in lower-case. Since uppercase and lowercase characters are not equivalent in C/C++, therefore the keywords must be entered in lowercase and must be spelled correctly.

★USER-DEFINED WORDS (Identifiers)

Words that are not reserved are called User-defined words or Programmer-supplied names, because the user or programmer makes them. Such words are also known as data-names or identifiers. Data-names are most common type of user-defined words, which must confirm the following rules.

  1. Legal characters of user-defined-names include letters (A to Z or a to z), digits (0 to 9) and only special character underscore ( _ ).
  2. User-defined-names or Identifier-names must be unique in a program, just as Roll-numbers of the students in a class are unique.
  3. Both upper and lower-case letters are allowed in a C/C++ program. But since C/C++ is a case-sensitive language, therefore it distinguishes between uppercase and lowercase letters and these are not interchangeable. For example, TAX, Tax, TaX or tax are four different identifiers, similarly RNO is different from Rno which is different from rno.
  4. The identifiers must start with a letter or an underscore. After the first letter or underscore, you can use letters, numbers and/or additional underscores.
  5. The only character must be alphabet.
  6. Identifier names may be of any length. . In C, at least the first 31 characters will be significant,
  7. No embedded blank (i.e. space) is allowed in an identifier-name.
  8. Reserved words cannot be used as an identifier-name.
  9. It is always a good programming practice to choose names that impart some meaning. For example, the identifier RADIUS is more meaningful than the identifier R.

Some Examples of Valid Identifiers

RNO        R_NO      ACC_NO

Z         ID_CARD_NO       A1234_56

SUMOFTOTAL        SUB_OF_TOTAL        SUM

Some Examples of Invalid Identifiers

3M  :                                The first character must be a letter or an underscore.

WHAT? :                          illegal character (?)

‘X’ :                                     Illegal character (‘)

Net-Pay :                           Illegal character (- )

ITEM NAME :                  Illegal character (space)

if :                                        C/C++ keyword

_  :                                        Underscore alone.

★VARIABLES

A value, which may vary during program execution, is called a variable. Each variable has a specific storage location in memory where its value is stored, or in simple words, we can say that a variable is a name used to represent a piece of information. You may think of a variable as a name of a box in memory. The box can hold any value that can fit in the variable. For example: Z=5

In the above example, Z is a variable, which holds a value 5. It is important to note that the value of a variable may change from time to time during the execution of a program, but the name remains the same. A variable in a program can hold numeric (integer or real) data as well as non-numeric data.

Z =  5 (5 is the Integer data )

Z =  3.14159 (3.14159 is the Real data)

Z =  ‘*’ (* is Character data)

★VARIABLE TYPES

In C, data may be divided into integer, floating point (real) and character data. Variable that holds the integer data, is known as integer variable. Integer types can further be divided into two varieties; signed and unsigned. “signed” integers are either negative or positive. “unsigned” integers are always positive. Sometimes you deal with very large numbers or very small numbers, therefore, an integer may either be short or long. A short integer is two bytes, whereas a long integer is four bytes long.

Like integer, floating-point variables hold the real numbers and character variables hold a single character.

The following table shows a list of variable types and ranges that each variable might hold.

DECLARATION NAME TYPE SIZE RANGE
char Character 1 byte -128 to 127
unsigned char Unsigned character 1 byte 0 to 255
Int Integer 2 bytes -32768 to 32767
unsigned int Unsigned integer 2 bytes 0 to 65,535
signed int Signed integer(same as int) 2 bytes -32768 to 32767
short int Short integer 2 bytes -32768 to 32767
unsigned short int Unsigned short integer 2 bytes 0 to 65,535
signed short int Signed short integer 2 bytes -32768 to 32767
long int Long integer 4 bytes -2,147,483,648 to 2,147,483,647
signed long int Signed long integer 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long int Unsigned long integer 4 bytes 0 to 4294967295
Float Floating point 4 bytes -3.4E+38 to 3.4E+38
Double Double floating point 8 bytes -1.7E+308 to 1.7E+308
long double Long double floating point 10 bytes -1.7E+4932 to 1.7E+4932
  • char occupies 1 byte in RAM.
  • int occupies 2 bytes in RAM
  • float occupies 4 bytes in RAM

★ Type “double” & “long double”

Type double & long double are similar to the floating point numbers except that they require more memory space and provide wider range of values and more precision.

The type “double” requires 8 bytes of storage and handles numbers in the range from 1.7 x 10-308 to 1.7 x 10+308 with a precision of 15 digits. Similarly type long double takes 10 bytes and stores numbers in the range of approximately 1.2 x 10-4932 to 1.2 x 10+4932 with a precision of 19 digits.

★ CONSTANTS

Unlike variables, a constant is a fixed value that does not change during the execution of a program. In simple words, we can say that constants are values while variables are holders of these values. In C/C++, a constant may be divided into main two types.

  1. Numeric Constants
  2. Non-Numeric Constants.

NUMERIC CONSTANT

Numbers are referred to as Numeric constants. Numeric constants are used for numeric purposes. It can contain:

  • Numeric digits 0 to 9.
  • Plus(+) or minus(-) sign. (if no sign precedes a constant, it is assumed to be positive).
  • Decimal point.

It is important to note that no commas or blanks are allowed in numeric constants. A few examples of numeric constants are given below.

25 0.0       +100         -300      3.14159           1.23E2

Numeric constant can be :

  • Integer constant.
  • Floating-point constant

INTEGER CONSTANT

The numeric constant, which doesn’t contain a decimal point, is known as Integer constant. Integer constants can either be positive or negative. A few examples of integer constants are:

25 1993          +134 0              – 500 7

FLOATING-POINT CONSTANTS

The numeric constant, which does contain a decimal point, is known as Floating-point constant. Like Integer constant, Floating-point constant could either be positive or negative. A few examples of these type of constants are given belo

0.3      -890.7         +98.33            0.08 etc.

NON-NUMERIC CONSTANT

Non-Numeric Constants are used for non-numeric purposes. Such as to produce output reports, heading or printing messages, etc. Non-numeric constants are divided into two types; Character constant and String constant.

CHARACTER CONSTANT

In C/C++, character constants are enclosed within single quotes. It is important to note that the single quotes are not part of the character constant, but they serve to delimit it. Some character constants are given belo

‘Z’ ‘3’ ‘*’ ‘=’ ‘.’ ‘ ‘

STRING CONSTANT

Any character or sequence of characters between double quotes is known as a string constant or simply a string. Characters within double quotes may be digits, alphabets or special characters as well as blank spaces and reserved words may also be used in double quotation marks. A few examples of string constants are:

“HELLO!”
“123.45”
“ ”
“BAKHTIAR”
“GOVERNMENT COLLEGE PABBI”

“———————————————————-“

Consider the following examples.

“ The Escape character is \\”
“This is a first line and \n this is a second line”
“I love \”PAKISTAN\” very much”

Output would be

The Escape character is \
This is a first line and
this is a second line
I love ”PAKISTAN” very much

★EXPRESSIONS

An expression in C is any valid combination of operators, constants and variables.. All expressions are statements. An expression is used to perform arithmetic operation(s) to return a value. Thus, the expression 2+3; returns the value 5. An expression may be defined in many different ways, as:

The expression may consist of a single entity; such as a constant, variable or an array element. For example:

7(Integer constant)                       3.2(Float constant)

Z(a variable)                                   X[3](array element)

PI (Float constant that returns the value 3.14159)

An expression may be a combination of variable and/or constant linked with the arithmetic operator(s). For example:

X = 5 + 6                                         X = A + B
X = Y                                                ++Z
Z = (X + B*B – 4.0 * A *C)

An expression can also represent logical conditions that are either true or false. For example:

X = = Y
X <= Y

★ C/C++ STATEMENTS

A statement is an instruction or a group of instructions that carry out certain action. It is used to evaluate an expression or to control the sequence of execution or does nothing (the null statement). C/C++ statements are free format: they may appear anywhere on a line. It is important to note that all C/C++ statements end with a semicolon. Semicolon is used to separate the statements. C/C++ statements can be divided into two types, these are:

  1. Simple Statement
  2. Structure Statement.

Simple Statement:

A simple statement consists of an expression to be evaluated, followed by a semicolon. A simple statement is essentially single and unconditional instructions like assignment statement, input/output statement, goto statement and so forth. For example:

X = 5;                    Z = X + Y;                        I++;

printf(“Hello! I am a statement”);

Structure Statement:

Structure statements can further be divided into three types. These are:

a) Compound Statement: The statements written in a pair of braces are considered to be one compound statement. A compound statement consists of several individual statements enclosed within a pair of braces. The individual statements may themselves be expression statements, compound statements or control statement. For example:

{
Pi = 3.14159;
Circumference = 2.0 * Pi * radius;
Area = Pi * radius * radius;
}
Note : that there is no semicolon after the closing brace.

b) Selection Statement:  Selection statement is used to choose among alternative courses of action.

if (Marks > 33)
printf( “Pass”);
else
printf(<< ”Fail”);

C/C++ supports two types of selection statements; if-else and switch statements. You can use the ? operator to replace if-else statements.

c) Iteration Statement: Iteration statements or looping statements are used to perform an action repeatedly while some condition remains true. For example:

count = 1;
while (count <= 5)
{ printf(“Pakistan”);
count ++;
}
In C/C++, the looping statements are for, while, and do-while

★Final Word:

Finally I am just saying Ahhhhhhh ! So after huge article Please dont forget to share this article along with your friends and Keep Remember me in your prayers Thanks :)

About Hkloma

Check Also

Hardware-Software

What is Computer and Types of Computers 2016

Computer is a device which process the data.It is used to receive, store, retrieve and …

Leave a Reply

Your email address will not be published. Required fields are marked *