Unit 1
Unit 1
                                                                                    Example:
                                                                                          #include<stdio.h>
              void main()                                                         Format String        Meaning
          {
               double x = 1.2;                                                    %d                   Scan or print an integer assigned decimal number
              // Explicit conversion from double to int
                       int sum = (int)x + 1;                                      %f                   Scan or print a floating point number
                     printf("sum = %d", sum);                                     %c                   scan or print a character
                }                                                                 %s                   scan or print a character string. The scanning ends at
Output:                                                                                                whitespace.
                    sum = 2
MANAGING INPUT AND OUTPUT OPERATIONS
                                                                                  Example: program to print cube of given number
    C programming language provides many built-in functions to read any                #include<stdio.h>void main()
       given inputand to display data on screen as output, the result.                  {
    Input means to provide the program with some data to be used in the                        int number;
       program                                                                                  printf("Enter a number:");scanf("%d",&number);
    Output means to display data on screen or write the data to a printer or a                 printf("Cube of number is:%d ",number*number*number);
       file.                                                                            }
    All the built-in functions are present in C header files                           Output:
INPUT AND OUTPUT FUNCTIONS IN C LANGUAGE                                                        Enter a number 5
    1.   scanf( )                                                                               Cube of number is       : 125
    2.   printf( )
    3.   getchar( )                                                               getchar( ) & putchar( ) functions
    4.   putchar( )                                                                The getchar( ) function reads a single character from the terminal and
    5.   gets( )                                                                      returns it asan integer.
                                                                                   The putchar( ) function displays the single character passed to it on the
    6. puts( )
                                                                                      screenand returns the same character.
scanf( ) and printf( ) functions
                                                                                  Example : program for getchar( ) and putchar( ) function
     The standard input-output header file, named stdio.h contains the                    #include <stdio.h>
      definition of thefunctions scanf( ) and printf( ) .                                  void main( )
     Input function takes input from user and displays output on screen                   {
      respectively.                                                                                int c;
     They are known as inbuilt library functions.                                                 printf("Enter a character :");
                                                                                                   c = getchar( );          /* Take a character as input and store it
         The syntax of scanf( ) function is                                                        in variable c*/
                                                                                                   putchar(c);              /*display the character stored in variable
                   scanf("format string",argument_list);                                           c */
                                                                                           }
         The syntax of printf( ) function is                                      gets( ) function
                  printf("format string",argument_list);
     The gets( ) function enables the user to enter some characters                    Simple if statement
      followed by theenter key.                                                         if..else statements
                                                                                        nested if statements
     All the characters entered by the user get stored in a character array.           else-if ladder
     The gets() allows the user to enter the space-separated strings. It          Simple if statement
      returns the stringentered by the user.                                            If statement is always used with a condition.
                                                                                        The condition is evaluated first before executing any statement inside the
     Syntax :           gets(char[]);                                                      body of it.
                                                                                     Syntax :
puts( ) function                                                                           if (test expression)
                                                                                                     {
     The puts() function is used to print the string on the console which is                                      statement block;
        previously readby using gets() or scanf() function.
                                                                                                    }
Program to read a string using gets( ) and print it on the console using puts( )           statement n;
#include<stdio.h> #include<string.h>                                               Example:
void main()                                                                        #include <stdio.h> void main()
{                                                                                  {
        char name[50]; printf("Enter your name: ");                                        int x = 20, y = 22;
        gets(name); //reads string from userprintf("Your name is: ");                      if (x<y)
        puts(name); //displays string                                                        {
}                                                                                                   printf("Variable x is less than y");
Output:                                                                                        }
        Enter your name : S. Hariharsudhan                                         }
        Your name is S. Hariharsudhan
DECISION MAKING AND BRANCHING                                                      Output: Variable x is less than y
                                                                                   if …else statement
     It supports sequential program statements which execute one                       The if-else is statement is an extended version of if statement.
        statement immediately after another.                                            It is used to execute the code if the condition is true or false.
     C language handles decision-making by supporting the following                    It is also called a two-way selection statement.
      statements:                                                                  Syntax:
          o if statement                                                                                  if (test-expression)
            o      switch statement                                                                   {
            o      conditional operator statement (? : operator)
                                                                                                               True block of statements
            o goto statement                                                                                        }
if statement                                                                                   else
                    {                                                                 When a series of the decision are involved in a statement, use the if-else
                                                                                        statement in nested form.
                           False block of statements
                                                                                     Syntax:
                           }                                                            if( expression )
Statements;                                                                               {
                                                                                                 if( expression1 )
Explanation:                                                                                       {
     If the value of test-expression is true, then the true block of statements                          statement-block1;
       will be executed.                                                                            }
     If the value of test-expression if false, then the false block of statements               else
       will be executed.                                                                            {
     In any case, after the execution, the control will be automatically                                 statement-block 2;
       transferred to the statements appearing outside the block of If.
                                                                                                    }
Example:                                                                                    }
#include<stdio.h>                                                                       else
void main()                                                                                {
{                                                                                                statement-block 3;
        int age;                                                                              }
        printf("Enter your age:"); scanf("%d",&age);                                 Example:
        if(age >=18)
        {                                                                            #include<stdio.h>
        }                                                                            void main( )
        else                                                                         {
                                                                                         int a,b,c;
        {
        printf("You are not eligible for voting");                                       clrscr();