2. C – Variables

/* Note multi line/block commenting starts with "/*"
   and single line commenting starts with "//"

   This program is to demonstrate the variable declarations

*/

// stdio.h header file is included so we can use the "printf" function
// Try commenting or removing the #include<stdio.h> line from
// code and see what error it produces ( one other way to learn ) 
#include<stdio.h> 
void main() // as usual we always need a main method
{
   int age;
   char firstname[20] = "Tiger";
   char lastname[20] = "Chan";
   age = 18;

   printf("FirstName :%s ", firstname); //use %s for character array or string in printf.
   printf("LastName :%s ", lastname);

   printf("Age:%d",age); // use %d or %i for integer variables in printf

}



Output: without printf(“\n”) for new line

Cleaner code without comments but using “printf(“\n”) for new line.

#include<stdio.h> 
void main() // as usual we always need a main method
{
   int age;
   char firstname[20] = "Tiger";
   char lastname[20] = "Chan";
   age = 18;

   printf("FirstName :%s ", firstname); //use %s for character array or string in printf.
   printf("\n");
   printf("LastName :%s ", lastname);
   printf("\n");
   printf("Age:%d",age); // use %d or %i for integer variables in printf

}

Output : with “printf(“\n”)

%d bloggers like this: