TRENDING NEWS

POPULAR NEWS

What Is The Code To Write/print Numbers Serially With C Or Codeblocks

What is the output of a=5 printf ("%d %d %d", a++, a++, ++a); in c and how?

The answer to this question is entirely dependent on the compiler used. I am familiar with 2 implementations of C compilers, Turbo C for Dos and GCC in Linux. So I will explain what goes on in both of them.#include

int main() {
int a = 5;
printf("%d%d%d", a++, a++, ++a);
}
This is the code snippet. If we run this in Turbo C, the output will be766
Reason: A stack is used to store the parameters of any function in C and stack follows Last In First Out (LIFO) order. Therefore the parameter are evaluated in Right to Left order. Here they are assigned to the format specifier as soon as they are evaluated.++a is pre-increment operator. So value of a becomes 6 and is assigned to the last %d.a++ is post-increment operator. So value of a , which is 6 is assigned to 2nd %d and then gets incremented to 7.Same happens with the 1st a++. Hence the values 766 are printed to console.If we run the same code in GCC compiler, the output obtained is768
Reason: For the same reason as in Turbo C, here also the parameter are evaluated in Right to Left order. However, they are not assigned as soon as they get evaluated but according to the nature of operator (pre or post).++a is pre-increment operator. So value of a becomes 6 but is not assigned to the last %d.a++ is post increment operator. So value of a , which is 6 is assigned to 2nd %d and then gets incremented to 7.The same thing happens with the 1st a++. The value, which is 7 is assigned to the 1st %d and then incremented to 8.Lastly, the value of a gets assigned to the 3rd %d. Hence the values 768 gets printed to the console.

How do I write a C program to merge two files using command line arguments?

Two files can be merged into third file by writing one file after other in the resultant file. Something like this:FILE * sourceFile1 = fopen( file1, "r" );
FILE * sourceFile2 = fopen( file2, "r" );
FILE * destinationFile = fopen( resultFile, "w" );
char ch;
while( ( ch = fgetc( sourceFile1 ) ) != EOF )
fputc( ch, destinationFile );
while( ( ch = fgetc( sourceFile2 ) ) != EOF )
fputc( ch, destinationFile );
fclose( sourceFile1 );
fclose( sourceFile2 );
fclose( destinationFile );
where file1, file2 and resultFile are file names of source files and destination files in char string form.Arguments can be taken from command line using argc and argv parameters in main method.int main ( int argc, char *argv[] )
{
char * file1, * file2, * resultFile;
if( argc == 3 )
{
file1 = argv[ 0 ];
file2 = argv[ 1 ];
resultFile = argv[ 2 ];
}
}
where argc is the argument count from command line and argv contains the command line arguments.Note: Above code is for reference only, proper error handling is required for file and argument handling.

What is the use of curly braces in C programming?

{} is the most prominent thing that you have to care about in the ‘C’ language.It has many meaning in the different places.Whatever you will declare in this bracket, their visibility , Life will be in this block.example:-int i=10;
printf("%d\n",i);
{
int i=20;
printf("%d\n",i);
}
printf("%d\n",i);
{
int i=30;
printf("%d\n",i);
}
printf("%d\n",i);
Output:-10
20
10
30
10
Here, you can see three variables i that I have taken.First one which is outside the bracket, it can be accessible from anywhere in the program. You can also say it is a global variable.Second one is just only accessible in the first bracket.Third one is just only accessible in the second bracket.Here, we understand everything by giving an example of variable. This will also works for functions , structures and many things.

How can I create a login system in C?

Well, it is very easy …Try this code…#include
#include
#include

//created by chaitanya

int main()
{
char username[15];
char password[12];


printf("Enter your username:\n");
scanf("%s",&username);

printf("Enter your password:\n");
scanf("%s",&password);

if(strcmp(username,"chaitu")==0){
if(strcmp(password,"123")==0){

printf("\nWelcome.Login Success!");


}else{
printf("\nwrong password");
}
}else{
printf("\nUser doesn't exist");
}





return 0;

}
Output:Suggestions regarding improvement of this code are welcome…Edit : To store data , there is a database needed, in PHP , c++& c# it is much easier to connect with MySQL to store data.As this is a basic programme , so password is clearly visible in this C code .Thanks : )Also Check this :Chaitanya Chaitu's answer to How do I write a program in C that gives the days of the week after n days (e.g., Monday+7 days = Monday).Hope it helps you…-Chaitanya

How can I repeat a simple C program?

Your question is not clear.  If you are talking about loops, here's a start:http://www.dummies.com/how-to/co...There are plenty of tutorials online, for example http://www.learn-c.org.  When learning a language, it's good to soak up information first, then go back and read it again and try the examples later.If you try to write code immediately, you won't fully understand it and any tiny changes will probably result in compile errors.  Some theory, some practice, some theory, some practice just like learning guitar - learn the scales, practice them, learn strumming techniques, practice them, etc.

What is the difference between 'int main()', 'void main()' and 'main() ' function in the C programing language?

Difference betwèen int main and void main: See like any other function, main is also a function but with a special characteristic that the program execution always start from main. So the function main needs arguments and a return type. These int and void are its return type. Void means it will not return any value, which is also ok.But if want to know whether the program has terminated successfully or not, we need a return value which can be zero or a non zero value. Hence the functuon becomes int main () and is recommended over vpid main ().Hope its helps you.

Is there an innovative way of wishing Happy birthday using C or C++?

this is a c program to wish happy birthday in a new way through a program_#includemain(){ char str[]="Ibqqz!Cjsuiebz",*p; p=str; while(*p!='\0')    --*p++; printf("%s",str);}                     if you run this program the output of this program will be Happy Birthday.you can print some thing else also,just what you want to print, type the next character (in the ASCII list) in the printf() function.if you know C programming you can understand the logic of the program.

How do I create an array or a vector of size 10^9 in C or C++?

I’ve done this. It is actually straightforward.The simple approach is justint *p = (int *) malloc(1L<<32);
assert(p);
which allocates 4 GB, or 1 gig worth of 32-bit integers.For such large arrays, it is often worth going to some extra effort to make them efficient. In particular, if the program will access the array in any order other than sequentially, it is good to use HUGE_PAGES rather than the dinky little 4K pages that most machines give you by default.On Intel and AMD machines, you can get pages of 4K, 2 Megabytes, or 1 Gigabyte, if you work at it a little bit. Doing so will avoid large numbers of translation lookaside buffer misses.A secondary issue is to align the array on a natural boundary for the big pages.Unfortunately, there are several ways to allocate big pages on different OS and even different versions of the same OS.One way on linux is to mmap(2) an anonymous file in the hugetlbfs filesystem. You may have to get the sysadmin to make sure the kernel is configured for hugetlbfs, to mount it somewhere, set the permissions, and to configure it for 1G pages.More recently, linux has kernel support for automatic huge pages, and I think you may just get them if you allocate, say, 4 GB on a 1 GB boundary in VM.Of course if your machine doesn’t have multi-gig regions of contiguous memory laying around, it is going to be very hard to allocate 1G pages. Try 2M pages, because that is still 512x fewer TLB misses than using 4K pages. And you won’t need contiguous memory in such large blocks.Oh. and then just cast the pointer you get back from mmap() to a pointer of the type you want.Aside: I was doing this in order to play with cache allocation policies, and you can only make headway with that by using contiguous physical memory, and the only way to get contiguous memory with VM turned on is by huge pages. Since things like Skylake-EP can have up to 48 MB of cache, you need at least a 1G page to play with.Double aside: Once you start playing around with multisocket systems with more than 48 GB Ram or so, you also need to pay attention to NUMA - non uniform memory access, which is the slowdowns that happen when you access RAM on a different socket. Read the manual page for “numactl”.

TRENDING NEWS