Basic codes for starting C programming.


In this article, i will be explaining about the basic codes necessary for writing your first C program. I will explain you about performing basic arithmetic operations in C language. C programming is the first step towards high-level programming languages and becoming a programmer.

Most of students out there face a lot of difficulty when they come to know about C programming. C is the most basic programming language which you need to learn before getting into more difficult programming languages. I won't be giving a long lecture on the C programming but try to help you in learning the basic codes you need to know for starting programming. This article is for those students who haven't attended the first one or two classes in school/college or those who want to learn the extreme basics of C programming i.e. basic codes for the the program and logic for arithmetic operations.

  • #includestdio.h- This is the first thing your teacher writes while writing a program. "#include" is called a pre-processor directive which must be used at the beginning of the program i.e. for showing the output of your program you need to type this code in the beginning. You will get a clear idea about this statement in much later classes (you can go through the chapter named "C Pre-processors" in your book).

  • main()- It is nothing but a function or set of statements. Only a single statement can be used while writing a program at this level. All statements that belong to main() should be typed within a pair of brace {}. This statement will return an integer type value if we type "int" before main() and it can even permit the return of no value when used with "void".

  • int, float- This statement is used to declare variable for compiling the program. "int" is used to declare integer type variable for a program. "float" is used to declare a variable in which we can do calculation up to decimal places. %d is the code used to declare and print integer type variable and %f for float type variables.

  • printf- This code is used to print the outputs or the statements we want the user to see.

  • scanf- This statement is used for taking the input from the user. A single ampersand(&) is used in scanf to take the input from user and assign it to a declared variable.

  • \n- This code is used to print a new line.

First C program


Take an input of numbers from user and print it.

int main()

{

int a;

printf("Enter the numbers.");

scanf("%d",&a);

printf("The numbers are %d",a);

}

This program is the application of only the codes that i explained above. If you compile the program and type the input:12345, the output that will be shown is:12345.

Basic Arithmetic Operations


Perform the basic arithmetic taking two inputs from the user.

int main()

{

float a,b,add,sub,mul,div;

printf("Enter the two numbers");

scanf("%f%f",&a,&b);

add=a+b;

sub=a-b;

mul=a*b;

div=a/b;

printf("The sum=%f, sub=%f,mul=%f,div=%f",add,sub,mul,div);

}

If the input provided is 12 and 4, the output will be add=16, sub=8, mul=48, div=3.

I hope that this program will help you in understanding the basic logic behind performing the arithmetic operations. Now, you will be able to perform arithmetic problems if you develop the logic for the programs.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: