Tuesday, 22 January 2013

Example of how C function works:



As mentioned earlier, every C program begins from main() and program starts executing the codes inside main function. When the control of program reaches to function_name() inside main. The control of program jumps to “void function_name()” and executes the codes inside it. When, all the code inside that user defined function is executed control of the program jumps to statement just below it. Analyze the figure below for understanding the concept of function in C.



Remember, the function name is an identifier and should be unique.

Advantage of User Defined Functions:

·         User Defined functions helps to decompose the large program into small segments which makes programmer easy to understand, maintain and debug.
·         If repeated code occurs in a program. Function can be used to include those codes and execute when needed by calling that function.
Programmer working on large project can divide the workload by making different functions.

Monday, 21 January 2013

Function


A function in programming is a segment that groups a number of program statements to perform specific task.
A c program has at least one function [main()]. Without main(), there is technically no C program.

Types of Function



Basically, there are two types of functions in C on basis of whether it is defined by user or not.

·     Library Function
·         User Defined Function



Library Function

Library functions are the in-built function in C programming system.
For Example:


main()
- The execution of every C program starts from this main().

printf()
- prinf() is used for displaying output in C.

scanf()
- scanf()  is used for taking input in C.

Visit this page to learn more about library function in C programming.


User Defined Function

C provides programmer to define their own function according to their requirement known as user defined functions.

Sunday, 20 January 2013

The First C Program in Three Different ways



“Hello World program”

C Hello World Program: - C programming language code to print Hello World. This program print Hello World, printf function is used to display text screen, ‘\n’ places cursor on the beginning of next line, stdio.h header file contains declaration of printf function, getch is used to hold screen. The code will work on all operating systems may be its Windows, Linux, Mac or any other and compilers. To learn a programming language you must start writing programs in it and may be your first C code while learning programming.

Example:
1 – C Hello World

#include<stdio.h>
int main()
{
     printf(“Hello World”);
     return 0;
}

2 – Hello World program in C

      We may store “Hello World” in a character array and then print it.

#include<stdio.h>
int main()
{
     char string[] = “Hello World”;
     printf(‘%s\n”,string);
     return 0;
}

3 – Print text “Hello World” Program in C

/* To Print any Text */
#include<stdio.h>
void main()
{
     printf(“Hello World”);
     getch();
}

OUTPUT
Hello World

Friday, 18 January 2013

Stay Connected

Please Stay connected with this blog to get more updates about C/C++ concepts and programming.

Compilation and Execution



There are several such IDEs available in the market targeted towards different operating systems. For example, Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS; Visual C++ and Borland C++ are the compilers that work under Windows, whereas gcc compiler works under Linux. Note that Turbo C++, Microsoft C++ and Borland C++ software also contain a C compiler bundled with them. If you are a beginner you would be better off using a simple compiler like Turbo C or Turbo C++. Once you have mastered the language elements you can then switch over to more sophisticated compilers like Visual C++ under Windows or gcc under Linux.
Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program…
a)      Start the compiler at C> prompt. The compiler (TC.EXE is usually present in C:\TC\BIN directory).
b)      Select New from the File menu.
c)      Type the program.
d)      Save the program using F2 under a proper name (say Program1.c).
e)      Use Ctrl + F9 to compile and execute the program.
f)       Use Alt + F5 to view the output.

Note that on compiling the program its machine language equivalent is stored as an EXE file (Program1.EXE) on the disk. This file is called an executable file.

Variables


An entity that may vary during program execution is called a variable.

Rules for Constructing Variable Names
a)      A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your typing effort.
b)      The first character in the variable name must be an alphabet or underscore.
c)      No commas or blanks are allowed within a variable name.
d)      No special symbol other than an underscore (as in gross_sal) can be used in a variable name.
Ex.: si_int
m_hra
pop_e_89 

Constants


A constant is an entity that doesn’t change whereas a variable is an entity that may change.

Rules for Constructing Integer Constants
a)      An integer constant must have at least one digit.
b)      It must not have a decimal point.
c)      It can be either positive or negative.
d)      If no sign precedes an integer constant it is assumed to be positive.
e)      No commas or blanks are allowed within an integer constant.
f)       The allowable range for integer constants is -32768 to 32767.

Rules for Constructing Real/Float Constants
Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form.
Following rules must be observed while constructing real constants expressed in fractional form:
a)      A real constant must have at least one digit.
b)      It must have a decimal point.
c)      It could be either positive or negative.
d)      Default sign is positive.
e)      No commas or blanks are allowed within a real constant.

Ex.: +630.58
76.0
-81.36
-12.6732

Rules for Constructing Character Constants
a)      A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not.
b)      The maximum length of a character constant can be 1 character.

e.g..: 'A'
'D'
'7'
'=' 

Getting Started With C


A straight-away learning how to write programs, we must first know what alphabets, numbers and special symbols are used in C, then how using them constants, variables and keywords are constructed, and finally how are these combined to form an instruction. A group of instructions would be combined later on to form a program. This is illustrated in the Figure 1.1. 


Introduction of C


Before we can begin to write serious programs in C, it would be interesting to find out what really is C, how it came into existence and how does it compare with other computer languages. In this Blog we would briefly outline these issues.
Four important aspects of any language are the way it stores data, the way it operates upon this data, how it accomplishes input and output and how it lets you control the sequence of execution of instructions in a program.

What Is C ?
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. No one pushed C. It wasn’t made the ‘official’ Bell Labs language. Thus, without any advertisement C’s reputation spread and its pool of users grew. Ritchie seems to have been rather surprised that so many programmers preferred C to older languages like FORTRAN or PL/I, or the newer ones like Pascal and APL. But, that's what happened.
Possibly why C seems so popular is because it is reliable, simple and easy to use. Moreover, in an industry where newer languages, tools and technologies emerge and vanish day in and day out, a language that has survived for more than 3 decades has to be really good.