Basic Program in C language

   We are going to learn a simple “Hello World” C program in this section. Also, all the below topics are explained in this section which are the basics of a C program.

    1. C basic program with output and explanation
    2. Steps to write C programs and get the output
    3. Creation, Compilation and Execution of a C program
      • How to install C compiler and IDE
    4. Basic structure of a C program
1. Basic Program in c Language:

Output:

Hello World!

Explanation for above C basic Program:

Let’s see all the sections of the above simple C program line by line.
S.noCommandExplanation
1#include <stdio.h>This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program
2int main()This is the main function from where execution of any C program begins.
3{This indicates the beginning of the main function.
4/*_some_comments_*/whatever is given inside the command “/*   */” in any C program, won’t be considered for compilation and execution.
5printf(“Hello_World! “);printf command prints the output onto the screen.
6getch();This command waits for any character input from keyboard.
7return 0;
This command terminates C program (main function) and returns 0.
8}
This indicates the end of the main function.

2. Steps to write programs in C language and get the output:

      Below are the steps to be followed for any C program to create and get the output. This is common to all C program and there is no exception whether its a very small C program or very large C program.
C Basic program

3. Creation, Compilation and Execution of a C program:

Prerequisite:
    • If  you want to create, compile and execute C programs by your own, you have to install C compiler in your machine. Then, you can start to execute your own C programs in your machine.
    • You can refer below link for how to install C compiler and compile and execute C programs in your machine.
    • Once C compiler is installed in your machine, you can create, compile and execute C programs as shown in below link.

C – Environment Setup Using IDE tool

C – Environment Setup Using GCC compiler

4. Basic structure of C program:

     Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned  below.
    1. Documentation section
    2. Link Section
    3. Definition Section
    4. Global declaration section
    5. Function prototype declaration section
    6. Main function
    7. User defined function definition section

Example C program to compare all the sections:

You can compare all the sections of a C program with the below C program.

Output:

This is a C basic program
Sum of two numbers : 2

Description for each section of a C language program:

    • Let us see about each section of a C basic program in detail below.
    • Please note that a C program mayn’t have all below mentioned sections except main function and link sections.
    • Also, a C program structure mayn’t be in below mentioned order.
S.NoSectionsDescription
1Documentation sectionWe can give comments about the program, creation or modified date, author name etc in this section. The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation.
Example : /* comment line1 comment line2 comment 3 */
2Link SectionHeader files that are required to execute a C program are included in this section
3Definition SectionIn this section, variables are defined and values are set to these variables.
4Global declaration sectionGlobal variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section.
5Function prototype declaration sectionFunction prototype gives many information about a function like return type, parameter names used inside the function.
6Main functionEvery C program is started from main function and this function contains two major sections called declaration section and executable section.
7User defined function sectionUser can define their own functions in this section which perform particular task as per the user requirement.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

C programming Language History

C Language History
  • C language is a structure oriented programming language, was developed at Bell Laboratories in 1972 by Dennis Ritchie
  • C language features were derived from earlier language called “B” (Basic Combined Programming Language – BCPL)
  • C language was invented for implementing UNIX operating system
  • In 1978, Dennis Ritchie and Brian Kernighan published the first edition  “The C Programming Language” and commonly known as K&R C
  • In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or “ANSI C”, was completed late 1988.
C standards
  • C89/C90 standard – First standardized specification for C language was developed by American National Standards Institute in 1989. C89 and C90 standards refer to the same programming language.
  • C99 standard – Next revision was published in 1999 that introduced new futures like advanced data types and other changes.

C11 and Embedded C

  • C11 standard adds new features to C and library like type generic macros, anonymous structures, improved Unicode support, atomic operations, multi-threading, and bounds-checked functions. It also makes some portions of the existing C99 library optional, and improves compatibility with C++.
  • Embedded C includes features not available in normal C like fixed-point arithmetic, named address spaces, and basic I/O hardware addressing
  • Operating systems, C compiler and all UNIX application programs are written in C language
  • It is also called as procedure oriented programming language
  • C language is reliable, simple and easy to use.
  • C has been coded in assembly language

Features of C language:

  • Reliability
  • Portability
  • Flexibility
  • Interactivity
  • Modularity
  • Efficiency and Effectiveness

Uses of C language:

C language is used for developing system applications that forms major portion of operating systems such as Windows, UNIX and Linux. Below are some examples of C being used.
  • Database systems
  • Graphics packages
  • Word processors
  • Spread sheets
  • Operating system development
  • Compilers and Assemblers
  • Network drivers
  • Interpreters

Which level the C language is belonging to?

S.no
High Level
Middle Level
Low Level
1High level languages provides almost everything that the programmer might need to do as already built into the languageMiddle level languages don’t provide all the built-in functions found in high level languages, but provides all building blocks that we need to produce the result we wantLow level languages provides nothing other than access to the machines basic instruction set
2Examples:
Java, Python
C, C++Assembler

C language is a structured language

S.no
Structure oriented
Object oriented
Non structure
1In this type of language, large programs are divided into small programs called functionsIn this type of language, programs are divided into objectsThere is no specific structure for programming this language
2Prime focus is on functions and procedures that operate on dataPrime focus is on the data that is being operated and not on the functions or proceduresN/A
3Data moves freely around the systems from one function to anotherData is hidden and cannot be accessed by external functionsN/A
4Program structure follows “Top Down Approach”Program structure follows “Bottom UP Approach”N/A
5
Examples:
C, Pascal, ALGOL and Modula-2
C++, JAVA and C# (C sharp)
 BASIC, COBOL, FORTRAN

Key points to remember:

  1. C language is structured, middle level programming language developed by Dennis Ritchie
  2. Operating system programs such as Windows, Unix, Linux are wriiten by C language
  3. C89/C90 and C99 are two standardized editions of C language
  4. C has been written in assembly language

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Operators in C programming

Frequently asked technical Operator questions of placement in  c programming language
1.What will be output of the following program?

#include<stdio.h>
int main(){
    float a=0.7;d 
    if(a<0.7){
         printf("C");
    }
    else{
         printf("C++");
    }
    return 0;
}

Answer:


Turbo C++ 3.0: c
Turbo C ++4.5: c
Linux GCC: c
Visual C++: c

Explanation: 0.7 is double constant (Default). Its binary value is written in 64 bit.
Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 )
Now here variable a is a floating point variable while 0.7 is double constant. So variable a will contain only 32 bit value i.e.
a = 0.1011 0011 0011 0011 0011 0011 0011 0011 while
0.7 = 0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011....
It is obvious a < 0.7


2.What will be output of the following program?

#include<stdio.h>
int main(){
    int a=2,b=7,c=10;
    c=a==b;
    printf("%d",c);
    return 0;
}

Answer:

Output: 
Turbo C++ 3.0: 0


Turbo C ++4.5: 0


Linux GCC: 0


Visual C++: 0



Explanation:
== is relational operator which returns only two values.
0: If a == b is false
1: If a == b is true
Since
a=2
b=7
So, a == b is false hence b=0
3.What will be output of the following program?
        
#include<stdio.h>
int main(){
    int i=5,j;
    j=++i+++i+++i;
    printf("%d %d",i,j);
    return 0;
}
Answer:

Output: 
Turbo C++ 3.0: 8 24


Turbo C ++4.5: Compilation error


Linux GCC: Compilation error


Visual C++: Compilation error



Explanation:

Rule :- ++ is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole expression then starts assigning the final value of variable in the expression.

Compiler will treat this expression j = ++i+++i+++i; as
i = ++i + ++i + ++i;

Initial value of i = 5 due to three pre increment operator final value of i=8.
Now final value of i i.e. 8 
So, j=8+8+8
j=24 and
i=8



www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Data types in c programming

Data types in c programming
The data type of the variable are determines how many bytes of memory are dedicated to that variable, and what kind of data can be stored in the variable. As you will learn later in this chapter, there are two basic types of data: value types and reference types. If the variable is defined as a value type, then there is a very specific range of values possible.
A list of the basic value data types are in Table 3-1. 


                                                              Table 3-1.C Value Data Types

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community