Labels

Android (1) bash (2) boost (2) C (34) C++ (2) cheatsheet (2) CLion (6) css (3) Debian (33) DL (17) Docker (2) Dreamweaver (2) Eclipse (3) fail2ban (4) git (5) GitHub (4) Hacking (3) html (8) http (1) iOS (1) iPad (1) IRC (1) Java (31) javascript (3) Linux (167) Mac (19) Machine Learning (1) mySQL (48) Netbeans (4) Networking (1) Nexus (1) OpenVMS (6) Oracle (1) Pandas (3) php (16) Postgresql (8) Python (9) raid (1) RedHat (14) Samba (2) Slackware (47) SQL (14) svn (1) tar (1) ThinkPad (1) Virtualbox (3) Visual Basic (1) Visual Studio (1) Windows (2)

Friday, 3 July 2020

Format specifiers in C with a note on data types.

With the printf() function, we use the following;

%d - integer variable.
%c - character.
%f - for float variable.
%s - for string variable.
%lf - for double.
%x - for hexadecimal.
\n - gives us a newline.

An example of use can be seen below;

#include <stdio.h>

int main()
{
    int i = 5;
    float f = 9.8;
    
    printf("Here we have an integer: %d",i);
    printf("\n");
    printf("and here we have a float: %f",f);
    return 0;
}


Another example, this time reading input with scanf can be found here

Some notes on datatypes





Source: https://www.studytonight.com/c/datatype-in-c.php/

Additionally;


%c Character - char, unsigned char
%d Signed Integer - short, unsigned short, int, long
%e or %E Scientific notation of float values - float, double
%f Floating point - float
%g or %G Similar as %e or %E - float, double
%hi Signed Integer(Short) - short
%hu Unsigned Integer(Short) - unsigned short
%i Signed Integer - short, unsigned short, int, long
%l or %ld or %li Signed Integer - long
%lf Floating point - double
%Lf Floating point - long double
%lu Unsigned integer - unsigned int, unsigned long
%lli, %lld Signed Integer long, long
%llu Unsigned Integer - unsigned long long
%o Octal representation of Integer - short, unsigned short, int, unsigned int, long
%p Address of pointer to void void * - void *
%s String - char *
%u Unsigned Integer - unsigned int, unsigned long
%x or %X Hexadecimal representation of Unsigned Integer - short, unsigned short, int, unsigned int, long
%n Prints nothing
%% Prints % character















Source: https://alvinalexander.com/programming/printf-format-cheat-sheet/

No comments:

Post a Comment

Note: only a member of this blog may post a comment.