Lesser known facts in C

Do you find this thread and it's facts intresting??

  • Yes

    Votes: 16 94.1%
  • No

    Votes: 1 5.9%

  • Total voters
    17
Status
Not open for further replies.

adi007

Youngling
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka

This thread is dedicated to lesser known facts in C


Don't forget to visit these other threads started by me
1.Solve the given C puzzle:mindcracking C puzzles.Updated atleast once in a week

2.C in linux/unix:Bored of using TC,switch to linux.All about running C programs in linux/unix

3.Presenmaker 1.0Presenmaker stands for presentation maker. It's a free software developed by me which can be used to create interactive agent animations in seconds. By using Presenmaker you can convert a lengthy text file into agent animation. It is very useful to create interactive presentations, tutorials, to read lengthy lessons etc
Index
1.Editset[#1]
2.scanf() return type [#6]
3.enum data type[#8]
4.const data type[#10]
5.floor and ceil[#15]
6.dynamic output[#17]
7.smiley in C[#21]
8.Garbage value in case of dataoverflow[#23]
9.Saving the output to a text file[#27]
10.atol(),atof() and atol()[#33]
11.storage class-1[#39]
12.storage class-2[#41]
13.ASCII chart and some shortcuts in TC editor[#47]
14.storage class-3[#51]

EditSet:

This will be great useful in strings.You can specify the characters you want to include or exclude in C.This will work with scanf.

Read it my site aka blog

This was asked in puzzle 1 of thread Solve the given C puzzle [*www.thinkdigit.com/forum/showthread.php?t=70697]
 
Last edited:

[xubz]

"The Cake is a Lie!!"
Bah! C Supports RegEx too eh? Interesting (didn't quite know :eek:)

Instead of scanf("%[^\n]s", a);, One _can_ use gets(a);

Interesting Post Anyway, Thanks :)


(Edit: Does anyone know of a RegEx based text replace function code in C? like in preg_replace in PHP?)
 
OP
A

adi007

Youngling
Here is the second lesser know fact

scanf() return type:

scanf() will return number of variables it succesfully stored new values.i.e,

Code:
a=scanf("%d%d",&b,&c);
where a,b,c are integer variables

if we input 12 b
then the value of a will be 1

if we input 12 1
then the value of a will be 2

that means it can be used to verify whether the user has given the values according to the specified data type or not.

here is the problem that was asked in 2nd C puzzle
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
scanf("%c%b%c",&a,&b,&c);
getch();
}
This is an program which accepts a character followed by 2 integer values.
NOW ALL I WANT TO DO IN THIS PROGRAM IS TO CHECK WHETHER THE USER HAS ENTERED CORRECT INPUT(FIRST CHARACTER AND FOLLOWED BY THE 2 INTEGER VALUES.
It should display "That's Good" if the user has inputted in correct sequence else it should display "That's Bad".
Here are some sample ouput's
Code:
Output:
Enter a character and 2 integer values
a 23 45.96
That's good
^^ decimal values are rounded off.Hence it is valid input
Code:
Output:
Enter a character and 2 integer values
2 23 45.96
That's good
^^'2' is also a character

Code:
Output:
Enter a character and 2 integer values
2 ad 45.96
That's bad
^^ ad is not a decimal value.
The following are the Rules:


1.The keyword if or operator '?' should come only once in a program
2.No logical operator's are allowed (that means && || ! should not be used)
3.No new variabels must be used
4.No header file other than the existing one should be used

Here is the solution

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
if (scanf("%c %d %d",&a,&b,&c)==3)
printf("\n thats good");
else
printf("\n thats bad");
getch();
}

Find more of the C puzzles at *www.thinkdigit.com/forum/showthread.php?t=70697

no contributions:(:(

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
if (scanf("%c %d %d",&a,&b,&c)==3)
printf("\n thats good\n");
else
printf("\n thats bad\n");
printf("The values are %c %d %d\n",a,b,c)
getch();
}
the above program is going to work correctly.I earlier stated that the output will be some garbage values but it is not so.
Sorry for the mistake:(
I just wonder how come no one recognised this mistake.That means no one is trying the facts specified here :(:(:(
 
Last edited:
OP
A

adi007

Youngling
^thanks:):)

Looks like only i have to post ..
Ok,here is a very unknown fact about C

enum data type
it means enumerated type.
It's a user defined datatype.It's syntax is
Code:
enum name{value1,value2,.....valuen);
ex:
enum week{sunday,monday,tuesday,wednesday,thursday,friday,saturday}

Read it my site aka blog


This was asked by me in puzzle3

Here is a simple C program
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
/*Complete the declartions*/
clrscr();
a=sunday;
b=monday;
c=tuesday;
d=wednesday;
e=thursday;
f=friday;
g=saturday;
printf("%d/n%d/n%d/n%d/n%d/n%d/n%d/n",a,b,c,d,e,f,g);
getch();
}
NOW ALL I WANT YOU TO DO IN THIS PROGRAM IS TO COMPLETE DECLARATIONS SO THAT IT MACTHES THE OUTPUT.
Code:
Output:
1
2
3
4
5
6
7
The following are the Rules:

1.sunday,monday,tuesday,wednesday,thursday,friday,saturday etc are not varibles nor constants.Means
Code:
int sunday=1;
or 
#define sunday 1
are not allowed
2.a,b,c,d,e,f,g are not of int datatype
Code:
int a;
^^ not allowed

And the solution to this is

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
enum days{
sunday=1,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
};
enum days a,b,c,d,e,f,g;
a=sunday,
b=monday,
c=tuesday,
d=wednesday,
e=thursday,
f=friday,
g=saturday;

clrscr();
printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e,f,g);
getch();
}
Note:!:

if i just specified
Code:
enum days{
sunday,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
};
then it would have generated the output
Code:
0
1
2
3
4
5
6
So it is necessary to assign 1 to sunday.

Now what's it's use:
I have never seen any exclusive use of enum data type.Otherwise it would have been know fact to everyone :D:D
If you know any specify it here....
 
Last edited:

ayush_chh

Ambassador of Buzz
may be i can give one...........don't know if all of you know this already.

There is only one special character which you can use in a identifier name.

that special character is _ (underscore)

so you give give a identifier name as var_1
or _var
or (the most interesting part) just _ (underscore)

so you can create identifiers as

int _;
int __;
int ___;
 
OP
A

adi007

Youngling
Poll added!

I request all members to Participate in the pole and Rate this thread

Here is another unknown fact
We all know that we can declare a constant by using #define but do you know that there is still other method to do so.

const data type
This lesser known datatype is used to declare constants.The syntax is
const type name=value;
ex:
Code:
const int a=10;

Continue here
 
Last edited:
OP
A

adi007

Youngling
^^thanks:D:D
will post another unknown fact tommorow :D:D
But the only thing i feel sad is there is less amount of responses and contributions :(:( and defenitely no ratings and poll partcipation :(:(
 
Last edited:
OP
A

adi007

Youngling
Thanks for all ur responses :D:D:D

floor and ceil

floor and ceil functions comes under math.h header file.

continue here
 
Last edited:
OP
A

adi007

Youngling
I am not sure whether this is an unknown fact or not.

This will work in Turbo or windows
In the TC editor press <CTRL><B>
Tell me what happens???:wink::wink:

^^Did anyone tried this???
:!::!:Important announcements:
if you google lesser known facts in c,then this thread will be the first result....:D:D:D
But if i select pages from india then this will be not the first result.I wonder why???:confused:
 
Last edited:
Status
Not open for further replies.
Top Bottom