Ticker

6/recent/ticker-posts

CPNM ASSIGNMENTS


ASSIGNMENT :- 1

Q1. What is the basic structure of C program ?

(A).
SectionDescription
DocumentationConsists of the description of the program, programmer's name, and creation date. These are generally written in the form of comments.
LinkAll header files are included in this section which contains different functions from the libraries. A copy of these header files is inserted into your code before compilation.
DefinitionIncludes preprocessor directive, which contains symbolic constants. E.g.: #define allows us to use constants in our code. It replaces all the constants with its value in the code.
Global DeclarationIncludes declaration of global variables, function declarations, static global variables, and functions.
Main() FunctionFor every C program, the execution starts from the main() function. It is mandatory to include a main() function in every C program.
SubprogramsIncludes all user-defined functions (functions the user provides). They can contain the inbuilt functions, and the function definitions declared in the Global Declaration section. These are called in the main() function.

Q2. Describe the data types of C ?

(A).
Types
Data Types
Basic data types int, char, float, double
Enumeration data type enum
Derived data type pointer, array, structure, union
Void data type void

Basic types

Type Size (bytes) Format Specifier
int at least 2, usually 4 %d, %i
char 1 %c
float 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %ld, %li
long long int at least 8 %lld, %lli
unsigned long int at least 4 %lu
unsigned long long int at least 8 %llu
signed char 1 %c
unsigned char 1 %c
long double at least 10, usually 12 or 16 %Lf

 DERIVED DATA TYPE IN C LANGUAGE:

  • Array, pointer, structure and union are called derived data type in C language.

ENUMERATION DATA TYPE IN C LANGUAGE:

  • Enumeration data type consists of named integer constants as a list.
  • It start with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list.
  • Enum syntax in C:
    enum identifier [optional{ enumerator-list }];

VOID DATA TYPE IN C LANGUAGE:

  • Void is an empty data type that has no value.
  • This can be used in functions and pointers.

Q3. Distinguish between signed and unsigned data types in C

(A). 

Parameters of ComparisonSignedUnsigned
Values IncludedSigned data categories include both positive and negative integers.Unsigned data categories include only zero and other positive integers. They cannot include negative integers.
MagnitudeSigned integers have a smaller magnitude than their unsigned counterparts of the same range.Unsigned integers have a greater magnitude than their signed counterparts of the same range.
Flag SignSigned data types use a flag sign before the negative numbers they represent.Unsigned data types do not use a flag sign before numbers, as they only represent positive integers.
Process of IdentificationThe leftover bit is used by the signed data containers.The leading bit of a value is used by the unsigned data containers.
Range in CharSigned integers range from -128 to 127 in chars.Unsigned integers range from 0 to 255 in chars.
Representation Method1’s complement form, 2’s complement form, and the sign-magnitude form methods can be used to represent signed binary variables.Unsigned binary variables do not have a preceding sign or symbol, and thus, there exists only one representation method for such binary variables.
Unambiguous Method of Representation1 out of 3 possible methods of representations is unambiguous.The only method of representation available is an unambiguous one.

Q4. Describe type conversions in C language ?

(A).
       

Type Conversion in C

A type cast is basically a conversion from one type to another. There are two types of type conversion:

  1. Implicit Type Conversion

    Also known as ‘automatic type conversion’.

    • Done by the compiler on its own, without any external trigger from the user.
    • Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid loss of data.
    • All the data types of the variables are upgraded to the data type of the variable with largest data type.
          
             bool -> char -> short int -> int -> 
             unsigned int -> long -> unsigned -> 
             long long -> float -> double -> long double
      
    • It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).

    Example of Type Implicit Conversion:

    // An example of implicit conversion
    #include<stdio.h>
    int main()
    {
        int x = 10;    // integer x
        char y = 'a'// character c
      
        // y implicitly converted to int. ASCII 
        // value of 'a' is 97
        x = x + y;
         
        // x is implicitly converted to float
        float z = x + 1.0;
      
        printf("x = %d, z = %f", x, z);
        return 0;
    }

    Output:

    x = 107, z = 108.000000
  1. Explicit Type Conversion

    This process is also called type casting and it is user defined. Here the user can type cast the result to make it of a particular data type.

    The syntax in C:

    (type) expression

    Type indicated the data type to which the final result is converted.

    // C program to demonstrate explicit type casting
    #include<stdio.h>
      
    int main()
    {
        double x = 1.2;
      
        // Explicit conversion from double to int
        int sum = (int)x + 1;
      
        printf("sum = %d", sum);
      
        return 0;
    }

    Output:

    sum = 2

    Advantages of Type Conversion

    • This is done to take advantage of certain features of type hierarchies or type representations.
    • It helps us to compute expressions containing variables of different data types
Q5. Illustrate the formatted input and formatted output in C?
(A).

Formatted I/O Functions

Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the user. These types of I/O functions can help to display the output to the user in different formats using the format specifiers. These I/O supports all data types like int, float, char, and many more.

Why they are called formatted I/O?  

These functions are called formatted I/O functions because we can use format specifiers in these functions and hence, we can format these functions according to our needs.

List of some format specifiers-

S NO.Format Specifier          Type            Description                                                    
1%dint/signed intused for I/O signed integer value
2%ccharUsed for I/O character value
3%ffloatUsed for I/O decimal floating-point value        
4%sstringUsed for I/O string/group of characters                                                       
5%ldlong intUsed for I/O long signed integer value
6%uunsigned int   Used for I/O unsigned integer value
7%iunsigned intused for the I/O integer value
8%lfdoubleUsed for I/O fractional or floating data 
9%nprintsprints nothing 

The following formatted I/O functions will be discussed in this section-

  1. printf()
  2. scanf()
  3. sprintf()
  4. sscanf()

printf():

printf() function is used in a C program to display any value like float, integer, character, string, etc on the console screen. It is a pre-defined function that is already declared in the stdio.h(header file). 

Syntax 1: 

To display any variable value.

printf(“Format Specifier”, var1, var2, …., varn);  

Example:

// C program to implement
// printf() function
#include <stdio.h>
  
// Driver code
int main()
{
    // Declaring an int type variable
    int a;
  
    // Assigning a value in a variable
    a = 20;
  
    // Printing the value of a variable
    printf("%d", a);
  
    return 0;
}
Output
20

Syntax 2:  

To display any string or a message

printf(“Enter the text which you want to display”);

Example:

// C program to implement
// printf() function
#include <stdio.h>
  
// Driver code
int main()
{
    // Displays the string written
    // inside the double quotes
    printf("This is a string");
    return 0;
}
Output
This is a string

scanf():

scanf() function is used in the C program for reading or taking any value from the keyboard by the user, these values can be of any data type like integer, float, character, string, and many more. This function is declared in stdio.h(header file), that’s why it is also a pre-defined function. In scanf() function we use &(address-of operator) which is used to store the variable value on the memory location of that variable.

Syntax: 

scanf(“Format Specifier”, &var1, &var2, …., &varn);  

Example:

// C program to implement
// scanf() function
#include <stdio.h>
  
// Driver code
int main()
{
    int num1;
  
    // Printing a message on
    // the output screen
    printf("Enter a integer number: ");
  
    // Taking an integer value
    // from keyboard
    scanf("%d", &num1);
  
    // Displaying the entered value
    printf("You have entered %d", num1);
  
    return 0;
}
Output
Enter a integer number: You have entered 0

Output:

Enter a integer number: 56
You have entered 56

sprintf():

sprintf stands for “string print”. This function is similar to printf() function but this function prints the string into a character array instead of printing it on the console screen.

Syntax:

sprintf(array_name, “format specifier”, variable_name);

Example:

// C program to implement
// the sprintf() function
#include <stdio.h>
  
// Driver code
int main()
{
    char str[50];
    int a = 2, b = 8;
  
    // The string "2 and 8 are even number"
    // is now stored into str
    sprintf(str, "%d and %d are even number",
            a, b);
  
    // Displays the string
    printf("%s", str);
    return 0;
}
Output
2 and 8 are even number

sscanf():

sscanf stands for “string scanf”. This function is similar to scanf() function but this function reads data from the string or character array instead of the console screen.

Syntax:

sscanf(array_name, “format specifier”, &variable_name);

Example: 

// C program to implement
// sscanf() function
#include <stdio.h>
  
// Driver code
int main()
{
    char str[50];
    int a = 2, b = 8, c, d;
  
    // The string "a = 2 and b = 8"
    // is now stored into str
    // character array
    sprintf(str, "a = %d and b = %d",
            a, b);
  
    // The value of a and b is now in
    // c and d
    sscanf(str, "a = %d and b = %d",
           &c, &d);
  
    // Displays the value of c and d
    printf("c = %d and d = %d", c, d);
    return 0;
}
Output
c = 2 and d = 8
Q6. Explain the input and output operations in C ?
(A).
      

Basic Input and Output in C

C language has standard libraries that allow input and output in a program. The stdio.h or standard input output library in C that has methods for input and output.

scanf()

The scanf() method, in C, reads the value from the console as per the type specified. Syntax:

scanf(“%X”, &variableOfXType); where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is the address operator in C, which tells the compiler to change the real value of this variable, stored at this address in the memory.

printf()

The printf() method, in C, prints the value passed as the parameter to it, on the console screen. Syntax:

printf(“%X”, variableOfXType); where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is the address operator in C, which tells the compiler to change the real value of this variable, stored at this address in the memory.

How to take input and output of basic types in C?

The basic type in C includes types like int, float, char, etc. Inorder to input or output the specific type, the X in the above syntax is changed with the specific format specifier of that type. The Syntax for input and output for these are:

  • Integer:
Input: scanf("%d", &intVariable);
Output: printf("%d", intVariable);
  • Float:
Input: scanf("%f", &floatVariable);
Output: printf("%f", floatVariable);
  • Character:
Input: scanf("%c", &charVariable);
Output: printf("%c", charVariable);

Please refer Format specifiers in C for more examples. 

// C program to show input and output
 
#include <stdio.h>
 
int main()
{
 
    // Declare the variables
    int num;
    char ch;
    float f;
 
    // --- Integer ---
 
    // Input the integer
    printf("Enter the integer: ");
    scanf("%d", &num);
 
    // Output the integer
    printf("\nEntered integer is: %d", num);
 
    // --- Float ---
   
    //For input Clearing buffer
      while((getchar()) != '\n');
 
    // Input the float
    printf("\n\nEnter the float: ");
    scanf("%f", &f);
 
    // Output the float
    printf("\nEntered float is: %f", f);
 
    // --- Character ---
 
    // Input the Character
    printf("\n\nEnter the Character: ");
    scanf("%c", &ch);
 
    // Output the Character
    printf("\nEntered character is: %c", ch);
 
    return 0;
}
Output:
Enter the integer: 10
Entered integer is: 10

Enter the float: 2.5
Entered float is: 2.500000

Enter the Character: A
Entered Character is: A
NOTE : ALL THE STUDENTS ARE INFORMED THAT YOU CAN WRITE THE ASSIGNMENTS WITH ATLEAST ONE EXAMPLE PROGRAM .
CPNM ASSIGNMENT PDF :-             DOWNLOAD



💖THANK YOU VISIT AGAIN💖

Post a Comment

1 Comments

HEMU THE SMART said…
I HOPE THIS WILL HELP YOU A LOT