Let's Get Started With C : [ Start Writing Programs ]

Writing Programs In C


[A]
Write a program to display “Hello My First Program In C” message on screen.

#include <stdio.h>

void main()

{

    printf ("Hello My First Program In C");  
 

}
Explanation

   1. 
#include – 

# is a pre-processor that is all pre-defined terms will be pre-processed before user-defined code's compilation. 

#include is the pre-processor 
directive that  used to include pre-defined library files in program.
   
   2.  studio.h – 

It is a library file also known as header file, provides list of pre-defined methods to be used in program. Every header file has extension .h. We can also create user-define header files. 

In the given example printf() 
is defined in the “studio.h” header file.There are many other methods available in studio.h file, we will discuss them later.

There are two different ways to include library files in program.

What is the difference between #include <studio.h> and #include "studio.h"

File name inside the < > means compiler searches file in standard 
directory of C.

File name declaration within “ “ means compiler searches file in specific file's directory.

   3.  void main() – 

C is a modular programming language so that C provides its own module name – “main() “.

Parenthesis indicates that it is a module.Every module is to perform any task and it may return its result to other module as well.

What is the difference between void main() and int main() in C.

If module/function is not returning any value, it will be defined by "void" keyword.
 

Example: void main() – Here main is not returning any value.

If module/function is returning any value to another method then it 
should have corresponding data-type declaration.

Example: int main() – Here main is returning any integer value.

Main method returns the status of program execution to the Operating System.
"0" is the standard value for success.

#include "studio.h"  // example of - library file within " "

int main()

{

printf ("Hello My First Program In C");

           
return 0;   


} 
Note: // is single line comment, it is avoidable by compiler.

Compile And Execute The Program -

Press Alt+F9 to compile - 

Compiler will check if is there any error it will report other wise program will be compiled successfully.  

Press Ctrl+F9 to run  

Program will be executed successfully.

Press Alt+F5 to see output -

Hello My First Program In C
       
[B]
Write a program to ask two numbers from user and find the sum.
#include <stdio.h>

void main()

{

    int num1, num2 , sum ;


    printf ("Enter 1st number");
   
    scanf("%d", &num1);


    printf ("Enter 2nd number");

    scanf("%d", &num2);


    sum = num1 + num2;


    printf ("Sum of the numbers%d ",sum);   

}
Explanation

1. Variable declaration


In this program we are asking two numbers from user and after that will get sum of those numbers. To store numbers in memory, we have to define 3 variables (2 for two numbers and 1 for sum). 

   
These variables are storing numeric values so that datatype would be of int type.
Every line will be ended with ; 

2. Format Specifier


When we store value into variable or when we get value from variable in both cases we have to define variable's value type by using format specifiers. 


For int %d is used. & indicating the address of variable.

   
3. Scanf() and Printf()


Both methods are available in studio.h header file.

scanf() - used to store value into memory.


printf() - used to print something.



// asking int value from user and storing into num1.

scanf("%d", &num1); 


// printing message and getting int value from num1.

printf ("Sum of the numbers%d ",sum);   

OUTPUT -
Enter 1st number - 4  

Enter 2nd number - 3


Sum of the numbers - 7  


[C]

Write a program to enter a string value and print it on screen.
Example 1 -

#include <stdio.h>

void main()

{

    char name[20];


    printf ("Enter your name");

    scanf("%s",name);


    printf ("My name is-%s",name);


}
In C, string is the set of characters known as "character's array" %s format specifier is used to enter string value.

It is the array of characters so that every character will store in same array.

If char array is of size 10 then string will store in array from 0 to 9 position.

last position will hold null character (\0), it indicates that string is terminated.

scanf method use pointer variable for string that is why we don't need to give with string variable. 




 String Representation In C. ByTechAchievers.com


Problem with set of characters in C :-


When you will run the above program you will see that C does not accept space.


Example : Enter your name - My name is ABC

                  My name is- My

We have entered My name is ABC but output is "My". Here we can make some change in scanf() or with use of gets() method to store string into memory.


 
scanf("%[^\n],s);


 OR


 gets (name);

Both changes accept set of characters until user press enter or new line but again problem comes, in C there is no in-build feature that will protect invalid data-entry, C will not stop you until you press enter key. 

This will create buffer-overflow situation and extra values can be used by some other memory allocations which is not good.


To see this problem enter name more than 10 characters, you will see, buffer is accepting all the characters until you press enter key.


To solve this problem C provides fgets() method, here you mention size limit and buffer will not accept extra characters.

 
fgets(name,20,stdin);

What is difference in scanf, gets and fgets method while storing string in C.

Scanf() does not accept spaces , gets() method creates buffer-overflow situation that is why it is dangerous to use and fgets() controls over the limit of characters being entered. So that fgets is best method to enter string values in C.



Thanks for reading, please do share and comment your suggestions.

No comments

Powered by Blogger.