Jump to content

Learn C programming from all angles-Part 1

- - - - -

  • Please log in to reply
No replies to this topic

#1
Vinod Takarkhede

Vinod Takarkhede

    Newbie

  • Members
  • Pip
  • 6 posts
Have a look at First C program

Welcome to series of C programming’s first stone of learning path. I will write series of tutorials to learn from Basic C programming to Advance C programming. I strongly recommend you to use Linux or UNIX operating system to learn C programming in detail. Along with C programming I will also cover some UNIX and Linux Concepts.
In this tutorial I cover basic understanding of C Programming. C program is nothing but a collection of functions. It is early to say about function. I will cover function when it require in subsequent tutorials.


First program
1 #include <stdio.h>
2 int main()
3 {
4 int = printf(“Hello”);
5 return 0;
6 }

Dissection of source code

Line1: #include <stdio.h>
System header files are included using the preprocessing directive “#include”. In our program we used stdio.h because printf library function is used in our program to display “hello” in standard output. stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations.

Line 2: int main()
Every C program should contain atleast one function and name of that function is main ().When C program runs operating system calls this function. This function is the starting point of execution.
Int is written before main function. Int is data types. Size of integer is 4 bytes in 32 bit machine and 2 bytes in 16 bit machine. In the programming languages C and C++, the unary operator 'sizeof' is used to calculate the sizes of data types, in number of bytes.
Meaning of first line is that main is a function which returns value of type integer and taking zero arguments. Now the question arise main function returns value but it returns to whom?
The answer is this function will return value of type integer to operating system because function return values to place from where it is being called. And there is no other place from where executable runs other than operating system.

Line 3: {

Every function has pair of braces ({}) which denotes starting and ending scope of a function.
In this line opening braces (“{“ ) starts the scope of function.

Line 4: int = printf (“Hello”);

printf() is a library function which takes input parameter as “hello” and returns number of characters to display in standard output. In this case variable J store value 5.
Printf function is defined in header file stdio.h


Line 5:
Return statement return integer value 0 to the operating system.

Line 6:
Closing braces closes the scope of function.





How to compile and run program
There are so many compilers available from various vendors. We will use gcc compiler to demonstrate this tutorial.
Let us say name of source code is hello.c.
$ gcc hello.c
It will compile and create executable file named a.out.
a.out is the default executable file name .
if we run again another C program then a.out will overwrite this a.out file.
To create user defined executable file we need to provide option to gcc as shown in following line
$gcc –o myexe hello.c
So “myexe” is an executable name created after successful compilation of hello.c




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users