Variable type specifier in C [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I just started in C programming and I know that "%d" defines that it will be a number value, as in:
int x = 9;
printf("X = %d", x);
getchar();
return 0;
but what are the other variable specifiers for C? (to define strings, and do they change for float, double, long, etc.?)

There's a good summary of the specifiers available at http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders .

Your question as phrased is meaningless. The %d is a format specifier, and has nothing to do with variable declarations.
If you were to google 'printf c', you would find many, many sites that answer your question.

You may want to pick up a copy of Kernighan and Ritchie's The C Programming Language. It short but fully packed. Working through this book is well worth the time.
But as to your question, you're asking about Format specifiers for string literals. You'll probably also want to look at escape characters soon (e.g. \n for new lines). Fortunately, it's all in K&R (the above book), specifically on page 153 (2nd edition).

Related

What the symbol of "nothing" in any programming language? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
For example in C, the NULL ('\0') stands for "nothing". And in mit-scheme the nil or the '() stands for nothing. So does Ruby use nil.
So i just wonder what's the symbol or variable that stands for "nothing" in other programming languages. thx.
You are wrong about C language NULL and '\0' being the same. Usually in C, NULL is used with pointers and '\0' is used with strings.
For example, The following code fragment compiles without any warning.
int *p=NULL;
char ch='\0';
But the following code gives you the warning "initialization makes integer from pointer without a cast" on compilation.
int *p='\0';
char ch=NULL;
So we can conclude that they are not equal and interchangeable.
Javascript: null
Python: None
It all depends from language developers preferences.
Everything in Ruby is object. nil is an object and empty string "" is also an object etc... But "" means 0 length string and nil means object with no reasonable value - in some situations it really is "nothing".

how to check out wordsize on linux [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
As we can see a macro defined in stdint.h or bits/types.h etc.. which is __WORDSIZE. I don't know where to check out whether this macro is defined. Also, is there a way to checkout different size of the basic types without using the sizeof in c. I mean, is there a document exhibits the size of those variables?
Well, it depends on the platform. First, there are some requirements set by the C standard and/or the POSIX standard if you use UNIX. Things like sizeof(int) <= sizeof(long) or sizeof(char) == 1
Then the ABI has the final say. For example, on linux/freebsd/solaris on x86_64, they use a common ABI: http://people.freebsd.org/~obrien/amd64-elf-abi.pdf
3.1.2 in this document has the size for all types for this ABI

Format specifier to print following data in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Could someone help me in printing the following data in C, exact format specifier and procedure to print.
union
{
char c[8];
short s[4];
long l[2];
void * vfp[2];
} info;
When i try using printf("%s\n", info.c); and printf("%s\n",info.s). I got some garbage values.
Also i need help in printing those pointers.
Thanks for the support.
You need to understand that the usage semantics of a union. You can only read the type that you stored inside a particular object instance of union. And you can only store one type in a union at a particular instance in time.
So either you stored s or c, it cannot be both. If your intention is to store both the types then what you need is a structure and not a union.
First of all you're using a union. the memory is shared between those 4 arrays. I'm not sure you want that.
Second, the list of format specifiers is here. %s is for strings. %p is for pointers, and %ld can be used for a signed long.

What is the meaning of f in printf? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the meaning of "f" in C's printf?
The f in printf stands for formatted, its used for printing with formatted output.
As others have noted, the trailing f indicates formatted output (or formatted input for functions in the scanf family).
However, I'll add that the distinction matters because it's important for callers to know that the string is expected to have format-specifier semantics. For example, do not do this:
char* s = get_some_user_input();
printf(s); // WRONG. Instead use: printf("%s", s) or fputs(stdout, s)
If s happens to contain % characters, printing it directly with printf can cause it to access non-existent arguments, leading to undefined behavior (and this is a cause for some security vulnerabilities). Keep this naming convention in mind if you ever define your own printf-like variadic functions.
If I'm not mistaken, printf stands for "Print formatted data to stdout".
printf allows for formatting, while print doesnt. Also, print doesn't exist in C. I don't even know what printg is.

c programming query regarding sizeof operator Please specifythe output with reason in windows and linux [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
void main()
{
char c='0';
printf("%d %d",sizeof(c),sizeof('0'));
}
In C, size of char literal is equal to sizeof(int). So sizeof('0') gives the value of sizeof(int) on your implementation.
Also sizeof(char) is always 1 as mandated by the Standard.
The output will be 1 4. The type of the '0' literal is int, which on most systems has a size of 4. The C standard requires that sizeof(char) is 1.
If you get anything less than 4, get in your time machine, and dial in +25 years.

Resources