Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

C for Beginner -- Part3 : C Basics -- Variable & Datatype

c beginner fresher variable datatype format specifier commenting

  • Please log in to reply
No replies to this topic

#1 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 28 February 2013 - 11:11 PM

In this series tutorial we will cover the basics of C language, Expressions,  C Weaponology (Control statement, Operators etc), decision making, data storage, functions (structured programming), pointers, Dynamic memory allocation and so on.

We will run step by step and thoroughly. We will learn about a weapon(statement/logic) on demand that means when we need some thing to use in our program we will learn that on time. We will explain everything with descriptive examples, various ways to solve problems, thus a learner can perceive everything about C Programming, logic development , advanced programming practice etc.
Finally we will run some C projects that will make sense on how to be a master in C.
 
In the last part we had come to know how to write a Hello World program, how to build and execute the program. Today we will learn about C Variables, Datatypes and finally how to use those in a C program.
 
 
Variables
In computer programming, a variable is a storage location and an associated symbolic name(an identifier) which contains some known or unknown quantity or information, a value. The variable name is the usual way to reference the stored value, this separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time(during execution of a program), and the value of the variable may thus change during the course of program execution. Variables in programming may not directly correspond to the concept of variables in mathematics. The value of a computing variable is not necessarily part of an equation or formula as in mathematics. In computing, a variable may be employed in a repetitive process: assigned a value in one place, then used elsewhere, then reassigned a new value and used again in the same way (iteration). Variables in computer programming are frequently given long names to make them relatively descriptive of their use, whereas variables in mathematics often have terse, one- or two-character names for brevity in transcription and manipulation.
A variable storage location may be referred by several different identifiers, a situation known as aliasing. Assigning a value to the variable using one of the identifiers will change the value that can be accessed through the other identifiers.
 
 
Identifiers
Before we can do anything in any language, we must know how to name an identifier. An identifier is used for any variable, function, data definition, etc. In the C programming language, an identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline.Two rules must be kept in mind when naming identifiers.
  • The case of alphabetic characters is significant. Using INDEX for a variable name is not the same as using index and neither of them is the same as using InDeX for a variable name. All three refer to different variables.

  • According to the ANSI-C standard, at least 31 significant characters can be used and will be considered significant by a conforming ANSI-C compiler. If more than 31 are used, all characters beyond the 31st may be ignored by any given compiler. There are limitations when using other special characters in an identifier such as,

  •  Name of identifier includes alphabets, digit   and underscore.

            Valid name: world, addition23, sum_of_number etc.

             Invalid name: factorial#, avg value, display*number etc.

  • First character of any identifier must be either alphabets or underscor

            Valid name:  _calulate, _5,a_, __ etc.

            Invalid name: 5_, 10_function, 123 etc.

  • Name of identifier cannot be any keyword of c program.(we will learn about C keyword later on)

 
 
Datatypes
In computer programming, a data type or simply type is a classification identifying one of various types of data, such as real-valued, integer or Boolean, that determines the possible values for that type, the operations that can be done on values of that type, the meaning of the data, and the way values of that type can be stored. In C language there are 4 basic data types listed below
 
datatypes.png
 
C89 defines five foundational data types: character, integer, floating-point, double floating-point, and valueless(void). These are declared using char, int, float, double, and void, respectively. These type form the basis for several other types. The size and range of these data types may vary among processor types and compilers. However, in all cases an object of type char is 1 byte. The size of a int is usually the same as the word length of the execution environment of the program. For most 16-bit environments, such as DOS or Windows 3.1, an int is 16 bits. For most 32-bit environments such as Windows 95/98/NT/2000, an int is 32 bits. However, you cannot make assumptions about the size of an integer if you want your programs to be portable to the widest range of environments.It is important to understand that C stipulates only the minimal range of each data type, not its size in bytes.
 
 
Variable Declaration Rules
Variable declaration is to name an identifier with its datatype. In C we must declare a variable before we use it in our program. 
void main(){
int a,b,c;
}
In the above example variable a,b and c of integer type are declared on the starting of main function. And now on we can use those variables in anywhere in our program. 
#include<stdio.h>
void main(){
printf("Program started");
int a;
printf("The previous line  produces error.");
}
In the above example this program will show an error(Message shown by compiler:Declaration is not allowed here) when we compile it because we cant declare variables after the program start that means after any runnable statement. Recommendation here is  to test our program for error reporting in a old fashion c compiler named TURBO C 3.00. In this light weight compiler we can observe the errors very effectively. We knew that we can save our c program with a .c/.C extension as well as .cpp/.CPP extension. in the .cpp/.CPP extension C file, this limitation is overcame. There are other rules as well we will learn  on time.
 
 
Examples
With some examples we will learn about 4 types of data types. First come with the char.c file
#include<stdio.h>
#include<conio.h>
 
void main(){
    char a,b;
    a = 'a';
    b = 'b';
    printf("Character a: %c and b: %c",a,b );
    getch();
}
Here in the beginning of the main function there is a statement starting with char keyword(we will know about keyword later on). The a and b are the two variables that can contain a single character(a character is defined in the single quote ''). Then we assigned(the   ‘=’  operator works assigns value right to left, will know about operator later on) character ‘a’ to variable a and ‘b’ to variable b. Finally we have showed up the data/value containing variable a and b. The %c named as format specifier(see below) is used to print character in the screen in respective order.
So for integer, float and double type variable we can change the char.c program as
/***file name:int.c**/
#include<stdio.h>
#include<conio.h>
 
void main(){
    int a,b;
    a = 10;
    b = 20;
    printf("Integer a: %d and b: %d",a,b );
    getch();
}
 
 
 
/***file float.c**/
#include<stdio.h>
#include<conio.h>
 
void main(){
    float a,b;
    a = 100.25;
    b = 100;
    printf("Float a: %f and b: %f",a,b );
    getch();
}
 
 
 
/***file double.c**/
#include<stdio.h>
#include<conio.h>
 
void main(){
    double a,b;
    a = 100.25;
    b = 100;
    printf("Double a: %lf and b: %lf",a,b );
    getch();
}
 
 
 
Comments in C Language
See there is a line having /* file float.c*/ we call this comment in a source program. These code will not affect on the program execution that means these are hidden to the compiler.
 
The syntax for a comment is: -- Single Line
/* comment goes here */
or
// comment goes here
 
Syntax for Spans Multiple Lines Comments
/*
 * Author: kernelcoder
 * Purpose: To show a comment that spans multiple lines.
 * Language:  C
 */
The compiler will assume that everything after the /* symbol is a comment until it reaches the */ symbol, even if it spans multiple lines within the C program.
 
Syntax: End of Code Line
#define AGE 6    /* This constant is called AGE */
or
#define AGE 6    // This constant is called AGE
In this example, the compiler will define a constant called AGE that contains the value of 6. Then it interprets everything after the /* symbol as a comment until it reaches the */ symbol.
 
 
 
Format Specifier
We have  used %c format specifier in the char.c program. These are used to print formatted output to the screen. The % specifiers that  can be used in ANSI C are:
%c -- single character
%d -- integer
%e -- float or double exponential format
%f, %lf  -- float or double signed decimal
%g -- (%G) float or double use %f or %e as required
%o -- int unsigned octal value
%p -- pointer address stored in pointer
%s -- array of char sequence of characters, string
%u -- int unsigned decimal
%x -- (%X) int unsigned hex value etc
 
 
The ASCII Code
The American Standard Code for Information Interchange (ASCII, pronunciation: /ˈæski/ **-kee) is a character-encoding scheme originally based on the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text. Most modern character-encoding schemes are based on ASCII, though they support many additional characters. Each character we are using for programming purpose has an ASCII code. 
#include<stdio.h>
#include<conio.h>
void main(){
    char a='a',B='B';
    printf("The Ascii code of a:%d and B:%d",a,B );
    getch();
}
See the program will show the ASCII code of the two characters a and B, also notice that there %d is used to print the integer value of the ASCII codes of that two characters.

  • 1





Also tagged with one or more of these keywords: c, beginner, fresher, variable, datatype, format specifier, commenting