Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts

How to color of text in C-Language using GCC Compiler

agyanadda

In C-Language when you will use text color then you must have to know about the color code of C language.

Name         | Value
             |
Black        |   0
Blue         |   1
Green        |   2
Cyan         |   3
Red          |   4
Magenta      |   5
Brown        |   6
Light Gray   |   7
Dark Gray    |   8
Light Blue   |   9
Light Green  |   10
Light Cyan   |   11
Light Red    |   12
Light Magenta|   13
Yellow       |   14
White        |   15
-----------------------
OR
-----------------------
0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White

Example 1 #include <stdio.h> int main() { /* Some list of combinations 0 = Black 1 = Blue 2 = Green 3 = Aqua 4 = Red 5 = Purple 6 = Yellow 7 = White 8 = Gray 9 = Light Blue A = Light Green B = Light Aqua C = Light Red D = Light Purple E = Light Yellow F = Bright White */ printf("This is a console color change program\n"); system("COLOR F2"); /* This will change the bgcolor F - White and textcolor to 2- Green */ getchar(); return 0; } Example 2 #include <stdio.h> #include <stdlib.h> int main() { system("COLOR FC"); printf("Welcome to the color changing application!\n"); printf("Press any key to change the background color!\n"); getch(); system("COLOR 6C"); printf("Now the background color is Yellow and Text Color is light Red\n"); printf("Press any key to exit..."); getch(); return 0; } Example 3 #include <windows.h> //This is the header file for windows. #include <stdio.h> //C standard library header file void SetColor(int ForgC){ WORD wColor; //This handle is needed to get the current background attribute HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; //csbi is used for wAttributes word if(GetConsoleScreenBufferInfo(hStdOut, &csbi)){ //To mask out all but the background attribute, and to add the color wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F); SetConsoleTextAttribute(hStdOut, wColor); } } int main() { printf("Test color"); //Here the text color is white SetColor(4); //Function call to change the text color printf("Test color"); //Now the text color is green return 0; }

How to clear screen in C Language

agyanadda

We can clear screen in C language according to the compiler.

  1. Using clrscr() - For TurboC Compiler
  2. Using system("cls") - For gcc/g++ & TurboC Compiler windows
  3. Using system("clear") - For gcc/g++ compiler in Linux
/* TurboC Compiler */ #include <stdio.h> #include <conio.h> /*for clrscr()*/ int main(){ int num=100; /*Use after declaration section*/ clrscr(); /*clear output screen*/ printf("value of num: %d\n",num); return 0; } /* GCC compiler and TurboC Compiler For Windows */ #include <stdio.h> #include <stdlib.h> /*for system()*/ int main(){ int num=100; /*Use after declaration section*/ printf("Not display due to clearscr"); system("cls"); /*clear output screen*/ printf("value of num: %d\n",num); return 0; } /* gcc/g++ compiler Linux */ #include <stdio.h> #include <stdlib.h> /*for system()*/ int main(){ int num=100; /*Use after declaration section*/ system("clear"); /*clear output screen*/ printf("value of num: %d\n",num); return 0; }

How to find the length of Array in C_Language

agyanadda

sizeof(): is used to get the data type size in C language.

#include<stdio.h> #include<stdlib.h> int main() { int arr[] = {1,2,3,4,5,6}; int length = sizeof(arr)/sizeof(int); //length of an integer array printf("Array length = %d",length); }

Function vs Methods

agyanadda

Function vs Methods

There are given, what is the difference between Function and Method.


                               Function
                            Method
It is a piece of code that is called by name.
It is also a piece of code that is called by a name
It can be passed data to operate on (i.e. the parameters)
It is associated with an object.
It can optionally return data (the return value).
It is identical to a function except for two key differences:

All data that is passed to a function is explicitly.
A method is implicitly passed the object on which it was called.

It has no any reference variable
It is called by reference variable
Function applies both object oriented and non-object oriented language. C  and JavaScript etc
It is only applicable to object oriented programming language like c#, C++ and java etc




C Input-Output functions - printf, scanf, putchar, getchar, gets and puts in GCC

agyanadda

­Input and output Functions
C language provides many in-built input-output library functions to read input and output. It is used to perform input output tasks.The standard input-output header file is stdio.h  that contains the printf() and scanf() function.

I/o Function
Description
scanf
It is used to take input through the input device.
getchar
It is used to accept a character from the console.
gets
It is used to scan a line of text from a standard input device.
printf
It is used for display output statement.
putchar
It is used to display a single character on the screen.
puts
It is used to display a string on a standard output device.


Printf() Function C Language
In C programming language, printf() function is used to print the integer,string,character, float or other value. It prints the given statement to the console.

Syntax:
            printf("format string",argument_list); 

Example 1
// C output
#include <stdio.h>    // Header File
int main(){
    printf("C Programming JavaTpoint"); 
    return 0;
}

Output
C Programming JavaTpoint

C scanf() Function C language
C  scanf() function is used as input statement. It is enclosed with double quotes. It reads the input data from the console.
Syntax:
            scanf("format string",argument_list); 
Example 1
// C Integer Input/Output
#include <stdio.h>    // Header File
int main(){
    int num;
    printf("Enter any Integer number:");
    scanf("%d",&num);
    printf("Your Integer number is :%d",num); 
    return 0;
}

Output
Enter any Integer number:567
Your Integer number is :567

Note: C language is case sensitive. For example printf() and scanf() are different from Printf() and Scanf() function. It function name must be in lowercase.



Following are the input-output format string in C Language.

Format string

Description

Example

%d
Scan or print an integer value.
45
%c
Scan or print  a single character.
‘C’
%s
Scan or print string character.
“Clanguage”
%f
Scan or print float number.
2.45
%.1f
Scan or print float number with 1 digit decimal.
10.3
%e
Scan or print a floating-point number in exponential (scientific notation).
2.000000e+000
%g
Scan or print a floating-point number in either fixed decimal or exponential format depending on the size of the number.
10.5
%lf
Scan or print double value.
  20.123456
%o
Scan or print octal value.
226
%x
Scan or print hexadecimal value.
96


Some other example of printf() and scanf() function
Example 1
//  C Integer Output
#include <stdio.h>    // Header File
int main(){
    int num=786;
    printf("Your Integer number is :%d",num);  //displays the content inside quotation
    return 0;
}

Output
Your Integer number is :786

Example 2
// C Floats Input/Output
#include <stdio.h>   
int main(){
    float num;   // declare float number
    printf("Enter any Float number:");
    scanf("%f",&num);
    printf("Your Float number is :%f",num);
    return 0;
}

Enter any Float number:23.56
Your Float number is :23.559999
Example 3
// C Character I/O
#include <stdio.h>   
int main(){
    char chr;   // declare character 
    printf("Enter a character:");
    scanf("%c",&chr);

    printf("Your Enterd Character  is :%c",chr);
    return 0;
}
Output
Enter a character:J
Your Entered Character number is :J
Example 4
// C ASCII Code
#include <stdio.h>   
int main(){
    char chr;   // declare float number
    printf("Enter a character:");
    scanf("%c",&chr);
    printf("Your Entered Character  is :%c \n",chr);
    printf("ASCII value of '%c' is: %d", chr, chr); 
    return 0;
}

Output
Enter a character:A
Your Entered Character  is :A
ASCII value of 'A' is: 65


Example 5
// I/O of Floats and Integers
#include<stdio.h>
int main(){
   char ch = 'a';
   char str[15] = "javaTpoint";
   float flt = 12.334;
   int no = 150;
   double dbl = 20.123456;
   printf("Your Character is: %c \n", ch);
   printf("Your String is: %s \n" , str);
   printf("Your Float value is: %.3f \n", flt);
   printf("Your Integer value is: %d\n" , no);
   printf("Your Double value is: %lf \n", dbl);
   printf("Your Octal value is %o: \n", no);
   printf("Your Hexadecimal value is: %x \n", no);
   return 0;
}
Output
Your Character is: a
Your String is: javaTpoint
Your Float value is: 12.334
Your Integer value is: 150
Your Double value is: 20.123456
Your Octal value is 226:
Your Hexadecimal value is: 96

============================================================================

Getchar() Function 

The getchar() function is used to accept a character from the console and display output immediately while typing  or press any key for proceeding.
Syntax:
                        getchar(void);

Function
Parameter
getchar()
It read a single character from the input device and return  unsigned char.

Example 1


#include<stdio.h>
int main(){
char a;
printf("Enter a character:");
a=getchar(); // take input a single character
printf("Given character is %c:",a);
return 0;
}


Output
Enter a character:C
Given character is C: