Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1st problem

Program :
 #include<stdio.h>
 #include<conio.h>
 
void main(){
 extern int a;
 printf("%d",a);
  }
int a=20;</conio.h></stdio.h>



when at first time i run this program , i gout output as 20
but after that whenever i tried to run this same program i got an error "Illegal variable at 0x1f"

and output should be 20 (according to solution manual of a book)


2nd problem

Program
 #include<stdio.h>
 #include<conio.h>

int main(){clrscr();
 ichar str[] = "Part time musicians ar semiconductors";
int a = 5;
printf(a>10?"%50s":"%s",str);
getch();
  }</conio.h></stdio.h>


here in this program i dont understand the significance of "%50s".
please explain me what "%50s" mean.


3rd Problem

Program :
main(){
      char far *s1,*s2;
      printf("%d%d",sizeof(s1),sizeof(s2));
}


this program gives output : 4 2
but i dont understan how it comes as no value is defined here.
Posted
Updated 24-Aug-10 14:24pm
v2
Comments
Dalek Dave 24-Aug-10 20:24pm    
Edited for Code Blocks

Problem 1 - your app should give the same output every time you run it. Are you SURE there's no change to your code ?

Problem 2 - That's a format specifier, in this case, it's specifying the minimum number of characters to print

Problem 3 - sizeof tells you how many bytes a value contains. Because your char * are not initialised, they point to random data. Not sure why the values differ, I am not sure what 'far' does or if it just applies to the first one, seems likely.

Some additional points:

1 - your book doesn't sound like it's that great, especially if it asked you to use format specifiers without explaining them

2 - google would have answered all of these questions if you tried

3 - C is an older language which is not OO and is mostly used for micro controllers and so on. Unless you have a specific reason for wanting to learn C, if you want to write general windows programs, C is the wrong way to go about it.
 
Share this answer
 
Comments
Niklas L 24-Aug-10 15:19pm    
Your last point is a good piece of advice. I suspect universities teach C as a pre-class to C++, thinking that's how the language evolved and so should our students, when they actually would be better off not dealing with C at all.
Christian Graus 24-Aug-10 15:26pm    
Yes, teaching C first is hideous, the trouble is, a lot of lecturers learned C and didn't really learn C++, so they teach BAD C++, reliant on a C way of doing things
pradeep_bhadani 24-Aug-10 15:46pm    
@Christian Graus

for problem 1 , i run the same code and got this problem

and in 2nd problem , if this mean that string should be of minimum 50 character but my string is around 35 character. So after end of string 15 character should be blank but it is not happening .
i testes as below

#include<stdio.h>
#include<conio.h>

int main(){clrscr();
ichar str[] = "Part time musicians ar semiconductors";
int a = 5;
printf(a>10?"%50s":"%s",str);
printf("hello");
getch();
}

and output was :

Part time musicians ar semiconductorshello


@Christian Graus
i am learning C because i found that one should learn C (must learn)
can you suggest me a language other than C and reason why to go for it.
Please guide me.
#2: %50s means writing a string with minimum of 50 chars. Padding with spaces if necessary.

#3: The sizeof operator reports the size in bytes of its argument, and that's what is printed (assuming far and near pointers)

As for #1, I have no idea. Rebuild / retry. It should work.
 
Share this answer
 
v2
Comments
Christian Graus 24-Aug-10 15:27pm    
Cool - so I was right ( like I said, I was guessing on that one, I've not done C++ for a long time )
pradeep_bhadani 24-Aug-10 15:46pm    
#include
#include

int main(){clrscr();
ichar str[] = "Part time musicians ar semiconductors";
int a = 5;
printf(a>10?"%50s":"%s",str);
printf("hello");
getch();
}

and output was :

Part time musicians ar semiconductorshello
Niklas L 24-Aug-10 17:37pm    
Since a is 5, "%50s", is not used at all, but "%s". There's the immediate if statement to consider as well. It reads:
if (a > 10) use "%50s"; <- Clearly this is not the case
else use "%s";
pradeep_bhadani 24-Aug-10 19:16pm    
thanx..
i got my mistake.
I have no idea why you think you 'must learn' C. C is not object oriented, and it's use of pointers makes it more difficult than modern languages. I would learn C# first, then go back to C++ if you ever need it for any reason. C is basically useless in this day and age for general programming, because it's too obtuse, and does not lend itself to being used in major projects because it does not scale, does not have the tools needed for modern development ( classes for one ), and there is no real support for it anywhere in any modern framework.
 
Share this answer
 
Comments
pradeep_bhadani 24-Aug-10 15:48pm    
actually i tried learning C# .but i never got cleared in that.
pradeep_bhadani 24-Aug-10 15:51pm    
i am doing BE in india and C language is must to get placed in a company.
thats why i m focusing on C
Christian Graus 24-Aug-10 19:39pm    
You obviously have no real knowledge of programming. Choose a language that is in demand in your area and learn it properly. You are clearly lost as to what most of the code you posted does. Being able to copy simple examples out of a book in several languages is not the skill you need to get a job. You're better off focusing on one language and actually being able to use it.
Christian Graus 24-Aug-10 19:39pm    
Are you SURE companies in India require C, and not C# ? I doubt it very much, unless they are all doing embedded work and not writing programs for Windows, or for Mac.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900