Page 1 of 1

C Part II: Hello World & Variables Rate Topic: -----

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 11 September 2009 - 09:55 PM

C Part II: Hello World & Variables

So, hopefully you've read C Part I: Introduction To C, and got a development environment set up, In this tutorial I’m going to cover The Hello World Program and Variables.

Topics:
• Hello World
• Variables
• Variable Naming
• Variable Declaring and using.


The Hello World! Program:
If you are using Code::Blockes, then Code::Blockes is creating by default a Hello World program when you are creating a new project, and if you are using other compilers then use this code:

main()
{
	printf(“Hello World!”);
}


Our every program starts with main() function, and the compilers are reading first main function.
Then brackets “{}”, it is the body of a function and our all code will be between brackets.
then printf(): this function is printing the given value.

Type this code in your compiler and press F9 to Compile and Run it you will see a black screen and then disappear, why it is doing that? It is printing the hello world in that screen and then disappearing back, because there is nothing more for it to do, then how do we know that our program compiled successfully and works fine. Now we are going to add another line of code to check to code and that line of code is return 0;

The return Statement
All functions in C can return values. For instance, when you make a function to add two numbers, you can make such a function that returns to you the value of the addition.
The main() function itself returns a value. By default, main() returns an integer. In C, integers are decimal numbers without fraction portions.
Therefore, in line 7 of Listing 2.1, there is a statement, return 0;, that indicates that 0 is returned from the main() function and the program is terminated normally.
A nonzero value returned by the return statement tells the operating system that an error has occurred. The bigger the return value, the more severe the error.

main()
{
	printf(“Hello World!”);
	return 0;
}





Variables:
Variables are very important features in C, or any other programming language. A variable is where you can store information and retrieve it (in computer terms, a variable refers to an area of computer memory where the variable’s value is stored). You must give the variable a name, which you then use when storing or retrieving information from that variable. The name you use for a variable can be anything that you choose, subject to a few restrictions:
• Variable names can only contain letters, numbers, and the underscore ‘_’ character. They cannot contain spaces or punctuation.
• Variable names cannot start with a number.
• Variable names must not clash with other variable names – you cannot use a variable name if it’s already in use in the same part of the program (this restriction isn’t as severe as it may seem, for reasons that will become apparent).
To help the computer set aside the right amount of memory for the variable, C insists that you specify a data type in addition to the variable name. A data type, as the name implies, denotes what type of data the variable will hold. There are many different data types in C; but some examples of the most commonly-used types are:

Posted Image

Variable Naming:
It must be Unique, only contains the characters: A-Z, a-z, 0-9 and “_”,
Must not start with a digit, must not be longer than 31 characters

Variable Using:
Variables must be declare before they set and must set before they read,
Variables names are Case-sensitive myname is not equal to Myname

Variable Declaring:
Must be declare before use, declaring at the top of a function is a good practice;
Variables are declared as follow:
Datatype-name (“-“ is space) int age;

Example:
//Variables
main()
{
//datatype-name
int 		   age;	//declareing a variable (integer)
age = 21;	   	//initializing a value
printf("I am %d years old",age); //pring out the value of variable
getchar();
}



Type the code in your Code::Blockes or DevC++ compiler and press F9 to compiled it,
Print out: I am 21 years old


We discussed About main(){} in our previews Tutorials,

int age: we declared an integer variable and named that age now we can store and retrieve value from age variable

age = 21: science before reading value from variable we must initialize it I am initializing a value of 21 and integer type (because our age datatype is int).

printf() : this function is printing the given value, and if we want to print a variable we need to add a parameter, and parameter for integer datatype is %d, now the code looks like this printf(“I am %d years old”,age);

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Page 1 of 1