Literals in C (Part 2)

part 2 of the literals in C. Discusses string and numeric and char types of literals

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.

In this chapter we shall take some example programs. These programs are based on the concept of literals and conversions, but some of the programs are general programs also. The level is of an absolute beginner. These programs are not discussed in the video tutorial for this chapter.

Integer to hexadecimal Conversion

Ask the user to enter a number. Write code to display the hexadecimal equivalent of that number. Do it using both printf and cout.


 
#include<iostream>
#include<cstdio>
 
using namespace std; 
 
int main() 
{
    
    cout << " Enter a number " ;
 
    int numb; 
 
    cin >> numb;
 
    cout << hex << numb << endl;
 
    printf( "%x", numb );
 
    return 0 ;
 
}

float to integer Conversion

Write a program to convert a float into integer.


#include<iostream>
#include<cstdio>
 
using namespace std ;
 
int main()
{ 
 
    float num1 = 20.56f;
     
    int num2 = (int)num1; 
     
    cout << "Float number is = " << num1 << endl;
     
    cout << "integer number is = " << num2 ; 
     
    return 0 ;
 
}

integer to float conversion

Write a program to convert integer to float

Ex. int = 9635 Ans. 9635.0000 
#include<iostream>
#include<cstdio>
 
using namespace std; 
 
int main()
{ 
 
    int num1 = 9635;
     
    float num2 = num1;
     
    printf( " Float number is = %f\n", num2 );
     
    cout << " float using cout is = " << num2;
     
    return 0;
 
}

Celsius to Fahrenheit Conversion

Write a program to convert °C to °F temperature.

#include<iostream>
#include<cstdio>
 
using namespace std ;
 
int main() 
{ 
 
    cout << " enter the temp in celsius = ";
     
    float temp; 
     
    cin >> temp ; 
     
    float fahrenheit = ( (temp * 9.0f ) / 5.0f ) + 32.0f;
     
    printf("Temp in fahrenheit = %f ", fahrenheit);
     
    return 0 ;
     
}

Hexadecimal to integer conversion

Write a program to convert hexadecimal number into base 10 integer



#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main() 
{ 
 
    int numb1 = 0xC;
     
    int numb2 = 0xF;
     
    cout << numb1 << endl;
     
    cout << numb2 << endl;
     
    return 0 ; 
 
}

Character to ascii code conversion

Write a program to convert a character into its ASCII Code


#include<iostream>
#include<cstdio>
 
using namespace std ;
 
int main() 
{
    
    char var1;
 
    printf("Enter character: ");
 
    cin >> var1; 
     
    printf("You entered = %c\n", var1); 
 
    printf("ASCII value of = %d", var1); 
 
    return 0;
 
}

Integer to octal conversion

Write a program to convert integer into octal


 
#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main() 
{
    
    int k = 14;
    
    cout << oct << k << endl;
    
    printf( "%o", k );
    
    return 0;
 
}

Escape sequences

#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main() 
{
    
    cout << "Name\tid\tsalry" << endl;
    
    cout << "shivalik\t0123\t5000" << endl;
    
    printf("hoven\t011\t9000\n");
    
    printf("trainings\t015\t8500\n");
    
     return 0;
 
}

Truncation of float value

Write a program to truncate float value into integer. Take float x = 45.920F.

#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main() 
{
    
    float x = 45.920F;
    
    int i = (int) x ;
    
    cout << "Before truncatoin =" << x << endl;
    
    cout << "After truncation =" << i << endl;
    
    return 0;
 
}

Printf format specifiers

Write a program to print int ,unsigned int , float , hexadecimal , octal data types. Use printf.

#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main()
{
    
    printf("First number: %d\n", 12345);    
    
    printf("Second number: %d\n", 25); // %d and %i are exactly same when used in printf
    
    printf("Third number: %i\n", 1234);        
    
    printf("Float number: %3.2f\n", 3.14159); // % 3.2f minimum 3 digit wide total and maximum 2 digit precision 
    
    printf("Hexadecimal: %x\n", 255);
    
    printf("Octal: %o\n", 255);
    
    printf("Unsigned value: %u\n", 150);
    
    return 0;
    
}

Different variables of same datatype

Write a program using int, unsigned int, signed int. For this you can create variables of these types and display them.

#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main()
{
    
    unsigned int num1 = 1234; // unsigned only be a positive
        
    signed int num2 = -5678; // signed may be positive or negtive
        
    int num3= 9870;        // int also be a positive or negtive
        
    cout << num1 << endl;
        
    cout << num2 << endl;
        
    cout << num3 << endl;
    
    return 0;
    
}

Constants

Write a program using const keyword in c. For this you have to create a variable called height, and mark it as constant. Similarly, create a const float and a const char variables also. Please note that when you try to change constant values after defining in C program, it will give error.

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    
    const int height = 100;        /* int constant */
    
    const float number = 3.14;    /* Real constant */
    
    const char letter = 'A';    /* char constant */
    
    printf("value of height : %d \n", height );
    
    printf("value of number : %f \n", number );
    
    printf("value of letter : %c \n", letter );    
    
    return 0;
    
}

Casting

Write a program that demonstrates the casting a char to an int.


#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main()
{
    
    char my_char = 'A';
    
    // automatic casting occurs, because no loss of accuracy
    int my_int = my_char;
    
    printf("value of %c is %d " ,my_char, my_int);
        
    return 0;
    
}

Use of ++ , --

Write a program to show use of increment and decrement with example.

#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main()
{
    
    
    int a = 33;
     
    int b = 88;
    
    printf("Before increment a is = %d \n", a);
     
    printf("Before decrement b is = %d \n", b);
     
    a++;    // increment by 1
     
    b--;    // decrement by 1
     
    printf("After increment a is = %d \n", a);
     
    printf("After decrement b is = %d \n", b);
    
    return 0;
                           
}
         
            
        

Classroom Training

Classroom training in C/C++ is available at our training institute. Click here for details. You can also register yourself here.

Video Tutorial

We have created a video tutorial that can be downloaded and watched offline on your windows based computer. It is in the form of an exe file. No installation is required, it runs by double clicking the file. Since the exe file is hosted on the Google Drive, it should be very safe because Google itself checks for any virus or malware. The video can be watched offline, no internet is required.

Pricing

This single video will cost you USD $5. When you download the video, you will be able to watch the first few minutes for free, as a sample. The video app will prompt you for payment through PayPal and other payment options. If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials The entire set is priced at USD $50.

Screenshots

This is a screenshot of the software that should give you a good idea of the available functionality. Click on the image to see a larger view.screenshot of the video being played in the software

Installation

This software doesn't require any installation. Just download and double-click to run it. It doesn't require administrative priviliges to run. It runs in limited access mode, so should be very safe.

supports windows xp and later Supported Operating Systems

These videos can be run on 32-bit as well as 64-bit versions of the Microsoft Windows Operating Systems. Windows XP and all later OS are supported. If you want to run it on Windows XP and Windows Vista, then you must also have the .NET Framework Version 3.5 installed on your machines. Those users who have Windows 7 and later donot have to worry because these operating systems already have the .NET Framework installed on them.

Download Links

IMPORTANT NOTE: This download contains ONLY ONE video. The video is about the topic of this tutorial.

Want ALL Videos? If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials. TIP: If you plan to buy these videos, then it would be more economical to buy the entire set.

DISCLAIMER: Even though every care has been taken in making this software bug-free, but still please take a proper backup of your PC data before you use it. We shall not be responsible for any damages or consequential damages arising out of the use of this software. Please use it solely at your own risk.

Please download the software here.
download the video Literals in C(Part 2)



Creative Commons License
This Blog Post/Article "Literals in C (Part 2)" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-10-05