3.What is Constant ? in C language | C Tutorials for Beginners #3

 3.Constant in C language | C Tutorials for Beginners #3

https://harshaup.blogspot.com/2021/05/3constant-in-c-language-c-tutorials-for.html?m=1


What is Constant ?

C constant is nothing but which doest not change value during execution of time.

Different Types of C Constants :

Integer Constant :-
         Integer Constant  are constant which stores integer value.
Floating Constant:-
        Character Constant  are constant which stores float value
Character Constant:-
        Integer Constant  are  Constant which stores character value
String Constant:-
Integer Constant  are  Constant which stores string value

How to Declare Constant in C :
const float b = 23.5; 
int const a = 13;

Integer Constant:

ans: An integer constant refers to a sequence of digit without any fractional or exponential part.

Types Of Integer Constant:

decimal constant(base 10)
octal constant(base 8)
hexadecimal constant(base 16)
Decimal Constant:

It consists of any combinations of digits taken from the set 0 through 9, preceded by an optional – or + sign.
The first digit must be other than 0.
Embedded spaces, commas, and non-digit characters are not permitted between digits. 
    Valid:             0          32767     -9999          -23
    Invalid: 
12,245            -   Illegal character (,) 10  20  30       -   Illegal   character(blank space)
 


Octal Constant;

It consists of any combinations of digits taken from the set 0 through 7.
If a constant contains two or more digits, the first digit must be 0.
In programming, octal numbers are used.
    Valid:                   037                 0          0435
    Invalid:
0786                Illegal digit 8
 123          -Does not begin with zero  
01.2         -    Illegal character (.)
 


Hexadecimal Constant:


It consists of any combinations of digits taken from the set 0 through 7 andalso a through f (either uppercase or lowercase).
The letters a through f (or A through F) represent the decimal quantities 10 through 15 respectively.
This constant must begin with either 0x or 0X.
In programming, hexadecimal numbers are used.
    Valid Hexadecimal Integer Constant:            0x                   0X1             0x7F            
    Invalid Hexadecimal Integer Constant:         
                                                                                0xefg  -           Illegal character g
                                                                                123     -

Character Constant:


ans: A character constant is nothing but which uses single quote after and before a character.

Examples of character Type :

'j'
'1'
'#'
'<'

'C'
Character Constant Declaration:


way 1:Declaring single variable

char variable_name;

way 1:Declaring Multiple variable

char var1,var2,var3

way 1:Declaring and Initializing

char var1='shav',var2='shvam', var3='codharsh';


String Constant:


ans:String constant is nothing but sequence of character or we can say that any character between double quote are string.

Examples of character Type :

"hey"
"1345"
"i am user

Escape Sequence:-

ans: It mainly used for print your output in next line or horizontally.

Below are the list of Escape sequence :

Escape Sequence Meaning
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\' Single Quote
\" Double Quote
\? Question Mark
\nnn octal number
\xhh hexadecimal number
\0 Null


 Escape Sequence Example

#include <stdio.h>
 
void main()
{
    printf(""You\nare\nlearning\n\'c\' language\n\"Do you know C language\""");
    
}
Output of above program:
You
are
learning
'c' language
"Do you know C language"


#C programs :-

 1.   #include<stdio.h>

int main(){
    // int a = 4; // Type declaration instruction
    // int a = 4, b, c; // Type declaration instruction
    // b = c = a;

    // printf("The value of a is %d\n", a);
    // printf("The value of b is %d\n", b);
    // printf("The value of c is %d\n", c);

    float a = 1.1;
    float b = a + 8.9;
    printf("The value of b is %f\n", b);



    return 0;

2.  #include<stdio.h>

int main(){
    // int a; int b=a;
    // int v = 3^3;
    // char dt = '2';
    // float d = (3.0/8-2);
    // printf("%d\n", v);
    // printf("%f\n", d);

    // Q3. Write a program to determine whether a number is divisible by 97 or not
    int num;
    printf("Enter the number\n");
    scanf("%d", &num);
    printf("Divisibility test returns: %d\n", num%97);

    // Q4. Step by step evaluation of 3*x/y-z+k
    int x = 2, y=3, z=3, k=1;
    int result = 3 * x / y - z + k;
    // 6/3 - 3 + 1
    // 2 - 3 + 1
    // 2 - 3 + 1 
    // 0
    printf("The value of result is %d", result);


        return 0;

3.#include <stdio.h>

int main()
{
    int x = 2;
    int y = 3;

    printf("The value of 3*x - 8*y is %d \n", 3*x - 8*y);
    printf("The value of 8*y / 3*x is %d \n", 8 * y / 3 * x);
    // 8*3 /3*x = 24/6 will give wrong answer
    // 24/3*2
    // 8*2
    // 16

    return 0;  

No comments

Comments system

Powered by Blogger.